Skip to content

Commit 5e6d84e

Browse files
committed
Made some minor improvements to the legend. Optimized PieData and PieChart legend creation and labels.
1 parent a733f7a commit 5e6d84e

12 files changed

Lines changed: 109 additions & 99 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ protected void onCreate(Bundle savedInstanceState) {
112112
Legend l = mChart.getLegend();
113113
l.setPosition(LegendPosition.BELOW_CHART_LEFT);
114114
l.setFormSize(8f);
115-
l.setEntrySpace(4f);
115+
l.setXEntrySpace(4f);
116116

117117
// mChart.setDrawLegend(false);
118118
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,9 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
185185
BarDataSet set1 = new BarDataSet(yVals1, "Company A");
186186
set1.setColors(ColorTemplate.createColors(getApplicationContext(), ColorTemplate.FRESH_COLORS));
187187
BarDataSet set2 = new BarDataSet(yVals2, "Company B");
188-
set2.setColor(getResources().getColor(R.color.liberty_2));
188+
set2.resetColors();
189+
set2.addColor(getResources().getColor(R.color.liberty_2));
190+
set2.addColor(getResources().getColor(R.color.liberty_3));
189191
BarDataSet set3 = new BarDataSet(yVals3, "Company C");
190192
set3.setColors(ColorTemplate.createColors(getApplicationContext(), ColorTemplate.COLORFUL_COLORS));
191193

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,6 @@ protected void onCreate(Bundle savedInstanceState) {
130130

131131
// dont forget to refresh the drawing
132132
mChart.invalidate();
133-
134-
135-
ArrayList<Entry> entries = new ArrayList<Entry>();
136-
137-
for(int i = 0; i < 15; i++) {
138-
entries.add(new Entry(new float[] { 10, 12, 14 }, i));
139-
}
140-
141-
BarDataSet bds = new BarDataSet(entries, "Set");
142-
Toast.makeText(getApplicationContext(), "min: " + bds.getYMin() + ", max: " + bds.getYMax() + ", stacksize: " + bds.getStackSize(), Toast.LENGTH_SHORT).show();
143133
}
144134

145135
@Override

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import com.github.mikephil.charting.data.filter.Approximator.ApproximatorType;
1919
import com.github.mikephil.charting.interfaces.OnChartValueSelectedListener;
2020
import com.github.mikephil.charting.utils.Highlight;
21+
import com.github.mikephil.charting.utils.Legend;
22+
import com.github.mikephil.charting.utils.Legend.LegendPosition;
2123
import com.github.mikephil.charting.utils.XLabels;
2224
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
2325

@@ -71,8 +73,8 @@ protected void onCreate(Bundle savedInstanceState) {
7173
mSeekBarX.setProgress(45);
7274
mSeekBarY.setProgress(100);
7375

74-
// Legend l = mChart.getLegend();
75-
// l.setPosition(LegendPosition.RIGHT_OF_CHART);
76+
Legend l = mChart.getLegend();
77+
l.setPosition(LegendPosition.RIGHT_OF_CHART);
7678
}
7779

7880
@Override

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,12 @@ protected void onCreate(Bundle savedInstanceState) {
6969
// add a selection listener
7070
mChart.setOnChartValueSelectedListener(this);
7171

72-
mSeekBarX.setProgress(5);
72+
mSeekBarX.setProgress(4);
7373
mSeekBarY.setProgress(100);
7474

7575
Legend l = mChart.getLegend();
7676
l.setPosition(LegendPosition.RIGHT_OF_CHART);
77+
l.setYEntrySpace(3f);
7778
}
7879

7980
@Override
@@ -134,6 +135,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
134135
}
135136
return true;
136137
}
138+
139+
private String[] mParties = new String[] { "Party A", "Party B", "Party C", "Party D", "Party E" };
137140

138141
@Override
139142
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
@@ -158,19 +161,13 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
158161
ArrayList<String> xVals = new ArrayList<String>();
159162

160163
for (int i = 0; i < mSeekBarX.getProgress(); i++)
161-
xVals.add("Text" + (i + 1));
164+
xVals.add(mParties[i % mParties.length]);
162165

163-
PieDataSet set1 = new PieDataSet(yVals1, "Content");
166+
PieDataSet set1 = new PieDataSet(yVals1, "Election Results");
164167
set1.setSliceSpace(3f);
165168
set1.setColors(ColorTemplate.createColors(getApplicationContext(), ColorTemplate.VORDIPLOM_COLORS));
166-
// PieDataSet set2 = new PieDataSet(yVals1, "Content 2");
167-
// set2.setSliceSpace(10f);
168-
169-
ArrayList<PieDataSet> dataSets = new ArrayList<PieDataSet>();
170-
dataSets.add(set1);
171-
// dataSets.add(set2);
172169

173-
PieData data = new PieData(xVals, dataSets);
170+
PieData data = new PieData(xVals, set1);
174171
mChart.setData(data);
175172

176173
// undo all highlights

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected void onCreate(Bundle savedInstanceState) {
9595
Legend l = mChart.getLegend();
9696
l.setPosition(LegendPosition.BELOW_CHART_LEFT);
9797
l.setFormSize(8f);
98-
l.setEntrySpace(4f);
98+
l.setXEntrySpace(4f);
9999

100100
// mChart.setDrawLegend(false);
101101
}

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

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.github.mikephil.charting.data.ChartData;
2929
import com.github.mikephil.charting.data.DataSet;
3030
import com.github.mikephil.charting.data.Entry;
31+
import com.github.mikephil.charting.data.PieDataSet;
3132
import com.github.mikephil.charting.interfaces.OnChartValueSelectedListener;
3233
import com.github.mikephil.charting.utils.Highlight;
3334
import com.github.mikephil.charting.utils.Legend;
@@ -433,6 +434,7 @@ public void prepareLegend() {
433434
ArrayList<String> labels = new ArrayList<String>();
434435
ArrayList<Integer> colors = new ArrayList<Integer>();
435436

437+
// loop for building up the colors and labels used in the legend
436438
for (int i = 0; i < mOriginalData.getDataSetCount(); i++) {
437439

438440
DataSet dataSet = mOriginalData.getDataSetByIndex(i);
@@ -452,9 +454,25 @@ public void prepareLegend() {
452454
colors.add(clrs.get(j));
453455
}
454456

457+
// add the legend description label
455458
colors.add(-1);
456459
labels.add(bds.getLabel());
457460

461+
} else if(dataSet instanceof PieDataSet) {
462+
463+
ArrayList<String> xVals = mOriginalData.getXVals();
464+
PieDataSet pds = (PieDataSet) dataSet;
465+
466+
for (int j = 0; j < clrs.size() && j < entryCount && j < xVals.size() ; j++) {
467+
468+
labels.add(xVals.get(j));
469+
colors.add(clrs.get(j));
470+
}
471+
472+
// add the legend description label
473+
colors.add(-1);
474+
labels.add(pds.getLabel());
475+
458476
} else { // all others
459477

460478
for (int j = 0; j < clrs.size() && j < entryCount; j++) {
@@ -474,9 +492,6 @@ public void prepareLegend() {
474492
}
475493
}
476494

477-
// Log.i(LOG_TAG, "Preparing legend, colors size: " + colors.size() +
478-
// ", labels size: " + labels.size());
479-
480495
Legend l = new Legend(colors, labels);
481496

482497
if (mLegend != null) {
@@ -650,7 +665,8 @@ protected void drawLegend() {
650665
float formToTextSpace = mLegend.getFormToTextSpace() + formSize;
651666

652667
// space between the entries
653-
float entrySpace = mLegend.getEntrySpace() + formSize;
668+
float xEntrySpace = mLegend.getXEntrySpace() + formSize;
669+
float yEntrySpace = mLegend.getYEntrySpace() + formSize;
654670

655671
float textSize = mLegendLabelPaint.getTextSize();
656672

@@ -681,9 +697,9 @@ protected void drawLegend() {
681697
if(mLegend.getColors()[i] != -1) posX += formToTextSpace;
682698

683699
mLegend.drawLabel(mDrawCanvas, posX, posY + textDrop, mLegendLabelPaint, i);
684-
posX += Utils.calcTextWidth(mLegendLabelPaint, labels[i]) + entrySpace;
700+
posX += Utils.calcTextWidth(mLegendLabelPaint, labels[i]) + xEntrySpace;
685701
} else {
686-
posX += entrySpace;
702+
posX += xEntrySpace;
687703
}
688704
}
689705

@@ -705,14 +721,14 @@ protected void drawLegend() {
705721
mLegend.drawForm(mDrawCanvas, posX, posY, mLegendFormPaint, i);
706722

707723
// make a step to the left
708-
posX -= entrySpace;
724+
posX -= xEntrySpace;
709725
}
710726

711727
break;
712728
case RIGHT_OF_CHART:
713729

714730
if (this instanceof BarLineChartBase) {
715-
posX = getWidth() - mLegend.getOffsetRight() + Utils.convertDpToPixel(10f);
731+
posX = getWidth() - mLegend.getOffsetRight() + Utils.convertDpToPixel(9f);
716732
posY = mLegend.getOffsetTop();
717733
} else {
718734
posX = getWidth() - mLegend.getMaximumEntryLength(mLegendLabelPaint);
@@ -733,20 +749,20 @@ protected void drawLegend() {
733749
float x = posX;
734750

735751
if(mLegend.getColors()[i] != -1) x+= formToTextSpace;
736-
737752

738753
mLegend.drawLabel(mDrawCanvas, x, posY + textDrop,
739754
mLegendLabelPaint, i);
740755
} else {
741756

742757
mLegend.drawLabel(mDrawCanvas, posX, posY + textSize + formSize
743-
+ mLegend.getEntrySpace(),
744-
mLegendLabelPaint, i);
745-
posY += entrySpace;
746-
}
758+
+ mLegend.getYEntrySpace(),
759+
mLegendLabelPaint, i);
760+
761+
posY += yEntrySpace;
762+
}
747763

748764
// make a step down
749-
posY += entrySpace + textSize;
765+
posY += mLegend.getYEntrySpace() + textSize;
750766
stack = 0f;
751767
} else {
752768
stack += formSize + 4f;

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -304,14 +304,14 @@ protected void prepareContentRect() {
304304
float height = mContentRect.height() + mOffsetTop + mOffsetBottom;
305305

306306
float diameter = getDiameter();
307-
float maxShift = ((PieData) mCurrentData).getMaxShift();
307+
float shift = ((PieData) mCurrentData).getDataSet().getSelectionShift();
308308

309309
// create the circle box that will contain the pie-chart (the bounds of
310310
// the pie-chart)
311-
mCircleBox.set(width / 2 - diameter / 2 + maxShift, height / 2 - diameter / 2
312-
+ maxShift,
313-
width / 2 + diameter / 2 - maxShift, height / 2 + diameter / 2
314-
- maxShift);
311+
mCircleBox.set(width / 2 - diameter / 2 + shift, height / 2 - diameter / 2
312+
+ shift,
313+
width / 2 + diameter / 2 - shift, height / 2 + diameter / 2
314+
- shift);
315315
}
316316

317317
@Override

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public abstract class ChartData {
2828
/** holds all x-values the chart represents */
2929
protected ArrayList<String> mXVals;
3030

31-
private ArrayList<? extends DataSet> mDataSets;
31+
/** array that holds all DataSets the ChartData object represents */
32+
protected ArrayList<? extends DataSet> mDataSets;
3233

3334
/**
3435
* constructor for chart data

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

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,29 @@
33

44
import java.util.ArrayList;
55

6+
/**
7+
* A PieData object can only represent one DataSet. Unlike all other charts, the
8+
* legend labels of the PieChart are created from the x-values array, and not
9+
* from the DataSet labels.
10+
*
11+
* @author Philipp Jahoda
12+
*/
613
public class PieData extends ChartData {
714

8-
/** the maximum shift distance across all PieDataSets this object holds */
9-
private float mMaxShift = 0f;
10-
11-
public PieData(ArrayList<String> xVals, ArrayList<PieDataSet> dataSets) {
12-
super(xVals, dataSets);
13-
14-
calcMaxShift(dataSets);
15-
}
16-
17-
public PieData(String[] xVals, ArrayList<PieDataSet> dataSets) {
18-
super(xVals, dataSets);
19-
20-
calcMaxShift(dataSets);
21-
}
22-
2315
public PieData(ArrayList<String> xVals, PieDataSet dataSet) {
2416
super(xVals, toArrayList(dataSet));
25-
26-
mMaxShift = dataSet.getSelectionShift();
2717
}
2818

2919
public PieData(String[] xVals, PieDataSet dataSet) {
3020
super(xVals, toArrayList(dataSet));
31-
32-
mMaxShift = dataSet.getSelectionShift();
33-
}
34-
35-
/**
36-
* calculates the maximum shift distance across all DataSets
37-
*
38-
* @param sets
39-
*/
40-
private void calcMaxShift(ArrayList<PieDataSet> sets) {
41-
42-
mMaxShift = 0;
43-
44-
for (int i = 0; i < sets.size(); i++) {
45-
if (sets.get(i).getSelectionShift() > mMaxShift)
46-
mMaxShift = sets.get(i).getSelectionShift();
47-
}
4821
}
4922

5023
/**
51-
* returns the maximum shift distance / selection distance across all
52-
* PieDataSets this object holds
24+
* Returns the DataSet this PieData object represents.
5325
*
5426
* @return
5527
*/
56-
public float getMaxShift() {
57-
return mMaxShift;
28+
public PieDataSet getDataSet() {
29+
return (PieDataSet) mDataSets.get(0);
5830
}
5931
}

0 commit comments

Comments
 (0)