Add feature that allows different styling for value and entry labels (former slice-text)
This commit is contained in:
parent
c18a2e270c
commit
82d5fde6f2
5 changed files with 89 additions and 29 deletions
|
@ -100,6 +100,11 @@ public class PieChartActivity extends DemoBase implements OnSeekBarChangeListene
|
|||
l.setXEntrySpace(7f);
|
||||
l.setYEntrySpace(0f);
|
||||
l.setYOffset(0f);
|
||||
|
||||
// entry label styling
|
||||
mChart.setEntryLabelColor(Color.WHITE);
|
||||
mChart.setEntryLabelTypeface(mTfRegular);
|
||||
mChart.setEntryLabelTextSize(12f);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -137,7 +142,7 @@ public class PieChartActivity extends DemoBase implements OnSeekBarChangeListene
|
|||
}
|
||||
case R.id.actionToggleXVals: {
|
||||
|
||||
mChart.setDrawSliceText(!mChart.isDrawSliceTextEnabled());
|
||||
mChart.setDrawEntryLabels(!mChart.isDrawEntryLabelsEnabled());
|
||||
mChart.invalidate();
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -141,7 +141,7 @@ public class PiePolylineChartActivity extends DemoBase implements OnSeekBarChang
|
|||
}
|
||||
case R.id.actionToggleXVals: {
|
||||
|
||||
mChart.setDrawSliceText(!mChart.isDrawSliceTextEnabled());
|
||||
mChart.setDrawEntryLabels(!mChart.isDrawEntryLabelsEnabled());
|
||||
mChart.invalidate();
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -10,8 +10,6 @@ import android.graphics.Typeface;
|
|||
import android.util.AttributeSet;
|
||||
|
||||
import com.github.mikephil.charting.components.XAxis;
|
||||
import com.github.mikephil.charting.data.DataSet;
|
||||
import com.github.mikephil.charting.data.Entry;
|
||||
import com.github.mikephil.charting.data.PieData;
|
||||
import com.github.mikephil.charting.highlight.Highlight;
|
||||
import com.github.mikephil.charting.highlight.PieHighlighter;
|
||||
|
@ -35,9 +33,9 @@ public class PieChart extends PieRadarChartBase<PieData> {
|
|||
private RectF mCircleBox = new RectF();
|
||||
|
||||
/**
|
||||
* flag indicating if the x-labels should be drawn or not
|
||||
* flag indicating if entry labels should be drawn or not
|
||||
*/
|
||||
private boolean mDrawXLabels = true;
|
||||
private boolean mDrawEntryLabels = true;
|
||||
|
||||
/**
|
||||
* array that holds the width of each pie-slice in degrees
|
||||
|
@ -560,21 +558,59 @@ public class PieChart extends PieRadarChartBase<PieData> {
|
|||
}
|
||||
|
||||
/**
|
||||
* set this to true to draw the x-value text into the pie slices
|
||||
* Set this to true to draw the entry labels into the pie slices (Provided by the getLabel() method of the PieEntry class).
|
||||
* Deprecated -> use setDrawEntryLabels(...) instead.
|
||||
*
|
||||
* @param enabled
|
||||
*/
|
||||
@Deprecated
|
||||
public void setDrawSliceText(boolean enabled) {
|
||||
mDrawXLabels = enabled;
|
||||
mDrawEntryLabels = enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns true if drawing x-values is enabled, false if not
|
||||
* Set this to true to draw the entry labels into the pie slices (Provided by the getLabel() method of the PieEntry class).
|
||||
*
|
||||
* @param enabled
|
||||
*/
|
||||
public void setDrawEntryLabels(boolean enabled) {
|
||||
mDrawEntryLabels = enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if drawing the entry labels is enabled, false if not.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean isDrawSliceTextEnabled() {
|
||||
return mDrawXLabels;
|
||||
public boolean isDrawEntryLabelsEnabled() {
|
||||
return mDrawEntryLabels;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the color the entry labels are drawn with.
|
||||
*
|
||||
* @param color
|
||||
*/
|
||||
public void setEntryLabelColor(int color) {
|
||||
((PieChartRenderer) mRenderer).getPaintEntryLabels().setColor(color);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a custom Typeface for the drawing of the entry labels.
|
||||
*
|
||||
* @param tf
|
||||
*/
|
||||
public void setEntryLabelTypeface(Typeface tf) {
|
||||
((PieChartRenderer) mRenderer).getPaintEntryLabels().setTypeface(tf);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the size of the entry labels in dp. Default: 13dp
|
||||
*
|
||||
* @param size
|
||||
*/
|
||||
public void setEntryLabelTextSize(float size) {
|
||||
((PieChartRenderer) mRenderer).getPaintEntryLabels().setTextSize(Utils.convertDpToPixel(size));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -18,7 +18,6 @@ import android.text.TextPaint;
|
|||
import com.github.mikephil.charting.animation.ChartAnimator;
|
||||
import com.github.mikephil.charting.charts.LineChart;
|
||||
import com.github.mikephil.charting.charts.PieChart;
|
||||
import com.github.mikephil.charting.data.DataSet;
|
||||
import com.github.mikephil.charting.data.Entry;
|
||||
import com.github.mikephil.charting.data.PieData;
|
||||
import com.github.mikephil.charting.data.PieDataSet;
|
||||
|
@ -51,6 +50,11 @@ public class PieChartRenderer extends DataRenderer {
|
|||
*/
|
||||
private TextPaint mCenterTextPaint;
|
||||
|
||||
/**
|
||||
* paint object used for drwing the slice-text
|
||||
*/
|
||||
private Paint mEntryLabelsPaint;
|
||||
|
||||
private StaticLayout mCenterTextLayout;
|
||||
private CharSequence mCenterTextLastValue;
|
||||
private RectF mCenterTextLastBounds = new RectF();
|
||||
|
@ -85,6 +89,11 @@ public class PieChartRenderer extends DataRenderer {
|
|||
mValuePaint.setColor(Color.WHITE);
|
||||
mValuePaint.setTextAlign(Align.CENTER);
|
||||
|
||||
mEntryLabelsPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||
mEntryLabelsPaint.setColor(Color.WHITE);
|
||||
mEntryLabelsPaint.setTextAlign(Align.CENTER);
|
||||
mEntryLabelsPaint.setTextSize(Utils.convertDpToPixel(13f));
|
||||
|
||||
mValueLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||
mValueLinePaint.setStyle(Style.STROKE);
|
||||
}
|
||||
|
@ -101,6 +110,10 @@ public class PieChartRenderer extends DataRenderer {
|
|||
return mCenterTextPaint;
|
||||
}
|
||||
|
||||
public Paint getPaintEntryLabels() {
|
||||
return mEntryLabelsPaint;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initBuffers() {
|
||||
// TODO Auto-generated method stub
|
||||
|
@ -392,7 +405,7 @@ public class PieChartRenderer extends DataRenderer {
|
|||
|
||||
float yValueSum = data.getYValueSum();
|
||||
|
||||
boolean drawXVals = mChart.isDrawSliceTextEnabled();
|
||||
boolean drawEntryLabels = mChart.isDrawEntryLabelsEnabled();
|
||||
|
||||
float angle;
|
||||
int xIndex = 0;
|
||||
|
@ -403,9 +416,9 @@ public class PieChartRenderer extends DataRenderer {
|
|||
|
||||
IPieDataSet dataSet = dataSets.get(i);
|
||||
|
||||
final boolean drawYVals = dataSet.isDrawValuesEnabled();
|
||||
final boolean drawValues = dataSet.isDrawValuesEnabled();
|
||||
|
||||
if (!drawYVals && !drawXVals)
|
||||
if (!drawValues && !drawEntryLabels)
|
||||
continue;
|
||||
|
||||
final PieDataSet.ValuePosition xValuePosition = dataSet.getXValuePosition();
|
||||
|
@ -453,13 +466,13 @@ public class PieChartRenderer extends DataRenderer {
|
|||
final float sliceXBase = (float) Math.cos(transformedAngle * Utils.FDEG2RAD);
|
||||
final float sliceYBase = (float) Math.sin(transformedAngle * Utils.FDEG2RAD);
|
||||
|
||||
final boolean drawXOutside = drawXVals &&
|
||||
final boolean drawXOutside = drawEntryLabels &&
|
||||
xValuePosition == PieDataSet.ValuePosition.OUTSIDE_SLICE;
|
||||
final boolean drawYOutside = drawYVals &&
|
||||
final boolean drawYOutside = drawValues &&
|
||||
yValuePosition == PieDataSet.ValuePosition.OUTSIDE_SLICE;
|
||||
final boolean drawXInside = drawXVals &&
|
||||
final boolean drawXInside = drawEntryLabels &&
|
||||
xValuePosition == PieDataSet.ValuePosition.INSIDE_SLICE;
|
||||
final boolean drawYInside = drawYVals &&
|
||||
final boolean drawYInside = drawValues &&
|
||||
yValuePosition == PieDataSet.ValuePosition.INSIDE_SLICE;
|
||||
|
||||
if (drawXOutside || drawYOutside) {
|
||||
|
@ -523,14 +536,12 @@ public class PieChartRenderer extends DataRenderer {
|
|||
dataSet.getValueTextColor(j));
|
||||
|
||||
if (j < data.getEntryCount() && entry.getLabel() != null) {
|
||||
c.drawText(entry.getLabel(), labelPtx, labelPty + lineHeight,
|
||||
mValuePaint);
|
||||
drawEntryLabel(c, entry.getLabel(), labelPtx, labelPty + lineHeight);
|
||||
}
|
||||
|
||||
} else if (drawXOutside) {
|
||||
if (j < data.getEntryCount() && entry.getLabel() != null) {
|
||||
mValuePaint.setColor(dataSet.getValueTextColor(j));
|
||||
c.drawText(entry.getLabel(), labelPtx, labelPty + lineHeight / 2.f, mValuePaint);
|
||||
drawEntryLabel(c, entry.getLabel(), labelPtx, labelPty + lineHeight / 2.f);
|
||||
}
|
||||
} else if (drawYOutside) {
|
||||
|
||||
|
@ -552,14 +563,12 @@ public class PieChartRenderer extends DataRenderer {
|
|||
drawValue(c, formatter, value, entry, 0, x, y, dataSet.getValueTextColor(j));
|
||||
|
||||
if (j < data.getEntryCount() && entry.getLabel() != null) {
|
||||
c.drawText(entry.getLabel(), x, y + lineHeight,
|
||||
mValuePaint);
|
||||
drawEntryLabel(c, entry.getLabel(), x, y + lineHeight);
|
||||
}
|
||||
|
||||
} else if (drawXInside) {
|
||||
if (j < data.getEntryCount() && entry.getLabel() != null) {
|
||||
mValuePaint.setColor(dataSet.getValueTextColor(j));
|
||||
c.drawText(entry.getLabel(), x, y + lineHeight / 2f, mValuePaint);
|
||||
drawEntryLabel(c, entry.getLabel(), x, y + lineHeight / 2f);
|
||||
}
|
||||
} else if (drawYInside) {
|
||||
|
||||
|
@ -574,6 +583,18 @@ public class PieChartRenderer extends DataRenderer {
|
|||
c.restore();
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws an entry label at the specified position.
|
||||
*
|
||||
* @param c
|
||||
* @param label
|
||||
* @param x
|
||||
* @param y
|
||||
*/
|
||||
protected void drawEntryLabel(Canvas c, String label, float x, float y) {
|
||||
c.drawText(label, x, y, mEntryLabelsPaint);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawExtras(Canvas c) {
|
||||
// drawCircles(c);
|
||||
|
|
|
@ -5,10 +5,8 @@ import android.graphics.Matrix;
|
|||
import android.graphics.Path;
|
||||
import android.graphics.RectF;
|
||||
|
||||
import com.github.mikephil.charting.data.BarData;
|
||||
import com.github.mikephil.charting.data.CandleEntry;
|
||||
import com.github.mikephil.charting.data.Entry;
|
||||
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
|
||||
import com.github.mikephil.charting.interfaces.datasets.IBubbleDataSet;
|
||||
import com.github.mikephil.charting.interfaces.datasets.ICandleDataSet;
|
||||
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
|
||||
|
|
Loading…
Add table
Reference in a new issue