Skip to content

Commit 83cb3ee

Browse files
committed
Minor changes on BubbleChart.
1 parent 82d8881 commit 83cb3ee

5 files changed

Lines changed: 87 additions & 56 deletions

File tree

MPChartExample/src/com/xxmassdeveloper/mpchartexample/BubbleChartActivity.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
package com.xxmassdeveloper.mpchartexample;
33

4+
import android.graphics.Color;
45
import android.graphics.Typeface;
56
import android.os.Bundle;
67
import android.util.Log;
@@ -16,11 +17,11 @@
1617
import com.github.mikephil.charting.components.Legend.LegendPosition;
1718
import com.github.mikephil.charting.components.XAxis;
1819
import com.github.mikephil.charting.components.YAxis;
20+
import com.github.mikephil.charting.data.BubbleData;
21+
import com.github.mikephil.charting.data.BubbleDataSet;
1922
import com.github.mikephil.charting.data.BubbleEntry;
2023
import com.github.mikephil.charting.data.DataSet;
2124
import com.github.mikephil.charting.data.Entry;
22-
import com.github.mikephil.charting.data.BubbleData;
23-
import com.github.mikephil.charting.data.BubbleDataSet;
2425
import com.github.mikephil.charting.data.filter.Approximator;
2526
import com.github.mikephil.charting.data.filter.Approximator.ApproximatorType;
2627
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
@@ -86,6 +87,9 @@ protected void onCreate(Bundle savedInstanceState) {
8687

8788
YAxis yl = mChart.getAxisLeft();
8889
yl.setTypeface(tf);
90+
yl.setSpaceTop(30f);
91+
yl.setStartAtZero(false);
92+
yl.setSpaceBottom(30f);
8993

9094
mChart.getAxisRight().setEnabled(false);
9195

@@ -209,13 +213,13 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
209213

210214
// create a dataset and give it a type
211215
BubbleDataSet set1 = new BubbleDataSet(yVals1, "DS 1");
212-
set1.setColor(ColorTemplate.COLORFUL_COLORS[0]);
216+
set1.setColor(ColorTemplate.COLORFUL_COLORS[0], 130);
213217
set1.setDrawValues(true);
214218
BubbleDataSet set2 = new BubbleDataSet(yVals2, "DS 2");
215-
set2.setColor(ColorTemplate.COLORFUL_COLORS[1]);
219+
set2.setColor(ColorTemplate.COLORFUL_COLORS[1], 130);
216220
set2.setDrawValues(true);
217221
BubbleDataSet set3 = new BubbleDataSet(yVals3, "DS 3");
218-
set3.setColor(ColorTemplate.COLORFUL_COLORS[2]);
222+
set3.setColor(ColorTemplate.COLORFUL_COLORS[2], 130);
219223
set3.setDrawValues(true);
220224

221225
ArrayList<BubbleDataSet> dataSets = new ArrayList<BubbleDataSet>();
@@ -227,6 +231,7 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
227231
BubbleData data = new BubbleData(xVals, dataSets);
228232
data.setValueTypeface(tf);
229233
data.setValueTextSize(8f);
234+
data.setValueTextColor(Color.WHITE);
230235
data.setHighlightCircleWidth(1.5f);
231236

232237
mChart.setData(data);

MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ protected void onCreate(Bundle savedInstanceState) {
5959
mChart.setUsePercentValues(true);
6060
mChart.setDescription("");
6161

62-
mChart.setDragDecelerationFrictionCoef(0.99f);
62+
mChart.setDragDecelerationFrictionCoef(0.95f);
6363

6464
tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");
6565

MPChartLib/src/com/github/mikephil/charting/charts/BubbleChart.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
1010
import com.github.mikephil.charting.renderer.BubbleChartRenderer;
1111

1212
/**
13-
* The BubbleChart. Draws bubbles.
14-
*
15-
* Bubble chart implementation:
16-
* Copyright 2015 Pierre-Marc Airoldi
17-
* Licensed under Apache License 2.0
13+
* The BubbleChart. Draws bubbles. Bubble chart implementation: Copyright 2015
14+
* Pierre-Marc Airoldi Licensed under Apache License 2.0. In the BubbleChart, it
15+
* is the area of the bubble, not the radius or diameter of the bubble that
16+
* conveys the data.
1817
*
1918
* @author Philipp Jahoda
2019
*/
@@ -47,7 +46,7 @@ protected void calcMinMax() {
4746
mDeltaX = 1;
4847

4948
mXChartMin = -0.5f;
50-
mXChartMax = (float)mData.getXValCount() - 0.5f;
49+
mXChartMax = (float) mData.getXValCount() - 0.5f;
5150

5251
if (mRenderer != null) {
5352
for (BubbleDataSet set : mData.getDataSets()) {

MPChartLib/src/com/github/mikephil/charting/data/BubbleDataSet.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,13 @@ public float getHighlightCircleWidth() {
3636
return mHighlightCircleWidth;
3737
}
3838

39-
@Override
40-
public void setColor(int color) {
41-
super.setColor(Color.argb(127, Color.red(color), Color.green(color), Color.blue(color)));
39+
/**
40+
* Sets a color with a specific alpha value.
41+
* @param color
42+
* @param alpha from 0-255
43+
*/
44+
public void setColor(int color, int alpha) {
45+
super.setColor(Color.argb(alpha, Color.red(color), Color.green(color), Color.blue(color)));
4246
}
4347

4448
@Override
@@ -48,8 +52,8 @@ protected void calcMinMax() {
4852

4953
// need chart width to guess this properly
5054

51-
for (BubbleEntry entry : entries)
52-
{
55+
for (BubbleEntry entry : entries) {
56+
5357
final float ymin = yMin(entry);
5458
final float ymax = yMax(entry);
5559

MPChartLib/src/com/github/mikephil/charting/renderer/BubbleChartRenderer.java

Lines changed: 62 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
import android.graphics.Paint.Style;
77

88
import com.github.mikephil.charting.animation.ChartAnimator;
9-
import com.github.mikephil.charting.data.BubbleEntry;
10-
import com.github.mikephil.charting.data.Entry;
119
import com.github.mikephil.charting.data.BubbleData;
1210
import com.github.mikephil.charting.data.BubbleDataSet;
11+
import com.github.mikephil.charting.data.BubbleEntry;
12+
import com.github.mikephil.charting.data.Entry;
1313
import com.github.mikephil.charting.interfaces.BubbleDataProvider;
1414
import com.github.mikephil.charting.utils.Highlight;
1515
import 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

Comments
 (0)