Skip to content

Commit eeb1a59

Browse files
committed
Code simplyfy
1 parent 2367d44 commit eeb1a59

File tree

2 files changed

+40
-127
lines changed

2 files changed

+40
-127
lines changed

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

Lines changed: 20 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@
3030
import com.github.mikephil.charting.data.Entry;
3131
import com.github.mikephil.charting.data.LineData;
3232
import com.github.mikephil.charting.data.LineDataSet;
33-
import com.github.mikephil.charting.formatter.IFillFormatter;
3433
import com.github.mikephil.charting.highlight.Highlight;
35-
import com.github.mikephil.charting.interfaces.dataprovider.LineDataProvider;
3634
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
3735
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
3836
import com.github.mikephil.charting.utils.Utils;
@@ -44,9 +42,6 @@
4442

4543
/**
4644
* Example of a heavily customized {@link LineChart} with limit lines, custom line shapes, etc.
47-
*
48-
* @version 3.1.0
49-
* @since 1.7.4
5045
*/
5146
public class LineChartActivity1 extends DemoBase implements OnSeekBarChangeListener, OnChartValueSelectedListener {
5247

@@ -97,8 +92,6 @@ protected void onCreate(Bundle savedInstanceState) {
9792
// enable scaling and dragging
9893
chart.setDragEnabled(true);
9994
chart.setScaleEnabled(true);
100-
// chart.setScaleXEnabled(true);
101-
// chart.setScaleYEnabled(true);
10295

10396
// force pinch zoom along both axis
10497
chart.setPinchZoom(true);
@@ -174,7 +167,7 @@ private void setData(int count, float range) {
174167
for (int i = 0; i < count; i++) {
175168

176169
float val = (float) (Math.random() * range) - 30;
177-
values.add(new Entry(i, val, getResources().getDrawable(R.drawable.star)));
170+
values.add(new Entry(i, val, ContextCompat.getDrawable(this, R.drawable.star)));
178171
}
179172

180173
LineDataSet set1;
@@ -218,12 +211,7 @@ private void setData(int count, float range) {
218211

219212
// set the filled area
220213
set1.setDrawFilled(true);
221-
set1.setFillFormatter(new IFillFormatter() {
222-
@Override
223-
public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
224-
return chart.getAxisLeft().getAxisMinimum();
225-
}
226-
});
214+
set1.setFillFormatter((dataSet, dataProvider) -> chart.getAxisLeft().getAxisMinimum());
227215

228216
// set color of filled area
229217
if (Utils.getSDKInt() >= 18) {
@@ -255,13 +243,12 @@ public boolean onCreateOptionsMenu(Menu menu) {
255243
public boolean onOptionsItemSelected(MenuItem item) {
256244

257245
switch (item.getItemId()) {
258-
case R.id.viewGithub: {
246+
case R.id.viewGithub -> {
259247
Intent i = new Intent(Intent.ACTION_VIEW);
260248
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity1.java"));
261249
startActivity(i);
262-
break;
263250
}
264-
case R.id.actionToggleValues: {
251+
case R.id.actionToggleValues -> {
265252
List<ILineDataSet> sets = chart.getData().getDataSets();
266253

267254
for (ILineDataSet iSet : sets) {
@@ -271,9 +258,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
271258
}
272259

273260
chart.invalidate();
274-
break;
275261
}
276-
case R.id.actionToggleIcons: {
262+
case R.id.actionToggleIcons -> {
277263
List<ILineDataSet> sets = chart.getData().getDataSets();
278264

279265
for (ILineDataSet iSet : sets) {
@@ -283,47 +269,35 @@ public boolean onOptionsItemSelected(MenuItem item) {
283269
}
284270

285271
chart.invalidate();
286-
break;
287272
}
288-
case R.id.actionToggleHighlight: {
273+
case R.id.actionToggleHighlight -> {
289274
if (chart.getData() != null) {
290275
chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled());
291276
chart.invalidate();
292277
}
293-
break;
294278
}
295-
case R.id.actionToggleFilled: {
279+
case R.id.actionToggleFilled -> {
296280

297281
List<ILineDataSet> sets = chart.getData().getDataSets();
298282

299283
for (ILineDataSet iSet : sets) {
300284

301285
LineDataSet set = (LineDataSet) iSet;
302-
if (set.isDrawFilledEnabled()) {
303-
set.setDrawFilled(false);
304-
} else {
305-
set.setDrawFilled(true);
306-
}
286+
set.setDrawFilled(!set.isDrawFilledEnabled());
307287
}
308288
chart.invalidate();
309-
break;
310289
}
311-
case R.id.actionToggleCircles: {
290+
case R.id.actionToggleCircles -> {
312291
List<ILineDataSet> sets = chart.getData().getDataSets();
313292

314293
for (ILineDataSet iSet : sets) {
315294

316295
LineDataSet set = (LineDataSet) iSet;
317-
if (set.isDrawCirclesEnabled()) {
318-
set.setDrawCircles(false);
319-
} else {
320-
set.setDrawCircles(true);
321-
}
296+
set.setDrawCircles(!set.isDrawCirclesEnabled());
322297
}
323298
chart.invalidate();
324-
break;
325299
}
326-
case R.id.actionToggleCubic: {
300+
case R.id.actionToggleCubic -> {
327301
List<ILineDataSet> sets = chart.getData().getDataSets();
328302

329303
for (ILineDataSet iSet : sets) {
@@ -332,9 +306,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
332306
set.setMode(set.getMode() == LineDataSet.Mode.CUBIC_BEZIER ? LineDataSet.Mode.LINEAR : LineDataSet.Mode.CUBIC_BEZIER);
333307
}
334308
chart.invalidate();
335-
break;
336309
}
337-
case R.id.actionToggleStepped: {
310+
case R.id.actionToggleStepped -> {
338311
List<ILineDataSet> sets = chart.getData().getDataSets();
339312

340313
for (ILineDataSet iSet : sets) {
@@ -343,9 +316,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
343316
set.setMode(set.getMode() == LineDataSet.Mode.STEPPED ? LineDataSet.Mode.LINEAR : LineDataSet.Mode.STEPPED);
344317
}
345318
chart.invalidate();
346-
break;
347319
}
348-
case R.id.actionToggleHorizontalCubic: {
320+
case R.id.actionToggleHorizontalCubic -> {
349321
List<ILineDataSet> sets = chart.getData().getDataSets();
350322

351323
for (ILineDataSet iSet : sets) {
@@ -354,42 +326,25 @@ public boolean onOptionsItemSelected(MenuItem item) {
354326
set.setMode(set.getMode() == LineDataSet.Mode.HORIZONTAL_BEZIER ? LineDataSet.Mode.LINEAR : LineDataSet.Mode.HORIZONTAL_BEZIER);
355327
}
356328
chart.invalidate();
357-
break;
358329
}
359-
case R.id.actionTogglePinch: {
360-
if (chart.isPinchZoomEnabled()) {
361-
chart.setPinchZoom(false);
362-
} else {
363-
chart.setPinchZoom(true);
364-
}
330+
case R.id.actionTogglePinch -> {
331+
chart.setPinchZoom(!chart.isPinchZoomEnabled());
365332

366333
chart.invalidate();
367-
break;
368334
}
369-
case R.id.actionToggleAutoScaleMinMax: {
335+
case R.id.actionToggleAutoScaleMinMax -> {
370336
chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled());
371337
chart.notifyDataSetChanged();
372-
break;
373-
}
374-
case R.id.animateX: {
375-
chart.animateX(2000);
376-
break;
377-
}
378-
case R.id.animateY: {
379-
chart.animateY(2000, Easing.EaseInCubic);
380-
break;
381-
}
382-
case R.id.animateXY: {
383-
chart.animateXY(2000, 2000);
384-
break;
385338
}
386-
case R.id.actionSave: {
339+
case R.id.animateX -> chart.animateX(2000);
340+
case R.id.animateY -> chart.animateY(2000, Easing.EaseInCubic);
341+
case R.id.animateXY -> chart.animateXY(2000, 2000);
342+
case R.id.actionSave -> {
387343
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
388344
saveToGallery();
389345
} else {
390346
requestStoragePermission(chart);
391347
}
392-
break;
393348
}
394349
}
395350
return true;

0 commit comments

Comments
 (0)