Skip to content

Commit

Permalink
Work on supporting SpannableString for PieChart center text (PhilJay#993
Browse files Browse the repository at this point in the history
).
  • Loading branch information
PhilJay committed Oct 10, 2015
1 parent 6a2f4d5 commit ad940c2
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
Expand Down Expand Up @@ -58,12 +62,14 @@ protected void onCreate(Bundle savedInstanceState) {
mChart = (PieChart) findViewById(R.id.chart1);
mChart.setUsePercentValues(true);
mChart.setDescription("");

mChart.setExtraOffsets(5, 10, 5, 5);

mChart.setDragDecelerationFrictionCoef(0.95f);

tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");

mChart.setCenterTextTypeface(Typeface.createFromAsset(getAssets(), "OpenSans-Light.ttf"));
mChart.setCenterText(generateCenterSpannableText());

mChart.setDrawHoleEnabled(true);
mChart.setHoleColorTransparent(true);
Expand All @@ -86,8 +92,6 @@ protected void onCreate(Bundle savedInstanceState) {
// add a selection listener
mChart.setOnChartValueSelectedListener(this);

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

setData(3, 100);

mChart.animateY(1500, Easing.EasingOption.EaseInOutQuad);
Expand Down Expand Up @@ -232,6 +236,17 @@ private void setData(int count, float range) {
mChart.invalidate();
}

private SpannableString generateCenterSpannableText() {

SpannableString s = new SpannableString("MPAndroidChart developed by Philipp Jahoda");
s.setSpan(new RelativeSizeSpan(1.7f), 0, 14, 0);
// s.setSpan(new StyleSpan(Typeface.NORMAL), 14, s.length() - 15, 0);
// 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);
return s;
}

@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {

Expand All @@ -258,46 +273,4 @@ public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

// private void removeLastEntry() {
//
// PieData data = mChart.getDataOriginal();
//
// if (data != null) {
//
// PieDataSet set = data.getDataSet();
//
// if (set != null) {
//
// Entry e = set.getEntryForXIndex(set.getEntryCount() - 1);
//
// data.removeEntry(e, 0);
// // or remove by index
// // mData.removeEntry(xIndex, dataSetIndex);
//
// mChart.notifyDataSetChanged();
// mChart.invalidate();
// }
// }
// }
//
// private void addEntry() {
//
// PieData data = mChart.getDataOriginal();
//
// if (data != null) {
//
// PieDataSet set = data.getDataSet();
// // set.addEntry(...);
//
// data.addEntry(new Entry((float) (Math.random() * 25) + 20f,
// set.getEntryCount()), 0);
//
// // let the chart know it's data has changed
// mChart.notifyDataSetChanged();
//
// // redraw the chart
// mChart.invalidate();
// }
// }
}
48 changes: 20 additions & 28 deletions MPChartLib/src/com/github/mikephil/charting/charts/PieChart.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.util.AttributeSet;

import com.github.mikephil.charting.data.DataSet;
Expand Down Expand Up @@ -65,10 +66,9 @@ public class PieChart extends PieRadarChartBase<PieData> {
private boolean mDrawRoundedSlices = false;

/**
* variable for the text that is drawn in the center of the pie-chart. If
* this value is null, the default is "Total Value\n + getYValueSum()"
* variable for the text that is drawn in the center of the pie-chart
*/
private String mCenterText = "";
private SpannableString mCenterText = new SpannableString("");

/**
* indicates the size of the hole in the center of the piechart, default:
Expand All @@ -86,8 +86,6 @@ public class PieChart extends PieRadarChartBase<PieData> {
*/
private boolean mDrawCenterText = true;

private boolean mCenterTextWordWrapEnabled = false;

private float mCenterTextRadiusPercent = 1.f;

public PieChart(Context context) {
Expand Down Expand Up @@ -373,21 +371,33 @@ public boolean isDrawHoleEnabled() {
}

/**
* sets the text that is displayed in the center of the pie-chart. By
* default, the text is "Total Value + sumofallvalues"
* Sets the text SpannableString that is displayed in the center of the PieChart.
*
* @param text
*/
public void setCenterText(SpannableString text) {

if (text == null)
mCenterText = new SpannableString("");
else
mCenterText = text;
}

/**
* Sets the text String that is displayed in the center of the PieChart.
*
* @param text
*/
public void setCenterText(String text) {
mCenterText = text;
setCenterText(new SpannableString(text));
}

/**
* returns the text that is drawn in the center of the pie-chart
*
* @return
*/
public String getCenterText() {
public SpannableString getCenterText() {
return mCenterText;
}

Expand Down Expand Up @@ -589,24 +599,6 @@ public boolean isUsePercentValuesEnabled() {
return mUsePercentValues;
}

/**
* should the center text be word wrapped?
* note that word wrapping takes a toll on performance
* if word wrapping is disabled, newlines are still respected
*/
public void setCenterTextWordWrapEnabled(boolean enabled) {
mCenterTextWordWrapEnabled = enabled;
}

/**
* should the center text be word wrapped?
* note that word wrapping takes a toll on performance
* if word wrapping is disabled, newlines are still respected
*/
public boolean isCenterTextWordWrapEnabled() {
return mCenterTextWordWrapEnabled;
}

/**
* the rectangular radius of the bounding box for the center text, as a percentage of the pie hole
* default 1.f (100%)
Expand All @@ -627,7 +619,7 @@ public float getCenterTextRadiusPercent() {
@Override
protected void onDetachedFromWindow() {
// releases the bitmap in the renderer to avoid oom error
if(mRenderer != null && mRenderer instanceof PieChartRenderer) {
if (mRenderer != null && mRenderer instanceof PieChartRenderer) {
((PieChartRenderer) mRenderer).releaseBitmap();
}
super.onDetachedFromWindow();
Expand Down
Loading

0 comments on commit ad940c2

Please sign in to comment.