From 6bdbae3589a0fb013e5117addc866fcd266b1a83 Mon Sep 17 00:00:00 2001 From: Philipp Jahoda Date: Mon, 18 Aug 2014 14:36:00 +0200 Subject: [PATCH] Improved animations, bugfixes. --- .../mpchartexample/LineChartActivity.java | 33 ++++++++++++------ .../ListViewBarChartActivity.java | 8 ++--- .../fragments/SimpleFragment.java | 4 +-- .../listviewitems/BarChartItem.java | 3 +- .../listviewitems/LineChartItem.java | 18 +++++----- .../listviewitems/PieChartItem.java | 18 +++++----- .../mikephil/charting/charts/BarChart.java | 6 ++-- .../charting/charts/BarLineChartBase.java | 2 -- .../mikephil/charting/charts/Chart.java | 34 ++++++++++++++++++- .../mikephil/charting/data/ChartData.java | 21 +++++++++++- .../mikephil/charting/data/DataSet.java | 33 +++++++++++++++--- 11 files changed, 136 insertions(+), 44 deletions(-) diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity.java index af73b2ac98..474c048515 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity.java @@ -114,6 +114,8 @@ protected void onCreate(Bundle savedInstanceState) { // add data setData(45, 100); + + mChart.animateX(2500); // // restrain the maximum scale-out factor // mChart.setScaleMinima(3f, 3f); @@ -237,6 +239,15 @@ public boolean onOptionsItemSelected(MenuItem item) { mChart.disableFiltering(); } mChart.invalidate(); + +// +// for(int i = 0; i < 10; i++) { +// mChart.addEntry(new Entry((float) (Math.random() * 100), i+2), 0); +// mChart.invalidate(); +// } +// +// Toast.makeText(getApplicationContext(), "valcount: " + mChart.getDataOriginal().getYValCount() + ", valsum: " + mChart.getDataOriginal().getYValueSum(), Toast.LENGTH_SHORT).show(); +// break; } case R.id.actionSave: { @@ -307,7 +318,7 @@ private void setData(int count, float range) { // 0.1) / 10); yVals.add(new Entry(val, i)); } - + // create a dataset and give it a type LineDataSet set1 = new LineDataSet(yVals, "DataSet 1"); @@ -322,16 +333,16 @@ private void setData(int count, float range) { // create a data object with the datasets LineData data = new LineData(xVals, dataSets); -// LimitLine ll = new LimitLine(130f); -// ll.setLineWidth(4f); -// ll.enableDashedLine(10f, 10f, 0f); -// -// LimitLine ll1 = new LimitLine(-30f); -// ll1.setLineWidth(4f); -// ll1.enableDashedLine(10f, 10f, 0f); -// -// data.addLimitLine(ll); -// data.addLimitLine(ll1); + LimitLine ll1 = new LimitLine(130f); + ll1.setLineWidth(4f); + ll1.enableDashedLine(10f, 10f, 0f); + + LimitLine ll2 = new LimitLine(-30f); + ll2.setLineWidth(4f); + ll2.enableDashedLine(10f, 10f, 0f); + + data.addLimitLine(ll1); + data.addLimitLine(ll2); // set data mChart.setData(data); diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/ListViewBarChartActivity.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/ListViewBarChartActivity.java index ffe9758670..a6822bfcbf 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/ListViewBarChartActivity.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/ListViewBarChartActivity.java @@ -43,7 +43,7 @@ protected void onCreate(Bundle savedInstanceState) { ListView lv = (ListView) findViewById(R.id.listView1); - ArrayList list = new ArrayList(); + ArrayList list = new ArrayList(); // 20 items for (int i = 0; i < 20; i++) { @@ -54,11 +54,11 @@ protected void onCreate(Bundle savedInstanceState) { lv.setAdapter(cda); } - private class ChartDataAdapter extends ArrayAdapter { + private class ChartDataAdapter extends ArrayAdapter { private Typeface mTf; - public ChartDataAdapter(Context context, List objects) { + public ChartDataAdapter(Context context, List objects) { super(context, 0, objects); mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf"); @@ -67,7 +67,7 @@ public ChartDataAdapter(Context context, List objects) { @Override public View getView(int position, View convertView, ViewGroup parent) { - ChartData c = getItem(position); + BarData c = getItem(position); ViewHolder holder = null; diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/SimpleFragment.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/SimpleFragment.java index d1d83b877a..0e7a486763 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/SimpleFragment.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/SimpleFragment.java @@ -22,7 +22,7 @@ public abstract class SimpleFragment extends Fragment { - protected ChartData generateBarData(int dataSets, float range, int count) { + protected BarData generateBarData(int dataSets, float range, int count) { ArrayList sets = new ArrayList(); @@ -45,7 +45,7 @@ protected ChartData generateBarData(int dataSets, float range, int count) { return d; } - protected ChartData generateScatterData(int dataSets, float range, int count) { + protected ScatterData generateScatterData(int dataSets, float range, int count) { ArrayList sets = new ArrayList(); diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/BarChartItem.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/BarChartItem.java index 06de9bfb11..e6def5964f 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/BarChartItem.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/BarChartItem.java @@ -6,6 +6,7 @@ import android.view.View; import com.github.mikephil.charting.charts.BarChart; +import com.github.mikephil.charting.data.BarData; import com.github.mikephil.charting.data.ChartData; import com.github.mikephil.charting.utils.XLabels; import com.github.mikephil.charting.utils.XLabels.XLabelPosition; @@ -63,7 +64,7 @@ public View getView(int position, View convertView, Context c) { yl.setLabelCount(5); // set data - holder.chart.setData(mChartData); + holder.chart.setData((BarData) mChartData); // do not forget to refresh the chart // holder.chart.invalidate(); diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/LineChartItem.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/LineChartItem.java index c77acd042e..ad530d226d 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/LineChartItem.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/LineChartItem.java @@ -1,3 +1,4 @@ + package com.xxmassdeveloper.mpchartexample.listviewitems; import android.content.Context; @@ -7,15 +8,16 @@ import com.github.mikephil.charting.charts.LineChart; import com.github.mikephil.charting.data.ChartData; +import com.github.mikephil.charting.data.LineData; import com.github.mikephil.charting.utils.XLabels; import com.github.mikephil.charting.utils.XLabels.XLabelPosition; import com.github.mikephil.charting.utils.YLabels; import com.xxmassdeveloper.mpchartexample.R; public class LineChartItem extends ChartItem { - + private Typeface mTf; - + public LineChartItem(ChartData cd, Context c) { super(cd); @@ -47,7 +49,7 @@ public View getView(int position, View convertView, Context c) { } // apply styling -// holder.chart.setValueTypeface(mTf); + // holder.chart.setValueTypeface(mTf); holder.chart.setDrawYValues(false); holder.chart.setDescription(""); holder.chart.setDrawVerticalGrid(false); @@ -57,21 +59,21 @@ public View getView(int position, View convertView, Context c) { xl.setCenterXLabelText(true); xl.setPosition(XLabelPosition.BOTTOM); xl.setTypeface(mTf); - + YLabels yl = holder.chart.getYLabels(); yl.setTypeface(mTf); yl.setLabelCount(5); // set data - holder.chart.setData(mChartData); - + holder.chart.setData((LineData) mChartData); + // do not forget to refresh the chart -// holder.chart.invalidate(); + // holder.chart.invalidate(); holder.chart.animateX(1000); return convertView; } - + private static class ViewHolder { LineChart chart; } diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/PieChartItem.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/PieChartItem.java index 3a8788c997..1b5cf29871 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/PieChartItem.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/PieChartItem.java @@ -1,3 +1,4 @@ + package com.xxmassdeveloper.mpchartexample.listviewitems; import android.content.Context; @@ -7,17 +8,18 @@ import com.github.mikephil.charting.charts.PieChart; import com.github.mikephil.charting.data.ChartData; +import com.github.mikephil.charting.data.PieData; import com.github.mikephil.charting.utils.Legend; import com.github.mikephil.charting.utils.Legend.LegendPosition; import com.xxmassdeveloper.mpchartexample.R; public class PieChartItem extends ChartItem { - + private Typeface mTf; - + public PieChartItem(ChartData cd, Context c) { super(cd); - + mTf = Typeface.createFromAsset(c.getAssets(), "OpenSans-Regular.ttf"); } @@ -57,18 +59,18 @@ public View getView(int position, View convertView, Context c) { holder.chart.setUsePercentValues(true); // set data - holder.chart.setData(mChartData); - + holder.chart.setData((PieData) mChartData); + Legend l = holder.chart.getLegend(); l.setPosition(LegendPosition.RIGHT_OF_CHART); - + // do not forget to refresh the chart -// holder.chart.invalidate(); + // holder.chart.invalidate(); holder.chart.animateXY(900, 900); return convertView; } - + private static class ViewHolder { PieChart chart; } diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/BarChart.java b/MPChartLib/src/com/github/mikephil/charting/charts/BarChart.java index 2f9383a7d0..f09cede183 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/BarChart.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/BarChart.java @@ -513,7 +513,8 @@ protected void drawValues() { if (isOffContentRight(valuePoints[j])) break; - if (isOffContentLeft(valuePoints[j])) + if (isOffContentLeft(valuePoints[j]) || isOffContentTop(valuePoints[j + 1]) + || isOffContentBottom(valuePoints[j + 1])) continue; float val = entries.get(j / 2).getSum(); @@ -530,7 +531,8 @@ protected void drawValues() { if (isOffContentRight(valuePoints[j])) break; - if (isOffContentLeft(valuePoints[j])) + if (isOffContentLeft(valuePoints[j]) || isOffContentTop(valuePoints[j + 1]) + || isOffContentBottom(valuePoints[j + 1])) continue; Entry e = entries.get(j / 2); diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java b/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java index c40b3daea2..7b92dc1f7b 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java @@ -307,8 +307,6 @@ protected void calculateOffsets() { if (mLegend == null) return; - Log.i(LOG_TAG, "Offsets calculated."); - // setup offsets for legend if (mLegend.getPosition() == LegendPosition.RIGHT_OF_CHART) { diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java b/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java index da6da144e0..247071db5c 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java @@ -309,7 +309,7 @@ protected void init() { * * @param data */ - public void setData(ChartData data) { + protected void setData(ChartData data) { if (data == null || !data.isValid()) { Log.e(LOG_TAG, @@ -1151,7 +1151,39 @@ public float getPhaseX() { public void setPhaseX(float phase) { mPhaseX = phase; } + + /** + * ################ ################ ################ ################ + */ + /** BELOW THIS FOR DYNAMICALLY ADDING ENTRIES AND DATASETS */ + public void addEntry(Entry e, int dataSetIndex) { + mOriginalData.getDataSetByIndex(dataSetIndex).addEntry(e); + + prepare(); + calcMinMax(false); + prepareMatrix(); + calculateOffsets(); + } + + public void addEntry(Entry e, String label) { + mOriginalData.getDataSetByLabel(label, false).addEntry(e); + + prepare(); + calcMinMax(false); + prepareMatrix(); + calculateOffsets(); + } + + public void addDataSet(DataSet d) { + mOriginalData.addDataSet(d); + + prepare(); + calcMinMax(false); + prepareMatrix(); + calculateOffsets(); + } + /** * ################ ################ ################ ################ */ diff --git a/MPChartLib/src/com/github/mikephil/charting/data/ChartData.java b/MPChartLib/src/com/github/mikephil/charting/data/ChartData.java index d26743bb8e..6edf8a8bd6 100644 --- a/MPChartLib/src/com/github/mikephil/charting/data/ChartData.java +++ b/MPChartLib/src/com/github/mikephil/charting/data/ChartData.java @@ -120,7 +120,7 @@ private void isLegal(ArrayList dataSets) { .getYVals() .size() > mXVals.size()) { throw new IllegalArgumentException( - "One or more of the DataSet Entry arrays are longer than the x-values array."); + "One or more of the DataSet Entry arrays are longer than the x-values array of this ChartData object."); } } } @@ -380,6 +380,25 @@ public DataSet getDataSetByIndex(int index) { return mDataSets.get(index); } + /** + * Adds a DataSet dynamically. + * + * @param d + */ + public void addDataSet(DataSet d) { + if (mDataSets == null) + mDataSets = new ArrayList(); + ((ArrayList) mDataSets).add(d); + + mYValCount += d.getEntryCount(); + mYValueSum += d.getYValueSum(); + + if (mYMax < d.getYMax()) + mYMax = d.getYMax(); + if (mYMin > d.getYMin()) + mYMin = d.getYMin(); + } + /** * Returns all colors used across all DataSet objects this object * represents. diff --git a/MPChartLib/src/com/github/mikephil/charting/data/DataSet.java b/MPChartLib/src/com/github/mikephil/charting/data/DataSet.java index 32604bfb33..69d1bae85d 100644 --- a/MPChartLib/src/com/github/mikephil/charting/data/DataSet.java +++ b/MPChartLib/src/com/github/mikephil/charting/data/DataSet.java @@ -54,9 +54,9 @@ public DataSet(ArrayList yVals, String label) { this.mLabel = label; this.mYVals = yVals; - if (yVals.size() <= 0) { - return; - } +// if (yVals.size() <= 0) { +// return; +// } mColors = new ArrayList(); @@ -108,7 +108,7 @@ private void calcYValueSum() { mYValueSum = 0; for (int i = 0; i < mYVals.size(); i++) { - mYValueSum += Math.abs(mYVals.get(i).getVal()); + mYValueSum += Math.abs(mYVals.get(i).getSum()); } } @@ -279,6 +279,31 @@ public String toSimpleString() { public String getLabel() { return mLabel; } + + /** + * Adds an Entry dynamically. + * + * @param d + */ + public void addEntry(Entry e) { + + float sum = e.getSum(); + + if(mYVals == null || mYVals.size() <= 0) { + + mYVals = new ArrayList(); + mYMax = sum; + mYMin = sum; + } else { + + if(mYMax < sum) mYMax = sum; + if(mYMin > sum) mYMin = sum; + } + + mYVals.add(e); + + mYValueSum += sum; + } /** BELOW THIS COLOR HANDLING */