Skip to content

Commit 04a3ed9

Browse files
committed
Fixed issue concering LineChart filling (#179). Made changes to the logic of filling and added FillFormatter interface for implementing a custom fill logic.
1 parent accdd0e commit 04a3ed9

File tree

4 files changed

+84
-6
lines changed

4 files changed

+84
-6
lines changed

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -492,13 +492,16 @@ protected void calcMinMax(boolean fixedValues) {
492492
// additional handling for space (default 15% space)
493493
// float space = Math.abs(mDeltaY / 100f * 15f);
494494
float space = Math
495-
.abs(Math.max(Math.abs(mYChartMax), Math.abs(mYChartMin)) / 100f * 15f);
496-
if (Math.abs(mYChartMax) - Math.abs(mYChartMin) < 0.00001f) {
495+
.abs(Math.abs(Math.max(Math.abs(mYChartMax), Math.abs(mYChartMin))) / 100f * 20f);
496+
497+
if (Math.abs(mYChartMax - mYChartMin) < 0.00001f) {
497498
if (Math.abs(mYChartMax) < 10f)
498499
space = 1f;
499500
else
500-
space = Math.abs(mYChartMax / 100f * 15f);
501+
space = Math.abs(mYChartMax / 100f * 20f);
501502
}
503+
504+
Log.i(LOG_TAG, "Space: " + space);
502505

503506
if (mStartAtZero) {
504507

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

+51-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
import android.graphics.Path;
88
import android.util.AttributeSet;
99

10+
import com.github.mikephil.charting.data.ChartData;
11+
import com.github.mikephil.charting.data.DataSet;
1012
import com.github.mikephil.charting.data.Entry;
1113
import com.github.mikephil.charting.data.LineData;
1214
import com.github.mikephil.charting.data.LineDataSet;
15+
import com.github.mikephil.charting.utils.FillFormatter;
1316

1417
import java.util.ArrayList;
1518

@@ -26,6 +29,8 @@ public class LineChart extends BarLineChartBase<LineData> {
2629
/** paint for the inner circle of the value indicators */
2730
protected Paint mCirclePaintInner;
2831

32+
private FillFormatter mFillFormatter;
33+
2934
public LineChart(Context context) {
3035
super(context);
3136
}
@@ -42,6 +47,8 @@ public LineChart(Context context, AttributeSet attrs, int defStyle) {
4247
protected void init() {
4348
super.init();
4449

50+
mFillFormatter = new DefaultFillFormatter();
51+
4552
mCirclePaintInner = new Paint(Paint.ANTI_ALIAS_FLAG);
4653
mCirclePaintInner.setStyle(Paint.Style.FILL);
4754
mCirclePaintInner.setColor(Color.WHITE);
@@ -263,9 +270,9 @@ && isOffContentBottom(valuePoints[j + 1]))
263270

264271
// mRenderPaint.setShader(dataSet.getShader());
265272

266-
float fillMin = dataSet.getYMin() >= 0 ? mYChartMin : 0;
267-
268-
Path filled = generateFilledPath(entries, fillMin);
273+
Path filled = generateFilledPath(entries,
274+
mFillFormatter.getFillLinePosition(dataSet, mOriginalData, mYChartMax,
275+
mYChartMin));
269276

270277
transformPath(filled);
271278

@@ -469,4 +476,45 @@ public Paint getPaint(int which) {
469476

470477
return null;
471478
}
479+
480+
/**
481+
* Default formatter that calculates the position of the filled line.
482+
*
483+
* @author Philipp Jahoda
484+
*/
485+
private class DefaultFillFormatter implements FillFormatter {
486+
487+
@Override
488+
public float getFillLinePosition(LineDataSet dataSet, LineData data,
489+
float chartMaxY, float chartMinY) {
490+
491+
float fillMin = 0f;
492+
493+
if (dataSet.getYMax() > 0 && dataSet.getYMin() < 0) {
494+
fillMin = 0f;
495+
} else {
496+
497+
if (!mStartAtZero) {
498+
499+
float max, min;
500+
501+
if (data.getYMax() > 0)
502+
max = 0f;
503+
else
504+
max = chartMaxY;
505+
if (data.getYMin() < 0)
506+
min = 0f;
507+
else
508+
min = chartMinY;
509+
510+
fillMin = dataSet.getYMin() >= 0 ? min : max;
511+
} else {
512+
fillMin = 0f;
513+
}
514+
515+
}
516+
517+
return fillMin;
518+
}
519+
}
472520
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
package com.github.mikephil.charting.utils;
3+
4+
import com.github.mikephil.charting.data.LineData;
5+
import com.github.mikephil.charting.data.LineDataSet;
6+
7+
/**
8+
* Interface for providing a custom logic to where the filling line of a DataSet
9+
* should end. If setFillEnabled(...) is set to true.
10+
*
11+
* @author Philipp Jahoda
12+
*/
13+
public interface FillFormatter {
14+
15+
/**
16+
* Returns the vertical (y-axis) position where the filled-line of the
17+
* DataSet should end.
18+
*
19+
* @param dataSet
20+
* @param data
21+
* @param chartMaxY
22+
* @param chartMinY
23+
* @return
24+
*/
25+
public float getFillLinePosition(LineDataSet dataSet, LineData data, float chartMaxY,
26+
float chartMinY);
27+
}

0 commit comments

Comments
 (0)