Skip to content

Commit c4d8468

Browse files
committed
Bugfix in the PieChart concerning its bounds. Improved the example project.
1 parent ebb5d1b commit c4d8468

15 files changed

Lines changed: 715 additions & 196 deletions

File tree

MPChartExample/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<uses-sdk
88
android:minSdkVersion="8"
99
android:targetSdkVersion="18" />
10+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
1011

1112
<application
1213
android:allowBackup="true"
@@ -24,6 +25,8 @@
2425
</activity>
2526
<activity android:name="LineChartActivity"></activity>
2627
<activity android:screenOrientation="landscape" android:name="MultipleChartsActivity"></activity>
28+
<activity android:name="BarChartActivity"></activity>
29+
<activity android:name="PieChartActivity"></activity>
2730
</application>
2831

2932
</manifest>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent" >
5+
6+
<com.github.mikephil.charting.BarChart
7+
android:id="@+id/chart1"
8+
android:layout_width="match_parent"
9+
android:layout_height="match_parent"
10+
android:layout_above="@+id/seekBar1" />
11+
12+
<SeekBar
13+
android:id="@+id/seekBar2"
14+
android:layout_width="match_parent"
15+
android:layout_height="wrap_content"
16+
android:layout_alignParentBottom="true"
17+
android:layout_alignParentLeft="true"
18+
android:layout_margin="8dp"
19+
android:layout_toLeftOf="@+id/tvYMax"
20+
android:layout_marginRight="5dp"
21+
android:max="2000"
22+
android:paddingBottom="12dp" />
23+
24+
<SeekBar
25+
android:id="@+id/seekBar1"
26+
android:layout_width="match_parent"
27+
android:layout_height="wrap_content"
28+
android:layout_above="@+id/seekBar2"
29+
android:layout_margin="8dp"
30+
android:layout_marginBottom="35dp"
31+
android:layout_toLeftOf="@+id/tvXMax"
32+
android:layout_marginRight="5dp"
33+
android:max="1000"
34+
android:paddingBottom="12dp" />
35+
36+
<TextView
37+
android:id="@+id/tvXMax"
38+
android:layout_width="50dp"
39+
android:layout_height="wrap_content"
40+
android:layout_alignBottom="@+id/seekBar1"
41+
android:layout_alignParentRight="true"
42+
android:text="500"
43+
android:layout_marginBottom="15dp"
44+
android:layout_marginRight="10dp"
45+
android:gravity="right"
46+
android:textAppearance="?android:attr/textAppearanceMedium" />
47+
48+
<TextView
49+
android:id="@+id/tvYMax"
50+
android:layout_width="50dp"
51+
android:layout_height="wrap_content"
52+
android:layout_alignBottom="@+id/seekBar2"
53+
android:layout_alignParentRight="true"
54+
android:text="500"
55+
android:layout_marginBottom="15dp"
56+
android:layout_marginRight="10dp"
57+
android:gravity="right"
58+
android:textAppearance="?android:attr/textAppearanceMedium" />
59+
60+
</RelativeLayout>

MPChartExample/res/layout/activity_main.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@
2020
android:id="@+id/button2"
2121
android:layout_width="match_parent"
2222
android:layout_height="wrap_content"
23+
android:text="BarChart" />
24+
<Button
25+
android:id="@+id/button3"
26+
android:layout_width="match_parent"
27+
android:layout_height="wrap_content"
28+
android:text="PieChart" />
29+
30+
<Button
31+
android:id="@+id/button4"
32+
android:layout_width="match_parent"
33+
android:layout_height="wrap_content"
2334
android:text="MultipleCharts" />
2435

2536
</LinearLayout>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent" >
5+
6+
<com.github.mikephil.charting.PieChart
7+
android:id="@+id/chart1"
8+
android:layout_width="match_parent"
9+
android:layout_height="wrap_content"
10+
android:layout_above="@+id/seekBar1" />
11+
12+
<SeekBar
13+
android:id="@+id/seekBar2"
14+
android:layout_width="match_parent"
15+
android:layout_height="wrap_content"
16+
android:layout_alignParentBottom="true"
17+
android:layout_alignParentLeft="true"
18+
android:layout_margin="8dp"
19+
android:layout_toLeftOf="@+id/tvYMax"
20+
android:layout_marginRight="5dp"
21+
android:max="300"
22+
android:paddingBottom="12dp" />
23+
24+
<SeekBar
25+
android:id="@+id/seekBar1"
26+
android:layout_width="match_parent"
27+
android:layout_height="wrap_content"
28+
android:layout_above="@+id/seekBar2"
29+
android:layout_margin="8dp"
30+
android:layout_marginBottom="35dp"
31+
android:layout_toLeftOf="@+id/tvXMax"
32+
android:layout_marginRight="5dp"
33+
android:max="30"
34+
android:paddingBottom="12dp" />
35+
36+
<TextView
37+
android:id="@+id/tvXMax"
38+
android:layout_width="50dp"
39+
android:layout_height="wrap_content"
40+
android:layout_alignBottom="@+id/seekBar1"
41+
android:layout_alignParentRight="true"
42+
android:text="500"
43+
android:layout_marginBottom="15dp"
44+
android:layout_marginRight="10dp"
45+
android:gravity="right"
46+
android:textAppearance="?android:attr/textAppearanceMedium" />
47+
48+
<TextView
49+
android:id="@+id/tvYMax"
50+
android:layout_width="50dp"
51+
android:layout_height="wrap_content"
52+
android:layout_alignBottom="@+id/seekBar2"
53+
android:layout_alignParentRight="true"
54+
android:text="500"
55+
android:layout_marginBottom="15dp"
56+
android:layout_marginRight="10dp"
57+
android:gravity="right"
58+
android:textAppearance="?android:attr/textAppearanceMedium" />
59+
60+
</RelativeLayout>

MPChartExample/res/menu/bar.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
3+
<item android:id="@+id/actionToggleRound" android:title="Toggle Round"></item>
4+
<item android:id="@+id/actionToggleValues" android:title="Toggle Values"></item>
5+
<item android:id="@+id/actionToggleFilled" android:title="Toggle Filled"></item>
6+
<item android:id="@+id/actionToggle3D" android:title="Toggle 3D"></item>
7+
<item android:id="@+id/actionToggleStartzero" android:title="Toggle StartZero"></item>
8+
<item android:id="@+id/actionToggleAdjustXLegend" android:title="Toggle AdjustXLegend"></item>
9+
<item android:id="@+id/actionSave" android:title="Save to Gallery"></item>
10+
</menu>

MPChartExample/res/menu/pie.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
3+
<item android:id="@+id/actionToggleValues" android:title="Toggle Values"></item>
4+
<item android:id="@+id/actionToggleHole" android:title="Toggle Hole"></item>
5+
<item android:id="@+id/actionDrawCenter" android:title="Draw CenterText"></item>
6+
<item android:id="@+id/actionSave" android:title="Save to Gallery"></item>
7+
</menu>
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package com.example.mpchartexample;
2+
3+
import android.app.Activity;
4+
import android.os.Bundle;
5+
import android.view.Menu;
6+
import android.view.MenuItem;
7+
import android.view.WindowManager;
8+
import android.widget.SeekBar;
9+
import android.widget.SeekBar.OnSeekBarChangeListener;
10+
import android.widget.TextView;
11+
12+
import com.github.mikephil.charting.BarChart;
13+
import com.github.mikephil.charting.ColorTemplate;
14+
15+
import java.util.ArrayList;
16+
17+
public class BarChartActivity extends Activity implements OnSeekBarChangeListener {
18+
19+
private BarChart mChart;
20+
private SeekBar mSeekBarX, mSeekBarY;
21+
private TextView tvX, tvY;
22+
23+
@Override
24+
protected void onCreate(Bundle savedInstanceState) {
25+
super.onCreate(savedInstanceState);
26+
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
27+
WindowManager.LayoutParams.FLAG_FULLSCREEN);
28+
setContentView(R.layout.activity_barchart);
29+
30+
tvX = (TextView) findViewById(R.id.tvXMax);
31+
tvY = (TextView) findViewById(R.id.tvYMax);
32+
33+
mSeekBarX = (SeekBar) findViewById(R.id.seekBar1);
34+
mSeekBarX.setOnSeekBarChangeListener(this);
35+
36+
37+
mSeekBarY = (SeekBar) findViewById(R.id.seekBar2);
38+
mSeekBarY.setOnSeekBarChangeListener(this);
39+
40+
mChart = (BarChart) findViewById(R.id.chart1);
41+
mChart.setColorTemplate(new ColorTemplate(ColorTemplate.getColors(this, ColorTemplate.FRESH_COLORS)));
42+
43+
// mChart.setDrawFilled(true);
44+
// mChart.setRoundedYLegend(false);
45+
// mChart.setStartAtZero(true);
46+
mChart.setDrawValues(false);
47+
mChart.set3DEnabled(false);
48+
mChart.setDrawAdditional(true);
49+
// mChart.setSpacePercent(20, 10);
50+
mChart.setYLegendCount(5);
51+
mChart.setTouchEnabled(true);
52+
53+
ArrayList<String> xVals = new ArrayList<String>();
54+
for (int i = 1; i <= 50; i++) {
55+
xVals.add(Integer.toString(i - 1));
56+
}
57+
58+
ArrayList<Float> yVals = new ArrayList<Float>();
59+
60+
for (int i = 1; i <= 50; i++) {
61+
float val = (float) (Math.random() * 10);
62+
yVals.add(val);
63+
}
64+
65+
mSeekBarX.setProgress(50);
66+
mSeekBarY.setProgress(100);
67+
68+
mChart.setData(xVals, yVals);
69+
}
70+
71+
@Override
72+
public boolean onCreateOptionsMenu(Menu menu) {
73+
getMenuInflater().inflate(R.menu.bar, menu);
74+
return true;
75+
}
76+
77+
@Override
78+
public boolean onOptionsItemSelected(MenuItem item) {
79+
80+
switch (item.getItemId()) {
81+
case R.id.actionToggleRound: {
82+
if (mChart.isYLegendRounded())
83+
mChart.setRoundedYLegend(false);
84+
else
85+
mChart.setRoundedYLegend(true);
86+
mChart.invalidate();
87+
break;
88+
}
89+
case R.id.actionToggleValues: {
90+
if (mChart.isDrawValuesEnabled())
91+
mChart.setDrawValues(false);
92+
else
93+
mChart.setDrawValues(true);
94+
mChart.invalidate();
95+
break;
96+
}
97+
case R.id.actionToggle3D: {
98+
if (mChart.is3DEnabled())
99+
mChart.set3DEnabled(false);
100+
else
101+
mChart.set3DEnabled(true);
102+
mChart.invalidate();
103+
break;
104+
}
105+
case R.id.actionToggleStartzero: {
106+
if (mChart.isStartAtZeroEnabled())
107+
mChart.setStartAtZero(false);
108+
else
109+
mChart.setStartAtZero(true);
110+
111+
mChart.invalidate();
112+
break;
113+
}
114+
case R.id.actionToggleAdjustXLegend: {
115+
if (mChart.isAdjustXLegendEnabled())
116+
mChart.setAdjustXLegend(false);
117+
else
118+
mChart.setAdjustXLegend(true);
119+
120+
mChart.invalidate();
121+
break;
122+
}
123+
case R.id.actionSave: {
124+
// mChart.saveToGallery("title"+System.currentTimeMillis());
125+
mChart.saveToPath("title"+System.currentTimeMillis(), "");
126+
break;
127+
}
128+
}
129+
return true;
130+
}
131+
132+
@Override
133+
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
134+
135+
ArrayList<String> xVals = new ArrayList<String>();
136+
for (int i = 1; i <= mSeekBarX.getProgress()+1; i++) {
137+
xVals.add((i - 1)+"");
138+
}
139+
140+
ArrayList<Float> yVals = new ArrayList<Float>();
141+
142+
for (int i = 1; i <= mSeekBarX.getProgress()+1; i++) {
143+
float mult = (mSeekBarY.getProgress()+1);
144+
float val = (float) (Math.random() * mult * 0.1) + 3;// + (float) ((mult * 0.1) / 10);
145+
yVals.add(val);
146+
}
147+
148+
tvX.setText(""+ (mSeekBarX.getProgress() + 1));
149+
tvY.setText(""+ (mSeekBarY.getProgress() / 10));
150+
151+
mChart.setData(xVals, yVals);
152+
mChart.invalidate();
153+
}
154+
155+
@Override
156+
public void onStartTrackingTouch(SeekBar seekBar) {
157+
// TODO Auto-generated method stub
158+
159+
}
160+
161+
@Override
162+
public void onStopTrackingTouch(SeekBar seekBar) {
163+
// TODO Auto-generated method stub
164+
165+
}
166+
}

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import android.widget.SeekBar.OnSeekBarChangeListener;
1010
import android.widget.TextView;
1111

12-
import com.github.mikephil.charting.Chart;
13-
import com.github.mikephil.charting.ColorTemplate;
1412
import com.github.mikephil.charting.LineChart;
1513

1614
import java.util.ArrayList;
@@ -49,7 +47,7 @@ protected void onCreate(Bundle savedInstanceState) {
4947
mChart.setCircleSize(5f);
5048
mChart.setDrawAdditional(true);
5149
// mChart.setSpacePercent(20, 10);
52-
mChart.setYLegendCount(5);
50+
mChart.setYLegendCount(6);
5351
mChart.setTouchEnabled(true);
5452

5553
ArrayList<String> xVals = new ArrayList<String>();
@@ -72,7 +70,7 @@ protected void onCreate(Bundle savedInstanceState) {
7270

7371
@Override
7472
public boolean onCreateOptionsMenu(Menu menu) {
75-
getMenuInflater().inflate(R.menu.main, menu);
73+
getMenuInflater().inflate(R.menu.line, menu);
7674
return true;
7775
}
7876

@@ -122,17 +120,17 @@ public boolean onOptionsItemSelected(MenuItem item) {
122120
break;
123121
}
124122
case R.id.actionToggleAdjustXLegend: {
125-
// if (mChart.isAdjustXLegendEnabled())
126-
// mChart.setAdjustXLegend(false);
127-
// else
128-
// mChart.setAdjustXLegend(true);
123+
if (mChart.isAdjustXLegendEnabled())
124+
mChart.setAdjustXLegend(false);
125+
else
126+
mChart.setAdjustXLegend(true);
129127

130128
mChart.invalidate();
131129
break;
132130
}
133131
case R.id.actionSave: {
134132
// mChart.saveToGallery("title"+System.currentTimeMillis());
135-
// mChart.saveToPath("title"+System.currentTimeMillis(), "");
133+
mChart.saveToPath("title"+System.currentTimeMillis(), "");
136134
break;
137135
}
138136
}

0 commit comments

Comments
 (0)