Skip to content

Commit a543bb2

Browse files
committed
Improved CombinedChart.
1 parent 67f0889 commit a543bb2

File tree

3 files changed

+207
-100
lines changed

3 files changed

+207
-100
lines changed

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

+36-29
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.view.WindowManager;
99

1010
import com.github.mikephil.charting.charts.CombinedChart;
11+
import com.github.mikephil.charting.charts.CombinedChart.DrawOrder;
1112
import com.github.mikephil.charting.components.XAxis;
1213
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
1314
import com.github.mikephil.charting.components.YAxis;
@@ -43,6 +44,12 @@ protected void onCreate(Bundle savedInstanceState) {
4344
mChart = (CombinedChart) findViewById(R.id.chart1);
4445
mChart.setDescription("");
4546
mChart.setDrawGridBackground(false);
47+
mChart.setDrawBarShadow(false);
48+
49+
// draw bars behind lines
50+
mChart.setDrawOrder(new DrawOrder[] {
51+
DrawOrder.BAR, DrawOrder.LINE, DrawOrder.CANDLE
52+
});
4653

4754
YAxis rightAxis = mChart.getAxisRight();
4855
rightAxis.setDrawGridLines(false);
@@ -57,8 +64,8 @@ protected void onCreate(Bundle savedInstanceState) {
5764

5865
data.setData(generateLineData());
5966
data.setData(generateBarData());
60-
// data.setData(generateScatterData());
61-
// data.setData(generateCandleData());
67+
// data.setData(generateScatterData());
68+
// data.setData(generateCandleData());
6269

6370
mChart.setData(data);
6471
mChart.invalidate();
@@ -153,34 +160,34 @@ private float getRandom(float range, float startsfrom) {
153160
return (float) (Math.random() * range) + startsfrom;
154161
}
155162

156-
@Override
157-
public boolean onCreateOptionsMenu(Menu menu) {
158-
getMenuInflater().inflate(R.menu.combined, menu);
159-
return true;
160-
}
161-
162-
@Override
163-
public boolean onOptionsItemSelected(MenuItem item) {
164-
switch (item.getItemId()) {
165-
case R.id.actionToggleLineValues: {
166-
for (DataSet<?> set : mChart.getData().getDataSets()) {
167-
if(set instanceof LineDataSet)
168-
set.setDrawValues(!set.isDrawValuesEnabled());
169-
}
163+
@Override
164+
public boolean onCreateOptionsMenu(Menu menu) {
165+
getMenuInflater().inflate(R.menu.combined, menu);
166+
return true;
167+
}
170168

171-
mChart.invalidate();
172-
break;
173-
}
174-
case R.id.actionToggleBarValues: {
175-
for (DataSet<?> set : mChart.getData().getDataSets()) {
176-
if(set instanceof BarDataSet)
177-
set.setDrawValues(!set.isDrawValuesEnabled());
169+
@Override
170+
public boolean onOptionsItemSelected(MenuItem item) {
171+
switch (item.getItemId()) {
172+
case R.id.actionToggleLineValues: {
173+
for (DataSet<?> set : mChart.getData().getDataSets()) {
174+
if (set instanceof LineDataSet)
175+
set.setDrawValues(!set.isDrawValuesEnabled());
176+
}
177+
178+
mChart.invalidate();
179+
break;
180+
}
181+
case R.id.actionToggleBarValues: {
182+
for (DataSet<?> set : mChart.getData().getDataSets()) {
183+
if (set instanceof BarDataSet)
184+
set.setDrawValues(!set.isDrawValuesEnabled());
185+
}
186+
187+
mChart.invalidate();
188+
break;
189+
}
178190
}
179-
180-
mChart.invalidate();
181-
break;
182-
}
191+
return true;
183192
}
184-
return true;
185-
}
186193
}

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

+102-10
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,41 @@
2525
public class CombinedChart extends BarLineChartBase<CombinedData> implements LineDataProvider,
2626
BarDataProvider, ScatterDataProvider, CandleDataProvider {
2727

28-
private FillFormatter mFillFormatter;
28+
/** the fill-formatter used for determining the position of the fill-line */
29+
protected FillFormatter mFillFormatter;
30+
31+
/** flag that enables or disables the highlighting arrow */
32+
private boolean mDrawHighlightArrow = false;
33+
34+
/**
35+
* if set to true, all values are drawn above their bars, instead of below
36+
* their top
37+
*/
38+
private boolean mDrawValueAboveBar = true;
39+
40+
/**
41+
* if set to true, all values of a stack are drawn individually, and not
42+
* just their sum
43+
*/
44+
private boolean mDrawValuesForWholeStack = true;
45+
46+
/**
47+
* if set to true, a grey area is darawn behind each bar that indicates the
48+
* maximum value
49+
*/
50+
private boolean mDrawBarShadow = true;
51+
52+
protected DrawOrder[] mDrawOrder = new DrawOrder[] {
53+
DrawOrder.BAR, DrawOrder.LINE, DrawOrder.CANDLE, DrawOrder.SCATTER
54+
};
55+
56+
/**
57+
* enum that allows to specify the order in which the different data objects
58+
* for the combined-chart are drawn
59+
*/
60+
public enum DrawOrder {
61+
BAR, LINE, CANDLE, SCATTER
62+
}
2963

3064
public CombinedChart(Context context) {
3165
super(context);
@@ -52,7 +86,7 @@ protected void init() {
5286
protected void calcMinMax() {
5387
super.calcMinMax();
5488

55-
if (getBarData() != null) {
89+
if (getBarData() != null || getCandleData() != null) {
5690
mXChartMin = -0.5f;
5791
mXChartMax = mData.getXVals().size() - 0.5f;
5892
mDeltaX = Math.abs(mXChartMax - mXChartMin);
@@ -109,25 +143,83 @@ public CandleData getCandleData() {
109143

110144
@Override
111145
public boolean isDrawBarShadowEnabled() {
112-
// TODO Auto-generated method stub
113-
return false;
146+
return mDrawBarShadow;
114147
}
115148

116149
@Override
117150
public boolean isDrawValueAboveBarEnabled() {
118-
// TODO Auto-generated method stub
119-
return true;
151+
return mDrawValueAboveBar;
120152
}
121153

122154
@Override
123155
public boolean isDrawHighlightArrowEnabled() {
124-
// TODO Auto-generated method stub
125-
return false;
156+
return mDrawHighlightArrow;
126157
}
127158

128159
@Override
129160
public boolean isDrawValuesForWholeStackEnabled() {
130-
// TODO Auto-generated method stub
131-
return false;
161+
return mDrawValuesForWholeStack;
162+
}
163+
164+
/**
165+
* set this to true to draw the highlightning arrow
166+
*
167+
* @param enabled
168+
*/
169+
public void setDrawHighlightArrow(boolean enabled) {
170+
mDrawHighlightArrow = enabled;
171+
}
172+
173+
/**
174+
* If set to true, all values are drawn above their bars, instead of below
175+
* their top.
176+
*
177+
* @param enabled
178+
*/
179+
public void setDrawValueAboveBar(boolean enabled) {
180+
mDrawValueAboveBar = enabled;
181+
}
182+
183+
/**
184+
* if set to true, all values of a stack are drawn individually, and not
185+
* just their sum
186+
*
187+
* @param enabled
188+
*/
189+
public void setDrawValuesForWholeStack(boolean enabled) {
190+
mDrawValuesForWholeStack = enabled;
191+
}
192+
193+
/**
194+
* If set to true, a grey area is drawn behind each bar that indicates the
195+
* maximum value. Enabling his will reduce performance by about 50%.
196+
*
197+
* @param enabled
198+
*/
199+
public void setDrawBarShadow(boolean enabled) {
200+
mDrawBarShadow = enabled;
201+
}
202+
203+
/**
204+
* Returns the currently set draw order.
205+
*
206+
* @return
207+
*/
208+
public DrawOrder[] getDrawOrder() {
209+
return mDrawOrder;
210+
}
211+
212+
/**
213+
* Sets the order in which the provided data objects should be drawn. The
214+
* earlier you place them in the provided array, the further they will be in
215+
* the background. e.g. if you provide new DrawOrer[] { DrawOrder.BAR,
216+
* DrawOrder.LINE }, the bars will be drawn behind the lines.
217+
*
218+
* @param order
219+
*/
220+
public void setDrawOrder(DrawOrder[] order) {
221+
if (order == null || order.length <= 0)
222+
return;
223+
mDrawOrder = order;
132224
}
133225
}

0 commit comments

Comments
 (0)