Skip to content

Commit

Permalink
Improved dynamical data adding.
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilJay committed Sep 30, 2014
1 parent f9d9f89 commit 1c984be
Show file tree
Hide file tree
Showing 22 changed files with 380 additions and 149 deletions.
2 changes: 1 addition & 1 deletion MPChartExample/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxmassdeveloper.mpchartexample"
android:versionCode="21"
android:versionCode="22"
android:versionName="1.6.2" >

<uses-sdk
Expand Down
2 changes: 1 addition & 1 deletion MPChartExample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ android {
applicationId 'com.xxmassdeveloper.mpchartexample'
minSdkVersion 16
targetSdkVersion 19
versionCode 21
versionCode 22
versionName '1.6.2'

sourceSets {
Expand Down
Binary file not shown.
9 changes: 9 additions & 0 deletions MPChartExample/res/menu/dynamical.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,13 @@
android:title="Remove DataSet">
</item>

<item
android:id="@+id/actionAddEmptyLineData"
android:title="Add empty LineData">
</item>

<item
android:id="@+id/actionClear"
android:title="Clear chart">
</item>
</menu>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
Expand Down Expand Up @@ -34,107 +35,115 @@ protected void onCreate(Bundle savedInstanceState) {
mChart.setDrawYValues(false);
mChart.setDrawGridBackground(false);
mChart.setDescription("");

addEmptyData();

// create 30 x-vals
String[] xVals = new String[30];

for (int i = 0; i < 30; i++)
xVals[i] = "" + i;

// create 10 y-vals
ArrayList<Entry> yVals = new ArrayList<Entry>();

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

LineDataSet set = new LineDataSet(yVals, "DataSet 1");
set.setLineWidth(2.5f);
set.setCircleSize(4.5f);
set.setColor(Color.rgb(240, 99, 99));
set.setCircleColor(Color.rgb(240, 99, 99));
set.setHighLightColor(Color.rgb(190, 190, 190));

LineData data = new LineData(xVals, set);

mChart.setData(data);
mChart.invalidate();
}
int[] mColors = ColorTemplate.VORDIPLOM_COLORS;

int[] mColors = ColorTemplate.VORDIPLOM_COLORS;

private void addEntry() {

LineData data = mChart.getDataOriginal();

LineDataSet set = data.getDataSetByIndex(0);
// set.addEntry(...);
LineData data = mChart.getDataOriginal();

if(set != null) {

if(data != null) {

LineDataSet set = data.getDataSetByIndex(0);
// set.addEntry(...);

if (set == null) {
set = createSet();
data.addDataSet(set);
}

data.addEntry(new Entry((float) (Math.random() * 50) + 50f, set.getEntryCount()), 0);

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

// redraw the chart
mChart.invalidate();
mChart.invalidate();
}
}

private void removeLastEntry() {

LineData data = mChart.getDataOriginal();

LineDataSet set = data.getDataSetByIndex(0);
LineData data = mChart.getDataOriginal();

if(set != null) {
if(data != null) {

LineDataSet set = data.getDataSetByIndex(0);

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

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

mChart.notifyDataSetChanged();
mChart.invalidate();
data.removeEntry(e, 0);
// or remove by index
// mData.removeEntry(xIndex, dataSetIndex);

mChart.notifyDataSetChanged();
mChart.invalidate();
}
}
}

private void addDataSet() {

LineData data = mChart.getDataOriginal();

int count = (data.getDataSetCount() + 1);
if(data != null) {

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

for (int i = 0; i < data.getXValCount(); i++)
yVals.add(new Entry((float) (Math.random() * 50f) + 50f * count, i));

// create 10 y-vals
ArrayList<Entry> yVals = new ArrayList<Entry>();

LineDataSet set = new LineDataSet(yVals, "DataSet " + count);
set.setLineWidth(2.5f);
set.setCircleSize(4.5f);

int color = mColors[count % mColors.length];

set.setColor(color);
set.setCircleColor(color);
set.setHighLightColor(color);
for (int i = 0; i < data.getXValCount(); i++)
yVals.add(new Entry((float) (Math.random() * 50f) + 50f * count, i));

data.addDataSet(set);
mChart.notifyDataSetChanged();
mChart.invalidate();
LineDataSet set = new LineDataSet(yVals, "DataSet " + count);
set.setLineWidth(2.5f);
set.setCircleSize(4.5f);

int color = mColors[count % mColors.length];

set.setColor(color);
set.setCircleColor(color);
set.setHighLightColor(color);

data.addDataSet(set);
mChart.notifyDataSetChanged();
mChart.invalidate();
}
}

private void removeDataSet() {

LineData data = mChart.getDataOriginal();

if(data != null) {

data.removeDataSet(data.getDataSetByIndex(data.getDataSetCount() - 1));
data.removeDataSet(data.getDataSetByIndex(data.getDataSetCount() - 1));

mChart.notifyDataSetChanged();
mChart.invalidate();
}
}

private void addEmptyData() {

mChart.notifyDataSetChanged();
// create 30 x-vals
String[] xVals = new String[30];

for (int i = 0; i < 30; i++)
xVals[i] = "" + i;

// create a chartdata object that contains only the x-axis labels (no entries or datasets)
LineData data = new LineData(xVals);

mChart.setData(data);
mChart.invalidate();
}

Expand Down Expand Up @@ -174,8 +183,28 @@ public boolean onOptionsItemSelected(MenuItem item) {
removeDataSet();
Toast.makeText(this, "DataSet removed!", Toast.LENGTH_SHORT).show();
break;
case R.id.actionAddEmptyLineData:
addEmptyData();
Toast.makeText(this, "Empty data added!", Toast.LENGTH_SHORT).show();
break;
case R.id.actionClear:
mChart.clear();
Toast.makeText(this, "Chart cleared!", Toast.LENGTH_SHORT).show();
break;
}

return true;
}

private LineDataSet createSet() {

LineDataSet set = new LineDataSet(null, "DataSet 1");
set.setLineWidth(2.5f);
set.setCircleSize(4.5f);
set.setColor(Color.rgb(240, 99, 99));
set.setCircleColor(Color.rgb(240, 99, 99));
set.setHighLightColor(Color.rgb(190, 190, 190));

return set;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,17 @@ protected void onCreate(Bundle savedInstanceState) {
// add a selection listener
mChart.setOnChartValueSelectedListener(this);
// mChart.setTouchEnabled(false);

mSeekBarX.setProgress(3);
mSeekBarY.setProgress(100);


setData(3, 100);

mChart.animateXY(1500, 1500);
// mChart.spin(2000, 0, 360);

Legend l = mChart.getLegend();
l.setPosition(LegendPosition.RIGHT_OF_CHART);
l.setXEntrySpace(7f);
l.setYEntrySpace(5f);

}

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

float mult = (float) mSeekBarY.getProgress();
setData(mSeekBarX.getProgress(), mSeekBarY.getProgress());
}

private void setData(int count, float range) {

float mult = range;

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

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

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

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

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

PieDataSet set1 = new PieDataSet(yVals1, "Election Results");
Expand Down
Loading

0 comments on commit 1c984be

Please sign in to comment.