55import android .graphics .Paint ;
66
77import com .github .mikephil .charting .animation .ChartAnimator ;
8+ import com .github .mikephil .charting .buffer .CandleBodyBuffer ;
9+ import com .github .mikephil .charting .buffer .CandleShadowBuffer ;
810import com .github .mikephil .charting .data .CandleData ;
911import com .github .mikephil .charting .data .CandleDataSet ;
1012import com .github .mikephil .charting .data .CandleEntry ;
1820public class CandleStickChartRenderer extends DataRenderer {
1921
2022 protected CandleDataProvider mChart ;
23+
24+ private CandleShadowBuffer [] mShadowBuffers ;
25+ private CandleBodyBuffer [] mBodyBuffers ;
2126
2227 public CandleStickChartRenderer (CandleDataProvider chart , ChartAnimator animator ,
2328 ViewPortHandler viewPortHandler ) {
@@ -27,8 +32,15 @@ public CandleStickChartRenderer(CandleDataProvider chart, ChartAnimator animator
2732
2833 @ Override
2934 public void initBuffers () {
30- // TODO Auto-generated method stub
31-
35+ CandleData candleData = mChart .getCandleData ();
36+ mShadowBuffers = new CandleShadowBuffer [candleData .getDataSetCount ()];
37+ mBodyBuffers = new CandleBodyBuffer [candleData .getDataSetCount ()];
38+
39+ for (int i = 0 ; i < mShadowBuffers .length ; i ++) {
40+ CandleDataSet set = candleData .getDataSetByIndex (i );
41+ mShadowBuffers [i ] = new CandleShadowBuffer (set .getValueCount () * 4 );
42+ mBodyBuffers [i ] = new CandleBodyBuffer (set .getValueCount () * 4 );
43+ }
3244 }
3345
3446 @ Override
@@ -44,53 +56,52 @@ public void drawData(Canvas c) {
4456 }
4557
4658 protected void drawDataSet (Canvas c , CandleDataSet dataSet ) {
47-
48- // pre allocate
49- float [] shadowPoints = new float [4 ];
50- float [] bodyPoints = new float [4 ];
59+
60+ Transformer trans = mChart .getTransformer (dataSet .getAxisDependency ());
61+ calcXBounds (trans );
62+
63+ int dataSetIndex = mChart .getCandleData ().getIndexOfDataSet (dataSet );
5164
5265 ArrayList <CandleEntry > entries = dataSet .getYVals ();
66+
67+ // draw the shadow
68+ int range = (mMaxX - mMinX ) * 4 ;
69+ int from = mMinX * 4 ;
70+
71+ CandleShadowBuffer shadowBuffer = mShadowBuffers [dataSetIndex ];
72+ shadowBuffer .feed (entries );
5373
74+ trans .pointValuesToPixel (shadowBuffer .buffer );
75+
76+ mRenderPaint .setStyle (Paint .Style .STROKE );
77+ mRenderPaint .setColor (dataSet .getColor ());
5478 mRenderPaint .setStrokeWidth (dataSet .getShadowWidth ());
5579
56- for (int j = 0 ; j < entries .size () * mAnimator .getPhaseX (); j ++) {
80+ c .drawLines (shadowBuffer .buffer , from , range , mRenderPaint );
81+
82+ CandleBodyBuffer bodyBuffer = mBodyBuffers [dataSetIndex ];
83+ bodyBuffer .setBodySpace (dataSet .getBodySpace ());
84+ bodyBuffer .feed (entries );
85+
86+ trans .pointValuesToPixel (bodyBuffer .buffer );
87+
88+ for (int j = 0 ; j < bodyBuffer .size (); j +=4 ) {
89+
90+ // get the entry
91+ CandleEntry e = entries .get (j / 4 );
92+
93+ if (!fitsBounds (e .getXIndex (), mMinX , mMaxX ))
94+ continue ;
5795
5896 // get the color that is specified for this position from
5997 // the DataSet, this will reuse colors, if the index is out
6098 // of bounds
6199 mRenderPaint .setColor (dataSet .getColor (j ));
62100
63- // get the entry
64- CandleEntry e = entries .get (j );
65-
66- Transformer trans = mChart .getTransformer (dataSet .getAxisDependency ());
67-
68- // transform the entries values for shadow and body
69- transformShadow (shadowPoints , e , trans );
70- transformBody (bodyPoints , e , dataSet .getBodySpace (), trans );
71-
72- float xShadow = shadowPoints [0 ];
73- float leftBody = bodyPoints [0 ];
74- float rightBody = bodyPoints [2 ];
75-
76- float high = shadowPoints [1 ];
77- float low = shadowPoints [3 ];
78-
79- float open = bodyPoints [1 ];
80- float close = bodyPoints [3 ];
81-
82- if (!mViewPortHandler .isInBoundsRight (leftBody ))
83- break ;
84-
85- // make sure the lines don't do shitty things outside
86- // bounds
87- if (j != 0 && !mViewPortHandler .isInBoundsLeft (rightBody )
88- && !mViewPortHandler .isInBoundsTop (low )
89- && !mViewPortHandler .isInBoundsBottom (high ))
90- continue ;
91-
92- // draw the shadow
93- c .drawLine (xShadow , low , xShadow , high , mRenderPaint );
101+ float leftBody = bodyBuffer .buffer [j ];
102+ float open = bodyBuffer .buffer [j + 1 ];
103+ float rightBody = bodyBuffer .buffer [j + 2 ];
104+ float close = bodyBuffer .buffer [j + 3 ];
94105
95106 // decide weather the body is hollow or filled
96107 if (open > close ) {
@@ -108,42 +119,42 @@ protected void drawDataSet(Canvas c, CandleDataSet dataSet) {
108119 }
109120 }
110121
111- /**
112- * Transforms the values of an entry in order to draw the candle-body.
113- *
114- * @param bodyPoints
115- * @param e
116- * @param bodySpace
117- */
118- private void transformBody (float [] bodyPoints , CandleEntry e , float bodySpace , Transformer trans ) {
119-
120- float phase = mAnimator .getPhaseY ();
121-
122- bodyPoints [0 ] = e .getXIndex () - 0.5f + bodySpace ;
123- bodyPoints [1 ] = e .getClose () * phase ;
124- bodyPoints [2 ] = e .getXIndex () + 0.5f - bodySpace ;
125- bodyPoints [3 ] = e .getOpen () * phase ;
126-
127- trans .pointValuesToPixel (bodyPoints );
128- }
129-
130- /**
131- * Transforms the values of an entry in order to draw the candle-shadow.
132- *
133- * @param shadowPoints
134- * @param e
135- */
136- private void transformShadow (float [] shadowPoints , CandleEntry e , Transformer trans ) {
137-
138- float phase = mAnimator .getPhaseY ();
139-
140- shadowPoints [0 ] = e .getXIndex ();
141- shadowPoints [1 ] = e .getHigh () * phase ;
142- shadowPoints [2 ] = e .getXIndex ();
143- shadowPoints [3 ] = e .getLow () * phase ;
144-
145- trans .pointValuesToPixel (shadowPoints );
146- }
122+ // /**
123+ // * Transforms the values of an entry in order to draw the candle-body.
124+ // *
125+ // * @param bodyPoints
126+ // * @param e
127+ // * @param bodySpace
128+ // */
129+ // private void transformBody(float[] bodyPoints, CandleEntry e, float bodySpace, Transformer trans) {
130+ //
131+ // float phase = mAnimator.getPhaseY();
132+ //
133+ // bodyPoints[0] = e.getXIndex() - 0.5f + bodySpace;
134+ // bodyPoints[1] = e.getClose() * phase;
135+ // bodyPoints[2] = e.getXIndex() + 0.5f - bodySpace;
136+ // bodyPoints[3] = e.getOpen() * phase;
137+ //
138+ // trans.pointValuesToPixel(bodyPoints);
139+ // }
140+ //
141+ // /**
142+ // * Transforms the values of an entry in order to draw the candle-shadow.
143+ // *
144+ // * @param shadowPoints
145+ // * @param e
146+ // */
147+ // private void transformShadow(float[] shadowPoints, CandleEntry e, Transformer trans) {
148+ //
149+ // float phase = mAnimator.getPhaseY();
150+ //
151+ // shadowPoints[0] = e.getXIndex();
152+ // shadowPoints[1] = e.getHigh() * phase;
153+ // shadowPoints[2] = e.getXIndex();
154+ // shadowPoints[3] = e.getLow() * phase;
155+ //
156+ // trans.pointValuesToPixel(shadowPoints);
157+ // }
147158
148159 @ Override
149160 public void drawValues (Canvas c ) {
0 commit comments