66import android .graphics .Paint .Style ;
77
88import com .github .mikephil .charting .animation .ChartAnimator ;
9- import com .github .mikephil .charting .data .BubbleEntry ;
10- import com .github .mikephil .charting .data .Entry ;
119import com .github .mikephil .charting .data .BubbleData ;
1210import com .github .mikephil .charting .data .BubbleDataSet ;
11+ import com .github .mikephil .charting .data .BubbleEntry ;
12+ import com .github .mikephil .charting .data .Entry ;
1313import com .github .mikephil .charting .interfaces .BubbleDataProvider ;
1414import com .github .mikephil .charting .utils .Highlight ;
1515import com .github .mikephil .charting .utils .Transformer ;
@@ -54,14 +54,19 @@ public void drawData(Canvas c) {
5454 }
5555 }
5656
57- private float [] _pointBuffer = new float [2 ];
57+ private float [] sizeBuffer = new float [4 ];
58+ private float [] pointBuffer = new float [2 ];
59+
60+ protected float getShapeSize (float entrySize , float maxSize , float reference ) {
61+ final float factor = (maxSize == 0f ) ? 1f : (float ) Math .sqrt (entrySize / maxSize );
62+ final float shapeSize = reference * factor ;
63+ return shapeSize ;
64+ }
5865
5966 protected void drawDataSet (Canvas c , BubbleDataSet dataSet ) {
6067
6168 Transformer trans = mChart .getTransformer (dataSet .getAxisDependency ());
6269
63- BubbleData bubbleData = mChart .getBubbleData ();
64-
6570 float phaseX = mAnimator .getPhaseX ();
6671 float phaseY = mAnimator .getPhaseY ();
6772
@@ -73,48 +78,57 @@ protected void drawDataSet(Canvas c, BubbleDataSet dataSet) {
7378 int minx = Math .max (dataSet .getEntryPosition (entryFrom ), 0 );
7479 int maxx = Math .min (dataSet .getEntryPosition (entryTo ) + 1 , entries .size ());
7580
76- final float chartSize = (mViewPortHandler .contentWidth () * mViewPortHandler .getScaleX ())
77- <= (mViewPortHandler .contentHeight () * mViewPortHandler .getScaleY ()) ?
78- mViewPortHandler .contentWidth () * mViewPortHandler .getScaleX () :
79- mViewPortHandler .contentHeight () * mViewPortHandler .getScaleY ();
81+ sizeBuffer [0 ] = 0f ;
82+ sizeBuffer [1 ] = mChart .getYChartMin ();
83+ sizeBuffer [2 ] = 1f ;
84+ sizeBuffer [3 ] = mChart .getYChartMax ();
85+
86+ trans .pointValuesToPixel (sizeBuffer );
8087
81- final float bubbleSizeFactor = (float ) (mChart .getXValCount () > 0 ? mChart .getXValCount () : 1 );
88+ // calcualte the full width of 1 step on the x-axis
89+ final float maxBubbleWidth = sizeBuffer [2 ] - sizeBuffer [0 ];
90+ final float maxBubbleHeight = Math .abs (sizeBuffer [1 ] - sizeBuffer [3 ]);
91+ final float referenceSize = Math .min (maxBubbleHeight , maxBubbleWidth );
8292
8393 for (int j = minx ; j < maxx ; j ++) {
8494
8595 final BubbleEntry entry = entries .get (j );
8696
87- _pointBuffer [0 ] = (float ) (entry .getXIndex () - minx ) * phaseX + (float ) minx ;
88- _pointBuffer [1 ] = (float ) (entry .getVal ()) * phaseY ;
89- trans .pointValuesToPixel (_pointBuffer );
97+ pointBuffer [0 ] = (float ) (entry .getXIndex () - minx ) * phaseX + (float ) minx ;
98+ pointBuffer [1 ] = (float ) (entry .getVal ()) * phaseY ;
99+ trans .pointValuesToPixel (pointBuffer );
100+
101+ float shapeHalf = getShapeSize (entry .getSize (), dataSet .getMaxSize (), referenceSize ) / 2f ;
90102
91- final float shapeSize = (chartSize / bubbleSizeFactor )
92- * (float ) (Math .sqrt (entry .getSize () /
93- (dataSet .getMaxSize () != 0.0 ? dataSet .getMaxSize () : 1.0 )));
94- final float shapeHalf = shapeSize / 2.f ;
103+ if (!mViewPortHandler .isInBoundsTop (pointBuffer [1 ] + shapeHalf )
104+ || !mViewPortHandler .isInBoundsBottom (pointBuffer [1 ] - shapeHalf ))
105+ continue ;
95106
96- if (!mViewPortHandler .isInBoundsY ( _pointBuffer [ 1 ] ))
107+ if (!mViewPortHandler .isInBoundsLeft ( pointBuffer [ 0 ] + shapeHalf ))
97108 continue ;
98109
110+ if (!mViewPortHandler .isInBoundsRight (pointBuffer [0 ] - shapeHalf ))
111+ break ;
112+
99113 final int color = dataSet .getColor (entry .getXIndex ());
100114
101115 mRenderPaint .setColor (color );
102- c .drawCircle (_pointBuffer [0 ], _pointBuffer [1 ], shapeHalf , mRenderPaint );
116+ c .drawCircle (pointBuffer [0 ], pointBuffer [1 ], shapeHalf , mRenderPaint );
103117 }
104118 }
105119
106120 @ Override
107121 public void drawValues (Canvas c ) {
108122
109123 BubbleData bubbleData = mChart .getBubbleData ();
110-
124+
111125 if (bubbleData == null )
112126 return ;
113127
114128 // if values are drawn
115129 if (bubbleData .getYValCount () < (int ) (Math .ceil ((float ) (mChart .getMaxVisibleCount ())
116130 * mViewPortHandler .getScaleX ()))) {
117-
131+
118132 final List <BubbleDataSet > dataSets = bubbleData .getDataSets ();
119133
120134 float lineHeight = Utils .calcTextHeight (mValuePaint , "1" );
@@ -153,7 +167,7 @@ public void drawValues(Canvas c) {
153167 if (!mViewPortHandler .isInBoundsRight (x ))
154168 break ;
155169
156- if ((!mViewPortHandler .isInBoundsLeft (x ) || !mViewPortHandler .isInBoundsY (y )))
170+ if ((!mViewPortHandler .isInBoundsLeft (x ) || !mViewPortHandler .isInBoundsY (y )))
157171 continue ;
158172
159173 final BubbleEntry entry = entries .get (j / 2 + minx );
@@ -184,13 +198,6 @@ public void drawHighlighted(Canvas c, Highlight[] indices) {
184198 float phaseX = mAnimator .getPhaseX ();
185199 float phaseY = mAnimator .getPhaseY ();
186200
187- final float chartSize = (mViewPortHandler .contentWidth () * mViewPortHandler .getScaleX ())
188- <= (mViewPortHandler .contentHeight () * mViewPortHandler .getScaleY ()) ?
189- mViewPortHandler .contentWidth () * mViewPortHandler .getScaleX () :
190- mViewPortHandler .contentHeight () * mViewPortHandler .getScaleY ();
191-
192- final float bubbleSizeFactor = (float ) (mChart .getXValCount () > 0 ? mChart .getXValCount () : 1 );
193-
194201 for (Highlight indice : indices ) {
195202
196203 BubbleDataSet dataSet = bubbleData .getDataSetByIndex (indice .getDataSetIndex ());
@@ -207,20 +214,36 @@ public void drawHighlighted(Canvas c, Highlight[] indices) {
207214 final BubbleEntry entry = (BubbleEntry ) bubbleData .getEntryForHighlight (indice );
208215
209216 Transformer trans = mChart .getTransformer (dataSet .getAxisDependency ());
217+
218+ sizeBuffer [0 ] = 0f ;
219+ sizeBuffer [1 ] = mChart .getYChartMin ();
220+ sizeBuffer [2 ] = 1f ;
221+ sizeBuffer [3 ] = mChart .getYChartMax ();
222+
223+ trans .pointValuesToPixel (sizeBuffer );
224+
225+ // calcualte the full width of 1 step on the x-axis
226+ final float maxBubbleWidth = sizeBuffer [2 ] - sizeBuffer [0 ];
227+ final float maxBubbleHeight = Math .abs (sizeBuffer [1 ] - sizeBuffer [3 ]);
228+ final float referenceSize = Math .min (maxBubbleHeight , maxBubbleWidth );
210229
211- _pointBuffer [0 ] = (float ) (entry .getXIndex () - minx ) * phaseX + (float ) minx ;
212- _pointBuffer [1 ] = (float ) (entry .getVal ()) * phaseY ;
213- trans .pointValuesToPixel (_pointBuffer );
230+ pointBuffer [0 ] = (float ) (entry .getXIndex () - minx ) * phaseX + (float ) minx ;
231+ pointBuffer [1 ] = (float ) (entry .getVal ()) * phaseY ;
232+ trans .pointValuesToPixel (pointBuffer );
214233
215- final float shapeSize = (chartSize / bubbleSizeFactor )
216- * (float ) (Math .sqrt (entry .getSize () /
217- (dataSet .getMaxSize () != 0.0 ? dataSet .getMaxSize () : 1.0 )));
218- final float shapeHalf = shapeSize / 2.f ;
234+ float shapeHalf = getShapeSize (entry .getSize (), dataSet .getMaxSize (), referenceSize ) / 2f ;
219235
220- if (indice .getXIndex () < minx || indice .getXIndex () >= maxx )
236+ if (!mViewPortHandler .isInBoundsTop (pointBuffer [1 ] + shapeHalf )
237+ || !mViewPortHandler .isInBoundsBottom (pointBuffer [1 ] - shapeHalf ))
238+ continue ;
239+
240+ if (!mViewPortHandler .isInBoundsLeft (pointBuffer [0 ] + shapeHalf ))
221241 continue ;
222242
223- if (!mViewPortHandler .isInBoundsY (_pointBuffer [1 ]))
243+ if (!mViewPortHandler .isInBoundsRight (pointBuffer [0 ] - shapeHalf ))
244+ break ;
245+
246+ if (indice .getXIndex () < minx || indice .getXIndex () >= maxx )
224247 continue ;
225248
226249 final int originalColor = dataSet .getColor (entry .getXIndex ());
@@ -232,7 +255,7 @@ public void drawHighlighted(Canvas c, Highlight[] indices) {
232255
233256 mHighlightPaint .setColor (color );
234257 mHighlightPaint .setStrokeWidth (dataSet .getHighlightCircleWidth ());
235- c .drawCircle (_pointBuffer [0 ], _pointBuffer [1 ], shapeHalf , mHighlightPaint );
258+ c .drawCircle (pointBuffer [0 ], pointBuffer [1 ], shapeHalf , mHighlightPaint );
236259 }
237260 }
238261}
0 commit comments