1717package com .androidplot .demos ;
1818
1919import android .app .Activity ;
20+ import android .graphics .Color ;
21+ import android .graphics .DashPathEffect ;
2022import android .os .Bundle ;
2123import com .androidplot .candlestick .CandlestickFormatter ;
2224import com .androidplot .candlestick .CandlestickMaker ;
25+ import com .androidplot .util .PixelUtils ;
2326import com .androidplot .xy .*;
2427
28+ import java .text .DecimalFormat ;
29+ import java .text .FieldPosition ;
30+ import java .text .Format ;
31+ import java .text .ParsePosition ;
32+ import java .util .Arrays ;
33+ import java .util .List ;
34+
2535/**
2636 * A simple example of a candlestick chart rendered on an {@link XYPlot}.
2737 */
@@ -34,26 +44,86 @@ public class CandlestickChartActivity extends Activity
3444 public void onCreate (Bundle savedInstanceState )
3545 {
3646 super .onCreate (savedInstanceState );
37- setContentView (R .layout .simple_xy_plot_example );
47+ setContentView (R .layout .candlestick_example );
3848
3949 // initialize our XYPlot reference:
4050 plot = (XYPlot ) findViewById (R .id .plot );
4151
42- plot .getLayoutManager ().moveToBottom (plot .getTitleWidget ());
4352
44- XYSeries highVals = new SimpleXYSeries (SimpleXYSeries .ArrayFormat .Y_VALS_ONLY , "high" , 12 , 10 , 15 , 8 , 7 );
45- XYSeries lowVals = new SimpleXYSeries (SimpleXYSeries .ArrayFormat .Y_VALS_ONLY , "low" , 3 , 1 , 5 , 0 , 2 );
46- XYSeries openVals = new SimpleXYSeries (SimpleXYSeries .ArrayFormat .Y_VALS_ONLY , "open" , 5 , 2 , 7 , 5 , 3 );
47- XYSeries closeVals = new SimpleXYSeries (SimpleXYSeries .ArrayFormat .Y_VALS_ONLY , "close" , 7 , 9 , 6 , 0 , 4 );
53+ // create our min, max, high and low values:
54+ List <Number > xVals = Arrays .asList (new Number []{1 , 2 , 3 , 4 , 5 });
55+ XYSeries highVals = new SimpleXYSeries (xVals ,
56+ Arrays .asList (12 , 10 , 15 , 8 , 7 ), "high" );
57+ XYSeries lowVals = new SimpleXYSeries (xVals ,
58+ Arrays .asList (3 , 1 , 5 , 0 , 2 ), "low" );
59+ XYSeries openVals = new SimpleXYSeries (xVals ,
60+ Arrays .asList (5 , 2 , 7 , 5 , 3 ), "open" );
61+ XYSeries closeVals = new SimpleXYSeries (xVals ,
62+ Arrays .asList (7 , 9 , 6 , 0 , 4 ), "close" );
63+
64+ // draw a simple line plot of the close vals:
65+ LineAndPointFormatter lpf = new LineAndPointFormatter (Color .WHITE , Color .WHITE , null , null );
66+ lpf .getLinePaint ().setPathEffect (
67+ new DashPathEffect (
68+ new float []{PixelUtils .dpToPix (5 ), PixelUtils .dpToPix (5 )}, 0 ));
69+ lpf .setInterpolationParams (
70+ new CatmullRomInterpolator .Params (20 , CatmullRomInterpolator .Type .Centripetal ));
71+
72+ plot .addSeries (closeVals , lpf );
4873
4974 CandlestickFormatter formatter = new CandlestickFormatter ();
75+
76+ // draw candlestick bodies as triangles instead of squares:
77+ // triangles will point up for items that closed higher than they opened
78+ // and down for those that closed lower:
5079 formatter .setBodyStyle (CandlestickFormatter .BodyStyle .Triangle );
80+
81+ // add the candlestick series data to the plot:
5182 CandlestickMaker .make (plot , formatter ,
5283 openVals , closeVals , highVals , lowVals );
5384
54- // reduce the number of range labels
85+ // setup the range label formatting, etc:
86+ plot .setRangeLabel ("Amount" );
5587 plot .setTicksPerRangeLabel (3 );
5688
89+ plot .setRangeValueFormat (new DecimalFormat ("$0.00" ));
90+
91+ // setup the domain label formatting, etc:
92+ plot .setDomainBoundaries (0 , 6 , BoundaryMode .FIXED );
93+ plot .setDomainLabel ("Day" );
94+ plot .setDomainStep (XYStepMode .INCREMENT_BY_VAL , 1 );
95+ plot .setDomainValueFormat (new Format () {
96+ @ Override
97+ public StringBuffer format (Object object , StringBuffer buffer , FieldPosition field ) {
98+ switch (((Number ) object ).intValue ()) {
99+ case 1 :
100+ buffer .append ("Mon" );
101+ break ;
102+ case 2 :
103+ buffer .append ("Tues" );
104+ break ;
105+ case 3 :
106+ buffer .append ("Wed" );
107+ break ;
108+ case 4 :
109+ buffer .append ("Thurs" );
110+ break ;
111+ case 5 :
112+ buffer .append ("Fri" );
113+ break ;
114+ default :
115+ // show nothing
116+
117+ }
118+ return buffer ;
119+ }
120+
121+ @ Override
122+ public Object parseObject (String string , ParsePosition position ) {
123+ return null ;
124+ }
125+ });
126+
57127 // rotate domain labels 45 degrees to make them more compact horizontally:
58128 plot .getGraphWidget ().setDomainLabelOrientation (-45 );
59129 }
0 commit comments