Skip to content

Commit fe9f904

Browse files
committed
Minor improvements to BubbleChart.
1 parent 4ba73f4 commit fe9f904

File tree

5 files changed

+76
-39
lines changed

5 files changed

+76
-39
lines changed

MPChartExample/res/layout/activity_bubblechart.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
android:layout_marginBottom="35dp"
3131
android:layout_toLeftOf="@+id/tvXMax"
3232
android:layout_marginRight="5dp"
33-
android:max="30"
33+
android:max="100"
3434
android:paddingBottom="12dp" />
3535

3636
<TextView

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,10 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
213213
set1.setDrawValues(true);
214214
BubbleDataSet set2 = new BubbleDataSet(yVals2, "DS 2");
215215
set2.setColor(ColorTemplate.COLORFUL_COLORS[1]);
216-
set2.setDrawValues(false);
216+
set2.setDrawValues(true);
217217
BubbleDataSet set3 = new BubbleDataSet(yVals3, "DS 3");
218218
set3.setColor(ColorTemplate.COLORFUL_COLORS[2]);
219-
set3.setDrawValues(false);
219+
set3.setDrawValues(true);
220220

221221
ArrayList<BubbleDataSet> dataSets = new ArrayList<BubbleDataSet>();
222222
dataSets.add(set1); // add the datasets
@@ -226,6 +226,8 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
226226
// create a data object with the datasets
227227
BubbleData data = new BubbleData(xVals, dataSets);
228228
data.setValueTypeface(tf);
229+
data.setValueTextSize(8f);
230+
data.setHighLightCircleWidth(1.5f);
229231

230232
mChart.setData(data);
231233
mChart.invalidate();
Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
package com.github.mikephil.charting.data;
23

34
import java.util.ArrayList;
@@ -8,34 +9,46 @@ public class BubbleData extends BarLineScatterCandleData<BubbleDataSet> {
89
public BubbleData() {
910
super();
1011
}
11-
12+
1213
public BubbleData(List<String> xVals) {
1314
super(xVals);
1415
}
15-
16+
1617
public BubbleData(String[] xVals) {
1718
super(xVals);
1819
}
19-
20+
2021
public BubbleData(List<String> xVals, List<BubbleDataSet> dataSets) {
2122
super(xVals, dataSets);
2223
}
2324

2425
public BubbleData(String[] xVals, List<BubbleDataSet> dataSets) {
2526
super(xVals, dataSets);
2627
}
27-
28+
2829
public BubbleData(List<String> xVals, BubbleDataSet dataSet) {
29-
super(xVals, toList(dataSet));
30+
super(xVals, toList(dataSet));
3031
}
31-
32+
3233
public BubbleData(String[] xVals, BubbleDataSet dataSet) {
3334
super(xVals, toList(dataSet));
3435
}
35-
36+
3637
private static List<BubbleDataSet> toList(BubbleDataSet dataSet) {
3738
List<BubbleDataSet> sets = new ArrayList<BubbleDataSet>();
3839
sets.add(dataSet);
3940
return sets;
4041
}
42+
43+
/**
44+
* Sets the width of the circle that surrounds the bubble when highlighted
45+
* for all DataSet objects this data object contains, in dp.
46+
*
47+
* @param width
48+
*/
49+
public void setHighLightCircleWidth(float width) {
50+
for (BubbleDataSet set : mDataSets) {
51+
set.setHighLightCircleWidth(width);
52+
}
53+
}
4154
}

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import android.graphics.Color;
55

6+
import com.github.mikephil.charting.utils.Utils;
7+
68
import java.util.ArrayList;
79
import java.util.List;
810

@@ -14,13 +16,29 @@ public class BubbleDataSet extends BarLineScatterCandleDataSet<BubbleEntry> {
1416
protected float mXMin;
1517
protected float mMaxSize;
1618

19+
private float mHighlightCircleWidth = 2.5f;
20+
1721
public BubbleDataSet(List<BubbleEntry> yVals, String label) {
1822
super(yVals, label);
1923

2024
if (mMaxSize < 1.f)
2125
mMaxSize = 1.f;
2226
}
2327

28+
/**
29+
* Sets the width of the circle that surrounds the bubble when highlighted,
30+
* in dp.
31+
*
32+
* @param width
33+
*/
34+
public void setHighLightCircleWidth(float width) {
35+
mHighlightCircleWidth = Utils.convertDpToPixel(width);
36+
}
37+
38+
public float getHighLightCircleWidth() {
39+
return mHighlightCircleWidth;
40+
}
41+
2442
@Override
2543
public void setColor(int color) {
2644
super.setColor(Color.argb(127, Color.red(color), Color.green(color), Color.blue(color)));
@@ -31,7 +49,7 @@ protected void calcMinMax()
3149
{
3250
final List<BubbleEntry> entries = getYVals();
3351

34-
//need chart width to guess this properly
52+
// need chart width to guess this properly
3553

3654
for (BubbleEntry entry : entries)
3755
{
@@ -107,11 +125,11 @@ private float yMax(BubbleEntry entry) {
107125
}
108126

109127
private float xMin(BubbleEntry entry) {
110-
return (float)entry.getXIndex();
128+
return (float) entry.getXIndex();
111129
}
112130

113131
private float xMax(BubbleEntry entry) {
114-
return (float)entry.getXIndex();
132+
return (float) entry.getXIndex();
115133
}
116134

117135
private float largestSize(BubbleEntry entry) {

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

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,26 @@
1919
import java.util.List;
2020

2121
/**
22-
* Bubble chart implementation:
23-
* Copyright 2015 Pierre-Marc Airoldi
24-
* Licensed under Apache License 2.0
25-
*
26-
* Ported by Daniel Cohen Gindi
22+
* Bubble chart implementation: Copyright 2015 Pierre-Marc Airoldi Licensed
23+
* under Apache License 2.0 Ported by Daniel Cohen Gindi
2724
*/
2825
public class BubbleChartRenderer extends DataRenderer {
2926

3027
protected BubbleDataProvider mChart;
3128

3229
public BubbleChartRenderer(BubbleDataProvider chart, ChartAnimator animator,
33-
ViewPortHandler viewPortHandler) {
30+
ViewPortHandler viewPortHandler) {
3431
super(animator, viewPortHandler);
3532
mChart = chart;
3633

3734
mRenderPaint.setStyle(Style.FILL);
3835

3936
mHighlightPaint.setStyle(Style.STROKE);
40-
mHighlightPaint.setStrokeWidth(Utils.convertDpToPixel(1f));
37+
mHighlightPaint.setStrokeWidth(Utils.convertDpToPixel(1.5f));
4138
}
4239

4340
@Override
44-
public void initBuffers()
45-
{
41+
public void initBuffers() {
4642

4743
}
4844

@@ -82,17 +78,19 @@ protected void drawDataSet(Canvas c, BubbleDataSet dataSet) {
8278
mViewPortHandler.contentWidth() * mViewPortHandler.getScaleX() :
8379
mViewPortHandler.contentHeight() * mViewPortHandler.getScaleY();
8480

85-
final float bubbleSizeFactor = (float)(bubbleData.getXVals().size() > 0 ? bubbleData.getXVals().size() : 1);
81+
final float bubbleSizeFactor = (float) (bubbleData.getXVals().size() > 0 ? bubbleData
82+
.getXVals().size() : 1);
8683

8784
for (int j = minx; j < maxx; j++) {
8885

8986
final BubbleEntry entry = entries.get(j);
9087

91-
_pointBuffer[0] = (float)(entry.getXIndex() - minx) * phaseX + (float)minx;
92-
_pointBuffer[1] = (float)(entry.getVal()) * phaseY;
88+
_pointBuffer[0] = (float) (entry.getXIndex() - minx) * phaseX + (float) minx;
89+
_pointBuffer[1] = (float) (entry.getVal()) * phaseY;
9390
trans.pointValuesToPixel(_pointBuffer);
9491

95-
final float shapeSize = (chartSize / bubbleSizeFactor) * (float)(Math.sqrt(entry.getSize()/dataSet.getMaxSize()));
92+
final float shapeSize = (chartSize / bubbleSizeFactor)
93+
* (float) (Math.sqrt(entry.getSize() / dataSet.getMaxSize()));
9694
final float shapeHalf = shapeSize / 2.f;
9795

9896
if (!mViewPortHandler.isInBoundsY(_pointBuffer[1]))
@@ -109,18 +107,20 @@ protected void drawDataSet(Canvas c, BubbleDataSet dataSet) {
109107
public void drawValues(Canvas c) {
110108

111109
BubbleData bubbleData = mChart.getBubbleData();
110+
112111
if (bubbleData == null)
113112
return;
114113

115114
// if values are drawn
116-
if (bubbleData.getYValCount() < (int)(Math.ceil((float)(mChart.getMaxVisibleCount()) * mViewPortHandler.getScaleX())))
117-
{
115+
if (bubbleData.getYValCount() < (int) (Math.ceil((float) (mChart.getMaxVisibleCount())
116+
* mViewPortHandler.getScaleX()))) {
117+
118118
final List<BubbleDataSet> dataSets = bubbleData.getDataSets();
119119

120-
float lineHeight = -mValuePaint.ascent() + mValuePaint.descent();
120+
float lineHeight = Utils.calcTextHeight(mValuePaint, "1");
121121

122122
for (BubbleDataSet dataSet : dataSets) {
123-
123+
124124
if (!dataSet.isDrawValuesEnabled())
125125
continue;
126126

@@ -129,7 +129,8 @@ public void drawValues(Canvas c) {
129129

130130
final float alpha = phaseX == 1 ? phaseY : phaseX;
131131
int valueTextColor = dataSet.getValueTextColor();
132-
valueTextColor = Color.argb(Math.round(255.f * alpha), Color.red(valueTextColor), Color.green(valueTextColor), Color.blue(valueTextColor));
132+
valueTextColor = Color.argb(Math.round(255.f * alpha), Color.red(valueTextColor),
133+
Color.green(valueTextColor), Color.blue(valueTextColor));
133134

134135
mValuePaint.setColor(valueTextColor);
135136

@@ -141,7 +142,8 @@ public void drawValues(Canvas c) {
141142
int minx = dataSet.getEntryPosition(entryFrom);
142143
int maxx = Math.min(dataSet.getEntryPosition(entryTo) + 1, dataSet.getEntryCount());
143144

144-
final float[] positions = mChart.getTransformer(dataSet.getAxisDependency()).generateTransformedValuesBubble(entries, phaseX, phaseY, minx, maxx);
145+
final float[] positions = mChart.getTransformer(dataSet.getAxisDependency())
146+
.generateTransformedValuesBubble(entries, phaseX, phaseY, minx, maxx);
145147

146148
for (int j = 0; j < positions.length; j += 2) {
147149

@@ -151,10 +153,8 @@ public void drawValues(Canvas c) {
151153
if (!mViewPortHandler.isInBoundsRight(x))
152154
break;
153155

154-
if ((!mViewPortHandler.isInBoundsLeft(x) || !mViewPortHandler.isInBoundsY(y)))
155-
{
156+
if ((!mViewPortHandler.isInBoundsLeft(x) || !mViewPortHandler.isInBoundsY(y)))
156157
continue;
157-
}
158158

159159
final BubbleEntry entry = entries.get(j / 2 + minx);
160160

@@ -167,7 +167,7 @@ public void drawValues(Canvas c) {
167167
}
168168
}
169169
}
170-
170+
171171
}
172172

173173
@Override
@@ -189,7 +189,8 @@ public void drawHighlighted(Canvas c, Highlight[] indices) {
189189
mViewPortHandler.contentWidth() * mViewPortHandler.getScaleX() :
190190
mViewPortHandler.contentHeight() * mViewPortHandler.getScaleY();
191191

192-
final float bubbleSizeFactor = (float)(bubbleData.getXVals().size() > 0 ? bubbleData.getXVals().size() : 1);
192+
final float bubbleSizeFactor = (float) (bubbleData.getXVals().size() > 0 ? bubbleData
193+
.getXVals().size() : 1);
193194

194195
for (Highlight indice : indices) {
195196

@@ -212,7 +213,8 @@ public void drawHighlighted(Canvas c, Highlight[] indices) {
212213
_pointBuffer[1] = (float) (entry.getVal()) * phaseY;
213214
trans.pointValuesToPixel(_pointBuffer);
214215

215-
final float shapeSize = (chartSize / bubbleSizeFactor) * (float) (Math.sqrt(entry.getSize() / dataSet.getMaxSize()));
216+
final float shapeSize = (chartSize / bubbleSizeFactor)
217+
* (float) (Math.sqrt(entry.getSize() / dataSet.getMaxSize()));
216218
final float shapeHalf = shapeSize / 2.f;
217219

218220
if (indice.getXIndex() < minx || indice.getXIndex() >= maxx)
@@ -223,11 +225,13 @@ public void drawHighlighted(Canvas c, Highlight[] indices) {
223225

224226
final int originalColor = dataSet.getColor(entry.getXIndex());
225227

226-
Color.RGBToHSV(Color.red(originalColor), Color.green(originalColor), Color.blue(originalColor), _hsvBuffer);
228+
Color.RGBToHSV(Color.red(originalColor), Color.green(originalColor),
229+
Color.blue(originalColor), _hsvBuffer);
227230
_hsvBuffer[2] *= 0.5f;
228231
final int color = Color.HSVToColor(Color.alpha(originalColor), _hsvBuffer);
229232

230233
mHighlightPaint.setColor(color);
234+
mHighlightPaint.setStrokeWidth(dataSet.getHighLightCircleWidth());
231235
c.drawCircle(_pointBuffer[0], _pointBuffer[1], shapeHalf, mHighlightPaint);
232236
}
233237
}

0 commit comments

Comments
 (0)