Skip to content

Commit 1c984be

Browse files
committed
Improved dynamical data adding.
1 parent f9d9f89 commit 1c984be

22 files changed

+380
-149
lines changed

MPChartExample/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.xxmassdeveloper.mpchartexample"
4-
android:versionCode="21"
4+
android:versionCode="22"
55
android:versionName="1.6.2" >
66

77
<uses-sdk

MPChartExample/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ android {
77
applicationId 'com.xxmassdeveloper.mpchartexample'
88
minSdkVersion 16
99
targetSdkVersion 19
10-
versionCode 21
10+
versionCode 22
1111
versionName '1.6.2'
1212

1313
sourceSets {
1.2 MB
Binary file not shown.

MPChartExample/res/menu/dynamical.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,13 @@
2121
android:title="Remove DataSet">
2222
</item>
2323

24+
<item
25+
android:id="@+id/actionAddEmptyLineData"
26+
android:title="Add empty LineData">
27+
</item>
28+
29+
<item
30+
android:id="@+id/actionClear"
31+
android:title="Clear chart">
32+
</item>
2433
</menu>

MPChartExample/src/com/xxmassdeveloper/mpchartexample/DynamicalAddingActivity.java

Lines changed: 92 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import android.graphics.Color;
55
import android.os.Bundle;
6+
import android.util.Log;
67
import android.view.Menu;
78
import android.view.MenuItem;
89
import android.view.WindowManager;
@@ -34,107 +35,115 @@ protected void onCreate(Bundle savedInstanceState) {
3435
mChart.setDrawYValues(false);
3536
mChart.setDrawGridBackground(false);
3637
mChart.setDescription("");
38+
39+
addEmptyData();
3740

38-
// create 30 x-vals
39-
String[] xVals = new String[30];
40-
41-
for (int i = 0; i < 30; i++)
42-
xVals[i] = "" + i;
43-
44-
// create 10 y-vals
45-
ArrayList<Entry> yVals = new ArrayList<Entry>();
46-
47-
for (int i = 0; i < 10; i++)
48-
yVals.add(new Entry((float) (Math.random() * 50) + 50f, i));
49-
50-
LineDataSet set = new LineDataSet(yVals, "DataSet 1");
51-
set.setLineWidth(2.5f);
52-
set.setCircleSize(4.5f);
53-
set.setColor(Color.rgb(240, 99, 99));
54-
set.setCircleColor(Color.rgb(240, 99, 99));
55-
set.setHighLightColor(Color.rgb(190, 190, 190));
56-
57-
LineData data = new LineData(xVals, set);
58-
59-
mChart.setData(data);
6041
mChart.invalidate();
6142
}
62-
63-
int[] mColors = ColorTemplate.VORDIPLOM_COLORS;
43+
44+
int[] mColors = ColorTemplate.VORDIPLOM_COLORS;
6445

6546
private void addEntry() {
66-
67-
LineData data = mChart.getDataOriginal();
6847

69-
LineDataSet set = data.getDataSetByIndex(0);
70-
// set.addEntry(...);
48+
LineData data = mChart.getDataOriginal();
7149

72-
if(set != null) {
73-
50+
if(data != null) {
51+
52+
LineDataSet set = data.getDataSetByIndex(0);
53+
// set.addEntry(...);
54+
55+
if (set == null) {
56+
set = createSet();
57+
data.addDataSet(set);
58+
}
59+
7460
data.addEntry(new Entry((float) (Math.random() * 50) + 50f, set.getEntryCount()), 0);
7561

7662
// let the chart know it's data has changed
7763
mChart.notifyDataSetChanged();
7864

7965
// redraw the chart
80-
mChart.invalidate();
66+
mChart.invalidate();
8167
}
8268
}
8369

8470
private void removeLastEntry() {
85-
86-
LineData data = mChart.getDataOriginal();
8771

88-
LineDataSet set = data.getDataSetByIndex(0);
72+
LineData data = mChart.getDataOriginal();
8973

90-
if(set != null) {
74+
if(data != null) {
75+
76+
LineDataSet set = data.getDataSetByIndex(0);
9177

92-
Entry e = set.getEntryForXIndex(set.getEntryCount() - 1);
78+
if (set != null) {
9379

94-
data.removeEntry(e, 0);
95-
// or remove by index
96-
// mData.removeEntry(xIndex, dataSetIndex);
80+
Entry e = set.getEntryForXIndex(set.getEntryCount() - 1);
9781

98-
mChart.notifyDataSetChanged();
99-
mChart.invalidate();
82+
data.removeEntry(e, 0);
83+
// or remove by index
84+
// mData.removeEntry(xIndex, dataSetIndex);
85+
86+
mChart.notifyDataSetChanged();
87+
mChart.invalidate();
88+
}
10089
}
10190
}
10291

10392
private void addDataSet() {
104-
93+
10594
LineData data = mChart.getDataOriginal();
10695

107-
int count = (data.getDataSetCount() + 1);
96+
if(data != null) {
10897

109-
// create 10 y-vals
110-
ArrayList<Entry> yVals = new ArrayList<Entry>();
98+
int count = (data.getDataSetCount() + 1);
11199

112-
for (int i = 0; i < data.getXValCount(); i++)
113-
yVals.add(new Entry((float) (Math.random() * 50f) + 50f * count, i));
114-
100+
// create 10 y-vals
101+
ArrayList<Entry> yVals = new ArrayList<Entry>();
115102

116-
LineDataSet set = new LineDataSet(yVals, "DataSet " + count);
117-
set.setLineWidth(2.5f);
118-
set.setCircleSize(4.5f);
119-
120-
int color = mColors[count % mColors.length];
121-
122-
set.setColor(color);
123-
set.setCircleColor(color);
124-
set.setHighLightColor(color);
103+
for (int i = 0; i < data.getXValCount(); i++)
104+
yVals.add(new Entry((float) (Math.random() * 50f) + 50f * count, i));
125105

126-
data.addDataSet(set);
127-
mChart.notifyDataSetChanged();
128-
mChart.invalidate();
106+
LineDataSet set = new LineDataSet(yVals, "DataSet " + count);
107+
set.setLineWidth(2.5f);
108+
set.setCircleSize(4.5f);
109+
110+
int color = mColors[count % mColors.length];
111+
112+
set.setColor(color);
113+
set.setCircleColor(color);
114+
set.setHighLightColor(color);
115+
116+
data.addDataSet(set);
117+
mChart.notifyDataSetChanged();
118+
mChart.invalidate();
119+
}
129120
}
130121

131122
private void removeDataSet() {
132-
123+
133124
LineData data = mChart.getDataOriginal();
125+
126+
if(data != null) {
134127

135-
data.removeDataSet(data.getDataSetByIndex(data.getDataSetCount() - 1));
128+
data.removeDataSet(data.getDataSetByIndex(data.getDataSetCount() - 1));
129+
130+
mChart.notifyDataSetChanged();
131+
mChart.invalidate();
132+
}
133+
}
134+
135+
private void addEmptyData() {
136136

137-
mChart.notifyDataSetChanged();
137+
// create 30 x-vals
138+
String[] xVals = new String[30];
139+
140+
for (int i = 0; i < 30; i++)
141+
xVals[i] = "" + i;
142+
143+
// create a chartdata object that contains only the x-axis labels (no entries or datasets)
144+
LineData data = new LineData(xVals);
145+
146+
mChart.setData(data);
138147
mChart.invalidate();
139148
}
140149

@@ -174,8 +183,28 @@ public boolean onOptionsItemSelected(MenuItem item) {
174183
removeDataSet();
175184
Toast.makeText(this, "DataSet removed!", Toast.LENGTH_SHORT).show();
176185
break;
186+
case R.id.actionAddEmptyLineData:
187+
addEmptyData();
188+
Toast.makeText(this, "Empty data added!", Toast.LENGTH_SHORT).show();
189+
break;
190+
case R.id.actionClear:
191+
mChart.clear();
192+
Toast.makeText(this, "Chart cleared!", Toast.LENGTH_SHORT).show();
193+
break;
177194
}
178195

179196
return true;
180197
}
198+
199+
private LineDataSet createSet() {
200+
201+
LineDataSet set = new LineDataSet(null, "DataSet 1");
202+
set.setLineWidth(2.5f);
203+
set.setCircleSize(4.5f);
204+
set.setColor(Color.rgb(240, 99, 99));
205+
set.setCircleColor(Color.rgb(240, 99, 99));
206+
set.setHighLightColor(Color.rgb(190, 190, 190));
207+
208+
return set;
209+
}
181210
}

MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,17 @@ protected void onCreate(Bundle savedInstanceState) {
7878
// add a selection listener
7979
mChart.setOnChartValueSelectedListener(this);
8080
// mChart.setTouchEnabled(false);
81-
82-
mSeekBarX.setProgress(3);
83-
mSeekBarY.setProgress(100);
84-
81+
82+
setData(3, 100);
83+
8584
mChart.animateXY(1500, 1500);
8685
// mChart.spin(2000, 0, 360);
8786

8887
Legend l = mChart.getLegend();
8988
l.setPosition(LegendPosition.RIGHT_OF_CHART);
9089
l.setXEntrySpace(7f);
9190
l.setYEntrySpace(5f);
91+
9292
}
9393

9494
@Override
@@ -172,15 +172,20 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
172172
tvX.setText("" + (mSeekBarX.getProgress() + 1));
173173
tvY.setText("" + (mSeekBarY.getProgress()));
174174

175-
float mult = (float) mSeekBarY.getProgress();
175+
setData(mSeekBarX.getProgress(), mSeekBarY.getProgress());
176+
}
177+
178+
private void setData(int count, float range) {
179+
180+
float mult = range;
176181

177182
ArrayList<Entry> yVals1 = new ArrayList<Entry>();
178183
// ArrayList<Entry> yVals2 = new ArrayList<Entry>();
179184

180185
// IMPORTANT: In a PieChart, no values (Entry) should have the same
181186
// xIndex (even if from different DataSets), since no values can be
182187
// drawn above each other.
183-
for (int i = 0; i < mSeekBarX.getProgress() + 1; i++) {
188+
for (int i = 0; i < count + 1; i++) {
184189
yVals1.add(new Entry((float) (Math.random() * mult) + mult / 5, i));
185190
}
186191

@@ -191,7 +196,7 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
191196

192197
ArrayList<String> xVals = new ArrayList<String>();
193198

194-
for (int i = 0; i < mSeekBarX.getProgress() + 1; i++)
199+
for (int i = 0; i < count + 1; i++)
195200
xVals.add(mParties[i % mParties.length]);
196201

197202
PieDataSet set1 = new PieDataSet(yVals1, "Election Results");

0 commit comments

Comments
 (0)