Skip to content

Commit b204016

Browse files
committed
Added colored LineChart example.
1 parent 4328804 commit b204016

3 files changed

Lines changed: 89 additions & 230 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:background="@android:color/white"
6+
android:orientation="vertical" >
7+
8+
<com.github.mikephil.charting.charts.LineChart
9+
android:id="@+id/chart1"
10+
android:layout_weight="1"
11+
android:layout_width="match_parent"
12+
android:layout_height="match_parent"/>
13+
<com.github.mikephil.charting.charts.LineChart
14+
android:id="@+id/chart2"
15+
android:layout_weight="1"
16+
android:layout_width="match_parent"
17+
android:layout_height="match_parent"/>
18+
<com.github.mikephil.charting.charts.LineChart
19+
android:id="@+id/chart3"
20+
android:layout_weight="1"
21+
android:layout_width="match_parent"
22+
android:layout_height="match_parent"/>
23+
<com.github.mikephil.charting.charts.LineChart
24+
android:id="@+id/chart4"
25+
android:layout_weight="1"
26+
android:layout_width="match_parent"
27+
android:layout_height="match_parent"/>
28+
</LinearLayout>

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

Lines changed: 61 additions & 230 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@
77
import android.view.Menu;
88
import android.view.MenuItem;
99
import android.view.WindowManager;
10-
import android.widget.SeekBar;
11-
import android.widget.SeekBar.OnSeekBarChangeListener;
12-
import android.widget.TextView;
1310
import android.widget.Toast;
1411

15-
import com.github.mikephil.charting.charts.BarLineChartBase.BorderPosition;
1612
import com.github.mikephil.charting.charts.LineChart;
1713
import com.github.mikephil.charting.data.Entry;
1814
import com.github.mikephil.charting.data.LineData;
@@ -27,253 +23,102 @@
2723

2824
import java.util.ArrayList;
2925

30-
public class LineChartActivityColored extends DemoBase implements OnSeekBarChangeListener {
26+
public class LineChartActivityColored extends DemoBase {
3127

32-
private LineChart mChart;
33-
private SeekBar mSeekBarX, mSeekBarY;
34-
private TextView tvX, tvY;
35-
private Typeface tf;
28+
private LineChart[] mCharts = new LineChart[4];
29+
private Typeface mTf;
3630

3731
@Override
3832
protected void onCreate(Bundle savedInstanceState) {
3933
super.onCreate(savedInstanceState);
4034
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
4135
WindowManager.LayoutParams.FLAG_FULLSCREEN);
42-
setContentView(R.layout.activity_linechart);
36+
setContentView(R.layout.activity_colored_lines);
4337

44-
tvX = (TextView) findViewById(R.id.tvXMax);
45-
tvY = (TextView) findViewById(R.id.tvYMax);
38+
mCharts[0] = (LineChart) findViewById(R.id.chart1);
39+
mCharts[1] = (LineChart) findViewById(R.id.chart2);
40+
mCharts[2] = (LineChart) findViewById(R.id.chart3);
41+
mCharts[3] = (LineChart) findViewById(R.id.chart4);
4642

47-
mSeekBarX = (SeekBar) findViewById(R.id.seekBar1);
48-
mSeekBarY = (SeekBar) findViewById(R.id.seekBar2);
43+
mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Bold.ttf");
4944

50-
mSeekBarX.setProgress(45);
51-
mSeekBarY.setProgress(100);
45+
LineData data = getData(36, 100);
5246

53-
mSeekBarY.setOnSeekBarChangeListener(this);
54-
mSeekBarX.setOnSeekBarChangeListener(this);
47+
for (int i = 0; i < mCharts.length; i++)
48+
// add some transparency to the color with "& 0x90FFFFFF"
49+
setupChart(mCharts[i], data, mColors[i % mColors.length] & 0x90FFFFFF);
50+
}
51+
52+
private int[] mColors = new int[] {
53+
Color.rgb(137, 230, 81),
54+
Color.rgb(240, 240, 30),
55+
Color.rgb(89, 199, 250),
56+
Color.rgb(250, 104, 104)
57+
};
5558

56-
mChart = (LineChart) findViewById(R.id.chart1);
59+
private void setupChart(LineChart chart, LineData data, int color) {
5760

5861
// if enabled, the chart will always start at zero on the y-axis
59-
mChart.setStartAtZero(true);
62+
chart.setStartAtZero(true);
6063

6164
// disable the drawing of values into the chart
62-
mChart.setDrawYValues(false);
65+
chart.setDrawYValues(false);
6366

64-
mChart.setDrawBorder(false);
67+
chart.setDrawBorder(false);
6568

6669
// no description text
67-
mChart.setDescription("");
68-
mChart.setNoDataTextDescription("You need to provide data for the chart.");
70+
chart.setDescription("");
71+
chart.setNoDataTextDescription("You need to provide data for the chart.");
6972

70-
// enable / disable grid lines
71-
mChart.setDrawVerticalGrid(false);
73+
// enable / disable grid lines
74+
chart.setDrawVerticalGrid(false);
7275
// mChart.setDrawHorizontalGrid(false);
7376
//
7477
// enable / disable grid background
75-
mChart.setDrawGridBackground(false);
76-
mChart.setGridColor(Color.WHITE & 0x70FFFFFF);
77-
mChart.setGridWidth(1f);
78+
chart.setDrawGridBackground(false);
79+
chart.setGridColor(Color.WHITE & 0x70FFFFFF);
80+
chart.setGridWidth(1.25f);
7881

7982
// enable touch gestures
80-
mChart.setTouchEnabled(true);
83+
chart.setTouchEnabled(true);
8184

8285
// enable scaling and dragging
83-
mChart.setDragScaleEnabled(true);
86+
chart.setDragScaleEnabled(true);
8487

8588
// if disabled, scaling can be done on x- and y-axis separately
86-
mChart.setPinchZoom(true);
89+
chart.setPinchZoom(false);
90+
91+
chart.setBackgroundColor(color);
8792

88-
mChart.setBackgroundColor(Color.rgb(137, 230, 81));
89-
90-
tf = Typeface.createFromAsset(getAssets(), "OpenSans-Bold.ttf");
91-
92-
mChart.setValueTypeface(tf);
93+
chart.setValueTypeface(mTf);
9394

9495
// add data
95-
setData(45, 100);
96+
chart.setData(data);
9697

97-
mChart.animateX(2500);
98+
// get the legend (only possible after setting data)
99+
Legend l = chart.getLegend();
98100

99-
// // restrain the maximum scale-out factor
100-
// mChart.setScaleMinima(3f, 3f);
101-
//
102-
// // center the view to a specific position inside the chart
103-
// mChart.centerViewPort(10, 50);
101+
// modify the legend ...
102+
// l.setPosition(LegendPosition.LEFT_OF_CHART);
103+
l.setForm(LegendForm.CIRCLE);
104+
l.setFormSize(6f);
105+
l.setTextColor(Color.WHITE);
106+
l.setTypeface(mTf);
104107

105-
YLabels y = mChart.getYLabels();
108+
YLabels y = chart.getYLabels();
106109
y.setTextColor(Color.WHITE);
107-
y.setTypeface(tf);
110+
y.setTypeface(mTf);
111+
y.setLabelCount(4);
108112

109-
XLabels x = mChart.getXLabels();
113+
XLabels x = chart.getXLabels();
110114
x.setTextColor(Color.WHITE);
111-
x.setTypeface(tf);
112-
113-
// dont forget to refresh the drawing
114-
mChart.invalidate();
115-
}
116-
117-
@Override
118-
public boolean onCreateOptionsMenu(Menu menu) {
119-
getMenuInflater().inflate(R.menu.line, menu);
120-
return true;
121-
}
122-
123-
@Override
124-
public boolean onOptionsItemSelected(MenuItem item) {
125-
126-
switch (item.getItemId()) {
127-
case R.id.actionToggleValues: {
128-
if (mChart.isDrawYValuesEnabled())
129-
mChart.setDrawYValues(false);
130-
else
131-
mChart.setDrawYValues(true);
132-
mChart.invalidate();
133-
break;
134-
}
135-
case R.id.actionToggleHighlight: {
136-
if (mChart.isHighlightEnabled())
137-
mChart.setHighlightEnabled(false);
138-
else
139-
mChart.setHighlightEnabled(true);
140-
mChart.invalidate();
141-
break;
142-
}
143-
case R.id.actionToggleFilled: {
144-
145-
ArrayList<LineDataSet> sets = (ArrayList<LineDataSet>) mChart.getDataCurrent()
146-
.getDataSets();
147-
148-
for (LineDataSet set : sets) {
149-
if (set.isDrawFilledEnabled())
150-
set.setDrawFilled(false);
151-
else
152-
set.setDrawFilled(true);
153-
}
154-
mChart.invalidate();
155-
break;
156-
}
157-
case R.id.actionToggleCircles: {
158-
ArrayList<LineDataSet> sets = (ArrayList<LineDataSet>) mChart.getDataCurrent()
159-
.getDataSets();
160-
161-
for (LineDataSet set : sets) {
162-
if (set.isDrawCirclesEnabled())
163-
set.setDrawCircles(false);
164-
else
165-
set.setDrawCircles(true);
166-
}
167-
mChart.invalidate();
168-
break;
169-
}
170-
case R.id.actionToggleCubic: {
171-
ArrayList<LineDataSet> sets = (ArrayList<LineDataSet>) mChart.getDataCurrent()
172-
.getDataSets();
173-
174-
for (LineDataSet set : sets) {
175-
if (set.isDrawCubicEnabled())
176-
set.setDrawCubic(false);
177-
else
178-
set.setDrawCubic(true);
179-
}
180-
mChart.invalidate();
181-
break;
182-
}
183-
case R.id.actionToggleStartzero: {
184-
if (mChart.isStartAtZeroEnabled())
185-
mChart.setStartAtZero(false);
186-
else
187-
mChart.setStartAtZero(true);
188-
189-
mChart.invalidate();
190-
break;
191-
}
192-
case R.id.actionTogglePinch: {
193-
if (mChart.isPinchZoomEnabled())
194-
mChart.setPinchZoom(false);
195-
else
196-
mChart.setPinchZoom(true);
197-
198-
mChart.invalidate();
199-
break;
200-
}
201-
case R.id.animateX: {
202-
mChart.animateX(3000);
203-
break;
204-
}
205-
case R.id.animateY: {
206-
mChart.animateY(3000);
207-
break;
208-
}
209-
case R.id.animateXY: {
210-
mChart.animateXY(3000, 3000);
211-
break;
212-
}
213-
case R.id.actionToggleAdjustXLegend: {
214-
XLabels xLabels = mChart.getXLabels();
215-
216-
if (xLabels.isAdjustXLabelsEnabled())
217-
xLabels.setAdjustXLabels(false);
218-
else
219-
xLabels.setAdjustXLabels(true);
220-
221-
mChart.invalidate();
222-
break;
223-
}
224-
case R.id.actionToggleFilter: {
225-
226-
// the angle of filtering is 35°
227-
Approximator a = new Approximator(ApproximatorType.DOUGLAS_PEUCKER, 35);
228-
229-
if (!mChart.isFilteringEnabled()) {
230-
mChart.enableFiltering(a);
231-
} else {
232-
mChart.disableFiltering();
233-
}
234-
mChart.invalidate();
235-
break;
236-
}
237-
case R.id.actionSave: {
238-
if (mChart.saveToPath("title" + System.currentTimeMillis(), "")) {
239-
Toast.makeText(getApplicationContext(), "Saving SUCCESSFUL!",
240-
Toast.LENGTH_SHORT).show();
241-
} else
242-
Toast.makeText(getApplicationContext(), "Saving FAILED!", Toast.LENGTH_SHORT)
243-
.show();
244-
245-
// mChart.saveToGallery("title"+System.currentTimeMillis())
246-
break;
247-
}
248-
}
249-
return true;
250-
}
251-
252-
@Override
253-
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
254-
255-
tvX.setText("" + (mSeekBarX.getProgress() + 1));
256-
tvY.setText("" + (mSeekBarY.getProgress()));
257-
258-
setData(mSeekBarX.getProgress() + 1, mSeekBarY.getProgress());
259-
260-
// redraw
261-
mChart.invalidate();
262-
}
263-
264-
@Override
265-
public void onStartTrackingTouch(SeekBar seekBar) {
266-
// TODO Auto-generated method stub
115+
x.setTypeface(mTf);
267116

117+
// animate calls invalidate()...
118+
chart.animateX(2500);
268119
}
269-
270-
@Override
271-
public void onStopTrackingTouch(SeekBar seekBar) {
272-
// TODO Auto-generated method stub
273-
274-
}
275-
276-
private void setData(int count, float range) {
120+
121+
private LineData getData(int count, float range) {
277122

278123
ArrayList<String> xVals = new ArrayList<String>();
279124
for (int i = 0; i < count; i++) {
@@ -283,20 +128,17 @@ private void setData(int count, float range) {
283128
ArrayList<Entry> yVals = new ArrayList<Entry>();
284129

285130
for (int i = 0; i < count; i++) {
286-
float mult = (range + 1);
287-
float val = (float) (Math.random() * mult) + 3;// + (float)
288-
// ((mult *
289-
// 0.1) / 10);
131+
float val = (float) (Math.random() * range) + 3;
290132
yVals.add(new Entry(val, i));
291133
}
292134

293135
// create a dataset and give it a type
294136
LineDataSet set1 = new LineDataSet(yVals, "DataSet 1");
295137
// set1.setFillAlpha(110);
296138
// set1.setFillColor(Color.RED);
297-
298-
set1.setLineWidth(2f);
299-
set1.setCircleSize(3.5f);
139+
140+
set1.setLineWidth(1.75f);
141+
set1.setCircleSize(3f);
300142
set1.setColor(Color.WHITE);
301143
set1.setCircleColor(Color.WHITE);
302144
set1.setHighLightColor(Color.WHITE);
@@ -307,17 +149,6 @@ private void setData(int count, float range) {
307149
// create a data object with the datasets
308150
LineData data = new LineData(xVals, dataSets);
309151

310-
// set data
311-
mChart.setData(data);
312-
313-
// get the legend (only possible after setting data)
314-
Legend l = mChart.getLegend();
315-
316-
// modify the legend ...
317-
// l.setPosition(LegendPosition.LEFT_OF_CHART);
318-
l.setForm(LegendForm.CIRCLE);
319-
l.setFormSize(10f);
320-
l.setTextColor(Color.WHITE);
321-
l.setTypeface(tf);
152+
return data;
322153
}
323154
}

screenshots/linechart_colored.png

80.5 KB
Loading

0 commit comments

Comments
 (0)