Skip to content

Commit

Permalink
Improved animations, bugfixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilJay committed Aug 18, 2014
1 parent 9861812 commit 6bdbae3
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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");

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected void onCreate(Bundle savedInstanceState) {

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

ArrayList<ChartData> list = new ArrayList<ChartData>();
ArrayList<BarData> list = new ArrayList<BarData>();

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

private class ChartDataAdapter extends ArrayAdapter<ChartData> {
private class ChartDataAdapter extends ArrayAdapter<BarData> {

private Typeface mTf;

public ChartDataAdapter(Context context, List<ChartData> objects) {
public ChartDataAdapter(Context context, List<BarData> objects) {
super(context, 0, objects);

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

ChartData c = getItem(position);
BarData c = getItem(position);

ViewHolder holder = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<BarDataSet> sets = new ArrayList<BarDataSet>();

Expand All @@ -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<ScatterDataSet> sets = new ArrayList<ScatterDataSet>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

package com.xxmassdeveloper.mpchartexample.listviewitems;

import android.content.Context;
Expand All @@ -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);

Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

package com.xxmassdeveloper.mpchartexample.listviewitems;

import android.content.Context;
Expand All @@ -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");
}

Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down
34 changes: 33 additions & 1 deletion MPChartLib/src/com/github/mikephil/charting/charts/Chart.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
}

/**
* ################ ################ ################ ################
*/
Expand Down
21 changes: 20 additions & 1 deletion MPChartLib/src/com/github/mikephil/charting/data/ChartData.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private void isLegal(ArrayList<? extends DataSet> 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.");
}
}
}
Expand Down Expand Up @@ -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<DataSet>();
((ArrayList<DataSet>) 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.
Expand Down
Loading

0 comments on commit 6bdbae3

Please sign in to comment.