Skip to content

Commit

Permalink
Fixed issue concerning PieChart center text (issue PhilJay#467).
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilJay committed Mar 25, 2015
1 parent fba34b1 commit 64f6ab0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected void onCreate(Bundle savedInstanceState) {
mChart.setOnChartValueSelectedListener(this);
// mChart.setTouchEnabled(false);

mChart.setCenterText("MPAndroidChart\nLibrary");
mChart.setCenterText("MPAndroidChart\nby Philipp Jahoda");

setData(3, 100);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,22 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
mChart = (PieChart) v.findViewById(R.id.pieChart1);
mChart.setDescription("");

mChart.setCenterTextTypeface(Typeface.createFromAsset(getActivity().getAssets(), "OpenSans-Light.ttf"));
mChart.setCenterText("Quarterly\nRevenue");
Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "OpenSans-Light.ttf");

mChart.setCenterTextTypeface(tf);
mChart.setCenterText("Revenues");
mChart.setCenterTextSize(22f);
mChart.setCenterTextTypeface(tf);

// radius of the center hole in percent of maximum radius
mChart.setHoleRadius(45f);
mChart.setTransparentCircleRadius(50f);

// enable / disable drawing of x- and y-values
// mChart.setDrawYValues(false);
// mChart.setDrawXValues(false);

mChart.setData(generatePieData());

Legend l = mChart.getLegend();
l.setPosition(LegendPosition.RIGHT_OF_CHART);

mChart.setData(generatePieData());

return v;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.xxmassdeveloper.mpchartexample.fragments;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
Expand Down Expand Up @@ -115,9 +116,12 @@ protected PieData generatePieData() {
PieDataSet ds1 = new PieDataSet(entries1, "Quarterly Revenues 2014");
ds1.setColors(ColorTemplate.VORDIPLOM_COLORS);
ds1.setSliceSpace(2f);
ds1.setValueTextColor(Color.WHITE);
ds1.setValueTextSize(12f);

PieData d = new PieData(xVals, ds1);
d.setValueTypeface(tf);

return d;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected void onCreate(Bundle savedInstanceState) {
objects.add(new ContentItem("Draw Chart",
"Demonstration of drawing values into the chart per touch-gesture. With callbacks."));
objects.add(new ContentItem(
"Charts in Fragments, awesome design.",
"Charts in ViewPager Fragments",
"Demonstration of charts inside ViewPager Fragments. In this example the focus was on the design and look and feel of the chart."));
objects.add(new ContentItem(
"BarChart inside ListView",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class PieChartRenderer extends DataRenderer {
* chart
*/
private Paint mCenterTextPaint;

/** Bitmap for drawing the center hole */
protected Bitmap mDrawBitmap;

Expand Down Expand Up @@ -66,16 +66,16 @@ public Paint getPaintHole() {
public Paint getPaintCenterText() {
return mCenterTextPaint;
}

@Override
public void initBuffers() {
// TODO Auto-generated method stub

}

@Override
public void drawData(Canvas c) {

if (mDrawBitmap == null) {
mDrawBitmap = Bitmap.createBitmap((int) mViewPortHandler.getChartWidth(),
(int) mViewPortHandler.getChartHeight(), Bitmap.Config.ARGB_4444);
Expand All @@ -91,7 +91,7 @@ public void drawData(Canvas c) {
if (set.isVisible())
drawDataSet(c, set);
}

c.drawBitmap(mDrawBitmap, 0, 0, mRenderPaint);
}

Expand Down Expand Up @@ -275,11 +275,18 @@ protected void drawCenterText(Canvas c) {
// get all lines from the text
String[] lines = centerText.split("\n");

// calculate the height for each line
float lineHeight = Utils.calcTextHeight(mCenterTextPaint, lines[0]);
float linespacing = lineHeight * 0.2f;
float maxlineheight = 0f;

// calc the maximum line height
for (String line : lines) {
float curHeight = Utils.calcTextHeight(mCenterTextPaint, line);
if (curHeight > maxlineheight)
maxlineheight = curHeight;
}

float linespacing = maxlineheight * 0.25f;

float totalheight = lineHeight * lines.length - linespacing * (lines.length - 1);
float totalheight = maxlineheight * lines.length - linespacing * (lines.length - 1);

int cnt = lines.length;

Expand All @@ -290,7 +297,7 @@ protected void drawCenterText(Canvas c) {
String line = lines[lines.length - i - 1];

c.drawText(line, center.x, y
+ lineHeight * cnt - totalheight / 2f,
+ maxlineheight * cnt - totalheight / 2f,
mCenterTextPaint);
cnt--;
y -= linespacing;
Expand Down Expand Up @@ -348,7 +355,8 @@ public void drawHighlighted(Canvas c, Highlight[] indices) {

// redefine the rect that contains the arc so that the
// highlighted pie is not cut off
mBitmapCanvas.drawArc(highlighted, angle + set.getSliceSpace() / 2f, sliceDegrees * mAnimator.getPhaseY()
mBitmapCanvas.drawArc(highlighted, angle + set.getSliceSpace() / 2f, sliceDegrees
* mAnimator.getPhaseY()
- set.getSliceSpace() / 2f, true, mRenderPaint);
}
}
Expand Down

0 comments on commit 64f6ab0

Please sign in to comment.