Skip to content

Commit

Permalink
Fixed issue concering LineChart filling (PhilJay#179). Made changes t…
Browse files Browse the repository at this point in the history
…o the logic of filling and added FillFormatter interface for implementing a custom fill logic.
  • Loading branch information
PhilJay committed Oct 30, 2014
1 parent accdd0e commit 04a3ed9
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 6 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -492,13 +492,16 @@ protected void calcMinMax(boolean fixedValues) {
// additional handling for space (default 15% space)
// float space = Math.abs(mDeltaY / 100f * 15f);
float space = Math
.abs(Math.max(Math.abs(mYChartMax), Math.abs(mYChartMin)) / 100f * 15f);
if (Math.abs(mYChartMax) - Math.abs(mYChartMin) < 0.00001f) {
.abs(Math.abs(Math.max(Math.abs(mYChartMax), Math.abs(mYChartMin))) / 100f * 20f);

if (Math.abs(mYChartMax - mYChartMin) < 0.00001f) {
if (Math.abs(mYChartMax) < 10f)
space = 1f;
else
space = Math.abs(mYChartMax / 100f * 15f);
space = Math.abs(mYChartMax / 100f * 20f);
}

Log.i(LOG_TAG, "Space: " + space);

if (mStartAtZero) {

Expand Down
54 changes: 51 additions & 3 deletions MPChartLib/src/com/github/mikephil/charting/charts/LineChart.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
import android.graphics.Path;
import android.util.AttributeSet;

import com.github.mikephil.charting.data.ChartData;
import com.github.mikephil.charting.data.DataSet;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.utils.FillFormatter;

import java.util.ArrayList;

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

private FillFormatter mFillFormatter;

public LineChart(Context context) {
super(context);
}
Expand All @@ -42,6 +47,8 @@ public LineChart(Context context, AttributeSet attrs, int defStyle) {
protected void init() {
super.init();

mFillFormatter = new DefaultFillFormatter();

mCirclePaintInner = new Paint(Paint.ANTI_ALIAS_FLAG);
mCirclePaintInner.setStyle(Paint.Style.FILL);
mCirclePaintInner.setColor(Color.WHITE);
Expand Down Expand Up @@ -263,9 +270,9 @@ && isOffContentBottom(valuePoints[j + 1]))

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

float fillMin = dataSet.getYMin() >= 0 ? mYChartMin : 0;

Path filled = generateFilledPath(entries, fillMin);
Path filled = generateFilledPath(entries,
mFillFormatter.getFillLinePosition(dataSet, mOriginalData, mYChartMax,
mYChartMin));

transformPath(filled);

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

return null;
}

/**
* Default formatter that calculates the position of the filled line.
*
* @author Philipp Jahoda
*/
private class DefaultFillFormatter implements FillFormatter {

@Override
public float getFillLinePosition(LineDataSet dataSet, LineData data,
float chartMaxY, float chartMinY) {

float fillMin = 0f;

if (dataSet.getYMax() > 0 && dataSet.getYMin() < 0) {
fillMin = 0f;
} else {

if (!mStartAtZero) {

float max, min;

if (data.getYMax() > 0)
max = 0f;
else
max = chartMaxY;
if (data.getYMin() < 0)
min = 0f;
else
min = chartMinY;

fillMin = dataSet.getYMin() >= 0 ? min : max;
} else {
fillMin = 0f;
}

}

return fillMin;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

package com.github.mikephil.charting.utils;

import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;

/**
* Interface for providing a custom logic to where the filling line of a DataSet
* should end. If setFillEnabled(...) is set to true.
*
* @author Philipp Jahoda
*/
public interface FillFormatter {

/**
* Returns the vertical (y-axis) position where the filled-line of the
* DataSet should end.
*
* @param dataSet
* @param data
* @param chartMaxY
* @param chartMinY
* @return
*/
public float getFillLinePosition(LineDataSet dataSet, LineData data, float chartMaxY,
float chartMinY);
}

0 comments on commit 04a3ed9

Please sign in to comment.