Skip to content

Commit fb11b6e

Browse files
committed
Added new realtime-chart example.
1 parent 5bd73bc commit fb11b6e

9 files changed

Lines changed: 258 additions & 39 deletions

File tree

MPChartExample/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<activity android:name="RadarChartActivitry"></activity>
4545
<activity android:name="LineChartActivityColored"></activity>
4646
<activity android:name="DynamicalAddingActivity"></activity>
47+
<activity android:name="RealtimeLineChartActivity"></activity>
4748
</application>
4849

4950
</manifest>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent">
5+
6+
<com.github.mikephil.charting.charts.LineChart
7+
android:id="@+id/chart1"
8+
android:layout_width="match_parent"
9+
android:layout_height="match_parent"/>
10+
11+
</RelativeLayout>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
3+
4+
<item
5+
android:id="@+id/actionAdd"
6+
android:title="Feed new Entry">
7+
</item>
8+
</menu>

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.github.mikephil.charting.components.Legend.LegendForm;
2020
import com.github.mikephil.charting.components.LimitLine;
2121
import com.github.mikephil.charting.components.LimitLine.LimitLabelPosition;
22-
import com.github.mikephil.charting.components.YAxis.AxisDependency;
2322
import com.github.mikephil.charting.components.XAxis;
2423
import com.github.mikephil.charting.data.DataSet;
2524
import com.github.mikephil.charting.data.Entry;
@@ -73,16 +72,6 @@ protected void onCreate(Bundle savedInstanceState) {
7372
mChart.setDescription("");
7473
mChart.setNoDataTextDescription("You need to provide data for the chart.");
7574

76-
// // enable / disable grid lines
77-
// mChart.setDrawVerticalGrid(false);
78-
// mChart.setDrawHorizontalGrid(false);
79-
//
80-
// // enable / disable grid background
81-
// mChart.setDrawGridBackground(false);
82-
//
83-
// mChart.setDrawXLegend(false);
84-
// mChart.setDrawYLegend(false);
85-
8675
// enable value highlighting
8776
mChart.setHighlightEnabled(true);
8877

@@ -109,10 +98,11 @@ protected void onCreate(Bundle savedInstanceState) {
10998
// enable/disable highlight indicators (the lines that indicate the
11099
// highlighted Entry)
111100
mChart.setHighlightIndicatorEnabled(false);
112-
101+
113102
// add data
114103
setData(45, 100);
115104
mChart.animateX(2500);
105+
// mChart.setVisibleYRange(30, AxisDependency.LEFT);
116106

117107
// // restrain the maximum scale-out factor
118108
// mChart.setScaleMinima(3f, 3f);
@@ -282,7 +272,6 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
282272
tvY.setText("" + (mSeekBarY.getProgress()));
283273

284274
setData(mSeekBarX.getProgress() + 1, mSeekBarY.getProgress());
285-
286275
// redraw
287276
mChart.invalidate();
288277
}
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
2+
package com.xxmassdeveloper.mpchartexample;
3+
4+
import android.graphics.Color;
5+
import android.graphics.Typeface;
6+
import android.os.Bundle;
7+
import android.util.Log;
8+
import android.view.Menu;
9+
import android.view.MenuItem;
10+
import android.view.WindowManager;
11+
12+
import com.github.mikephil.charting.charts.BarLineChartBase.BorderPosition;
13+
import com.github.mikephil.charting.charts.LineChart;
14+
import com.github.mikephil.charting.components.Legend;
15+
import com.github.mikephil.charting.components.Legend.LegendForm;
16+
import com.github.mikephil.charting.components.XAxis;
17+
import com.github.mikephil.charting.components.YAxis;
18+
import com.github.mikephil.charting.components.YAxis.AxisDependency;
19+
import com.github.mikephil.charting.data.Entry;
20+
import com.github.mikephil.charting.data.LineData;
21+
import com.github.mikephil.charting.data.LineDataSet;
22+
import com.github.mikephil.charting.interfaces.OnChartValueSelectedListener;
23+
import com.github.mikephil.charting.utils.ColorTemplate;
24+
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
25+
26+
public class RealtimeLineChartActivity extends DemoBase implements
27+
OnChartValueSelectedListener {
28+
29+
private LineChart mChart;
30+
31+
@Override
32+
protected void onCreate(Bundle savedInstanceState) {
33+
super.onCreate(savedInstanceState);
34+
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
35+
WindowManager.LayoutParams.FLAG_FULLSCREEN);
36+
setContentView(R.layout.activity_realtime_linechart);
37+
38+
mChart = (LineChart) findViewById(R.id.chart1);
39+
mChart.setOnChartValueSelectedListener(this);
40+
mChart.setValueTextColor(Color.WHITE);
41+
42+
mChart.setDrawBorder(true);
43+
mChart.setBorderPositions(new BorderPosition[] {
44+
BorderPosition.BOTTOM
45+
});
46+
47+
// no description text
48+
mChart.setDescription("");
49+
mChart.setNoDataTextDescription("You need to provide data for the chart.");
50+
51+
// enable value highlighting
52+
mChart.setHighlightEnabled(true);
53+
54+
// enable touch gestures
55+
mChart.setTouchEnabled(true);
56+
57+
// enable scaling and dragging
58+
mChart.setDragEnabled(true);
59+
mChart.setScaleEnabled(true);
60+
mChart.setDrawGridBackground(false);
61+
62+
// if disabled, scaling can be done on x- and y-axis separately
63+
mChart.setPinchZoom(true);
64+
65+
// set an alternative background color
66+
mChart.setBackgroundColor(Color.LTGRAY);
67+
68+
// add empty data
69+
mChart.setData(new LineData());
70+
71+
Typeface tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");
72+
73+
// get the legend (only possible after setting data)
74+
Legend l = mChart.getLegend();
75+
76+
// modify the legend ...
77+
// l.setPosition(LegendPosition.LEFT_OF_CHART);
78+
l.setForm(LegendForm.LINE);
79+
l.setTypeface(tf);
80+
l.setTextColor(Color.WHITE);
81+
82+
XAxis xl = mChart.getXAxis();
83+
xl.setTypeface(tf);
84+
xl.setTextColor(Color.WHITE);
85+
xl.setDrawGridLines(false);
86+
xl.setAvoidFirstLastClipping(true);
87+
88+
YAxis leftAxis = mChart.getAxisLeft();
89+
leftAxis.setTypeface(tf);
90+
leftAxis.setTextColor(Color.WHITE);
91+
leftAxis.setAxisMaxValue(120f);
92+
leftAxis.setDrawGridLines(true);
93+
94+
YAxis rightAxis = mChart.getAxisRight();
95+
rightAxis.setEnabled(false);
96+
}
97+
98+
@Override
99+
public boolean onCreateOptionsMenu(Menu menu) {
100+
getMenuInflater().inflate(R.menu.realtime, menu);
101+
return true;
102+
}
103+
104+
@Override
105+
public boolean onOptionsItemSelected(MenuItem item) {
106+
107+
switch (item.getItemId()) {
108+
case R.id.actionAdd: {
109+
addEntry();
110+
break;
111+
}
112+
}
113+
return true;
114+
}
115+
116+
private int year = 15;
117+
118+
private void addEntry() {
119+
120+
LineData data = mChart.getData();
121+
122+
if (data != null) {
123+
124+
LineDataSet set = data.getDataSetByIndex(0);
125+
// set.addEntry(...); // can be called as well
126+
127+
if (set == null) {
128+
set = createSet();
129+
data.addDataSet(set);
130+
}
131+
132+
// add a new x-value first
133+
data.addXValue(mMonths[data.getXValCount() % 12] + " " + (year + data.getXValCount() / 12));
134+
data.addEntry(new Entry((float) (Math.random() * 40) + 40f, set.getEntryCount()), 0);
135+
136+
// let the chart know it's data has changed
137+
mChart.notifyDataSetChanged();
138+
139+
// limit the number of visible entries
140+
mChart.setVisibleXRange(6);
141+
// mChart.setVisibleYRange(30, AxisDependency.LEFT);
142+
143+
// move to the latest entry
144+
mChart.moveViewToX(data.getXValCount()-7);
145+
146+
// this automatically refreshes the chart (calls invalidate())
147+
// mChart.moveViewTo(data.getXValCount()-7, 55f, AxisDependency.LEFT);
148+
149+
// redraw the chart
150+
// mChart.invalidate();
151+
}
152+
}
153+
154+
private LineDataSet createSet() {
155+
156+
LineDataSet set = new LineDataSet(null, "Dynamic Data");
157+
set.setAxisDependency(AxisDependency.LEFT);
158+
set.setColor(ColorTemplate.getHoloBlue());
159+
set.setCircleColor(ColorTemplate.getHoloBlue());
160+
set.setLineWidth(2f);
161+
set.setCircleSize(4f);
162+
set.setFillAlpha(65);
163+
set.setFillColor(ColorTemplate.getHoloBlue());
164+
set.setHighLightColor(Color.rgb(244, 117, 117));
165+
return set;
166+
}
167+
168+
@Override
169+
public void onValueSelected(Entry e, int dataSetIndex) {
170+
Log.i("Entry selected", e.toString());
171+
}
172+
173+
@Override
174+
public void onNothingSelected() {
175+
Log.i("Nothing selected", "Nothing selected.");
176+
}
177+
}

MPChartExample/src/com/xxmassdeveloper/mpchartexample/notimportant/MainActivity.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.xxmassdeveloper.mpchartexample.PieChartActivity;
3838
import com.xxmassdeveloper.mpchartexample.R;
3939
import com.xxmassdeveloper.mpchartexample.RadarChartActivitry;
40+
import com.xxmassdeveloper.mpchartexample.RealtimeLineChartActivity;
4041
import com.xxmassdeveloper.mpchartexample.ScatterChartActivity;
4142
import com.xxmassdeveloper.mpchartexample.StackedBarActivity;
4243
import com.xxmassdeveloper.mpchartexample.fragments.SimpleChartDemo;
@@ -98,6 +99,9 @@ protected void onCreate(Bundle savedInstanceState) {
9899
objects.add(new ContentItem(
99100
"Colored Line Chart",
100101
"Shows a LineChart with different background and line color."));
102+
objects.add(new ContentItem(
103+
"Realtime Chart",
104+
"This chart is fed with new data in realtime."));
101105
objects.add(new ContentItem(
102106
"Dynamical data adding",
103107
"This Activity demonstrates dynamical adding of Entries and DataSets (real time graph)."));
@@ -198,7 +202,11 @@ public void onItemClick(AdapterView<?> av, View v, int pos, long arg3) {
198202
i = new Intent(this, LineChartActivityColored.class);
199203
startActivity(i);
200204
break;
201-
case 19:
205+
case 19 :
206+
i = new Intent(this, RealtimeLineChartActivity.class);
207+
startActivity(i);
208+
break;
209+
case 20:
202210
i = new Intent(this, DynamicalAddingActivity.class);
203211
startActivity(i);
204212
break;

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public abstract class ChartData<T extends DataSet<? extends Entry>> {
4747

4848
/** array that holds all DataSets the ChartData object represents */
4949
protected ArrayList<T> mDataSets;
50-
50+
5151
public ChartData() {
5252
mXVals = new ArrayList<String>();
5353
mDataSets = new ArrayList<T>();
@@ -248,13 +248,7 @@ protected void calcMinMax(ArrayList<T> dataSets) {
248248
}
249249

250250
// in case there is only one axis, adjust the second axis
251-
if (firstLeft == null) {
252-
mLeftAxisMax = mRightAxisMax;
253-
mLeftAxisMin = mRightAxisMin;
254-
} else if (firstRight == null) {
255-
mRightAxisMax = mLeftAxisMax;
256-
mRightAxisMin = mLeftAxisMin;
257-
}
251+
handleEmptyAxis(firstLeft, firstRight);
258252
}
259253
}
260254

@@ -571,6 +565,26 @@ public void addDataSet(T d) {
571565
if (mRightAxisMin > d.getYMin())
572566
mRightAxisMin = d.getYMin();
573567
}
568+
569+
handleEmptyAxis(getFirstLeft(), getFirstRight());
570+
}
571+
572+
/**
573+
* This adjusts the other axis if one axis is empty and the other is not.
574+
*
575+
* @param firstLeft
576+
* @param firstRight
577+
*/
578+
private void handleEmptyAxis(T firstLeft, T firstRight) {
579+
580+
// in case there is only one axis, adjust the second axis
581+
if (firstLeft == null) {
582+
mLeftAxisMax = mRightAxisMax;
583+
mLeftAxisMin = mRightAxisMin;
584+
} else if (firstRight == null) {
585+
mRightAxisMax = mLeftAxisMax;
586+
mRightAxisMin = mLeftAxisMin;
587+
}
574588
}
575589

576590
/**
@@ -659,6 +673,8 @@ public void addEntry(Entry e, int dataSetIndex) {
659673
mRightAxisMin = e.getVal();
660674
}
661675

676+
handleEmptyAxis(getFirstLeft(), getFirstRight());
677+
662678
// add the entry to the dataset
663679
set.addEntry(e);
664680
}

MPChartLib/src/com/github/mikephil/charting/renderer/ViewPortHandler.java

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -205,23 +205,33 @@ public Matrix fitScreen() {
205205
*/
206206
public synchronized void centerViewPort(final float[] transformedPts, final ChartInterface chart) {
207207

208-
final View v = chart.getChartView();
209-
210-
v.post(new Runnable() {
211-
212-
@Override
213-
public void run() {
214-
Matrix save = new Matrix();
215-
save.set(mMatrixTouch);
216-
217-
final float x = transformedPts[0] - offsetLeft();
218-
final float y = transformedPts[1] - offsetTop();
208+
Matrix save = new Matrix();
209+
save.set(mMatrixTouch);
210+
211+
final float x = transformedPts[0] - offsetLeft();
212+
final float y = transformedPts[1] - offsetTop();
219213

220-
save.postTranslate(-x, -y);
214+
save.postTranslate(-x, -y);
221215

222-
refresh(save, chart, false);
223-
}
224-
});
216+
refresh(save, chart, false);
217+
218+
// final View v = chart.getChartView();
219+
//
220+
// v.post(new Runnable() {
221+
//
222+
// @Override
223+
// public void run() {
224+
// Matrix save = new Matrix();
225+
// save.set(mMatrixTouch);
226+
//
227+
// final float x = transformedPts[0] - offsetLeft();
228+
// final float y = transformedPts[1] - offsetTop();
229+
//
230+
// save.postTranslate(-x, -y);
231+
//
232+
// refresh(save, chart, false);
233+
// }
234+
// });
225235
}
226236

227237
/**

0 commit comments

Comments
 (0)