Skip to content

Commit cd68c6f

Browse files
committed
Merge legend customization.
1 parent 7c57220 commit cd68c6f

5 files changed

Lines changed: 88 additions & 68 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.github.mikephil.charting.data.filter.Approximator;
3333
import com.github.mikephil.charting.data.filter.Approximator.ApproximatorType;
3434
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
35+
import com.github.mikephil.charting.utils.ColorTemplate;
3536
import com.github.mikephil.charting.utils.Highlight;
3637
import com.github.mikephil.charting.utils.ValueFormatter;
3738
import com.xxmassdeveloper.mpchartexample.custom.MyValueFormatter;
@@ -114,6 +115,7 @@ protected void onCreate(Bundle savedInstanceState) {
114115
l.setFormSize(9f);
115116
l.setTextSize(11f);
116117
l.setXEntrySpace(4f);
118+
//l.setCustom(ColorTemplate.VORDIPLOM_COLORS, new String[] { "a","b", "c", "d", "e" });
117119

118120
setData(12, 50);
119121

@@ -277,6 +279,8 @@ public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
277279

278280
Log.i("bounds", bounds.toString());
279281
Log.i("position", position.toString());
282+
283+
Log.i("x-index", "low: " + mChart.getLowestVisibleXIndex() + ", high: " + mChart.getHighestVisibleXIndex());
280284
}
281285

282286
public void onNothingSelected() {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ protected void onCreate(Bundle savedInstanceState) {
5353
mChart.setOnChartValueSelectedListener(this);
5454

5555
mChart.setDrawGridBackground(false);
56+
mChart.setDescription("");
5657

5758
// mChart.setStartAtZero(true);
5859

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ public int getLowestVisibleXIndex() {
344344
};
345345

346346
getTransformer(AxisDependency.LEFT).pixelsToValue(pts);
347-
return (int) (((pts[0] <= 0) ? 0 : ((pts[0])) / div) + 1);
347+
return (int) ((pts[0] <= getXChartMin()) ? 0 : (pts[0] / div) + 1);
348348
}
349349

350350
/**

MPChartLib/src/com/github/mikephil/charting/components/Legend.java

Lines changed: 81 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,30 @@ public enum LegendDirection {
3131
LEFT_TO_RIGHT, RIGHT_TO_LEFT
3232
}
3333

34-
/** the legend colors array, each color is for the form drawn at the same index */
34+
/**
35+
* the legend colors array, each color is for the form drawn at the same
36+
* index
37+
*/
3538
private int[] mColors;
3639

3740
/** the legend text array. a null label will start a group. */
3841
private String[] mLabels;
3942

40-
/** colors that will be appended to the end of the colors array after calculating the legend. */
43+
/**
44+
* colors that will be appended to the end of the colors array after
45+
* calculating the legend.
46+
*/
4147
private int[] mExtraColors;
4248

43-
/** labels that will be appended to the end of the labels array after calculating the legend. a null label will start a group. */
49+
/**
50+
* labels that will be appended to the end of the labels array after
51+
* calculating the legend. a null label will start a group.
52+
*/
4453
private String[] mExtraLabels;
4554

4655
/**
47-
* Are the legend labels/colors a custom value or auto calculated? If false, then it's auto, if true, then custom.
48-
* default false (automatic legend)
56+
* Are the legend labels/colors a custom value or auto calculated? If false,
57+
* then it's auto, if true, then custom. default false (automatic legend)
4958
*/
5059
private boolean mIsLegendCustom = false;
5160

@@ -79,7 +88,7 @@ public enum LegendDirection {
7988
private float mFormToTextSpace = 5f;
8089

8190
/** the space that should be left between stacked forms */
82-
private float mStackSpace = 3f;
91+
private float mStackSpace = 3f;
8392

8493
/** default constructor */
8594
public Legend() {
@@ -137,13 +146,15 @@ public Legend(List<Integer> colors, List<String> labels) {
137146
this.mColors = Utils.convertIntegers(colors);
138147
this.mLabels = Utils.convertStrings(labels);
139148
}
140-
149+
141150
public void setColors(List<Integer> colors) {
142-
mColors = Utils.convertIntegers(colors);;
151+
mColors = Utils.convertIntegers(colors);
152+
;
143153
}
144-
154+
145155
public void setLabels(List<String> labels) {
146-
mLabels = Utils.convertStrings(labels);;
156+
mLabels = Utils.convertStrings(labels);
157+
;
147158
}
148159

149160
/**
@@ -209,7 +220,7 @@ public int[] getColors() {
209220
*
210221
* @return
211222
*/
212-
public String[] getLegendLabels() {
223+
public String[] getLabels() {
213224
return mLabels;
214225
}
215226

@@ -234,81 +245,84 @@ public String getLabel(int index) {
234245
}
235246

236247
/**
237-
* colors that will be appended to the end of the colors array after calculating the legend.
248+
* colors that will be appended to the end of the colors array after
249+
* calculating the legend.
238250
*/
239251
public int[] getExtraColors() {
240252
return mExtraColors;
241253
}
242254

243255
/**
244-
* colors that will be appended to the end of the colors array after calculating the legend.
245-
* (if the legend has already been calculated, you will need to call notifyDataSetChanged())
256+
* colors that will be appended to the end of the colors array after
257+
* calculating the legend. (if the legend has already been calculated, you
258+
* will need to call notifyDataSetChanged())
246259
*/
247260
public void setExtraColors(List<Integer> colors) {
248261
this.mExtraColors = Utils.convertIntegers(colors);
249262
}
250263

251264
/**
252-
* labels that will be appended to the end of the labels array after calculating the legend. a null label will start a group.
265+
* labels that will be appended to the end of the labels array after
266+
* calculating the legend. a null label will start a group.
253267
*/
254268
public String[] getExtraLabels() {
255269
return mExtraLabels;
256270
}
257271

258272
/**
259-
* labels that will be appended to the end of the labels array after calculating the legend. a null label will start a group.
260-
* (if the legend has already been calculated, you will need to call notifyDataSetChanged())
273+
* labels that will be appended to the end of the labels array after
274+
* calculating the legend. a null label will start a group. (if the legend
275+
* has already been calculated, you will need to call
276+
* notifyDataSetChanged())
261277
*/
262278
public void setExtraLabels(String[] labels) {
263279
this.mExtraLabels = labels;
264280
}
265281

266282
/**
267-
* Sets a custom legend's labels and colors arrays.
268-
* The colors count should match the labels count.
269-
* * Each color is for the form drawn at the same index.
270-
* * A null label will start a group.
271-
* * A (-2) color will avoid drawing a form
272-
* This will disable the feature that automatically calculates the legend labels and colors from the datasets.
273-
* Call resetLegendToAuto(...) to re-enable automatic calculation.
283+
* Sets a custom legend's labels and colors arrays. The colors count should
284+
* match the labels count. * Each color is for the form drawn at the same
285+
* index. * A null label will start a group. * A (-2) color will avoid
286+
* drawing a form This will disable the feature that automatically
287+
* calculates the legend labels and colors from the datasets. Call
288+
* resetCustom() to re-enable automatic calculation (and then
289+
* notifyDataSetChanged() is needed to auto-calculate the legend again)
274290
*/
275-
public void setLegend(int[] colors, String[] labels)
276-
{
291+
public void setCustom(int[] colors, String[] labels) {
277292
mLabels = labels;
278293
mColors = colors;
279294
mIsLegendCustom = true;
280295
}
281296

282297
/**
283-
* Sets a custom legend's labels and colors arrays.
284-
* The colors count should match the labels count.
285-
* * Each color is for the form drawn at the same index.
286-
* * A null label will start a group.
287-
* * A (-2) color will avoid drawing a form
288-
* This will disable the feature that automatically calculates the legend labels and colors from the datasets.
289-
* Call resetLegendToAuto(...) to re-enable automatic calculation (and then notifyDataSetChanged() is needed)
298+
* Sets a custom legend's labels and colors arrays. The colors count should
299+
* match the labels count. * Each color is for the form drawn at the same
300+
* index. * A null label will start a group. * A (-2) color will avoid
301+
* drawing a form This will disable the feature that automatically
302+
* calculates the legend labels and colors from the datasets. Call
303+
* resetCustom() to re-enable automatic calculation (and then
304+
* notifyDataSetChanged() is needed to auto-calculate the legend again)
290305
*/
291-
public void setLegend(List<Integer> colors, List<String> labels)
292-
{
306+
public void setCustom(List<Integer> colors, List<String> labels) {
293307
mColors = Utils.convertIntegers(colors);
294308
mLabels = Utils.convertStrings(labels);
295309
mIsLegendCustom = true;
296310
}
297311

298312
/**
299-
* Calling this will disable the custom legend labels (set by setLegend(...)). Instead, the labels will again be calculated automatically (after notifyDataSetChanged() is called).
313+
* Calling this will disable the custom legend labels (set by
314+
* setCustom(...)). Instead, the labels will again be calculated
315+
* automatically (after notifyDataSetChanged() is called).
300316
*/
301-
public void resetLegendToAuto()
302-
{
317+
public void resetCustom() {
303318
mIsLegendCustom = false;
304319
}
305320

306321
/**
307-
* @return true if a custom legend labels and colors has been set
308-
* default false (automatic legend)
322+
* @return true if a custom legend labels and colors has been set default
323+
* false (automatic legend)
309324
*/
310-
public boolean isLegendCustom()
311-
{
325+
public boolean isLegendCustom() {
312326
return mIsLegendCustom;
313327
}
314328

@@ -443,28 +457,29 @@ public void setFormToTextSpace(float space) {
443457
this.mFormToTextSpace = Utils.convertDpToPixel(space);
444458
}
445459

446-
// /**
447-
// * applies the state from the legend in the parameter to this legend (except
448-
// * colors, labels and offsets)
449-
// *
450-
// * @param l
451-
// */
452-
// public void apply(Legend l) {
453-
//
454-
// mPosition = l.mPosition;
455-
// mShape = l.mShape;
456-
// mTypeface = l.mTypeface;
457-
// mFormSize = l.mFormSize;
458-
// mXEntrySpace = l.mXEntrySpace;
459-
// mYEntrySpace = l.mYEntrySpace;
460-
// mFormToTextSpace = l.mFormToTextSpace;
461-
// mTextSize = l.mTextSize;
462-
// mStackSpace = l.mStackSpace;
463-
// mTextColor = l.mTextColor;
464-
// mEnabled = l.mEnabled;
465-
// mXOffset = l.mXOffset;
466-
// mYOffset = l.mYOffset;
467-
// }
460+
// /**
461+
// * applies the state from the legend in the parameter to this legend
462+
// (except
463+
// * colors, labels and offsets)
464+
// *
465+
// * @param l
466+
// */
467+
// public void apply(Legend l) {
468+
//
469+
// mPosition = l.mPosition;
470+
// mShape = l.mShape;
471+
// mTypeface = l.mTypeface;
472+
// mFormSize = l.mFormSize;
473+
// mXEntrySpace = l.mXEntrySpace;
474+
// mYEntrySpace = l.mYEntrySpace;
475+
// mFormToTextSpace = l.mFormToTextSpace;
476+
// mTextSize = l.mTextSize;
477+
// mStackSpace = l.mStackSpace;
478+
// mTextColor = l.mTextColor;
479+
// mEnabled = l.mEnabled;
480+
// mXOffset = l.mXOffset;
481+
// mYOffset = l.mYOffset;
482+
// }
468483

469484
/**
470485
* returns the space that is left out between stacked forms (with no label)
@@ -546,9 +561,9 @@ public float getFullHeight(Paint labelpaint) {
546561

547562
/** the total height of the legend (needed height space) */
548563
public float mNeededHeight = 0f;
549-
564+
550565
public float mTextHeightMax = 0f;
551-
566+
552567
public float mTextWidthMax = 0f;
553568

554569
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public void renderLegend(Canvas c) {
168168
mLegendLabelPaint.setTextSize(mLegend.getTextSize());
169169
mLegendLabelPaint.setColor(mLegend.getTextColor());
170170

171-
String[] labels = mLegend.getLegendLabels();
171+
String[] labels = mLegend.getLabels();
172172
int[] colors = mLegend.getColors();
173173

174174
float formToTextSpace = mLegend.getFormToTextSpace();

0 commit comments

Comments
 (0)