Skip to content

Commit a733f7a

Browse files
committed
Implemented highlighting for stacked-bars, legends now supported for stacked-bars.
1 parent 0b30293 commit a733f7a

10 files changed

Lines changed: 142 additions & 44 deletions

File tree

‎MPChartExample/src/com/xxmassdeveloper/mpchartexample/StackedBarActivity.java‎

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package com.xxmassdeveloper.mpchartexample;
33

44
import android.os.Bundle;
5+
import android.util.Log;
56
import android.view.Menu;
67
import android.view.MenuItem;
78
import android.view.WindowManager;
@@ -16,7 +17,9 @@
1617
import com.github.mikephil.charting.data.Entry;
1718
import com.github.mikephil.charting.data.filter.Approximator;
1819
import com.github.mikephil.charting.data.filter.Approximator.ApproximatorType;
20+
import com.github.mikephil.charting.interfaces.OnChartValueSelectedListener;
1921
import com.github.mikephil.charting.utils.ColorTemplate;
22+
import com.github.mikephil.charting.utils.Highlight;
2023
import com.github.mikephil.charting.utils.Legend;
2124
import com.github.mikephil.charting.utils.Legend.LegendPosition;
2225
import com.github.mikephil.charting.utils.XLabels;
@@ -27,7 +30,8 @@
2730

2831
import java.util.ArrayList;
2932

30-
public class StackedBarActivity extends DemoBase implements OnSeekBarChangeListener {
33+
public class StackedBarActivity extends DemoBase implements OnSeekBarChangeListener,
34+
OnChartValueSelectedListener {
3135

3236
private BarChart mChart;
3337
private SeekBar mSeekBarX, mSeekBarY;
@@ -50,6 +54,7 @@ protected void onCreate(Bundle savedInstanceState) {
5054
mSeekBarY.setOnSeekBarChangeListener(this);
5155

5256
mChart = (BarChart) findViewById(R.id.chart1);
57+
mChart.setOnChartValueSelectedListener(this);
5358

5459
// enable the drawing of values
5560
mChart.setDrawYValues(true);
@@ -70,8 +75,6 @@ protected void onCreate(Bundle savedInstanceState) {
7075
// scaling can now only be done on x- and y-axis separately
7176
mChart.setPinchZoom(false);
7277

73-
mChart.setUnit(" €");
74-
7578
mChart.setDrawBarShadow(false);
7679

7780
// change the position of the y-labels
@@ -207,20 +210,25 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
207210
ArrayList<Entry> yVals1 = new ArrayList<Entry>();
208211

209212
for (int i = 0; i < mSeekBarX.getProgress(); i++) {
210-
float mult = (mSeekBarY.getProgress() + 1);
213+
float mult = (mSeekBarY.getProgress() + 1) * 1000;
211214
float val1 = (float) (Math.random() * mult) + mult / 3;
212215
float val2 = (float) (Math.random() * mult) + mult / 3;
213216
float val3 = (float) (Math.random() * mult) + mult / 3;
214-
215-
yVals1.add(new Entry(new float[] { val1, val2, val3 }, i));
217+
218+
yVals1.add(new Entry(new float[] {
219+
val1, val2, val3
220+
}, i));
216221
}
217222

218-
BarDataSet set1 = new BarDataSet(yVals1, "DataSet");
219-
set1.setColors(ColorTemplate.createColors(getApplicationContext(), ColorTemplate.VORDIPLOM_COLORS));
220-
223+
BarDataSet set1 = new BarDataSet(yVals1, "Statistics Europe 2014");
224+
set1.setColors(ColorTemplate.createColors(getApplicationContext(),
225+
ColorTemplate.VORDIPLOM_COLORS));
226+
set1.setStackLabels(new String[] {
227+
"Births", "Divorces", "Marriages"
228+
});
229+
221230
ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
222231
dataSets.add(set1);
223-
224232

225233
BarData data = new BarData(xVals, dataSets);
226234

@@ -239,4 +247,17 @@ public void onStopTrackingTouch(SeekBar seekBar) {
239247
// TODO Auto-generated method stub
240248

241249
}
250+
251+
@Override
252+
public void onValuesSelected(Entry[] values, Highlight[] highlights) {
253+
254+
Log.i("VALUE SELECTED",
255+
values[0].toString() + ", dataSet: " + highlights[0].getDataSetIndex());
256+
}
257+
258+
@Override
259+
public void onNothingSelected() {
260+
// TODO Auto-generated method stub
261+
262+
}
242263
}

‎MPChartLib/src/com/github/mikephil/charting/charts/BarChart.java‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.github.mikephil.charting.data.BarData;
1313
import com.github.mikephil.charting.data.BarDataSet;
1414
import com.github.mikephil.charting.data.Entry;
15+
import com.github.mikephil.charting.utils.Highlight;
1516
import com.github.mikephil.charting.utils.Utils;
1617

1718
import java.util.ArrayList;
@@ -186,21 +187,25 @@ protected void drawHighlights() {
186187

187188
for (int i = 0; i < mIndicesToHightlight.length; i++) {
188189

189-
int index = mIndicesToHightlight[i].getXIndex();
190+
Highlight h = mIndicesToHightlight[i];
191+
int index = h.getXIndex();
190192

191-
int dataSetIndex = mIndicesToHightlight[i].getDataSetIndex();
193+
int dataSetIndex = h.getDataSetIndex();
192194
BarDataSet ds = (BarDataSet) mCurrentData.getDataSetByIndex(dataSetIndex);
193195

194196
// check outofbounds
195197
if (index < mCurrentData.getYValCount() && index >= 0) {
196198

197199
mHighlightPaint.setAlpha(120);
200+
201+
Entry e = getEntryByDataSetIndex(index, dataSetIndex);
198202

199-
float y = getYValueByDataSetIndex(index, dataSetIndex);
200-
float left = index + ds.getBarSpace() / 2f;
201-
float right = index + 1f - ds.getBarSpace() / 2f;
203+
float y = e.getSum();
202204
float top = y >= 0 ? y : 0;
203205
float bottom = y <= 0 ? y : 0;
206+
207+
float left = index + ds.getBarSpace() / 2f;
208+
float right = index + 1f - ds.getBarSpace() / 2f;
204209

205210
RectF highlight = new RectF(left, top, right, bottom);
206211
transformRect(highlight);

‎MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1544,7 +1544,7 @@ public Highlight getHighlightByTouchPoint(float x, float y) {
15441544
if (dataSetIndex == -1)
15451545
return null;
15461546

1547-
return new Highlight(xIndex, dataSetIndex);
1547+
return new Highlight(xIndex, (float) yTouchVal, dataSetIndex);
15481548
}
15491549

15501550
/**

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

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import com.github.mikephil.charting.data.DataSet;
3030
import com.github.mikephil.charting.data.Entry;
3131
import com.github.mikephil.charting.interfaces.OnChartValueSelectedListener;
32-
import com.github.mikephil.charting.utils.ColorTemplate;
3332
import com.github.mikephil.charting.utils.Highlight;
3433
import com.github.mikephil.charting.utils.Legend;
3534
import com.github.mikephil.charting.utils.MarkerView;
@@ -439,33 +438,29 @@ public void prepareLegend() {
439438
DataSet dataSet = mOriginalData.getDataSetByIndex(i);
440439

441440
ArrayList<Integer> clrs = dataSet.getColors();
442-
int dataSetCount = dataSet.getEntryCount();
441+
int entryCount = dataSet.getEntryCount();
443442

443+
// if we have a barchart with stacked bars
444444
if(dataSet instanceof BarDataSet && ((BarDataSet) dataSet).getStackSize() > 1) {
445445

446446
BarDataSet bds = (BarDataSet) dataSet;
447+
String[] sLabels = bds.getStackLabels();
447448

448-
for (int j = 0; j < clrs.size() && j < dataSetCount && j < bds.getStackSize(); j++) {
449-
450-
// if multiple colors are set for a DataSet, group them
451-
if (j < clrs.size() - 1 && j < dataSetCount - 1 && j < bds.getStackSize() - 1) {
452-
453-
labels.add(null);
454-
} else { // add label to the last entry
455-
456-
String label = mOriginalData.getDataSetByIndex(i).getLabel();
457-
labels.add(label);
458-
}
459-
449+
for (int j = 0; j < clrs.size() && j < entryCount && j < bds.getStackSize(); j++) {
450+
451+
labels.add(sLabels[j % sLabels.length]);
460452
colors.add(clrs.get(j));
461453
}
462454

463-
} else {
455+
colors.add(-1);
456+
labels.add(bds.getLabel());
457+
458+
} else { // all others
464459

465-
for (int j = 0; j < clrs.size() && j < dataSetCount; j++) {
460+
for (int j = 0; j < clrs.size() && j < entryCount; j++) {
466461

467462
// if multiple colors are set for a DataSet, group them
468-
if (j < clrs.size() - 1 && j < dataSetCount - 1) {
463+
if (j < clrs.size() - 1 && j < entryCount - 1) {
469464

470465
labels.add(null);
471466
} else { // add label to the last entry
@@ -683,7 +678,7 @@ protected void drawLegend() {
683678
if (labels[i] != null) {
684679

685680
// make a step to the left
686-
posX += formToTextSpace;
681+
if(mLegend.getColors()[i] != -1) posX += formToTextSpace;
687682

688683
mLegend.drawLabel(mDrawCanvas, posX, posY + textDrop, mLegendLabelPaint, i);
689684
posX += Utils.calcTextWidth(mLegendLabelPaint, labels[i]) + entrySpace;
@@ -704,7 +699,7 @@ protected void drawLegend() {
704699

705700
posX -= Utils.calcTextWidth(mLegendLabelPaint, labels[i]);
706701
mLegend.drawLabel(mDrawCanvas, posX, posY + textDrop, mLegendLabelPaint, i);
707-
posX -= formToTextSpace;
702+
if(mLegend.getColors()[i] != -1) posX -= formToTextSpace;
708703
}
709704

710705
mLegend.drawForm(mDrawCanvas, posX, posY, mLegendFormPaint, i);
@@ -734,7 +729,13 @@ protected void drawLegend() {
734729
if (labels[i] != null) {
735730

736731
if (!wasStacked) {
737-
mLegend.drawLabel(mDrawCanvas, posX + formToTextSpace, posY + textDrop,
732+
733+
float x = posX;
734+
735+
if(mLegend.getColors()[i] != -1) x+= formToTextSpace;
736+
737+
738+
mLegend.drawLabel(mDrawCanvas, x, posY + textDrop,
738739
mLegendLabelPaint, i);
739740
} else {
740741

‎MPChartLib/src/com/github/mikephil/charting/data/BarDataSet.java‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public class BarDataSet extends DataSet {
1111
private float mBarSpace = 0.15f;
1212

1313
/**
14-
* the number of bars that are stacked upon each other, this value is
15-
* calculated from the Entries that are added to the DataSet
14+
* the maximum number of bars that are stacked upon each other, this value
15+
* is calculated from the Entries that are added to the DataSet
1616
*/
1717
private int mStackSize = 1;
1818

@@ -24,6 +24,12 @@ public class BarDataSet extends DataSet {
2424
*/
2525
private int mEntryCountStacks = 0;
2626

27+
/**
28+
* array of labels used to describe the different values of the stacked bars
29+
*/
30+
private String[] mStackLabels = new String[] { "Stack" };
31+
32+
2733
public BarDataSet(ArrayList<Entry> yVals, String label) {
2834
super(yVals, label);
2935

@@ -149,4 +155,22 @@ public void setBarShadowColor(int color) {
149155
public int getBarShadowColor() {
150156
return mBarShadowColor;
151157
}
158+
159+
/**
160+
* Sets labels for different values of bar-stacks, in case there are one.
161+
*
162+
* @param labels
163+
*/
164+
public void setStackLabels(String[] labels) {
165+
mStackLabels = labels;
166+
}
167+
168+
/**
169+
* returns the labels used for the different value-stacks
170+
*
171+
* @return
172+
*/
173+
public String[] getStackLabels() {
174+
return mStackLabels;
175+
}
152176
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@ public int getEntryCount() {
131131
*/
132132
public float getYValForXIndex(int xIndex) {
133133

134-
Entry s = getEntryForXIndex(xIndex);
134+
Entry e = getEntryForXIndex(xIndex);
135135

136-
if (s != null)
137-
return s.getVal();
136+
if (e != null)
137+
return e.getSum();
138138
else
139139
return Float.NaN;
140140
}

‎MPChartLib/src/com/github/mikephil/charting/data/Entry.java‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,33 @@ public Entry copy() {
138138
public String toString() {
139139
return "Entry, xIndex: " + mXIndex + " val (sum): " + getSum();
140140
}
141+
142+
/**
143+
* Returns the closest value inside the values array (for stacked barchart)
144+
* to the value given as a parameter. The closest value must be higher
145+
* (above) the provided value.
146+
*
147+
* @param val
148+
* @return
149+
*/
150+
public int getClosestIndexAbove(float val) {
151+
152+
if (mVals == null)
153+
return 0;
154+
155+
float dist = 0f;
156+
int closestIndex = 0;
157+
158+
for (int i = 0; i < mVals.length; i++) {
159+
160+
float newDist = Math.abs((getSum() - mVals[i]) - val);
161+
162+
if (newDist < dist && mVals[i] > val) {
163+
dist = newDist;
164+
closestIndex = i;
165+
}
166+
}
167+
168+
return closestIndex;
169+
}
141170
}

‎MPChartLib/src/com/github/mikephil/charting/listener/PieChartTouchListener.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public boolean onSingleTapUp(MotionEvent e) {
9797
int index = mChart.getIndexForAngle(mChart.getAngleForPoint(e.getX(), e.getY()));
9898
int dataSetIndex = mChart.getDataSetIndexForIndex(index);
9999

100-
Highlight h = new Highlight(index, dataSetIndex);
100+
Highlight h = new Highlight(index, 0f, dataSetIndex);
101101

102102
if(h.equalTo(mLastHighlight)) {
103103

‎MPChartLib/src/com/github/mikephil/charting/utils/Highlight.java‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,22 @@ public class Highlight {
1111
/** the x-index of the highlighted value */
1212
private int mXIndex;
1313

14+
/** the value (on y-axis) representing the touch position */
15+
private float mVal;
16+
1417
/** the index of the dataset the highlighted value is in */
1518
private int mDataSetIndex;
1619

1720
/**
1821
* constructor
1922
*
2023
* @param x the index of the highlighted value on the x-axis
24+
* @param val the value at the position the user touched
2125
* @param dataSet the index of the DataSet the highlighted value belongs to
2226
*/
23-
public Highlight(int x, int dataSet) {
27+
public Highlight(int x, float val, int dataSet) {
2428
this.mXIndex = x;
29+
this.mVal = val;
2530
this.mDataSetIndex = dataSet;
2631
}
2732

@@ -44,7 +49,18 @@ public int getXIndex() {
4449
}
4550

4651
/**
47-
* returns true if this highlight object is equal to the other
52+
* returns the value (on the y-axis) that was highlighted (the value
53+
* representing the touch position)
54+
*
55+
* @return
56+
*/
57+
public float getVal() {
58+
return mVal;
59+
}
60+
61+
/**
62+
* returns true if this highlight object is equal to the other (compares
63+
* xIndex and dataSetIndex)
4864
*
4965
* @param h
5066
* @return

‎MPChartLib/src/com/github/mikephil/charting/utils/Legend.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ public void setFormToTextSpace(float space) {
289289
*/
290290
public void drawForm(Canvas c, float x, float y, Paint p, int index) {
291291

292+
if(mColors[index] == -1) return;
293+
292294
p.setColor(mColors[index]);
293295

294296
float half = mFormSize / 2f;

0 commit comments

Comments
 (0)