Skip to content

Commit 6bdbae3

Browse files
committed
Improved animations, bugfixes.
1 parent 9861812 commit 6bdbae3

File tree

11 files changed

+136
-44
lines changed

11 files changed

+136
-44
lines changed

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ protected void onCreate(Bundle savedInstanceState) {
114114

115115
// add data
116116
setData(45, 100);
117+
118+
mChart.animateX(2500);
117119

118120
// // restrain the maximum scale-out factor
119121
// mChart.setScaleMinima(3f, 3f);
@@ -237,6 +239,15 @@ public boolean onOptionsItemSelected(MenuItem item) {
237239
mChart.disableFiltering();
238240
}
239241
mChart.invalidate();
242+
243+
//
244+
// for(int i = 0; i < 10; i++) {
245+
// mChart.addEntry(new Entry((float) (Math.random() * 100), i+2), 0);
246+
// mChart.invalidate();
247+
// }
248+
//
249+
// Toast.makeText(getApplicationContext(), "valcount: " + mChart.getDataOriginal().getYValCount() + ", valsum: " + mChart.getDataOriginal().getYValueSum(), Toast.LENGTH_SHORT).show();
250+
//
240251
break;
241252
}
242253
case R.id.actionSave: {
@@ -307,7 +318,7 @@ private void setData(int count, float range) {
307318
// 0.1) / 10);
308319
yVals.add(new Entry(val, i));
309320
}
310-
321+
311322
// create a dataset and give it a type
312323
LineDataSet set1 = new LineDataSet(yVals, "DataSet 1");
313324

@@ -322,16 +333,16 @@ private void setData(int count, float range) {
322333
// create a data object with the datasets
323334
LineData data = new LineData(xVals, dataSets);
324335

325-
// LimitLine ll = new LimitLine(130f);
326-
// ll.setLineWidth(4f);
327-
// ll.enableDashedLine(10f, 10f, 0f);
328-
//
329-
// LimitLine ll1 = new LimitLine(-30f);
330-
// ll1.setLineWidth(4f);
331-
// ll1.enableDashedLine(10f, 10f, 0f);
332-
//
333-
// data.addLimitLine(ll);
334-
// data.addLimitLine(ll1);
336+
LimitLine ll1 = new LimitLine(130f);
337+
ll1.setLineWidth(4f);
338+
ll1.enableDashedLine(10f, 10f, 0f);
339+
340+
LimitLine ll2 = new LimitLine(-30f);
341+
ll2.setLineWidth(4f);
342+
ll2.enableDashedLine(10f, 10f, 0f);
343+
344+
data.addLimitLine(ll1);
345+
data.addLimitLine(ll2);
335346

336347
// set data
337348
mChart.setData(data);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ protected void onCreate(Bundle savedInstanceState) {
4343

4444
ListView lv = (ListView) findViewById(R.id.listView1);
4545

46-
ArrayList<ChartData> list = new ArrayList<ChartData>();
46+
ArrayList<BarData> list = new ArrayList<BarData>();
4747

4848
// 20 items
4949
for (int i = 0; i < 20; i++) {
@@ -54,11 +54,11 @@ protected void onCreate(Bundle savedInstanceState) {
5454
lv.setAdapter(cda);
5555
}
5656

57-
private class ChartDataAdapter extends ArrayAdapter<ChartData> {
57+
private class ChartDataAdapter extends ArrayAdapter<BarData> {
5858

5959
private Typeface mTf;
6060

61-
public ChartDataAdapter(Context context, List<ChartData> objects) {
61+
public ChartDataAdapter(Context context, List<BarData> objects) {
6262
super(context, 0, objects);
6363

6464
mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");
@@ -67,7 +67,7 @@ public ChartDataAdapter(Context context, List<ChartData> objects) {
6767
@Override
6868
public View getView(int position, View convertView, ViewGroup parent) {
6969

70-
ChartData c = getItem(position);
70+
BarData c = getItem(position);
7171

7272
ViewHolder holder = null;
7373

MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/SimpleFragment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
public abstract class SimpleFragment extends Fragment {
2424

25-
protected ChartData generateBarData(int dataSets, float range, int count) {
25+
protected BarData generateBarData(int dataSets, float range, int count) {
2626

2727
ArrayList<BarDataSet> sets = new ArrayList<BarDataSet>();
2828

@@ -45,7 +45,7 @@ protected ChartData generateBarData(int dataSets, float range, int count) {
4545
return d;
4646
}
4747

48-
protected ChartData generateScatterData(int dataSets, float range, int count) {
48+
protected ScatterData generateScatterData(int dataSets, float range, int count) {
4949

5050
ArrayList<ScatterDataSet> sets = new ArrayList<ScatterDataSet>();
5151

MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/BarChartItem.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.view.View;
77

88
import com.github.mikephil.charting.charts.BarChart;
9+
import com.github.mikephil.charting.data.BarData;
910
import com.github.mikephil.charting.data.ChartData;
1011
import com.github.mikephil.charting.utils.XLabels;
1112
import com.github.mikephil.charting.utils.XLabels.XLabelPosition;
@@ -63,7 +64,7 @@ public View getView(int position, View convertView, Context c) {
6364
yl.setLabelCount(5);
6465

6566
// set data
66-
holder.chart.setData(mChartData);
67+
holder.chart.setData((BarData) mChartData);
6768

6869
// do not forget to refresh the chart
6970
// holder.chart.invalidate();

MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/LineChartItem.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
package com.xxmassdeveloper.mpchartexample.listviewitems;
23

34
import android.content.Context;
@@ -7,15 +8,16 @@
78

89
import com.github.mikephil.charting.charts.LineChart;
910
import com.github.mikephil.charting.data.ChartData;
11+
import com.github.mikephil.charting.data.LineData;
1012
import com.github.mikephil.charting.utils.XLabels;
1113
import com.github.mikephil.charting.utils.XLabels.XLabelPosition;
1214
import com.github.mikephil.charting.utils.YLabels;
1315
import com.xxmassdeveloper.mpchartexample.R;
1416

1517
public class LineChartItem extends ChartItem {
16-
18+
1719
private Typeface mTf;
18-
20+
1921
public LineChartItem(ChartData cd, Context c) {
2022
super(cd);
2123

@@ -47,7 +49,7 @@ public View getView(int position, View convertView, Context c) {
4749
}
4850

4951
// apply styling
50-
// holder.chart.setValueTypeface(mTf);
52+
// holder.chart.setValueTypeface(mTf);
5153
holder.chart.setDrawYValues(false);
5254
holder.chart.setDescription("");
5355
holder.chart.setDrawVerticalGrid(false);
@@ -57,21 +59,21 @@ public View getView(int position, View convertView, Context c) {
5759
xl.setCenterXLabelText(true);
5860
xl.setPosition(XLabelPosition.BOTTOM);
5961
xl.setTypeface(mTf);
60-
62+
6163
YLabels yl = holder.chart.getYLabels();
6264
yl.setTypeface(mTf);
6365
yl.setLabelCount(5);
6466

6567
// set data
66-
holder.chart.setData(mChartData);
67-
68+
holder.chart.setData((LineData) mChartData);
69+
6870
// do not forget to refresh the chart
69-
// holder.chart.invalidate();
71+
// holder.chart.invalidate();
7072
holder.chart.animateX(1000);
7173

7274
return convertView;
7375
}
74-
76+
7577
private static class ViewHolder {
7678
LineChart chart;
7779
}

MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/PieChartItem.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
package com.xxmassdeveloper.mpchartexample.listviewitems;
23

34
import android.content.Context;
@@ -7,17 +8,18 @@
78

89
import com.github.mikephil.charting.charts.PieChart;
910
import com.github.mikephil.charting.data.ChartData;
11+
import com.github.mikephil.charting.data.PieData;
1012
import com.github.mikephil.charting.utils.Legend;
1113
import com.github.mikephil.charting.utils.Legend.LegendPosition;
1214
import com.xxmassdeveloper.mpchartexample.R;
1315

1416
public class PieChartItem extends ChartItem {
15-
17+
1618
private Typeface mTf;
17-
19+
1820
public PieChartItem(ChartData cd, Context c) {
1921
super(cd);
20-
22+
2123
mTf = Typeface.createFromAsset(c.getAssets(), "OpenSans-Regular.ttf");
2224
}
2325

@@ -57,18 +59,18 @@ public View getView(int position, View convertView, Context c) {
5759
holder.chart.setUsePercentValues(true);
5860

5961
// set data
60-
holder.chart.setData(mChartData);
61-
62+
holder.chart.setData((PieData) mChartData);
63+
6264
Legend l = holder.chart.getLegend();
6365
l.setPosition(LegendPosition.RIGHT_OF_CHART);
64-
66+
6567
// do not forget to refresh the chart
66-
// holder.chart.invalidate();
68+
// holder.chart.invalidate();
6769
holder.chart.animateXY(900, 900);
6870

6971
return convertView;
7072
}
71-
73+
7274
private static class ViewHolder {
7375
PieChart chart;
7476
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,8 @@ protected void drawValues() {
513513
if (isOffContentRight(valuePoints[j]))
514514
break;
515515

516-
if (isOffContentLeft(valuePoints[j]))
516+
if (isOffContentLeft(valuePoints[j]) || isOffContentTop(valuePoints[j + 1])
517+
|| isOffContentBottom(valuePoints[j + 1]))
517518
continue;
518519

519520
float val = entries.get(j / 2).getSum();
@@ -530,7 +531,8 @@ protected void drawValues() {
530531
if (isOffContentRight(valuePoints[j]))
531532
break;
532533

533-
if (isOffContentLeft(valuePoints[j]))
534+
if (isOffContentLeft(valuePoints[j]) || isOffContentTop(valuePoints[j + 1])
535+
|| isOffContentBottom(valuePoints[j + 1]))
534536
continue;
535537

536538
Entry e = entries.get(j / 2);

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,6 @@ protected void calculateOffsets() {
307307
if (mLegend == null)
308308
return;
309309

310-
Log.i(LOG_TAG, "Offsets calculated.");
311-
312310
// setup offsets for legend
313311
if (mLegend.getPosition() == LegendPosition.RIGHT_OF_CHART) {
314312

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ protected void init() {
309309
*
310310
* @param data
311311
*/
312-
public void setData(ChartData data) {
312+
protected void setData(ChartData data) {
313313

314314
if (data == null || !data.isValid()) {
315315
Log.e(LOG_TAG,
@@ -1151,7 +1151,39 @@ public float getPhaseX() {
11511151
public void setPhaseX(float phase) {
11521152
mPhaseX = phase;
11531153
}
1154+
1155+
/**
1156+
* ################ ################ ################ ################
1157+
*/
1158+
/** BELOW THIS FOR DYNAMICALLY ADDING ENTRIES AND DATASETS */
11541159

1160+
public void addEntry(Entry e, int dataSetIndex) {
1161+
mOriginalData.getDataSetByIndex(dataSetIndex).addEntry(e);
1162+
1163+
prepare();
1164+
calcMinMax(false);
1165+
prepareMatrix();
1166+
calculateOffsets();
1167+
}
1168+
1169+
public void addEntry(Entry e, String label) {
1170+
mOriginalData.getDataSetByLabel(label, false).addEntry(e);
1171+
1172+
prepare();
1173+
calcMinMax(false);
1174+
prepareMatrix();
1175+
calculateOffsets();
1176+
}
1177+
1178+
public void addDataSet(DataSet d) {
1179+
mOriginalData.addDataSet(d);
1180+
1181+
prepare();
1182+
calcMinMax(false);
1183+
prepareMatrix();
1184+
calculateOffsets();
1185+
}
1186+
11551187
/**
11561188
* ################ ################ ################ ################
11571189
*/

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ private void isLegal(ArrayList<? extends DataSet> dataSets) {
120120
.getYVals()
121121
.size() > mXVals.size()) {
122122
throw new IllegalArgumentException(
123-
"One or more of the DataSet Entry arrays are longer than the x-values array.");
123+
"One or more of the DataSet Entry arrays are longer than the x-values array of this ChartData object.");
124124
}
125125
}
126126
}
@@ -380,6 +380,25 @@ public DataSet getDataSetByIndex(int index) {
380380
return mDataSets.get(index);
381381
}
382382

383+
/**
384+
* Adds a DataSet dynamically.
385+
*
386+
* @param d
387+
*/
388+
public void addDataSet(DataSet d) {
389+
if (mDataSets == null)
390+
mDataSets = new ArrayList<DataSet>();
391+
((ArrayList<DataSet>) mDataSets).add(d);
392+
393+
mYValCount += d.getEntryCount();
394+
mYValueSum += d.getYValueSum();
395+
396+
if (mYMax < d.getYMax())
397+
mYMax = d.getYMax();
398+
if (mYMin > d.getYMin())
399+
mYMin = d.getYMin();
400+
}
401+
383402
/**
384403
* Returns all colors used across all DataSet objects this object
385404
* represents.

0 commit comments

Comments
 (0)