Skip to content

Commit f36ba8d

Browse files
committed
Made changes to the data model (especially Entry and DataSet class) to support CandleStickChart.
1 parent f8d34e2 commit f36ba8d

24 files changed

Lines changed: 662 additions & 229 deletions

‎MPChartExample/AndroidManifest.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<activity android:name="StackedBarActivity"></activity>
3838
<activity android:name="AnotherBarActivity"></activity>
3939
<activity android:name="InvertedLineChartActivity"></activity>
40+
<activity android:name="CandleStickChartActivity"></activity>
4041
</application>
4142

4243
</manifest>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.CandleStickChart
7+
android:id="@+id/chart1"
8+
android:layout_width="match_parent"
9+
android:layout_height="match_parent"
10+
android:layout_above="@+id/seekBar1" />
11+
12+
<SeekBar
13+
android:id="@+id/seekBar2"
14+
android:layout_width="match_parent"
15+
android:layout_height="wrap_content"
16+
android:layout_alignParentBottom="true"
17+
android:layout_alignParentLeft="true"
18+
android:layout_margin="8dp"
19+
android:layout_toLeftOf="@+id/tvYMax"
20+
android:layout_marginRight="5dp"
21+
android:max="200"
22+
android:paddingBottom="12dp" />
23+
24+
<SeekBar
25+
android:id="@+id/seekBar1"
26+
android:layout_width="match_parent"
27+
android:layout_height="wrap_content"
28+
android:layout_above="@+id/seekBar2"
29+
android:layout_margin="8dp"
30+
android:layout_marginBottom="35dp"
31+
android:layout_toLeftOf="@+id/tvXMax"
32+
android:layout_marginRight="5dp"
33+
android:max="500"
34+
android:paddingBottom="12dp" />
35+
36+
<TextView
37+
android:id="@+id/tvXMax"
38+
android:layout_width="50dp"
39+
android:layout_height="wrap_content"
40+
android:layout_alignBottom="@+id/seekBar1"
41+
android:layout_alignParentRight="true"
42+
android:text="500"
43+
android:layout_marginBottom="15dp"
44+
android:layout_marginRight="10dp"
45+
android:gravity="right"
46+
android:textAppearance="?android:attr/textAppearanceMedium" />
47+
48+
<TextView
49+
android:id="@+id/tvYMax"
50+
android:layout_width="50dp"
51+
android:layout_height="wrap_content"
52+
android:layout_alignBottom="@+id/seekBar2"
53+
android:layout_alignParentRight="true"
54+
android:text="500"
55+
android:layout_marginBottom="15dp"
56+
android:layout_marginRight="10dp"
57+
android:gravity="right"
58+
android:textAppearance="?android:attr/textAppearanceMedium" />
59+
60+
</RelativeLayout>

‎MPChartExample/src/com/xxmassdeveloper/mpchartexample/AnotherBarActivity.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import com.github.mikephil.charting.charts.BarChart;
1414
import com.github.mikephil.charting.data.BarData;
1515
import com.github.mikephil.charting.data.BarDataSet;
16-
import com.github.mikephil.charting.data.Entry;
16+
import com.github.mikephil.charting.data.BarEntry;
1717
import com.github.mikephil.charting.data.filter.Approximator;
1818
import com.github.mikephil.charting.data.filter.Approximator.ApproximatorType;
1919
import com.github.mikephil.charting.utils.ColorTemplate;
@@ -209,12 +209,12 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
209209
tvX.setText("" + (mSeekBarX.getProgress() + 1));
210210
tvY.setText("" + (mSeekBarY.getProgress()));
211211

212-
ArrayList<Entry> yVals1 = new ArrayList<Entry>();
212+
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
213213

214214
for (int i = 0; i < mSeekBarX.getProgress()+1; i++) {
215215
float mult = (mSeekBarY.getProgress() + 1);
216216
float val1 = (float) (Math.random() * mult) + mult / 3;
217-
yVals1.add(new Entry((int) val1, i));
217+
yVals1.add(new BarEntry((int) val1, i));
218218
}
219219

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

‎MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivity.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import com.github.mikephil.charting.charts.BarChart;
1515
import com.github.mikephil.charting.data.BarData;
1616
import com.github.mikephil.charting.data.BarDataSet;
17-
import com.github.mikephil.charting.data.Entry;
17+
import com.github.mikephil.charting.data.BarEntry;
1818
import com.github.mikephil.charting.data.filter.Approximator;
1919
import com.github.mikephil.charting.data.filter.Approximator.ApproximatorType;
2020
import com.github.mikephil.charting.utils.Legend;
@@ -239,12 +239,12 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
239239
xVals.add(mMonths[i % 12]);
240240
}
241241

242-
ArrayList<Entry> yVals1 = new ArrayList<Entry>();
242+
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
243243

244244
for (int i = 0; i < mSeekBarX.getProgress()+1; i++) {
245245
float mult = (mSeekBarY.getProgress() + 1);
246246
float val = (float) (Math.random() * mult) + 3;
247-
yVals1.add(new Entry(val, i));
247+
yVals1.add(new BarEntry(val, i));
248248
}
249249

250250
BarDataSet set1 = new BarDataSet(yVals1, "DataSet");

‎MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivityMultiDataset.java‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@
1212
import com.github.mikephil.charting.charts.BarChart;
1313
import com.github.mikephil.charting.data.BarData;
1414
import com.github.mikephil.charting.data.BarDataSet;
15-
import com.github.mikephil.charting.data.Entry;
15+
import com.github.mikephil.charting.data.BarEntry;
1616
import com.github.mikephil.charting.utils.ColorTemplate;
1717
import com.github.mikephil.charting.utils.Legend;
1818
import com.github.mikephil.charting.utils.Legend.LegendPosition;
1919
import com.github.mikephil.charting.utils.XLabels;
20-
import com.github.mikephil.charting.utils.YLabels;
21-
import com.github.mikephil.charting.utils.YLabels.YLabelPosition;
2220
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
2321

2422
import java.util.ArrayList;
@@ -176,23 +174,23 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
176174
xVals.add((i) + "");
177175
}
178176

179-
ArrayList<Entry> yVals1 = new ArrayList<Entry>();
180-
ArrayList<Entry> yVals2 = new ArrayList<Entry>();
181-
ArrayList<Entry> yVals3 = new ArrayList<Entry>();
177+
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
178+
ArrayList<BarEntry> yVals2 = new ArrayList<BarEntry>();
179+
ArrayList<BarEntry> yVals3 = new ArrayList<BarEntry>();
182180

183181
for (int i = 0; i < mSeekBarX.getProgress() / 3; i++) {
184182
float val = (float) (Math.random() * mSeekBarY.getProgress()) + 3;
185-
yVals1.add(new Entry(val, i));
183+
yVals1.add(new BarEntry(val, i));
186184
}
187185

188186
for (int i = mSeekBarX.getProgress() / 3; i < mSeekBarX.getProgress() / 3 * 2; i++) {
189187
float val = (float) (Math.random() * mSeekBarY.getProgress()) + 3;
190-
yVals2.add(new Entry(val, i));
188+
yVals2.add(new BarEntry(val, i));
191189
}
192190

193191
for (int i = mSeekBarX.getProgress() / 3 * 2; i < mSeekBarX.getProgress(); i++) {
194192
float val = (float) (Math.random() * mSeekBarY.getProgress()) + 3;
195-
yVals3.add(new Entry(val, i));
193+
yVals3.add(new BarEntry(val, i));
196194
}
197195

198196
// create 3 datasets with different types
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
2+
package com.xxmassdeveloper.mpchartexample;
3+
4+
import android.os.Bundle;
5+
import android.view.Menu;
6+
import android.view.MenuItem;
7+
import android.view.WindowManager;
8+
import android.widget.SeekBar;
9+
import android.widget.SeekBar.OnSeekBarChangeListener;
10+
import android.widget.TextView;
11+
import android.widget.Toast;
12+
13+
import com.github.mikephil.charting.charts.CandleStickChart;
14+
import com.github.mikephil.charting.data.CandleData;
15+
import com.github.mikephil.charting.data.CandleDataSet;
16+
import com.github.mikephil.charting.data.CandleEntry;
17+
import com.github.mikephil.charting.data.filter.Approximator;
18+
import com.github.mikephil.charting.data.filter.Approximator.ApproximatorType;
19+
import com.github.mikephil.charting.utils.ColorTemplate;
20+
import com.github.mikephil.charting.utils.XLabels;
21+
import com.github.mikephil.charting.utils.XLabels.XLabelPosition;
22+
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
23+
24+
import java.util.ArrayList;
25+
26+
public class CandleStickChartActivity extends DemoBase implements OnSeekBarChangeListener {
27+
28+
private CandleStickChart mChart;
29+
private SeekBar mSeekBarX, mSeekBarY;
30+
private TextView tvX, tvY;
31+
32+
@Override
33+
protected void onCreate(Bundle savedInstanceState) {
34+
super.onCreate(savedInstanceState);
35+
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
36+
WindowManager.LayoutParams.FLAG_FULLSCREEN);
37+
setContentView(R.layout.activity_candlechart);
38+
39+
tvX = (TextView) findViewById(R.id.tvXMax);
40+
tvY = (TextView) findViewById(R.id.tvYMax);
41+
42+
mSeekBarX = (SeekBar) findViewById(R.id.seekBar1);
43+
mSeekBarX.setOnSeekBarChangeListener(this);
44+
45+
mSeekBarY = (SeekBar) findViewById(R.id.seekBar2);
46+
mSeekBarY.setOnSeekBarChangeListener(this);
47+
48+
mChart = (CandleStickChart) findViewById(R.id.chart1);
49+
50+
mChart.setDrawYValues(false);
51+
52+
mChart.setUnit(" €");
53+
mChart.setDescription("");
54+
55+
// if more than 60 entries are displayed in the chart, no values will be
56+
// drawn
57+
mChart.setMaxVisibleValueCount(60);
58+
59+
// scaling can now only be done on x- and y-axis separately
60+
mChart.setPinchZoom(false);
61+
62+
mChart.setDrawVerticalGrid(false);
63+
mChart.setDrawHorizontalGrid(false);
64+
mChart.setDrawGridBackground(false);
65+
66+
XLabels xLabels = mChart.getXLabels();
67+
xLabels.setPosition(XLabelPosition.BOTTOM);
68+
xLabels.setCenterXLabelText(true);
69+
xLabels.setSpaceBetweenLabels(0);
70+
71+
mChart.setDrawYLabels(false);
72+
mChart.setDrawLegend(false);
73+
74+
// setting data
75+
mSeekBarX.setProgress(10);
76+
mSeekBarY.setProgress(100);
77+
78+
// Legend l = mChart.getLegend();
79+
// l.setPosition(LegendPosition.BELOW_CHART_CENTER);
80+
// l.setFormSize(8f);
81+
// l.setFormToTextSpace(4f);
82+
// l.setXEntrySpace(6f);
83+
84+
// mChart.setDrawLegend(false);
85+
}
86+
87+
@Override
88+
public boolean onCreateOptionsMenu(Menu menu) {
89+
getMenuInflater().inflate(R.menu.bar, menu);
90+
return true;
91+
}
92+
93+
@Override
94+
public boolean onOptionsItemSelected(MenuItem item) {
95+
96+
switch (item.getItemId()) {
97+
case R.id.actionToggleValues: {
98+
if (mChart.isDrawYValuesEnabled())
99+
mChart.setDrawYValues(false);
100+
else
101+
mChart.setDrawYValues(true);
102+
mChart.invalidate();
103+
break;
104+
}
105+
case R.id.actionToggleHighlight: {
106+
if (mChart.isHighlightEnabled())
107+
mChart.setHighlightEnabled(false);
108+
else
109+
mChart.setHighlightEnabled(true);
110+
mChart.invalidate();
111+
break;
112+
}
113+
case R.id.actionTogglePinch: {
114+
if (mChart.isPinchZoomEnabled())
115+
mChart.setPinchZoom(false);
116+
else
117+
mChart.setPinchZoom(true);
118+
119+
mChart.invalidate();
120+
break;
121+
}
122+
case R.id.actionToggleStartzero: {
123+
if (mChart.isStartAtZeroEnabled())
124+
mChart.setStartAtZero(false);
125+
else
126+
mChart.setStartAtZero(true);
127+
128+
mChart.invalidate();
129+
break;
130+
}
131+
case R.id.actionToggleAdjustXLegend: {
132+
XLabels xLabels = mChart.getXLabels();
133+
134+
if (xLabels.isAdjustXLabelsEnabled())
135+
xLabels.setAdjustXLabels(false);
136+
else
137+
xLabels.setAdjustXLabels(true);
138+
139+
mChart.invalidate();
140+
break;
141+
}
142+
case R.id.animateX: {
143+
mChart.animateX(3000);
144+
break;
145+
}
146+
case R.id.animateY: {
147+
mChart.animateY(3000);
148+
break;
149+
}
150+
case R.id.animateXY: {
151+
152+
mChart.animateXY(3000, 3000);
153+
break;
154+
}
155+
case R.id.actionToggleFilter: {
156+
157+
Approximator a = new Approximator(ApproximatorType.DOUGLAS_PEUCKER, 25);
158+
159+
if (!mChart.isFilteringEnabled()) {
160+
mChart.enableFiltering(a);
161+
} else {
162+
mChart.disableFiltering();
163+
}
164+
mChart.invalidate();
165+
break;
166+
}
167+
case R.id.actionSave: {
168+
if (mChart.saveToGallery("title" + System.currentTimeMillis(), 50)) {
169+
Toast.makeText(getApplicationContext(), "Saving SUCCESSFUL!",
170+
Toast.LENGTH_SHORT).show();
171+
} else
172+
Toast.makeText(getApplicationContext(), "Saving FAILED!", Toast.LENGTH_SHORT)
173+
.show();
174+
break;
175+
}
176+
}
177+
return true;
178+
}
179+
180+
@Override
181+
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
182+
183+
tvX.setText("" + (mSeekBarX.getProgress() + 1));
184+
tvY.setText("" + (mSeekBarY.getProgress()));
185+
186+
ArrayList<CandleEntry> yVals1 = new ArrayList<CandleEntry>();
187+
188+
for (int i = 0; i < mSeekBarX.getProgress()+1; i++) {
189+
float mult = (mSeekBarY.getProgress() + 1);
190+
float val = (float) (Math.random() * mult) + mult / 3;
191+
yVals1.add(new CandleEntry(val, i, val+5, val-5, val+7, val-7));
192+
}
193+
194+
ArrayList<String> xVals = new ArrayList<String>();
195+
for (int i = 0; i < mSeekBarX.getProgress()+1; i++) {
196+
xVals.add((int) yVals1.get(i).getVal() + " " + mChart.getUnit());
197+
}
198+
199+
CandleDataSet set1 = new CandleDataSet(yVals1, "Data Set");
200+
set1.setColors(ColorTemplate.createColors(getApplicationContext(),
201+
ColorTemplate.VORDIPLOM_COLORS));
202+
203+
CandleData data = new CandleData(xVals, set1);
204+
205+
mChart.setData(data);
206+
mChart.invalidate();
207+
}
208+
209+
@Override
210+
public void onStartTrackingTouch(SeekBar seekBar) {
211+
// TODO Auto-generated method stub
212+
213+
}
214+
215+
@Override
216+
public void onStopTrackingTouch(SeekBar seekBar) {
217+
// TODO Auto-generated method stub
218+
219+
}
220+
}

0 commit comments

Comments
 (0)