Fixed issue concerning PieChart center text (issue #467).

This commit is contained in:
Philipp Jahoda 2015-03-25 21:28:41 +01:00
parent fba34b19b6
commit 64f6ab0a41
5 changed files with 32 additions and 21 deletions

View file

@ -84,7 +84,7 @@ public class PieChartActivity extends DemoBase implements OnSeekBarChangeListene
mChart.setOnChartValueSelectedListener(this);
// mChart.setTouchEnabled(false);
mChart.setCenterText("MPAndroidChart\nLibrary");
mChart.setCenterText("MPAndroidChart\nby Philipp Jahoda");
setData(3, 100);

View file

@ -27,23 +27,22 @@ public class PieChartFrag extends SimpleFragment {
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;
}
}

View file

@ -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;
@ -115,9 +116,12 @@ public abstract class SimpleFragment extends Fragment {
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;
}

View file

@ -83,7 +83,7 @@ public class MainActivity extends Activity implements OnItemClickListener {
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",

View file

@ -35,7 +35,7 @@ public class PieChartRenderer extends DataRenderer {
* chart
*/
private Paint mCenterTextPaint;
/** Bitmap for drawing the center hole */
protected Bitmap mDrawBitmap;
@ -66,16 +66,16 @@ public class PieChartRenderer extends DataRenderer {
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);
@ -91,7 +91,7 @@ public class PieChartRenderer extends DataRenderer {
if (set.isVisible())
drawDataSet(c, set);
}
c.drawBitmap(mDrawBitmap, 0, 0, mRenderPaint);
}
@ -275,11 +275,18 @@ public class PieChartRenderer extends DataRenderer {
// 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;
float totalheight = lineHeight * lines.length - linespacing * (lines.length - 1);
// 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 = maxlineheight * lines.length - linespacing * (lines.length - 1);
int cnt = lines.length;
@ -290,7 +297,7 @@ public class PieChartRenderer extends DataRenderer {
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;
@ -348,7 +355,8 @@ public class PieChartRenderer extends DataRenderer {
// 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);
}
}