Skip to content

Commit 8cf6fef

Browse files
committed
Worked on dynamically adding and removing Entries and DataSets.
1 parent 61ffa43 commit 8cf6fef

6 files changed

Lines changed: 170 additions & 44 deletions

File tree

MPChartExample/res/menu/dynamical.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,14 @@
1111
android:title="Remove Entry">
1212
</item>
1313

14+
<item
15+
android:id="@+id/actionAddDataSet"
16+
android:title="Add DataSet">
17+
</item>
18+
19+
<item
20+
android:id="@+id/actionRemoveDataSet"
21+
android:title="Remove DataSet">
22+
</item>
23+
1424
</menu>

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

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
import android.widget.Toast;
1010

1111
import com.github.mikephil.charting.charts.LineChart;
12-
import com.github.mikephil.charting.data.DataSet;
12+
import com.github.mikephil.charting.data.BarEntry;
1313
import com.github.mikephil.charting.data.Entry;
1414
import com.github.mikephil.charting.data.LineData;
1515
import com.github.mikephil.charting.data.LineDataSet;
1616
import com.github.mikephil.charting.interfaces.OnChartValueSelectedListener;
17+
import com.github.mikephil.charting.utils.ColorTemplate;
1718
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
1819

1920
import java.util.ArrayList;
@@ -46,7 +47,7 @@ protected void onCreate(Bundle savedInstanceState) {
4647
ArrayList<Entry> yVals = new ArrayList<Entry>();
4748

4849
for (int i = 0; i < 10; i++)
49-
yVals.add(new Entry((float) (Math.random() * 100) + 50f, i));
50+
yVals.add(new Entry((float) (Math.random() * 50) + 50f, i));
5051

5152
LineDataSet set = new LineDataSet(yVals, "DataSet 1");
5253
set.setLineWidth(2.5f);
@@ -60,15 +61,20 @@ protected void onCreate(Bundle savedInstanceState) {
6061
mChart.setData(mData);
6162
mChart.invalidate();
6263
}
64+
65+
int[] mColors = ColorTemplate.VORDIPLOM_COLORS;
6366

6467
private void addEntry() {
6568

6669
LineDataSet set = mData.getDataSetByIndex(0);
67-
// set.addEntry();
70+
// set.addEntry(...);
6871

69-
mData.addEntry(new Entry((float) (Math.random() * 100) + 500f, set.getEntryCount()), 0);
72+
mData.addEntry(new Entry((float) (Math.random() * 50) + 50f, set.getEntryCount()), 0);
7073

74+
// let the chart know it's data has changed
7175
mChart.notifyDataSetChanged();
76+
77+
// redraw the chart
7278
mChart.invalidate();
7379
}
7480

@@ -79,11 +85,48 @@ private void removeLastEntry() {
7985
Entry e = set.getEntryForXIndex(set.getEntryCount() - 1);
8086

8187
mData.removeEntry(e, 0);
88+
// or remove by index
89+
// mData.removeEntry(xIndex, dataSetIndex);
8290

8391
mChart.notifyDataSetChanged();
8492
mChart.invalidate();
8593
}
8694

95+
private void addDataSet() {
96+
97+
int count = (mData.getDataSetCount() + 1);
98+
99+
// create 10 y-vals
100+
ArrayList<Entry> yVals = new ArrayList<Entry>();
101+
102+
for (int i = 0; i < mData.getXValCount(); i++)
103+
yVals.add(new Entry((float) (Math.random() * 50f) + 50f * count, i));
104+
105+
106+
LineDataSet set = new LineDataSet(yVals, "DataSet " + count);
107+
set.setLineWidth(2.5f);
108+
set.setCircleSize(4.5f);
109+
110+
int color = getResources().getColor(mColors[count % mColors.length]);
111+
112+
set.setColor(color);
113+
set.setCircleColor(color);
114+
set.setHighLightColor(color);
115+
116+
mData.addDataSet(set);
117+
118+
mChart.notifyDataSetChanged();
119+
mChart.invalidate();
120+
}
121+
122+
private void removeDataSet() {
123+
124+
mData.removeDataSet(mData.getDataSetByIndex(mData.getDataSetCount() - 1));
125+
126+
mChart.notifyDataSetChanged();
127+
mChart.invalidate();
128+
}
129+
87130
@Override
88131
public void onValueSelected(Entry e, int dataSetIndex) {
89132
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
@@ -112,6 +155,14 @@ public boolean onOptionsItemSelected(MenuItem item) {
112155
removeLastEntry();
113156
Toast.makeText(this, "Entry removed!", Toast.LENGTH_SHORT).show();
114157
break;
158+
case R.id.actionAddDataSet:
159+
addDataSet();
160+
Toast.makeText(this, "DataSet added!", Toast.LENGTH_SHORT).show();
161+
break;
162+
case R.id.actionRemoveDataSet:
163+
removeDataSet();
164+
Toast.makeText(this, "DataSet removed!", Toast.LENGTH_SHORT).show();
165+
break;
115166
}
116167

117168
return true;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.util.Log;
77
import android.view.Menu;
88
import android.view.MenuItem;
9+
import android.view.View;
910
import android.view.WindowManager;
1011
import android.widget.SeekBar;
1112
import android.widget.SeekBar.OnSeekBarChangeListener;
@@ -17,7 +18,6 @@
1718
import com.github.mikephil.charting.data.PieDataSet;
1819
import com.github.mikephil.charting.interfaces.OnChartValueSelectedListener;
1920
import com.github.mikephil.charting.utils.ColorTemplate;
20-
import com.github.mikephil.charting.utils.Highlight;
2121
import com.github.mikephil.charting.utils.Legend;
2222
import com.github.mikephil.charting.utils.Legend.LegendPosition;
2323
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,10 @@ protected void setData(ChartData data) {
406406
*/
407407
public abstract void prepare();
408408

409-
/** lets the chart know its unterlying data has changed */
409+
/**
410+
* Lets the chart know its underlying data has changed and performs all
411+
* necessary recalculations.
412+
*/
410413
public abstract void notifyDataSetChanged();
411414

412415
/**
@@ -499,7 +502,7 @@ protected void prepareMatrixOffset() {
499502
mMatrixOffset.reset();
500503

501504
mMatrixOffset.postTranslate(mOffsetLeft, getHeight() - mOffsetBottom);
502-
505+
503506
// mMatrixOffset.setTranslate(mOffsetLeft, 0);
504507
// mMatrixOffset.postScale(1.0f, -1.0f);
505508
}
@@ -1289,15 +1292,15 @@ public void setPhaseX(float phase) {
12891292
// prepareMatrix();
12901293
// calculateOffsets();
12911294
// }
1292-
//
1293-
// public void addDataSet(DataSet d) {
1294-
// mOriginalData.addDataSet(d);
1295-
//
1296-
// prepare();
1297-
// calcMinMax(false);
1298-
// prepareMatrix();
1299-
// calculateOffsets();
1300-
// }
1295+
//
1296+
// public void addDataSet(DataSet d) {
1297+
// mOriginalData.addDataSet(d);
1298+
//
1299+
// prepare();
1300+
// calcMinMax(false);
1301+
// prepareMatrix();
1302+
// calculateOffsets();
1303+
// }
13011304

13021305
/**
13031306
* ################ ################ ################ ################

MPChartLib/src/com/github/mikephil/charting/data/ChartData.java

Lines changed: 75 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,22 @@ public void notifyDataChanged() {
136136
*/
137137
protected void calcMinMax(ArrayList<T> dataSets) {
138138

139-
mYMin = dataSets.get(0).getYMin();
140-
mYMax = dataSets.get(0).getYMax();
139+
if (dataSets == null || dataSets.size() < 1) {
140+
141+
mYMax = 0f;
142+
mYMin = 0f;
143+
} else {
141144

142-
for (int i = 0; i < dataSets.size(); i++) {
143-
if (dataSets.get(i).getYMin() < mYMin)
144-
mYMin = dataSets.get(i).getYMin();
145+
mYMin = dataSets.get(0).getYMin();
146+
mYMax = dataSets.get(0).getYMax();
147+
148+
for (int i = 0; i < dataSets.size(); i++) {
149+
if (dataSets.get(i).getYMin() < mYMin)
150+
mYMin = dataSets.get(i).getYMin();
145151

146-
if (dataSets.get(i).getYMax() > mYMax)
147-
mYMax = dataSets.get(i).getYMax();
152+
if (dataSets.get(i).getYMax() > mYMax)
153+
mYMax = dataSets.get(i).getYMax();
154+
}
148155
}
149156
}
150157

@@ -397,6 +404,48 @@ public void addDataSet(T d) {
397404
mYMin = d.getYMin();
398405
}
399406

407+
/**
408+
* Removes the given DataSet from this data object. Also recalculates all
409+
* minimum and maximum values. Returns true if a DataSet was removed, false
410+
* if no DataSet could be removed.
411+
*
412+
* @param d
413+
*/
414+
public boolean removeDataSet(T d) {
415+
416+
if (mDataSets == null || d == null)
417+
return false;
418+
419+
boolean removed = mDataSets.remove(d);
420+
421+
// if a DataSet was removed
422+
if (removed) {
423+
424+
mYValCount -= d.getEntryCount();
425+
mYValueSum -= d.getYValueSum();
426+
427+
calcMinMax(mDataSets);
428+
}
429+
430+
return removed;
431+
}
432+
433+
/**
434+
* Removes the DataSet at the given index in the DataSet array from the data
435+
* object. Also recalculates all minimum and maximum values. Returns true if
436+
* a DataSet was removed, false if no DataSet could be removed.
437+
*
438+
* @param index
439+
*/
440+
public boolean removeDataSet(int index) {
441+
442+
if (mDataSets == null || index >= mDataSets.size())
443+
return false;
444+
445+
T set = mDataSets.get(index);
446+
return removeDataSet(set);
447+
}
448+
400449
/**
401450
* Adds an Entry to the DataSet at the specified index. Entries are added to
402451
* the end of the list.
@@ -426,39 +475,46 @@ public void addEntry(Entry e, int dataSetIndex) {
426475
* @param e
427476
* @param dataSetIndex
428477
*/
429-
public void removeEntry(Entry e, int dataSetIndex) {
478+
public boolean removeEntry(Entry e, int dataSetIndex) {
430479

431480
// entry null, outofbounds
432481
if (e == null || dataSetIndex >= mDataSets.size())
433-
return;
482+
return false;
434483

435-
float val = e.getVal();
484+
// remove the entry from the dataset
485+
boolean removed = mDataSets.get(dataSetIndex).removeEntry(e.getXIndex());
436486

437-
mYValCount -= 1;
438-
mYValueSum -= val;
487+
if (removed) {
439488

440-
// remove the entry from the dataset
441-
mDataSets.get(dataSetIndex).removeEntry(e.getXIndex());
489+
float val = e.getVal();
442490

443-
calcMinMax(mDataSets);
491+
mYValCount -= 1;
492+
mYValueSum -= val;
493+
494+
calcMinMax(mDataSets);
495+
}
496+
497+
return removed;
444498
}
445499

446500
/**
447501
* Removes the Entry object at the given xIndex from the DataSet at the
448-
* specified index.
502+
* specified index. Returns true if an Entry was removed, false if no Entry
503+
* was found that meets the specified requirements.
449504
*
450505
* @param xIndex
451506
* @param dataSetIndex
507+
* @return
452508
*/
453-
public void removeEntry(int xIndex, int dataSetIndex) {
509+
public boolean removeEntry(int xIndex, int dataSetIndex) {
454510

455511
if (dataSetIndex >= mDataSets.size())
456-
return;
512+
return false;
457513

458514
T dataSet = mDataSets.get(dataSetIndex);
459515
Entry e = dataSet.getEntryForXIndex(xIndex);
460516

461-
removeEntry(e, dataSetIndex);
517+
return removeEntry(e, dataSetIndex);
462518
}
463519

464520
/**

MPChartLib/src/com/github/mikephil/charting/data/DataSet.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -333,34 +333,40 @@ public void addEntry(Entry e) {
333333
/**
334334
* Removes an Entry from the DataSets entries array. This will also
335335
* recalculate the current minimum and maximum values of the DataSet and the
336-
* value-sum.
336+
* value-sum. Returns true if an Entry was removed, false if no Entry could
337+
* be removed.
337338
*
338339
* @param e
339340
*/
340-
public void removeEntry(T e) {
341+
public boolean removeEntry(T e) {
341342

342343
if (e == null)
343-
return;
344+
return false;
344345

345346
// remove the entry
346-
mYVals.remove(e);
347+
boolean removed = mYVals.remove(e);
347348

348-
float val = e.getVal();
349+
if (removed) {
349350

350-
calcMinMax();
351+
float val = e.getVal();
352+
mYValueSum -= val;
353+
354+
calcMinMax();
355+
}
351356

352-
mYValueSum -= val;
357+
return removed;
353358
}
354359

355360
/**
356361
* Removes the Entry object that has the given xIndex from the DataSet.
362+
* Returns true if an Entry was removed, false if no Entry could be removed.
357363
*
358364
* @param xIndex
359365
*/
360-
public void removeEntry(int xIndex) {
366+
public boolean removeEntry(int xIndex) {
361367

362368
T e = getEntryForXIndex(xIndex);
363-
removeEntry(e);
369+
return removeEntry(e);
364370
}
365371

366372
/** BELOW THIS COLOR HANDLING */

0 commit comments

Comments
 (0)