Skip to content

Commit 3917ec5

Browse files
committed
Improved examples, improved piechart design.
1 parent d1350ec commit 3917ec5

11 files changed

Lines changed: 5519 additions & 47 deletions

File tree

MPChartExample/assets/hugecosine.txt

Lines changed: 2697 additions & 0 deletions
Large diffs are not rendered by default.

MPChartExample/assets/hugesine.txt

Lines changed: 2697 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:id="@+id/parentLayout"
34
android:layout_width="match_parent"
45
android:layout_height="match_parent"
56
android:orientation="vertical" >
67

7-
<com.github.mikephil.charting.charts.BarChart
8+
<!-- <com.github.mikephil.charting.charts.BarChart
89
android:id="@+id/barChart1"
910
android:layout_width="wrap_content"
10-
android:layout_height="wrap_content" />
11+
android:layout_height="wrap_content" /> -->
12+
1113

12-
</LinearLayout>
14+
</FrameLayout>

MPChartExample/src/com/example/mpchartexample/BarChartActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class BarChartActivity extends Activity implements OnSeekBarChangeListene
2727
private TextView tvX, tvY;
2828

2929
@Override
30-
protected void onCreate(Bundle savedInstanceState) {
30+
protected void onCreate(Bundle savedInstanceState) {
3131
super.onCreate(savedInstanceState);
3232
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
3333
WindowManager.LayoutParams.FLAG_FULLSCREEN);

MPChartExample/src/com/example/mpchartexample/simple/BarChartFrag.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.view.LayoutInflater;
66
import android.view.View;
77
import android.view.ViewGroup;
8+
import android.widget.FrameLayout;
89

910
import com.example.mpchartexample.MyMarkerView;
1011
import com.example.mpchartexample.R;
@@ -24,7 +25,8 @@ public static Fragment newInstance() {
2425
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
2526
View v = inflater.inflate(R.layout.frag_simple_bar, container, false);
2627

27-
mChart = (BarChart) v.findViewById(R.id.barChart1);
28+
// create a new chart object
29+
mChart = new BarChart(getActivity());
2830
mChart.setYLabelCount(6);
2931
mChart.setDescription("");
3032

@@ -51,6 +53,10 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
5153
Legend l = mChart.getLegend();
5254
l.setTypeface(tf);
5355

56+
// programatically add the chart
57+
FrameLayout parent = (FrameLayout) v.findViewById(R.id.parentLayout);
58+
parent.addView(mChart);
59+
5460
return v;
5561
}
5662
}

MPChartExample/src/com/example/mpchartexample/simple/LineChartFrag.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import com.example.mpchartexample.MyMarkerView;
1111
import com.example.mpchartexample.R;
1212
import com.github.mikephil.charting.charts.LineChart;
13+
import com.github.mikephil.charting.data.filter.Approximator;
14+
import com.github.mikephil.charting.data.filter.Approximator.ApproximatorType;
1315
import com.github.mikephil.charting.utils.ColorTemplate;
1416
import com.github.mikephil.charting.utils.Legend;
1517

@@ -48,6 +50,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
4850
mChart.setStartAtZero(false);
4951

5052
mChart.setYRange(-1f, 1f, false);
53+
mChart.setDrawCircles(true);
5154

5255
mChart.setData(getComplexity());
5356

MPChartExample/src/com/example/mpchartexample/simple/PieChartFrag.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
3535
mChart.setUsePercentValues(true);
3636
mChart.setCenterText("Quarterly\nRevenue");
3737
mChart.setCenterTextSize(23f);
38-
39-
// radius in percent
40-
mChart.setHoleRadius(25f);
38+
39+
// radius of the center hole in percent of maximum radius
40+
mChart.setHoleRadius(45f);
41+
mChart.setTransparentCircleRadius(50f);
4142

4243
mChart.setData(generateLessData());
4344

MPChartExample/src/com/example/mpchartexample/simple/SimpleFragment.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ protected ChartData getComplexity() {
8080
// sets.add(FileUtils.dataSetFromAssets(getActivity().getAssets(), "square.txt"));
8181
// sets.add(FileUtils.dataSetFromAssets(getActivity().getAssets(), "three.txt"));
8282

83-
ChartData d = new ChartData(ChartData.generateXVals(0, 752), sets);
83+
int max = Math.max(sets.get(0).getEntryCount(), sets.get(1).getEntryCount());
84+
85+
ChartData d = new ChartData(ChartData.generateXVals(0, max), sets);
8486
return d;
8587
}
8688

MPChartLib/src/com/github/mikephil/charting/charts/LineChart.java

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public class LineChart extends BarLineChartBase {
3636

3737
/** paint for the inner circle of the value indicators */
3838
protected Paint mCirclePaintInner;
39+
40+
protected boolean mDrawSpline = false;
3941

4042
public LineChart(Context context) {
4143
super(context);
@@ -111,30 +113,54 @@ protected void drawData() {
111113
ArrayList<Entry> entries = dataSet.getYVals();
112114

113115
float[] valuePoints = generateTransformedValues(entries, 0f);
114-
116+
115117
// Get the colors for the DataSet at the current index. If the index
116118
// is out of bounds, reuse DataSet colors.
117119
ArrayList<Integer> colors = mCt.getDataSetColors(i % mCt.getColors().size());
118120

119121
Paint paint = mRenderPaint;
122+
123+
if(mDrawSpline) {
124+
125+
Path spline = new Path();
126+
spline.moveTo(entries.get(0).getXIndex(), entries.get(0).getVal());
127+
128+
paint.setColor(colors.get(i % colors.size()));
129+
130+
// create a new path
131+
for (int x = 1; x < entries.size()-3; x+=2) {
132+
133+
// spline.rQuadTo(entries.get(x).getXIndex(), entries.get(x).getVal(), entries.get(x+1).getXIndex(), entries.get(x+1).getVal());
134+
135+
spline.cubicTo(entries.get(x).getXIndex(), entries.get(x).getVal(), entries.get(x+1).getXIndex(), entries.get(x+1).getVal(), entries.get(x+2).getXIndex(), entries.get(x+2).getVal());
136+
}
137+
138+
// spline.close();
120139

121-
for (int j = 0; j < valuePoints.length - 2; j += 2) {
140+
transformPath(spline);
122141

123-
// Set the color for the currently drawn value. If the index is
124-
// out of bounds, reuse colors.
125-
paint.setColor(colors.get(j % colors.size()));
142+
mDrawCanvas.drawPath(spline, paint);
143+
144+
} else {
145+
146+
for (int j = 0; j < valuePoints.length - 2; j += 2) {
126147

127-
if (isOffContentRight(valuePoints[j]))
128-
break;
148+
// Set the color for the currently drawn value. If the index is
149+
// out of bounds, reuse colors.
150+
paint.setColor(colors.get(j % colors.size()));
129151

130-
// make sure the lines don't do shitty things outside bounds
131-
if (j != 0 && isOffContentLeft(valuePoints[j - 1])
132-
&& isOffContentTop(valuePoints[j + 1])
133-
&& isOffContentBottom(valuePoints[j + 1]))
134-
continue;
152+
if (isOffContentRight(valuePoints[j]))
153+
break;
135154

136-
mDrawCanvas.drawLine(valuePoints[j], valuePoints[j + 1], valuePoints[j + 2],
137-
valuePoints[j + 3], paint);
155+
// make sure the lines don't do shitty things outside bounds
156+
if (j != 0 && isOffContentLeft(valuePoints[j - 1])
157+
&& isOffContentTop(valuePoints[j + 1])
158+
&& isOffContentBottom(valuePoints[j + 1]))
159+
continue;
160+
161+
mDrawCanvas.drawLine(valuePoints[j], valuePoints[j + 1], valuePoints[j + 2],
162+
valuePoints[j + 3], paint);
163+
}
138164
}
139165

140166
// if drawing filled is enabled

MPChartLib/src/com/github/mikephil/charting/charts/PieChart.java

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,15 @@ public class PieChart extends Chart {
5757

5858
/** indicates the selection distance of a pie slice */
5959
private float mShift = 20f;
60-
60+
6161
/**
62-
* indicates the size of the hole in the center of the piechart, default: diameter / 4
62+
* indicates the size of the hole in the center of the piechart, default:
63+
* radius / 2
6364
*/
64-
private float mHoleRadius = 0f;
65+
private float mHoleRadiusPercent = 50f;
66+
67+
/** the radius of the transparent circle next to the chart-hole in the center */
68+
private float mTransparentCircleRadius = 55f;
6569

6670
/** if enabled, centertext is drawn */
6771
private boolean mDrawCenterText = true;
@@ -105,9 +109,9 @@ protected void init() {
105109
super.init();
106110

107111
// // piechart has no offsets
108-
// mOffsetTop = 0;
112+
// mOffsetTop = 0;
109113
// mOffsetBottom = 0;
110-
// mOffsetLeft = 0;
114+
// mOffsetLeft = 0;
111115
// mOffsetRight = 0;
112116

113117
mShift = Utils.convertDpToPixel(mShift);
@@ -283,13 +287,11 @@ public void updateRotation(float x, float y) {
283287
protected void prepareContentRect() {
284288
super.prepareContentRect();
285289

286-
if(mHoleRadius <= 0) mHoleRadius = getDiameter() / 4;
287-
288290
int width = mContentRect.width() + mOffsetLeft + mOffsetRight;
289291
int height = mContentRect.height() + mOffsetTop + mOffsetBottom;
290292

291293
float diameter = getDiameter();
292-
294+
293295
// create the circle box that will contain the pie-chart (the bounds of
294296
// the pie-chart)
295297
mCircleBox.set(width / 2 - diameter / 2 + mShift, height / 2 - diameter / 2
@@ -425,9 +427,24 @@ private void drawHole() {
425427

426428
if (mDrawHole) {
427429

430+
float radius = getRadius();
431+
428432
PointF c = getCenterCircleBox();
433+
434+
int color = mHolePaint.getColor();
435+
436+
// draw the hole-circle
437+
mDrawCanvas.drawCircle(c.x, c.y,
438+
radius / 100 * mHoleRadiusPercent, mHolePaint);
439+
440+
// make transparent
441+
mHolePaint.setColor(color & 0x60FFFFFF);
442+
443+
// draw the transparent-circle
429444
mDrawCanvas.drawCircle(c.x, c.y,
430-
mHoleRadius, mHolePaint);
445+
radius / 100 * mTransparentCircleRadius, mHolePaint);
446+
447+
mHolePaint.setColor(color);
431448
}
432449
}
433450

@@ -471,15 +488,16 @@ protected void drawValues() {
471488

472489
PointF center = getCenterCircleBox();
473490

474-
float off = mCircleBox.width() / 8;
491+
// get whole the radius
492+
float r = getRadius();
493+
494+
float off = r / 2f;
475495

476-
// increase offset if there is no hole
477-
if (!mDrawHole)
478-
off += off / 2;
496+
if (mDrawHole) {
497+
off = (r - (r / 100f * mHoleRadiusPercent)) / 2f;
498+
}
479499

480-
// get the radius
481-
float r = mCircleBox.width() / 2 - off; // offset to keep things inside
482-
// the chart
500+
r -= off; // offset to keep things inside the chart
483501

484502
ArrayList<DataSet> dataSets = mCurrentData.getDataSets();
485503

@@ -742,7 +760,7 @@ public float getRadius() {
742760
if (mCircleBox == null)
743761
return 0;
744762
else
745-
return mCircleBox.width() / 2f;
763+
return Math.min(mCircleBox.width() / 2f, mCircleBox.height() / 2f);
746764
}
747765

748766
/**
@@ -858,20 +876,40 @@ public void setCenterTextTypeface(Typeface t) {
858876
public void setCenterTextSize(float size) {
859877
mCenterTextPaint.setTextSize(Utils.convertDpToPixel(size));
860878
}
861-
879+
862880
/**
863-
* sets the radius of the hole in the center of the piechart in percent of the total piechart size, default 25%
881+
* sets the radius of the hole in the center of the piechart in percent of
882+
* the maximum radius (max = the radius of the whole chart), default 50%
883+
*
864884
* @param size
865885
*/
866886
public void setHoleRadius(final float percent) {
867-
887+
868888
Handler h = new Handler();
869889
h.post(new Runnable() {
870-
890+
891+
@Override
892+
public void run() {
893+
mHoleRadiusPercent = percent;
894+
}
895+
});
896+
}
897+
898+
/**
899+
* sets the radius of the transparent circle that is drawn next to the hole
900+
* in the piechart in percent of the maximum radius (max = the radius of the
901+
* whole chart), default 55% -> means 5% larger than the center-hole by
902+
* default
903+
*
904+
* @param percent
905+
*/
906+
public void setTransparentCircleRadius(final float percent) {
907+
Handler h = new Handler();
908+
h.post(new Runnable() {
909+
871910
@Override
872911
public void run() {
873-
float smaller = Math.min(getWidth(), getHeight());
874-
mHoleRadius = smaller / 100f * percent;
912+
mTransparentCircleRadius = percent;
875913
}
876914
});
877915
}

0 commit comments

Comments
 (0)