Skip to content

Commit 9b1aa92

Browse files
authored
Merge pull request #142 from AppDevNext/ViewBinding
View binding
2 parents 0ac4f59 + ea92588 commit 9b1aa92

File tree

2 files changed

+88
-92
lines changed

2 files changed

+88
-92
lines changed

MPChartExample/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ android {
2121
kotlinOptions {
2222
jvmTarget = '17'
2323
}
24+
buildFeatures {
25+
viewBinding true
26+
}
2427
buildTypes {
2528
release {
2629
minifyEnabled false

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

Lines changed: 85 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import android.view.WindowManager;
1818
import android.widget.SeekBar;
1919
import android.widget.SeekBar.OnSeekBarChangeListener;
20-
import android.widget.TextView;
2120

2221
import com.github.mikephil.charting.animation.Easing;
2322
import com.github.mikephil.charting.charts.LineChart;
@@ -35,6 +34,7 @@
3534
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
3635
import com.github.mikephil.charting.utils.Utils;
3736
import com.xxmassdeveloper.mpchartexample.custom.MyMarkerView;
37+
import com.xxmassdeveloper.mpchartexample.databinding.ActivityLinechartBinding;
3838
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
3939

4040
import java.util.ArrayList;
@@ -45,68 +45,61 @@
4545
*/
4646
public class LineChartActivity1 extends DemoBase implements OnSeekBarChangeListener, OnChartValueSelectedListener {
4747

48-
private LineChart chart1;
49-
private SeekBar seekBarX, seekBarY;
50-
private TextView tvX, tvY;
48+
private ActivityLinechartBinding binding;
5149

5250
@Override
5351
protected void onCreate(Bundle savedInstanceState) {
5452
super.onCreate(savedInstanceState);
53+
binding = ActivityLinechartBinding.inflate(getLayoutInflater());
54+
setContentView(binding.getRoot());
55+
5556
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
56-
setContentView(R.layout.activity_linechart);
5757

5858
setTitle("LineChartActivity1");
5959

60-
tvX = findViewById(R.id.tvXMax);
61-
tvY = findViewById(R.id.tvYMax);
62-
63-
seekBarX = findViewById(R.id.seekBarX);
64-
seekBarX.setOnSeekBarChangeListener(this);
65-
66-
seekBarY = findViewById(R.id.seekBarY);
67-
seekBarY.setMax(180);
68-
seekBarY.setOnSeekBarChangeListener(this);
60+
binding.seekBarX.setOnSeekBarChangeListener(this);
6961

70-
chart1 = findViewById(R.id.chart1);
62+
binding.seekBarY.setMax(180);
63+
binding.seekBarY.setOnSeekBarChangeListener(this);
7164

7265
// background color
73-
chart1.setBackgroundColor(Color.WHITE);
66+
binding.chart1.setBackgroundColor(Color.WHITE);
7467

7568
// disable description text
76-
chart1.getDescription().setEnabled(false);
69+
binding.chart1.getDescription().setEnabled(false);
7770

7871
// enable touch gestures
79-
chart1.setTouchEnabled(true);
72+
binding.chart1.setTouchEnabled(true);
8073

8174
// set listeners
82-
chart1.setOnChartValueSelectedListener(this);
83-
chart1.setDrawGridBackground(false);
75+
binding.chart1.setOnChartValueSelectedListener(this);
76+
binding.chart1.setDrawGridBackground(false);
8477

8578
// create marker to display box when values are selected
8679
MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker_view);
8780

8881
// Set the marker to the chart
89-
mv.setChartView(chart1);
90-
chart1.setMarker(mv);
82+
mv.setChartView(binding.chart1);
83+
binding.chart1.setMarker(mv);
9184

9285
// enable scaling and dragging
93-
chart1.setDragEnabled(true);
94-
chart1.setScaleEnabled(true);
86+
binding.chart1.setDragEnabled(true);
87+
binding.chart1.setScaleEnabled(true);
9588

9689
// force pinch zoom along both axis
97-
chart1.setPinchZoom(true);
90+
binding.chart1.setPinchZoom(true);
9891

9992
XAxis xAxis;
100-
xAxis = chart1.getXAxis();
93+
xAxis = binding.chart1.getXAxis();
10194

10295
// vertical grid lines
10396
xAxis.enableGridDashedLine(10f, 10f, 0f);
10497

10598
YAxis yAxis;
106-
yAxis = chart1.getAxisLeft();
99+
yAxis = binding.chart1.getAxisLeft();
107100

108101
// disable dual axis (only use LEFT axis)
109-
chart1.getAxisRight().setEnabled(false);
102+
binding.chart1.getAxisRight().setEnabled(false);
110103

111104
// horizontal grid lines
112105
yAxis.enableGridDashedLine(10f, 10f, 0f);
@@ -146,90 +139,90 @@ protected void onCreate(Bundle savedInstanceState) {
146139
//xAxis.addLimitLine(llXAxis);
147140

148141
// add data
149-
seekBarX.setProgress(45);
150-
seekBarY.setProgress(180);
142+
binding.seekBarX.setProgress(45);
143+
binding.seekBarY.setProgress(180);
151144
setData(45, 180);
152145

153146
// draw points over time
154-
chart1.animateX(1500);
147+
binding.chart1.animateX(1500);
155148

156149
// get the legend (only possible after setting data)
157-
Legend l = chart1.getLegend();
150+
Legend l = binding.chart1.getLegend();
158151

159152
// draw legend entries as lines
160153
l.setForm(LegendForm.LINE);
161154
}
162155

163156
private void setData(int count, float range) {
164-
157+
Log.d("setData", count + "= range=" + range);
165158
ArrayList<Entry> values = new ArrayList<>();
166159

167160
for (int i = 0; i < count; i++) {
168-
169161
float val = (float) (Math.random() * range) - 30;
162+
Log.v("setData", i + "=" + val);
170163
values.add(new Entry(i, val, ContextCompat.getDrawable(this, R.drawable.star)));
171164
}
172165

173-
LineDataSet set1;
166+
LineDataSet lineDataSet0;
174167

175-
if (chart1.getData() != null && chart1.getData().getDataSetCount() > 0) {
176-
set1 = (LineDataSet) chart1.getData().getDataSetByIndex(0);
177-
set1.setValues(values);
178-
set1.notifyDataSetChanged();
179-
chart1.getData().notifyDataChanged();
180-
chart1.notifyDataSetChanged();
168+
if (binding.chart1.getData() != null && binding.chart1.getData().getDataSetCount() > 0) {
169+
lineDataSet0 = (LineDataSet) binding.chart1.getData().getDataSetByIndex(0);
170+
lineDataSet0.setEntries(values);
171+
lineDataSet0.notifyDataSetChanged();
172+
binding.chart1.getData().notifyDataChanged();
173+
binding.chart1.notifyDataSetChanged();
181174
} else {
182175
// create a dataset and give it a type
183-
set1 = new LineDataSet(values, "DataSet 1");
176+
lineDataSet0 = new LineDataSet(values, "DataSet 1");
184177

185-
set1.setDrawIcons(false);
178+
lineDataSet0.setDrawIcons(false);
186179

187180
// draw dashed line
188-
set1.enableDashedLine(10f, 5f, 0f);
181+
lineDataSet0.enableDashedLine(10f, 5f, 0f);
189182

190183
// black lines and points
191-
set1.setColor(Color.BLACK);
192-
set1.setCircleColor(Color.BLACK);
184+
lineDataSet0.setColor(Color.BLACK);
185+
lineDataSet0.setCircleColor(Color.BLACK);
193186

194187
// line thickness and point size
195-
set1.setLineWidth(1f);
196-
set1.setCircleRadius(3f);
188+
lineDataSet0.setLineWidth(1f);
189+
lineDataSet0.setCircleRadius(3f);
197190

198191
// draw points as solid circles
199-
set1.setDrawCircleHole(false);
192+
lineDataSet0.setDrawCircleHole(false);
200193

201194
// customize legend entry
202-
set1.setFormLineWidth(1f);
203-
set1.setFormLineDashEffect(new DashPathEffect(new float[]{10f, 5f}, 0f));
204-
set1.setFormSize(15.f);
195+
lineDataSet0.setFormLineWidth(1f);
196+
lineDataSet0.setFormLineDashEffect(new DashPathEffect(new float[]{10f, 5f}, 0f));
197+
lineDataSet0.setFormSize(15.f);
205198

206199
// text size of values
207-
set1.setValueTextSize(9f);
200+
lineDataSet0.setValueTextSize(9f);
208201

209202
// draw selection line as dashed
210-
set1.enableDashedHighlightLine(10f, 5f, 0f);
203+
lineDataSet0.enableDashedHighlightLine(10f, 5f, 0f);
211204

212205
// set the filled area
213-
set1.setDrawFilled(true);
214-
set1.setFillFormatter((dataSet, dataProvider) -> chart1.getAxisLeft().getAxisMinimum());
206+
lineDataSet0.setDrawFilled(true);
207+
lineDataSet0.setFillFormatter((dataSet, dataProvider) -> binding.chart1.getAxisLeft().getAxisMinimum());
215208

216209
// set color of filled area
217210
if (Utils.getSDKInt() >= 18) {
218211
// drawables only supported on api level 18 and above
219212
Drawable drawable = ContextCompat.getDrawable(this, R.drawable.fade_red);
220-
set1.setFillDrawable(drawable);
213+
lineDataSet0.setFillDrawable(drawable);
221214
} else {
222-
set1.setFillColor(Color.BLACK);
215+
lineDataSet0.setFillColor(Color.BLACK);
223216
}
224217

225218
ArrayList<ILineDataSet> dataSets = new ArrayList<>();
226-
dataSets.add(set1); // add the data sets
219+
dataSets.add(lineDataSet0); // add the data sets
227220

228221
// create a data object with the data sets
229222
LineData data = new LineData(dataSets);
230223

231224
// set data
232-
chart1.setData(data);
225+
binding.chart1.setData(data);
233226
}
234227
}
235228

@@ -249,101 +242,101 @@ public boolean onOptionsItemSelected(MenuItem item) {
249242
startActivity(i);
250243
}
251244
case R.id.actionToggleValues -> {
252-
List<ILineDataSet> sets = chart1.getData().getDataSets();
245+
List<ILineDataSet> sets = binding.chart1.getData().getDataSets();
253246

254247
for (ILineDataSet iSet : sets) {
255248

256249
LineDataSet set = (LineDataSet) iSet;
257250
set.setDrawValues(!set.isDrawValuesEnabled());
258251
}
259252

260-
chart1.invalidate();
253+
binding.chart1.invalidate();
261254
}
262255
case R.id.actionToggleIcons -> {
263-
List<ILineDataSet> sets = chart1.getData().getDataSets();
256+
List<ILineDataSet> sets = binding.chart1.getData().getDataSets();
264257

265258
for (ILineDataSet iSet : sets) {
266259

267260
LineDataSet set = (LineDataSet) iSet;
268261
set.setDrawIcons(!set.isDrawIconsEnabled());
269262
}
270263

271-
chart1.invalidate();
264+
binding.chart1.invalidate();
272265
}
273266
case R.id.actionToggleHighlight -> {
274-
if (chart1.getData() != null) {
275-
chart1.getData().setHighlightEnabled(!chart1.getData().isHighlightEnabled());
276-
chart1.invalidate();
267+
if (binding.chart1.getData() != null) {
268+
binding.chart1.getData().setHighlightEnabled(!binding.chart1.getData().isHighlightEnabled());
269+
binding.chart1.invalidate();
277270
}
278271
}
279272
case R.id.actionToggleFilled -> {
280273

281-
List<ILineDataSet> sets = chart1.getData().getDataSets();
274+
List<ILineDataSet> sets = binding.chart1.getData().getDataSets();
282275

283276
for (ILineDataSet iSet : sets) {
284277

285278
LineDataSet set = (LineDataSet) iSet;
286279
set.setDrawFilled(!set.isDrawFilledEnabled());
287280
}
288-
chart1.invalidate();
281+
binding.chart1.invalidate();
289282
}
290283
case R.id.actionToggleCircles -> {
291-
List<ILineDataSet> sets = chart1.getData().getDataSets();
284+
List<ILineDataSet> sets = binding.chart1.getData().getDataSets();
292285

293286
for (ILineDataSet iSet : sets) {
294287

295288
LineDataSet set = (LineDataSet) iSet;
296289
set.setDrawCircles(!set.isDrawCirclesEnabled());
297290
}
298-
chart1.invalidate();
291+
binding.chart1.invalidate();
299292
}
300293
case R.id.actionToggleCubic -> {
301-
List<ILineDataSet> sets = chart1.getData().getDataSets();
294+
List<ILineDataSet> sets = binding.chart1.getData().getDataSets();
302295

303296
for (ILineDataSet iSet : sets) {
304297

305298
LineDataSet set = (LineDataSet) iSet;
306299
set.setMode(set.getMode() == LineDataSet.Mode.CUBIC_BEZIER ? LineDataSet.Mode.LINEAR : LineDataSet.Mode.CUBIC_BEZIER);
307300
}
308-
chart1.invalidate();
301+
binding.chart1.invalidate();
309302
}
310303
case R.id.actionToggleStepped -> {
311-
List<ILineDataSet> sets = chart1.getData().getDataSets();
304+
List<ILineDataSet> sets = binding.chart1.getData().getDataSets();
312305

313306
for (ILineDataSet iSet : sets) {
314307

315308
LineDataSet set = (LineDataSet) iSet;
316309
set.setMode(set.getMode() == LineDataSet.Mode.STEPPED ? LineDataSet.Mode.LINEAR : LineDataSet.Mode.STEPPED);
317310
}
318-
chart1.invalidate();
311+
binding.chart1.invalidate();
319312
}
320313
case R.id.actionToggleHorizontalCubic -> {
321-
List<ILineDataSet> sets = chart1.getData().getDataSets();
314+
List<ILineDataSet> sets = binding.chart1.getData().getDataSets();
322315

323316
for (ILineDataSet iSet : sets) {
324317

325318
LineDataSet set = (LineDataSet) iSet;
326319
set.setMode(set.getMode() == LineDataSet.Mode.HORIZONTAL_BEZIER ? LineDataSet.Mode.LINEAR : LineDataSet.Mode.HORIZONTAL_BEZIER);
327320
}
328-
chart1.invalidate();
321+
binding.chart1.invalidate();
329322
}
330323
case R.id.actionTogglePinch -> {
331-
chart1.setPinchZoom(!chart1.isPinchZoomEnabled());
324+
binding.chart1.setPinchZoom(!binding.chart1.isPinchZoomEnabled());
332325

333-
chart1.invalidate();
326+
binding.chart1.invalidate();
334327
}
335328
case R.id.actionToggleAutoScaleMinMax -> {
336-
chart1.setAutoScaleMinMaxEnabled(!chart1.isAutoScaleMinMaxEnabled());
337-
chart1.notifyDataSetChanged();
329+
binding.chart1.setAutoScaleMinMaxEnabled(!binding.chart1.isAutoScaleMinMaxEnabled());
330+
binding.chart1.notifyDataSetChanged();
338331
}
339-
case R.id.animateX -> chart1.animateX(2000);
340-
case R.id.animateY -> chart1.animateY(2000, Easing.EaseInCubic);
341-
case R.id.animateXY -> chart1.animateXY(2000, 2000);
332+
case R.id.animateX -> binding.chart1.animateX(2000);
333+
case R.id.animateY -> binding.chart1.animateY(2000, Easing.EaseInCubic);
334+
case R.id.animateXY -> binding.chart1.animateXY(2000, 2000);
342335
case R.id.actionSave -> {
343336
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
344337
saveToGallery();
345338
} else {
346-
requestStoragePermission(chart1);
339+
requestStoragePermission(binding.chart1);
347340
}
348341
}
349342
}
@@ -353,18 +346,18 @@ public boolean onOptionsItemSelected(MenuItem item) {
353346
@Override
354347
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
355348

356-
tvX.setText(String.valueOf(seekBarX.getProgress()));
357-
tvY.setText(String.valueOf(seekBarY.getProgress()));
349+
binding.tvXMax.setText(String.valueOf(binding.seekBarX.getProgress()));
350+
binding.tvYMax.setText(String.valueOf(binding.seekBarY.getProgress()));
358351

359-
setData(seekBarX.getProgress(), seekBarY.getProgress());
352+
setData(binding.seekBarX.getProgress(), binding.seekBarY.getProgress());
360353

361354
// redraw
362-
chart1.invalidate();
355+
binding.chart1.invalidate();
363356
}
364357

365358
@Override
366359
protected void saveToGallery() {
367-
saveToGallery(chart1, "LineChartActivity1");
360+
saveToGallery(binding.chart1, "LineChartActivity1");
368361
}
369362

370363
@Override
@@ -378,8 +371,8 @@ public void onStopTrackingTouch(SeekBar seekBar) {
378371
@Override
379372
public void onValueSelected(Entry e, Highlight h) {
380373
Log.i("Entry selected", e.toString());
381-
Log.i("LOW HIGH", "low: " + chart1.getLowestVisibleX() + ", high: " + chart1.getHighestVisibleX());
382-
Log.i("MIN MAX", "xMin: " + chart1.getXChartMin() + ", xMax: " + chart1.getXChartMax() + ", yMin: " + chart1.getYChartMin() + ", yMax: " + chart1.getYChartMax());
374+
Log.i("LOW HIGH", "low: " + binding.chart1.getLowestVisibleX() + ", high: " + binding.chart1.getHighestVisibleX());
375+
Log.i("MIN MAX", "xMin: " + binding.chart1.getXChartMin() + ", xMax: " + binding.chart1.getXChartMax() + ", yMin: " + binding.chart1.getYChartMin() + ", yMax: " + binding.chart1.getYChartMax());
383376
}
384377

385378
@Override

0 commit comments

Comments
 (0)