Skip to content

Commit 213987e

Browse files
committed
Added new chart-type. The ScatterChart.
1 parent b642116 commit 213987e

9 files changed

Lines changed: 562 additions & 2 deletions

File tree

‎MPChartExample/AndroidManifest.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<activity android:name="MultiLineChartActivity"></activity>
3131
<activity android:name="BarChartActivityMultiDataset"></activity>
3232
<activity android:name="DrawChartActivity"></activity>
33+
<activity android:name="ScatterChartActivity"></activity>
3334
</application>
3435

3536
</manifest>

‎MPChartExample/res/layout/activity_main.xml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@
7272
android:layout_height="wrap_content"
7373
android:layout_margin="4dp"
7474
android:text="View on GitHub" />
75+
76+
<Button
77+
android:id="@+id/button9"
78+
android:layout_width="match_parent"
79+
android:layout_height="wrap_content"
80+
android:layout_margin="4dp"
81+
android:text="ScatterChart" />
7582
</LinearLayout>
7683
</ScrollView>
7784

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.ScatterChart
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="2000"
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>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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/actionToggleRound"
6+
android:title="Toggle Round">
7+
</item>
8+
<item
9+
android:id="@+id/actionToggleValues"
10+
android:title="Toggle Values">
11+
</item>
12+
<item
13+
android:id="@+id/actionToggleHighlight"
14+
android:title="Toggle Highlight">
15+
</item>
16+
<item
17+
android:id="@+id/actionToggleStartzero"
18+
android:title="Toggle StartZero">
19+
</item>
20+
<item
21+
android:id="@+id/actionToggleAdjustXLegend"
22+
android:title="Toggle AdjustXLegend">
23+
</item>
24+
<item
25+
android:id="@+id/actionToggleFilter"
26+
android:title="Toggle filter">
27+
</item>
28+
<item
29+
android:id="@+id/actionSave"
30+
android:title="Save to Gallery">
31+
</item>
32+
33+
</menu>

‎MPChartExample/src/com/example/mpchartexample/MainActivity.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ protected void onCreate(Bundle savedInstanceState) {
2525
Button btn6 = (Button) findViewById(R.id.button6);
2626
Button btn7 = (Button) findViewById(R.id.button7);
2727
Button btn8 = (Button) findViewById(R.id.button8);
28+
Button btn9 = (Button) findViewById(R.id.button9);
2829
btn1.setOnClickListener(this);
2930
btn2.setOnClickListener(this);
3031
btn3.setOnClickListener(this);
@@ -33,6 +34,7 @@ protected void onCreate(Bundle savedInstanceState) {
3334
btn6.setOnClickListener(this);
3435
btn7.setOnClickListener(this);
3536
btn8.setOnClickListener(this);
37+
btn9.setOnClickListener(this);
3638
}
3739

3840
@Override
@@ -73,6 +75,10 @@ public void onClick(View v) {
7375
i = new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/PhilJay/MPAndroidChart"));
7476
startActivity(i);
7577
break;
78+
case R.id.button9:
79+
i = new Intent(this, ScatterChartActivity.class);
80+
startActivity(i);
81+
break;
7682
}
7783
}
7884
}
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
2+
package com.example.mpchartexample;
3+
4+
import android.app.Activity;
5+
import android.os.Bundle;
6+
import android.util.Log;
7+
import android.view.Menu;
8+
import android.view.MenuItem;
9+
import android.view.WindowManager;
10+
import android.widget.SeekBar;
11+
import android.widget.SeekBar.OnSeekBarChangeListener;
12+
import android.widget.TextView;
13+
14+
import com.github.mikephil.charting.charts.ScatterChart;
15+
import com.github.mikephil.charting.data.ChartData;
16+
import com.github.mikephil.charting.data.DataSet;
17+
import com.github.mikephil.charting.data.Entry;
18+
import com.github.mikephil.charting.data.filter.Approximator.ApproximatorType;
19+
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
20+
import com.github.mikephil.charting.utils.ColorTemplate;
21+
import com.github.mikephil.charting.utils.Highlight;
22+
23+
import java.util.ArrayList;
24+
25+
public class ScatterChartActivity extends Activity implements OnSeekBarChangeListener,
26+
OnChartValueSelectedListener {
27+
28+
private ScatterChart 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_scatterchart);
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+
// create a color template for one dataset with only one color
49+
ColorTemplate ct = new ColorTemplate();
50+
ct.addColorsForDataSets(new int[] {
51+
R.color.colorful_1, R.color.colorful_2, R.color.colorful_3
52+
}, this);
53+
54+
mChart = (ScatterChart) findViewById(R.id.chart1);
55+
mChart.setOnChartValueSelectedListener(this);
56+
mChart.setColorTemplate(ct);
57+
58+
// mChart.setDrawFilled(true);
59+
// mChart.setRoundedYLegend(false);
60+
// mChart.setStartAtZero(true);
61+
// mChart.setSpacePercent(20, 10);
62+
mChart.setYLegendCount(6);
63+
mChart.setTouchEnabled(true);
64+
mChart.setHighlightEnabled(true);
65+
66+
// highlight index 2 and 6 in dataset 0
67+
// mChart.highlightValues(new Highlight[] {new Highlight(2, 0), new
68+
// Highlight(6, 0)});
69+
mChart.setDragEnabled(true);
70+
71+
mChart.setMaxVisibleValueCount(300);
72+
73+
mSeekBarX.setProgress(45);
74+
mSeekBarY.setProgress(100);
75+
}
76+
77+
@Override
78+
public boolean onCreateOptionsMenu(Menu menu) {
79+
getMenuInflater().inflate(R.menu.scatter, menu);
80+
return true;
81+
}
82+
83+
@Override
84+
public boolean onOptionsItemSelected(MenuItem item) {
85+
86+
switch (item.getItemId()) {
87+
case R.id.actionToggleValues: {
88+
if (mChart.isDrawYValuesEnabled())
89+
mChart.setDrawYValues(false);
90+
else
91+
mChart.setDrawYValues(true);
92+
mChart.invalidate();
93+
break;
94+
}
95+
case R.id.actionToggleHighlight: {
96+
if (mChart.isHighlightEnabled())
97+
mChart.setHighlightEnabled(false);
98+
else
99+
mChart.setHighlightEnabled(true);
100+
mChart.invalidate();
101+
break;
102+
}
103+
case R.id.actionToggleStartzero: {
104+
if (mChart.isStartAtZeroEnabled())
105+
mChart.setStartAtZero(false);
106+
else
107+
mChart.setStartAtZero(true);
108+
109+
mChart.invalidate();
110+
break;
111+
}
112+
case R.id.actionToggleAdjustXLegend: {
113+
if (mChart.isAdjustXLegendEnabled())
114+
mChart.setAdjustXLegend(false);
115+
else
116+
mChart.setAdjustXLegend(true);
117+
118+
mChart.invalidate();
119+
break;
120+
}
121+
case R.id.actionToggleFilter:
122+
if (mChart.isFilterSet()) {
123+
mChart.setFilter(ApproximatorType.NONE, 0);
124+
} else {
125+
mChart.setFilter(ApproximatorType.DOUGLAS_PEUCKER, 2);
126+
}
127+
mChart.invalidate();
128+
break;
129+
case R.id.actionSave: {
130+
// mChart.saveToGallery("title"+System.currentTimeMillis());
131+
mChart.saveToPath("title" + System.currentTimeMillis(), "");
132+
break;
133+
}
134+
}
135+
return true;
136+
}
137+
138+
@Override
139+
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
140+
141+
ArrayList<String> xVals = new ArrayList<String>();
142+
for (int i = 0; i < mSeekBarX.getProgress(); i++) {
143+
xVals.add((i) + "");
144+
}
145+
146+
ArrayList<Entry> yVals1 = new ArrayList<Entry>();
147+
ArrayList<Entry> yVals2 = new ArrayList<Entry>();
148+
ArrayList<Entry> yVals3 = new ArrayList<Entry>();
149+
150+
for (int i = 0; i < mSeekBarX.getProgress(); i++) {
151+
float mult = (mSeekBarY.getProgress() + 1);
152+
float val = (float) (Math.random() * mult * 0.1) + 3;// + (float)
153+
// ((mult *
154+
// 0.1) / 10);
155+
yVals1.add(new Entry(val, i));
156+
}
157+
158+
for (int i = 0; i < mSeekBarX.getProgress(); i++) {
159+
float mult = (mSeekBarY.getProgress() + 1);
160+
float val = (float) (Math.random() * mult * 0.1) + 3;// + (float)
161+
// ((mult *
162+
// 0.1) / 10);
163+
yVals2.add(new Entry(val, i));
164+
}
165+
166+
for (int i = 0; i < mSeekBarX.getProgress(); i++) {
167+
float mult = (mSeekBarY.getProgress() + 1);
168+
float val = (float) (Math.random() * mult * 0.1) + 3;// + (float)
169+
// ((mult *
170+
// 0.1) / 10);
171+
yVals3.add(new Entry(val, i));
172+
}
173+
174+
tvX.setText("" + (mSeekBarX.getProgress() + 1));
175+
tvY.setText("" + (mSeekBarY.getProgress() / 10));
176+
177+
// create a dataset and give it a type (0)
178+
DataSet set1 = new DataSet(yVals1, 0);
179+
DataSet set2 = new DataSet(yVals2, 1);
180+
DataSet set3 = new DataSet(yVals3, 2);
181+
182+
ArrayList<DataSet> dataSets = new ArrayList<DataSet>();
183+
dataSets.add(set1); // add the datasets
184+
dataSets.add(set2);
185+
dataSets.add(set3);
186+
187+
// create a data object with the datasets
188+
ChartData data = new ChartData(xVals, dataSets);
189+
190+
mChart.setData(data);
191+
mChart.invalidate();
192+
}
193+
194+
@Override
195+
public void onValuesSelected(Entry[] values, Highlight[] highlights) {
196+
Log.i("VALS SELECTED",
197+
"Value: " + values[0].getVal() + ", xIndex: " + highlights[0].getXIndex()
198+
+ ", DataSet index: " + highlights[0].getDataSetIndex());
199+
}
200+
201+
@Override
202+
public void onNothingSelected() {
203+
// TODO Auto-generated method stub
204+
205+
}
206+
207+
@Override
208+
public void onStartTrackingTouch(SeekBar seekBar) {
209+
// TODO Auto-generated method stub
210+
211+
}
212+
213+
@Override
214+
public void onStopTrackingTouch(SeekBar seekBar) {
215+
// TODO Auto-generated method stub
216+
217+
}
218+
}

‎MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,7 @@ public Highlight getHighlightByTouchPoint(float x, float y) {
976976
Log.i(LOG_TAG, "touchindex x: " + xTouchVal + ", touchindex y: " + yTouchVal);
977977

978978
// touch out of chart
979-
if (this instanceof LineChart && (xTouchVal < 0 || xTouchVal > mDeltaX))
979+
if ((this instanceof LineChart || this instanceof ScatterChart) && (xTouchVal < 0 || xTouchVal > mDeltaX))
980980
return null;
981981
if (this instanceof BarChart && (xTouchVal < 0 || xTouchVal > mDeltaX + 1))
982982
return null;
@@ -985,7 +985,7 @@ public Highlight getHighlightByTouchPoint(float x, float y) {
985985
int dataSetIndex = 0; // index of the DataSet inside the ChartData
986986
// object
987987

988-
if (this instanceof LineChart) {
988+
if (this instanceof LineChart || this instanceof ScatterChart) {
989989

990990
// check if we are more than half of a x-value or not
991991
if (xTouchVal - base > 0.5) {

0 commit comments

Comments
 (0)