Skip to content

Commit

Permalink
Improvements on piechart.
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilJay committed Oct 11, 2015
1 parent 280cb5c commit 1eae8e0
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected void onCreate(Bundle savedInstanceState) {

setData(3, 100);

mChart.animateY(1500, Easing.EasingOption.EaseInOutQuad);
mChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);
// mChart.spin(2000, 0, 360);

Legend l = mChart.getLegend();
Expand Down Expand Up @@ -153,15 +153,15 @@ public boolean onOptionsItemSelected(MenuItem item) {
mChart.invalidate();
break;
case R.id.animateX: {
mChart.animateX(1800);
mChart.animateX(1400);
break;
}
case R.id.animateY: {
mChart.animateY(1800);
mChart.animateY(1400);
break;
}
case R.id.animateXY: {
mChart.animateXY(1800, 1800);
mChart.animateXY(1400, 1400);
break;
}
}
Expand Down Expand Up @@ -196,7 +196,7 @@ private void setData(int count, float range) {
xVals.add(mParties[i % mParties.length]);

PieDataSet dataSet = new PieDataSet(yVals1, "Election Results");
dataSet.setSliceSpace(3f);
dataSet.setSliceSpace(2f);
dataSet.setSelectionShift(5f);

// add a lot of colors
Expand Down Expand Up @@ -244,6 +244,7 @@ private SpannableString generateCenterSpannableText() {
s.setSpan(new ForegroundColorSpan(Color.GRAY), 14, s.length() - 15, 0);
s.setSpan(new RelativeSizeSpan(.8f), 14, s.length() - 15, 0);
s.setSpan(new StyleSpan(Typeface.ITALIC), s.length() - 14, s.length(), 0);
s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), s.length() - 14, s.length(), 0);
return s;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.xxmassdeveloper.mpchartexample.fragments;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -30,12 +34,12 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "OpenSans-Light.ttf");

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

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

Legend l = mChart.getLegend();
Expand All @@ -45,4 +49,11 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa

return v;
}

private SpannableString generateCenterText() {
SpannableString s = new SpannableString("Revenues\nQuarters 2015");
s.setSpan(new RelativeSizeSpan(2f), 0, 8, 0);
s.setSpan(new ForegroundColorSpan(Color.GRAY), 8, s.length(), 0);
return s;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ protected PieData generatePieData() {
entries1.add(new Entry((float) (Math.random() * 60) + 40, i));
}

PieDataSet ds1 = new PieDataSet(entries1, "Quarterly Revenues 2014");
PieDataSet ds1 = new PieDataSet(entries1, "Quarterly Revenues 2015");
ds1.setColors(ColorTemplate.VORDIPLOM_COLORS);
ds1.setSliceSpace(2f);
ds1.setValueTextColor(Color.WHITE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;

Expand All @@ -13,16 +16,19 @@
import com.github.mikephil.charting.data.ChartData;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.xxmassdeveloper.mpchartexample.R;

public class PieChartItem extends ChartItem {

private Typeface mTf;
private SpannableString mCenterText;

public PieChartItem(ChartData<?> cd, Context c) {
super(cd);

mTf = Typeface.createFromAsset(c.getAssets(), "OpenSans-Regular.ttf");
mCenterText = generateCenterText();
}

@Override
Expand Down Expand Up @@ -53,9 +59,9 @@ public View getView(int position, View convertView, Context c) {
holder.chart.setDescription("");
holder.chart.setHoleRadius(52f);
holder.chart.setTransparentCircleRadius(57f);
holder.chart.setCenterText("MPChart\nAndroid");
holder.chart.setCenterText(mCenterText);
holder.chart.setCenterTextTypeface(mTf);
holder.chart.setCenterTextSize(18f);
holder.chart.setCenterTextSize(9f);
holder.chart.setUsePercentValues(true);
holder.chart.setExtraOffsets(5, 10, 50, 10);

Expand All @@ -78,6 +84,17 @@ public View getView(int position, View convertView, Context c) {
return convertView;
}

private SpannableString generateCenterText() {
SpannableString s = new SpannableString("MPAndroidChart\ncreated by\nPhilipp Jahoda");
s.setSpan(new RelativeSizeSpan(1.6f), 0, 14, 0);
s.setSpan(new ForegroundColorSpan(ColorTemplate.VORDIPLOM_COLORS[0]), 0, 14, 0);
s.setSpan(new RelativeSizeSpan(.9f), 14, 25, 0);
s.setSpan(new ForegroundColorSpan(Color.GRAY), 14, 25, 0);
s.setSpan(new RelativeSizeSpan(1.4f), 25, s.length(), 0);
s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), 25, s.length(), 0);
return s;
}

private static class ViewHolder {
PieChart chart;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public PieChartRenderer(PieChart chart, ChartAnimator animator,
mTransparentCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mTransparentCirclePaint.setColor(Color.WHITE);
mTransparentCirclePaint.setStyle(Style.FILL);
mTransparentCirclePaint.setAlpha(100);
mTransparentCirclePaint.setAlpha(105);

mCenterTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
mCenterTextPaint.setColor(Color.BLACK);
Expand Down Expand Up @@ -266,19 +266,19 @@ protected void drawHole(Canvas c) {

PointF center = mChart.getCenterCircleBox();

if (transparentCircleRadius > holeRadius && mAnimator.getPhaseX() >= 1f
&& mAnimator.getPhaseY() >= 1f) {
// only draw the circle if it can be seen (not covered by the hole)
if (transparentCircleRadius > holeRadius) {

//int color = mTransparentCirclePaint.getColor();

// make transparent
//mTransparentCirclePaint.setColor(color & 0x60FFFFFF);
// get original alpha
int alpha = mTransparentCirclePaint.getAlpha();
mTransparentCirclePaint.setAlpha((int) ((float) alpha * mAnimator.getPhaseX() * mAnimator.getPhaseY()));

// draw the transparent-circle
mBitmapCanvas.drawCircle(center.x, center.y,
radius / 100 * transparentCircleRadius, mTransparentCirclePaint);

//mTransparentCirclePaint.setColor(color);
// reset alpha
mTransparentCirclePaint.setAlpha(alpha);
}

// draw the hole-circle
Expand Down

0 comments on commit 1eae8e0

Please sign in to comment.