Skip to content

Commit

Permalink
Improved LimitLines, added feature to draw labels for LimitLines.
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilJay committed Oct 2, 2014
1 parent 42d40d8 commit 25cafdc
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 209 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.github.mikephil.charting.interfaces.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.Legend;
import com.github.mikephil.charting.utils.Legend.LegendForm;
import com.github.mikephil.charting.utils.LimitLine.LimitLabelPosition;
import com.github.mikephil.charting.utils.LimitLine;
import com.github.mikephil.charting.utils.XLabels;
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
Expand Down Expand Up @@ -59,6 +60,9 @@ protected void onCreate(Bundle savedInstanceState) {
mChart = (LineChart) findViewById(R.id.chart1);
mChart.setOnChartGestureListener(this);
mChart.setOnChartValueSelectedListener(this);

mChart.setUnit(" $");
mChart.setDrawUnitsInChart(true);

// if enabled, the chart will always start at zero on the y-axis
mChart.setStartAtZero(false);
Expand Down Expand Up @@ -346,10 +350,14 @@ private void setData(int count, float range) {
LimitLine ll1 = new LimitLine(130f);
ll1.setLineWidth(4f);
ll1.enableDashedLine(10f, 10f, 0f);
ll1.setDrawValue(true);
ll1.setLabelPosition(LimitLabelPosition.RIGHT);

LimitLine ll2 = new LimitLine(-30f);
ll2.setLineWidth(4f);
ll2.enableDashedLine(10f, 10f, 0f);
ll2.setDrawValue(true);
ll2.setLabelPosition(LimitLabelPosition.RIGHT);

data.addLimitLine(ll1);
data.addLimitLine(ll2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.github.mikephil.charting.utils.Legend;
import com.github.mikephil.charting.utils.Legend.LegendPosition;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.github.mikephil.charting.utils.LimitLine;
import com.github.mikephil.charting.utils.XLabels;
import com.github.mikephil.charting.utils.YLabels;
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
Expand Down
208 changes: 1 addition & 207 deletions MPChartLib/src/com/github/mikephil/charting/charts/BarChart.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,75 +88,6 @@ protected void init() {
// calculate3DColors();
}

// @Override
// public void setColorTemplate(ColorTemplate ct) {
// super.setColorTemplate(ct);
//
// calculate3DColors();
// }

// /** array that holds all the colors for the top 3D effect */
// private ArrayList<ArrayList<Integer>> mTopColors;
//
// /** array that holds all the colors for the side 3D effect */
// private ArrayList<ArrayList<Integer>> mSideColors;
//
// /**
// * calculates the 3D color arrays
// */
// protected void calculate3DColors() {
//
// // generate the colors for the 3D effect
// mTopColors = new ArrayList<ArrayList<Integer>>();
// mSideColors = new ArrayList<ArrayList<Integer>>();
//
// float[] hsv = new float[3];
//
// for (int i = 0; i < mCt.getColors().size(); i++) {
//
// // Get the colors for the DataSet at the current index. If the index
// // is out of bounds, reuse DataSet colors.
// ArrayList<Integer> colors = mCt.getDataSetColors(i);
// ArrayList<Integer> topColors = new ArrayList<Integer>();
// ArrayList<Integer> sideColors = new ArrayList<Integer>();
//
// for (int j = 0; j < colors.size(); j++) {
//
// // extract the color
// int c = colors.get(j);
// Color.colorToHSV(c, hsv); // convert to hsv
//
// // make brighter
// hsv[1] = hsv[1] - 0.1f; // less saturation
// hsv[2] = hsv[2] + 0.1f; // more brightness
//
// // convert back
// c = Color.HSVToColor(hsv);
//
// // assign
// topColors.add(c);
//
// // get color again
// c = colors.get(j);
//
// // convert
// Color.colorToHSV(c, hsv);
//
// // make darker
// hsv[1] = hsv[1] + 0.1f; // more saturation
// hsv[2] = hsv[2] - 0.1f; // less brightness
//
// // reassing
// c = Color.HSVToColor(hsv);
//
// sideColors.add(c);
// }
//
// mTopColors.add(topColors);
// mSideColors.add(sideColors);
// }
// }

@Override
protected void calcMinMax(boolean fixedValues) {
super.calcMinMax(fixedValues);
Expand Down Expand Up @@ -438,144 +369,7 @@ protected void drawVerticalGrid() {
}
}
}

// @Override
// protected void drawData() {
//
// ArrayList<Path> topPaths = new ArrayList<Path>();
// ArrayList<Path> sidePaths = new ArrayList<Path>();
//
// ArrayList<BarDataSet> dataSets = (ArrayList<BarDataSet>)
// mCurrentData.getDataSets();
//
// // preparations for 3D bars
// if (m3DEnabled) {
//
// float[] pts = new float[] {
// 0f, 0f, 1f, 0f
// };
//
// // calculate the depth depending on scale
//
// transformPointArray(pts);
//
// pts[3] = pts[2] - pts[0];
// pts[2] = 0f;
// pts[1] = 0f;
// pts[0] = 0f;
//
// Matrix invert = new Matrix();
//
// mMatrixOffset.invert(invert);
// invert.mapPoints(pts);
//
// mMatrixTouch.invert(invert);
// invert.mapPoints(pts);
//
// mMatrixValueToPx.invert(invert);
// invert.mapPoints(pts);
//
// float depth = Math.abs(pts[3] - pts[1]) * mDepth;
//
// for (int i = 0; i < mCurrentData.getDataSetCount(); i++) {
//
// DataSet dataSet = dataSets.get(i);
// ArrayList<Entry> series = dataSet.getYVals();
//
// for (int j = 0; j < series.size(); j++) {
//
// float x = series.get(j).getXIndex();
// float y = series.get(j).getVal();
// float left = x + mBarSpace / 2f;
// float right = x + 1f - mBarSpace / 2f;
// float top = y >= 0 ? y : 0;
//
// // create the 3D effect paths for the top and side
// Path topPath = new Path();
// topPath.moveTo(left, top);
// topPath.lineTo(left + mSkew, top + depth);
// topPath.lineTo(right + mSkew, top + depth);
// topPath.lineTo(right, top);
//
// topPaths.add(topPath);
//
// Path sidePath = new Path();
// sidePath.moveTo(right, top);
// sidePath.lineTo(right + mSkew, top + depth);
// sidePath.lineTo(right + mSkew, depth);
// sidePath.lineTo(right, 0);
//
// sidePaths.add(sidePath);
// }
// }
//
// transformPaths(topPaths);
// transformPaths(sidePaths);
// }
//
// int cnt = 0;
//
// // 2D drawing
// for (int i = 0; i < mCurrentData.getDataSetCount(); i++) {
//
// BarDataSet dataSet = dataSets.get(i);
// ArrayList<Entry> series = dataSet.getYVals();
//
// // Get the colors for the DataSet at the current index. If the
// // index
// // is out of bounds, reuse DataSet colors.
// ArrayList<Integer> colors = mCt.getDataSetColors(i %
// mCt.getColors().size());
// ArrayList<Integer> colors3DTop = mTopColors.get(i %
// mCt.getColors().size());
// ArrayList<Integer> colors3DSide = mSideColors.get(i %
// mCt.getColors().size());
//
// // do the drawing
// for (int j = 0; j < dataSet.getEntryCount(); j++) {
//
// // Set the color for the currently drawn value. If the index
// // is
// // out of bounds, reuse colors.
// mRenderPaint.setColor(colors.get(j % colors.size()));
//
// int x = series.get(j).getXIndex();
// float y = series.get(j).getVal();
// float left = x + mBarSpace / 2f;
// float right = x + 1f - mBarSpace / 2f;
// float top = y >= 0 ? y : 0;
// float bottom = y <= 0 ? y : 0;
//
// mBarRect.set(left, top, right, bottom);
//
// transformRect(mBarRect);
//
// // avoid drawing outofbounds values
// if (isOffContentRight(mBarRect.left))
// break;
//
// if (isOffContentLeft(mBarRect.right)) {
// cnt++;
// continue;
// }
//
// mDrawCanvas.drawRect(mBarRect, mRenderPaint);
//
// // 3D drawing
// if (m3DEnabled) {
//
// mRenderPaint.setColor(colors3DTop.get(j % colors3DTop.size()));
// mDrawCanvas.drawPath(topPaths.get(cnt), mRenderPaint);
//
// mRenderPaint.setColor(colors3DSide.get(j % colors3DSide.size()));
// mDrawCanvas.drawPath(sidePaths.get(cnt), mRenderPaint);
// }
//
// cnt++;
// }
// }
// }


@Override
protected void drawValues() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.github.mikephil.charting.listener.BarLineChartTouchListener;
import com.github.mikephil.charting.utils.Highlight;
import com.github.mikephil.charting.utils.Legend.LegendPosition;
import com.github.mikephil.charting.utils.LimitLine.LimitLabelPosition;
import com.github.mikephil.charting.utils.LimitLine;
import com.github.mikephil.charting.utils.PointD;
import com.github.mikephil.charting.utils.SelInfo;
Expand Down Expand Up @@ -912,6 +913,36 @@ private void drawLimitLines() {
mLimitLinePaint.setStrokeWidth(l.getLineWidth());

mDrawCanvas.drawLine(pts[0], pts[1], pts[2], pts[3], mLimitLinePaint);

// if drawing the limit-value is enabled
if (l.isDrawValueEnabled()) {

// save text align
Align align = mValuePaint.getTextAlign();

float xOffset = Utils.convertDpToPixel(4f);
float yOffset = l.getLineWidth() + xOffset;
String label = mValueFormatter.getFormattedValue(l.getLimit());

if (mDrawUnitInChart)
label += mUnit;

if (l.getLabelPosition() == LimitLabelPosition.RIGHT) {

mValuePaint.setTextAlign(Align.RIGHT);
mDrawCanvas.drawText(label, getWidth() - mOffsetRight
- xOffset,
pts[1] - yOffset, mValuePaint);

} else {
mValuePaint.setTextAlign(Align.LEFT);
mDrawCanvas.drawText(label, mOffsetLeft
+ xOffset,
pts[1] - yOffset, mValuePaint);
}

mValuePaint.setTextAlign(align);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.github.mikephil.charting.data.RadarData;
import com.github.mikephil.charting.data.RadarDataSet;
import com.github.mikephil.charting.utils.Legend.LegendPosition;
import com.github.mikephil.charting.utils.LimitLine.LimitLabelPosition;
import com.github.mikephil.charting.utils.LimitLine;
import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.XLabels;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public boolean equalTo(Entry e) {
if (e.mXIndex != this.mXIndex)
return false;

if (Math.abs(e.mVal - this.mVal) > 0.0001)
if (Math.abs(e.mVal - this.mVal) > 0.00001f)
return false;

return true;
Expand Down
51 changes: 50 additions & 1 deletion MPChartLib/src/com/github/mikephil/charting/utils/LimitLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,20 @@ public class LimitLine {
/** the color of the limit line */
private int mLineColor = Color.rgb(237, 91, 91);

/** flag that indicates if the value of the limit line is drawn or not */
private boolean mDrawValue = true;

/** the path effect of this LimitLine that makes dashed lines possible */
private DashPathEffect mDashPathEffect = null;

/** indicates the position of the LimitLine label */
private LimitLabelPosition mLabelPosition = LimitLabelPosition.RIGHT;

/** enum that indicates the position of the LimitLine label */
public enum LimitLabelPosition {
LEFT, RIGHT
}

/**
* Constructor with limit.
*
Expand Down Expand Up @@ -115,7 +126,7 @@ public void disableDashedLine() {
public boolean isDashedLineEnabled() {
return mDashPathEffect == null ? false : true;
}

/**
* returns the DashPathEffect that is set for this LimitLine
*
Expand All @@ -124,4 +135,42 @@ public boolean isDashedLineEnabled() {
public DashPathEffect getDashPathEffect() {
return mDashPathEffect;
}

/**
* Set this to true to enable the value of the LimitLine to be drawn next to
* it. Default: true, not supported for RadarChart.
*
* @param enabled
*/
public void setDrawValue(boolean enabled) {
mDrawValue = enabled;
}

/**
* Returns true if drawing the value is enabled, false if not.
*
* @return
*/
public boolean isDrawValueEnabled() {
return mDrawValue;
}

/**
* Sets the position of the LimitLine value label (either on the right or on
* the left edge of the chart). Not supported for RadarChart.
*
* @param pos
*/
public void setLabelPosition(LimitLabelPosition pos) {
mLabelPosition = pos;
}

/**
* Returns the position of the LimitLine label (value).
*
* @return
*/
public LimitLabelPosition getLabelPosition() {
return mLabelPosition;
}
}

0 comments on commit 25cafdc

Please sign in to comment.