Skip to content

Commit 75b549d

Browse files
committed
* moved com.androidplot.candlestick.* to com.androidplot.xy
* added candlestickseries / test. * added an xml-configurable IDE preview mode attr to XYPlot.
1 parent e7df061 commit 75b549d

11 files changed

Lines changed: 392 additions & 89 deletions

File tree

androidplot-core/src/main/java/com/androidplot/Plot.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,20 @@ public void setRenderMode(RenderMode mode) {
300300
}
301301

302302
/**
303-
* Concrete implementations should do any final setup / initialization
303+
* Concrete implementations may do any final setup / initialization
304304
* here. Immediately following this method's invocation, AndroidPlot assumes
305305
* that the Plot instance is ready for final configuration via the Configurator.
306306
*/
307-
protected abstract void onPreInit();
307+
protected void onPreInit() {
308+
// nothing to do by default
309+
}
310+
311+
/**
312+
* Invoked immediately following configurator / styleable attr application.
313+
*/
314+
protected void onAfterConfig() {
315+
// nothing to do by default
316+
}
308317

309318

310319
private void init(Context context, AttributeSet attrs, int defStyle) {
@@ -328,6 +337,8 @@ private void init(Context context, AttributeSet attrs, int defStyle) {
328337
loadAttrs(attrs, defStyle);
329338
}
330339

340+
onAfterConfig();
341+
331342
layoutManager.onPostInit();
332343
if (renderMode == RenderMode.USE_BACKGROUND_THREAD) {
333344
renderThread = new Thread(new Runnable() {

androidplot-core/src/main/java/com/androidplot/candlestick/CandlestickMaker.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

androidplot-core/src/main/java/com/androidplot/candlestick/CandlestickFormatter.java renamed to androidplot-core/src/main/java/com/androidplot/xy/CandlestickFormatter.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.androidplot.candlestick;
17+
package com.androidplot.xy;
1818

1919
import android.graphics.Color;
2020
import android.graphics.Paint;
2121
import com.androidplot.ui.SeriesRenderer;
2222
import com.androidplot.util.PixelUtils;
23-
import com.androidplot.xy.XYPlot;
24-
import com.androidplot.xy.XYRegionFormatter;
25-
import com.androidplot.xy.XYSeriesFormatter;
2623

2724
/**
2825
* Format for drawing a value using {@link CandlestickRenderer}.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2016 AndroidPlot.com
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.androidplot.xy;
18+
19+
/**
20+
* Helper utility to simplify the creation of of candlestick charts
21+
* @since 0.9.7
22+
*/
23+
public abstract class CandlestickMaker {
24+
25+
/**
26+
* Adds a candlestick chart to the specified plot using the specified
27+
* high, low, open and close values.
28+
* @param plot
29+
* @param formatter
30+
* @param openVals
31+
* @param closeVals
32+
* @param highVals
33+
* @param lowVals
34+
*/
35+
public static void make(XYPlot plot, CandlestickFormatter formatter,
36+
XYSeries openVals, XYSeries closeVals, XYSeries highVals, XYSeries lowVals) {
37+
plot.addSeries(formatter, highVals, lowVals, openVals, closeVals);
38+
}
39+
40+
/**
41+
* Add a candlestick chart to the specified plot using the specified {@link CandlestickSeries}.
42+
* @param plot
43+
* @param formatter
44+
* @param series
45+
* @since 0.9.8
46+
*/
47+
public static void make(XYPlot plot, CandlestickFormatter formatter, CandlestickSeries series) {
48+
make(plot, formatter, series.getOpenSeries(), series.getCloseSeries(),
49+
series.getHighSeries(), series.getLowSeries());
50+
}
51+
52+
/**
53+
* Check the validity of series data comprising a {@link CandlestickSeries}.
54+
* This is a development aid; be sure to remove any usage of this method in production code.
55+
* @param series
56+
* @since 0.9.8
57+
*/
58+
public static void check(CandlestickSeries series) {
59+
check(series.getOpenSeries(), series.getCloseSeries(), series.getHighSeries(), series.getLowSeries());
60+
}
61+
62+
/**
63+
* Check the validity of series data comprising a candlestick chart.
64+
* This is a development aid; be sure to remove any usage of this method in production code.
65+
* @param openVals
66+
* @param closeVals
67+
* @param highVals
68+
* @param lowVals
69+
* @since 0.9.8
70+
*/
71+
public static void check(XYSeries openVals, XYSeries closeVals, XYSeries highVals, XYSeries lowVals) {
72+
final int size = openVals.size();
73+
assert closeVals.size() == size : "closeVals has irregular size.";
74+
assert highVals.size() == size : "highVals has irregular size.";
75+
assert lowVals.size() == size : "lowVals has irregular size.";
76+
77+
for(int i = 0; i < size; i++) {
78+
79+
final double highVal = highVals.getY(i).doubleValue();
80+
final double lowVal = lowVals.getY(i).doubleValue();
81+
final double openVal = openVals.getY(i).doubleValue();
82+
final double closeVal = closeVals.getY(i).doubleValue();
83+
84+
assert openVal <= highVal : "Detected openVal > highVal at index " + i;
85+
assert openVal >= lowVal : "Detected openVal < lowVal at index " + i;
86+
assert closeVal <= highVal : "Detected closeVal > highVal at index " + i;
87+
assert closeVal >= lowVal : "Detected closeVal < lowVal at index " + i;
88+
assert lowVal <= highVal : "Detected lowVal > highVal at index " + i;
89+
}
90+
}
91+
}

androidplot-core/src/main/java/com/androidplot/candlestick/CandlestickRenderer.java renamed to androidplot-core/src/main/java/com/androidplot/xy/CandlestickRenderer.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.androidplot.candlestick;
17+
package com.androidplot.xy;
1818

1919
import android.graphics.*;
2020
import com.androidplot.ui.RenderStack;
2121
import com.androidplot.ui.SeriesAndFormatter;
2222
import com.androidplot.util.ValPixConverter;
23-
import com.androidplot.xy.GroupRenderer;
24-
import com.androidplot.xy.XYPlot;
25-
import com.androidplot.xy.XYSeries;
2623

2724
import java.util.List;
2825

@@ -36,7 +33,8 @@
3633
* - Expects that series are added in the order of:
3734
* high, low, open, close
3835
*
39-
* {@link CandlestickMaker} simplified methods for setting up a candlestick chart.
36+
* {@link CandlestickSeries} and {@link CandlestickMaker} provide simplified classes and methods
37+
* for setting up a candlestick chart.
4038
* @since 0.9.7
4139
*/
4240
public class CandlestickRenderer<FormatterType extends CandlestickFormatter> extends GroupRenderer<FormatterType> {
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
* Copyright 2016 AndroidPlot.com
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.androidplot.xy;
18+
19+
import com.androidplot.xy.SimpleXYSeries;
20+
21+
import java.util.*;
22+
23+
/**
24+
* Convenience class for representing a series of candlestick values;
25+
* is NOT a descendant of {@link com.androidplot.xy.XYSeries} and therefore
26+
* cannot be directly added to an {@link com.androidplot.xy.XYPlot}.
27+
*
28+
* This class is NOT threadsafe.
29+
*
30+
* @since 0.9.8
31+
*/
32+
public class CandlestickSeries {
33+
34+
private SimpleXYSeries highSeries = new SimpleXYSeries(null);
35+
private SimpleXYSeries lowSeries = new SimpleXYSeries(null);
36+
private SimpleXYSeries openSeries = new SimpleXYSeries(null);
37+
private SimpleXYSeries closeSeries = new SimpleXYSeries(null);
38+
39+
protected static List<Number> generateRange(int start, int end) {
40+
List<Number> range = new ArrayList<>(end - start);
41+
for(int i = start; i < end; i++) {
42+
range.add(i);
43+
}
44+
return range;
45+
}
46+
47+
public CandlestickSeries(Item... items) {
48+
this(Arrays.asList(items));
49+
}
50+
51+
/**
52+
* Creates a new CandlestickSeries.
53+
* Calls {@link #CandlestickSeries(List, List)} with a list of xVals
54+
* generated using the formula x=i.
55+
* @param items
56+
*/
57+
public CandlestickSeries(List<Item> items) {
58+
this(generateRange(0, items.size()), items);
59+
}
60+
61+
public CandlestickSeries(List<Number> xVals, List<Item> items) {
62+
if(xVals.size() != items.size()) {
63+
throw new IllegalArgumentException("xVals and yVals length must be identical.");
64+
}
65+
for(int i = 0; i < xVals.size(); i++) {
66+
Number x = xVals.get(i);
67+
highSeries.addLast(x, items.get(i).getHigh());
68+
lowSeries.addLast(x, items.get(i).getLow());
69+
openSeries.addLast(x, items.get(i).getOpen());
70+
closeSeries.addLast(x, items.get(i).getClose());
71+
}
72+
}
73+
74+
public SimpleXYSeries getHighSeries() {
75+
return highSeries;
76+
}
77+
78+
public void setHighSeries(SimpleXYSeries highSeries) {
79+
this.highSeries = highSeries;
80+
}
81+
82+
public SimpleXYSeries getLowSeries() {
83+
return lowSeries;
84+
}
85+
86+
public void setLowSeries(SimpleXYSeries lowSeries) {
87+
this.lowSeries = lowSeries;
88+
}
89+
90+
public SimpleXYSeries getOpenSeries() {
91+
return openSeries;
92+
}
93+
94+
public void setOpenSeries(SimpleXYSeries openSeries) {
95+
this.openSeries = openSeries;
96+
}
97+
98+
public SimpleXYSeries getCloseSeries() {
99+
return closeSeries;
100+
}
101+
102+
public void setCloseSeries(SimpleXYSeries closeSeries) {
103+
this.closeSeries = closeSeries;
104+
}
105+
106+
public static class Item {
107+
private double low;
108+
private double high;
109+
private double open;
110+
private double close;
111+
112+
/**
113+
* An individual candlestick value. Since it is illegal to include
114+
* null values for any member of a candlestick, this class is modeled with
115+
* double values instead of {@link Number} instances.
116+
* @param low
117+
* @param high
118+
* @param open
119+
* @param close
120+
*/
121+
public Item(double low, double high, double open, double close) {
122+
this.low = low;
123+
this.high = high;
124+
this.open = open;
125+
this.close = close;
126+
}
127+
128+
public double getLow() {
129+
return low;
130+
}
131+
132+
public void setLow(double low) {
133+
this.low = low;
134+
}
135+
136+
public double getHigh() {
137+
return high;
138+
}
139+
140+
public void setHigh(double high) {
141+
this.high = high;
142+
}
143+
144+
public double getOpen() {
145+
return open;
146+
}
147+
148+
public void setOpen(double open) {
149+
this.open = open;
150+
}
151+
152+
public double getClose() {
153+
return close;
154+
}
155+
156+
public void setClose(double close) {
157+
this.close = close;
158+
}
159+
}
160+
}

0 commit comments

Comments
 (0)