Skip to content

Commit 1c58192

Browse files
committed
Worked on integrating BubbleChart into CombinedChart.
1 parent 1301f11 commit 1c58192

11 files changed

Lines changed: 112 additions & 30 deletions

File tree

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
import com.github.mikephil.charting.data.BarData;
1616
import com.github.mikephil.charting.data.BarDataSet;
1717
import com.github.mikephil.charting.data.BarEntry;
18+
import com.github.mikephil.charting.data.BubbleData;
19+
import com.github.mikephil.charting.data.BubbleDataSet;
20+
import com.github.mikephil.charting.data.BubbleEntry;
1821
import com.github.mikephil.charting.data.CandleData;
1922
import com.github.mikephil.charting.data.CandleDataSet;
2023
import com.github.mikephil.charting.data.CandleEntry;
@@ -25,6 +28,7 @@
2528
import com.github.mikephil.charting.data.LineDataSet;
2629
import com.github.mikephil.charting.data.ScatterData;
2730
import com.github.mikephil.charting.data.ScatterDataSet;
31+
import com.github.mikephil.charting.utils.ColorTemplate;
2832
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
2933

3034
import java.util.ArrayList;
@@ -48,7 +52,7 @@ protected void onCreate(Bundle savedInstanceState) {
4852

4953
// draw bars behind lines
5054
mChart.setDrawOrder(new DrawOrder[] {
51-
DrawOrder.BAR, DrawOrder.CANDLE, DrawOrder.LINE, DrawOrder.SCATTER
55+
DrawOrder.BAR, DrawOrder.BUBBLE, DrawOrder.CANDLE, DrawOrder.LINE, DrawOrder.SCATTER
5256
});
5357

5458
YAxis rightAxis = mChart.getAxisRight();
@@ -64,6 +68,7 @@ protected void onCreate(Bundle savedInstanceState) {
6468

6569
data.setData(generateLineData());
6670
data.setData(generateBarData());
71+
// data.setData(generateBubbleData());
6772
// data.setData(generateScatterData());
6873
// data.setData(generateCandleData());
6974

@@ -125,7 +130,7 @@ protected ScatterData generateScatterData() {
125130
ArrayList<Entry> entries = new ArrayList<Entry>();
126131

127132
for (int index = 0; index < itemcount; index++)
128-
entries.add(new Entry(getRandom(20, 30), index));
133+
entries.add(new Entry(getRandom(20, 15), index));
129134

130135
ScatterDataSet set = new ScatterDataSet(entries, "Scatter DataSet");
131136
set.setColor(Color.GREEN);
@@ -155,6 +160,28 @@ protected CandleData generateCandleData() {
155160

156161
return d;
157162
}
163+
164+
protected BubbleData generateBubbleData() {
165+
166+
BubbleData bd = new BubbleData();
167+
168+
ArrayList<BubbleEntry> entries = new ArrayList<BubbleEntry>();
169+
170+
for (int index = 0; index < itemcount; index++) {
171+
float rnd = getRandom(20, 30);
172+
entries.add(new BubbleEntry(index, rnd, rnd));
173+
}
174+
175+
BubbleDataSet set = new BubbleDataSet(entries, "Bubble DataSet");
176+
set.setColors(ColorTemplate.VORDIPLOM_COLORS);
177+
set.setValueTextSize(10f);
178+
set.setValueTextColor(Color.WHITE);
179+
set.setHighlightCircleWidth(1.5f);
180+
set.setDrawValues(true);
181+
bd.addDataSet(set);
182+
183+
return bd;
184+
}
158185

159186
private float getRandom(float range, float startsfrom) {
160187
return (float) (Math.random() * range) + startsfrom;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,11 @@ public float getXChartMin() {
908908
return mXChartMin;
909909
}
910910

911+
@Override
912+
public int getXValCount() {
913+
return mData.getXValCount();
914+
}
915+
911916
/**
912917
* returns the average value of all values the chart holds
913918
*

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

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
import android.util.AttributeSet;
66

77
import com.github.mikephil.charting.data.BarData;
8+
import com.github.mikephil.charting.data.BubbleData;
9+
import com.github.mikephil.charting.data.BubbleDataSet;
810
import com.github.mikephil.charting.data.CandleData;
911
import com.github.mikephil.charting.data.CombinedData;
1012
import com.github.mikephil.charting.data.LineData;
1113
import com.github.mikephil.charting.data.ScatterData;
1214
import com.github.mikephil.charting.interfaces.BarDataProvider;
15+
import com.github.mikephil.charting.interfaces.BubbleDataProvider;
1316
import com.github.mikephil.charting.interfaces.CandleDataProvider;
1417
import com.github.mikephil.charting.interfaces.LineDataProvider;
1518
import com.github.mikephil.charting.interfaces.ScatterDataProvider;
@@ -23,7 +26,7 @@
2326
* @author Philipp Jahoda
2427
*/
2528
public class CombinedChart extends BarLineChartBase<CombinedData> implements LineDataProvider,
26-
BarDataProvider, ScatterDataProvider, CandleDataProvider {
29+
BarDataProvider, ScatterDataProvider, CandleDataProvider, BubbleDataProvider {
2730

2831
/** the fill-formatter used for determining the position of the fill-line */
2932
protected FillFormatter mFillFormatter;
@@ -50,15 +53,15 @@ public class CombinedChart extends BarLineChartBase<CombinedData> implements Lin
5053
private boolean mDrawBarShadow = false;
5154

5255
protected DrawOrder[] mDrawOrder = new DrawOrder[] {
53-
DrawOrder.BAR, DrawOrder.LINE, DrawOrder.CANDLE, DrawOrder.SCATTER
56+
DrawOrder.BAR, DrawOrder.BUBBLE, DrawOrder.LINE, DrawOrder.CANDLE, DrawOrder.SCATTER
5457
};
5558

5659
/**
5760
* enum that allows to specify the order in which the different data objects
5861
* for the combined-chart are drawn
5962
*/
6063
public enum DrawOrder {
61-
BAR, LINE, CANDLE, SCATTER
64+
BAR, BUBBLE, LINE, CANDLE, SCATTER
6265
}
6366

6467
public CombinedChart(Context context) {
@@ -85,10 +88,26 @@ protected void init() {
8588
@Override
8689
protected void calcMinMax() {
8790
super.calcMinMax();
88-
89-
if (getBarData() != null || getCandleData() != null) {
91+
92+
if (getBarData() != null || getCandleData() != null || getBubbleData() != null) {
9093
mXChartMin = -0.5f;
9194
mXChartMax = mData.getXVals().size() - 0.5f;
95+
96+
if (getBubbleData() != null) {
97+
98+
for (BubbleDataSet set : getBubbleData().getDataSets()) {
99+
100+
final float xmin = set.getXMin();
101+
final float xmax = set.getXMax();
102+
103+
if (xmin < mXChartMin)
104+
mXChartMin = xmin;
105+
106+
if (xmax > mXChartMax)
107+
mXChartMax = xmax;
108+
}
109+
}
110+
92111
mDeltaX = Math.abs(mXChartMax - mXChartMin);
93112
}
94113
}
@@ -141,6 +160,13 @@ public CandleData getCandleData() {
141160
return mData.getCandleData();
142161
}
143162

163+
@Override
164+
public BubbleData getBubbleData() {
165+
if (mData == null)
166+
return null;
167+
return mData.getBubbleData();
168+
}
169+
144170
@Override
145171
public boolean isDrawBarShadowEnabled() {
146172
return mDrawBarShadow;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public void setColor(int color) {
4242
}
4343

4444
@Override
45-
protected void calcMinMax()
46-
{
45+
protected void calcMinMax() {
46+
4747
final List<BubbleEntry> entries = getYVals();
4848

4949
// need chart width to guess this properly

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
package com.github.mikephil.charting.data;
33

44
/**
5-
* Subclass of Entry that holds a value for one entry in a BubbleChart.
6-
*
7-
* Bubble chart implementation:
8-
* Copyright 2015 Pierre-Marc Airoldi
9-
* Licensed under Apache License 2.0
5+
* Subclass of Entry that holds a value for one entry in a BubbleChart. Bubble
6+
* chart implementation: Copyright 2015 Pierre-Marc Airoldi Licensed under
7+
* Apache License 2.0
108
*
119
* @author Philipp Jahoda
1210
*/
@@ -19,8 +17,8 @@ public class BubbleEntry extends Entry {
1917
* Constructor.
2018
*
2119
* @param xIndex The index on the x-axis.
22-
* @param val
23-
* @param size The size value.
20+
* @param val The value on the y-axis.
21+
* @param size The size of the bubble.
2422
*/
2523
public BubbleEntry(int xIndex, float val, float size) {
2624
super(val, xIndex);
@@ -32,8 +30,8 @@ public BubbleEntry(int xIndex, float val, float size) {
3230
* Constructor.
3331
*
3432
* @param xIndex The index on the x-axis.
35-
* @param val
36-
* @param size The size value.
33+
* @param val The value on the y-axis.
34+
* @param size The size of the bubble.
3735
* @param data Spot for additional data this Entry represents.
3836
*/
3937
public BubbleEntry(int xIndex, float val, float size, Object data) {
@@ -49,9 +47,8 @@ public BubbleEntry copy() {
4947
return c;
5048
}
5149

52-
5350
/**
54-
* Returns the size of this entry
51+
* Returns the size of this entry (the size of the bubble).
5552
*
5653
* @return
5754
*/

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,8 @@ public List<String> getXVals() {
389389
* @param xVal
390390
*/
391391
public void addXValue(String xVal) {
392-
393-
mXValAverageLength = (mXValAverageLength + xVal.length()) / 2f;
392+
393+
mXValAverageLength = (mXValAverageLength + xVal.length()) / 2f;
394394
mXVals.add(xVal);
395395
}
396396

@@ -891,7 +891,8 @@ public void setDrawValues(boolean enabled) {
891891
}
892892

893893
/**
894-
* Clears this data object from all DataSets and removes all Entries.
894+
* Clears this data object from all DataSets and removes all Entries. Don't
895+
* forget to invalidate the chart after this.
895896
*/
896897
public void clearValues() {
897898
mDataSets.clear();

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class CombinedData extends BarLineScatterCandleData<BarLineScatterCandleD
1515
private BarData mBarData;
1616
private ScatterData mScatterData;
1717
private CandleData mCandleData;
18+
private BubbleData mBubbleData;
1819

1920
public CombinedData() {
2021
super();
@@ -51,6 +52,16 @@ public void setData(CandleData data) {
5152
mDataSets.addAll(data.getDataSets());
5253
init(data.getDataSets());
5354
}
55+
56+
public void setData(BubbleData data) {
57+
mBubbleData = data;
58+
mDataSets.addAll(data.getDataSets());
59+
init(data.getDataSets());
60+
}
61+
62+
public BubbleData getBubbleData() {
63+
return mBubbleData;
64+
}
5465

5566
public LineData getLineData() {
5667
return mLineData;

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public DataSet(List<T> yVals, String label) {
7878

7979
if (mYVals == null)
8080
mYVals = new ArrayList<T>();
81-
81+
8282
mColors = new ArrayList<Integer>();
8383

8484
// default color
@@ -346,7 +346,9 @@ public String toSimpleString() {
346346
*
347347
* @return
348348
*/
349-
public void setLabel(String label) { mLabel = label; }
349+
public void setLabel(String label) {
350+
mLabel = label;
351+
}
350352

351353
/**
352354
* Returns the label string that describes the DataSet.
@@ -430,7 +432,7 @@ public void addEntry(Entry e) {
430432

431433
float val = e.getVal();
432434

433-
if(mYVals == null) {
435+
if (mYVals == null) {
434436
mYVals = new ArrayList<T>();
435437
}
436438

@@ -718,4 +720,12 @@ public boolean contains(Entry e) {
718720

719721
return false;
720722
}
723+
724+
/**
725+
* Removes all values from this DataSet and recalculates min and max value.
726+
*/
727+
public void clear() {
728+
mYVals.clear();
729+
notifyDataSetChanged();
730+
}
721731
}

MPChartLib/src/com/github/mikephil/charting/interfaces/ChartInterface.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public interface ChartInterface {
2121
public float getYChartMin();
2222

2323
public float getYChartMax();
24+
25+
public int getXValCount();
2426

2527
public int getWidth();
2628

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ protected void drawDataSet(Canvas c, BubbleDataSet dataSet) {
7878
mViewPortHandler.contentWidth() * mViewPortHandler.getScaleX() :
7979
mViewPortHandler.contentHeight() * mViewPortHandler.getScaleY();
8080

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

8483
for (int j = minx; j < maxx; j++) {
8584

@@ -190,8 +189,7 @@ public void drawHighlighted(Canvas c, Highlight[] indices) {
190189
mViewPortHandler.contentWidth() * mViewPortHandler.getScaleX() :
191190
mViewPortHandler.contentHeight() * mViewPortHandler.getScaleY();
192191

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

196194
for (Highlight indice : indices) {
197195

0 commit comments

Comments
 (0)