Bugfix in the piechart, improved highlighting, added feature for space between slices.

This commit is contained in:
Philipp Jahoda 2014-08-02 22:15:37 +02:00
parent c19b1a415c
commit 28e726a1e4
8 changed files with 123 additions and 94 deletions

View file

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxmassdeveloper.mpchartexample"
android:versionCode="4"
android:versionName="1.0.4" >
android:versionCode="5"
android:versionName="1.0.5" >
<uses-sdk
android:minSdkVersion="9"

View file

@ -1,5 +1,6 @@
package com.xxmassdeveloper.mpchartexample;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
@ -45,11 +46,21 @@ public class PieChartActivity extends DemoBase implements OnSeekBarChangeListene
mChart = (PieChart) findViewById(R.id.chart1);
Typeface tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");
mChart.setValueTypeface(tf);
mChart.setCenterTextTypeface(Typeface.createFromAsset(getAssets(), "OpenSans-Light.ttf"));
ColorTemplate ct = new ColorTemplate();
ct.addDataSetColors(ColorTemplate.COLORFUL_COLORS, this);
ct.addDataSetColors(ColorTemplate.VORDIPLOM_COLORS, this);
mChart.setColorTemplate(ct);
// set a space between the slices
mChart.setSliceSpace(3f);
mChart.setHoleRadius(60f);
mChart.setDrawYValues(true);
mChart.setDrawCenterText(true);

View file

@ -329,18 +329,18 @@ public abstract class BarLineChartBase extends Chart {
if (mYLabels.getPosition() == YLabelPosition.LEFT) {
if (mYChartMin >= 0)
yleft = Utils.calcTextWidth(mYLabelPaint, (int) mDeltaY + ".000" + mUnit);
yleft = Utils.calcTextWidth(mYLabelPaint, (int) mDeltaY + ".00" + mUnit);
else
yleft = Utils.calcTextWidth(mYLabelPaint, (int) (mDeltaY * -1) + ".000" + mUnit);
yleft = Utils.calcTextWidth(mYLabelPaint, (int) (mDeltaY * -1) + ".00" + mUnit);
mYLabelPaint.setTextAlign(Align.RIGHT);
} else if (mYLabels.getPosition() == YLabelPosition.RIGHT) {
if (mYChartMin >= 0)
yright = Utils.calcTextWidth(mYLabelPaint, (int) mDeltaY + ".000" + mUnit);
yright = Utils.calcTextWidth(mYLabelPaint, (int) mDeltaY + ".00" + mUnit);
else
yright = Utils.calcTextWidth(mYLabelPaint, (int) (mDeltaY * -1) + ".000" + mUnit);
yright = Utils.calcTextWidth(mYLabelPaint, (int) (mDeltaY * -1) + ".00" + mUnit);
mYLabelPaint.setTextAlign(Align.LEFT);
@ -349,9 +349,9 @@ public abstract class BarLineChartBase extends Chart {
float width = 0f;
if (mYChartMin >= 0)
width = Utils.calcTextWidth(mYLabelPaint, (int) mDeltaY + ".000" + mUnit);
width = Utils.calcTextWidth(mYLabelPaint, (int) mDeltaY + ".00" + mUnit);
else
width = Utils.calcTextWidth(mYLabelPaint, (int) (mDeltaY * -1) + ".000" + mUnit);
width = Utils.calcTextWidth(mYLabelPaint, (int) (mDeltaY * -1) + ".00" + mUnit);
yright = width;
yleft = width;

View file

@ -58,6 +58,9 @@ public class PieChart extends Chart {
/** indicates the selection distance of a pie slice */
private float mShift = 20f;
/** the space in degrees between the chart-slices, default 0f */
private float mSliceSpace = 0f;
/**
* indicates the size of the hole in the center of the piechart, default:
* radius / 2
@ -191,7 +194,7 @@ public class PieChart extends Chart {
}
@Override
protected void calculateOffsets() {
protected void calculateOffsets() {
if (mDrawLegend) {
if (mLegend.getPosition() == LegendPosition.RIGHT_OF_CHART) {
@ -386,7 +389,8 @@ public class PieChart extends Chart {
// redefine the rect that contains the arc so that the
// highlighted pie is not cut off
mDrawCanvas.drawArc(highlighted, angle, sliceDegrees, true, mRenderPaint);
mDrawCanvas.drawArc(highlighted, angle + mSliceSpace / 2f, sliceDegrees
- mSliceSpace / 2f, true, mRenderPaint);
}
}
}
@ -416,7 +420,8 @@ public class PieChart extends Chart {
if (!needsHighlight(entries.get(j).getXIndex(), i)) {
mRenderPaint.setColor(colors.get(j % colors.size()));
mDrawCanvas.drawArc(mCircleBox, angle, newangle, true, mRenderPaint);
mDrawCanvas.drawArc(mCircleBox, angle + mSliceSpace / 2f, newangle
- mSliceSpace / 2f, true, mRenderPaint);
}
angle += newangle;
@ -887,6 +892,32 @@ public class PieChart extends Chart {
mCenterTextPaint.setTextSize(Utils.convertDpToPixel(size));
}
/**
* sets the space that is left out between the piechart-slices, default: 0°
* --> no space, maximum 45, minimum 0 (no space)
*
* @param degrees
*/
public void setSliceSpace(float degrees) {
if (degrees > 45)
degrees = 45f;
if (degrees < 0)
degrees = 0f;
mSliceSpace = degrees;
}
/**
* returns the space that is set to be between the piechart-slices, in
* degrees
*
* @return
*/
public float getSliceSpace() {
return mSliceSpace;
}
/**
* sets the radius of the hole in the center of the piechart in percent of
* the maximum radius (max = the radius of the whole chart), default 50%

View file

@ -379,8 +379,9 @@ public class BarLineChartTouchListener extends SimpleOnGestureListener implement
Highlight h = mChart.getHighlightByTouchPoint(e.getX(), e.getY());
if (h == null || h.equals(mLastHighlighted)) {
if (h == null || h.equalTo(mLastHighlighted)) {
mChart.highlightValues(null);
mLastHighlighted = null;
} else {
mLastHighlighted = h;
mChart.highlightValues(new Highlight[] {

View file

@ -1,9 +1,6 @@
package com.github.mikephil.charting.listener;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.utils.Highlight;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.view.GestureDetector;
@ -12,6 +9,9 @@ import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.utils.Highlight;
public class PieChartTouchListener extends SimpleOnGestureListener implements OnTouchListener {
private static final float MAX_SCALE = Float.MAX_VALUE; // 10f;
@ -78,36 +78,6 @@ public class PieChartTouchListener extends SimpleOnGestureListener implements On
return matrix;
}
// @Override
// public boolean onDoubleTap(MotionEvent e) {
//
// float[] values = new float[9];
// matrix.getValues(values);
// float sX = values[Matrix.MSCALE_X];
// float minScale = minScale();
//
// if (sX > minScale * 1.5f) {
// matrix.postScale(0.5f, 0.5f, e.getX(), e.getY());
// } else {
// matrix.postScale(2, 2, e.getX(), e.getY());
// }
// limitScale();
// limitPan();
// ctx.update();
//
// return true;
// }
// @Override
// public boolean onSingleTapConfirmed(MotionEvent e) {
//
// PointF pointF = calcImagePosition(start);
//
//
//
// return super.onSingleTapConfirmed(e);
// }
@Override
public void onLongPress(MotionEvent arg0) {
if (mode == NONE) {
@ -120,6 +90,9 @@ public class PieChartTouchListener extends SimpleOnGestureListener implements On
public boolean onSingleTapConfirmed(MotionEvent e) {
return true;
}
/** reference to the last highlighted object */
private Highlight mLastHighlight = null;
@Override
public boolean onSingleTapUp(MotionEvent e) {
@ -130,15 +103,24 @@ public class PieChartTouchListener extends SimpleOnGestureListener implements On
if(distance < mChart.getRadius() / 2 || distance > mChart.getRadius()) {
// if no slice was touched, highlight nothing
mChart.highlightValues(null);
mChart.highlightValues(null);
mLastHighlight = null;
} else {
int index = mChart.getIndexForAngle(mChart.getAngleForPoint(e.getX(), e.getY()));
int dataSetIndex = mChart.getDataSetIndexForIndex(index);
if(dataSetIndex == -1) mChart.highlightValues(null);
mChart.highlightValues(new Highlight[] { new Highlight(index, dataSetIndex) });
Highlight h = new Highlight(index, dataSetIndex);
if(h.equalTo(mLastHighlight)) {
mChart.highlightValues(null);
mLastHighlight = null;
} else {
mChart.highlightValues(new Highlight[] { h });
mLastHighlight = h;
}
}
return true;

View file

@ -1,3 +1,4 @@
package com.github.mikephil.charting.utils;
/**
@ -7,53 +8,56 @@ package com.github.mikephil.charting.utils;
*/
public class Highlight {
/** the x-index of the highlighted value */
private int mXIndex;
/** the x-index of the highlighted value */
private int mXIndex;
/** the index of the dataset the highlighted value is in */
private int mDataSetIndex;
/** the index of the dataset the highlighted value is in */
private int mDataSetIndex;
/**
* constructor
*
* @param x
* the index of the highlighted value on the x-axis
* @param dataSet
* the index of the DataSet the highlighted value belongs to
*/
public Highlight(int x, int dataSet) {
this.mXIndex = x;
this.mDataSetIndex = dataSet;
}
/**
* constructor
*
* @param x the index of the highlighted value on the x-axis
* @param dataSet the index of the DataSet the highlighted value belongs to
*/
public Highlight(int x, int dataSet) {
this.mXIndex = x;
this.mDataSetIndex = dataSet;
}
/**
* returns the index of the DataSet the highlighted value is in
*
* @return
*/
public int getDataSetIndex() {
return mDataSetIndex;
}
/**
* returns the index of the DataSet the highlighted value is in
*
* @return
*/
public int getDataSetIndex() {
return mDataSetIndex;
}
/**
* returns the index of the highlighted value on the x-axis
*
* @return
*/
public int getXIndex() {
return mXIndex;
}
/**
* returns the index of the highlighted value on the x-axis
*
* @return
*/
public int getXIndex() {
return mXIndex;
}
@Override
public boolean equals(Object o) {
if (this != null && o != null) {
if (o instanceof Highlight) {
Highlight other = (Highlight) o;
if (mXIndex == other.mXIndex && mDataSetIndex == other.mDataSetIndex) {
return true;
}
}
}
return false;
}
/**
* returns true if this highlight object is equal to the other
*
* @param h
* @return
*/
public boolean equalTo(Highlight h) {
if (h == null)
return false;
else {
if (this.mDataSetIndex == h.mDataSetIndex && this.mXIndex == h.mXIndex)
return true;
else
return false;
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB