1717import android .view .WindowManager ;
1818import android .widget .SeekBar ;
1919import android .widget .SeekBar .OnSeekBarChangeListener ;
20- import android .widget .TextView ;
2120
2221import com .github .mikephil .charting .animation .Easing ;
2322import com .github .mikephil .charting .charts .LineChart ;
3534import com .github .mikephil .charting .listener .OnChartValueSelectedListener ;
3635import com .github .mikephil .charting .utils .Utils ;
3736import com .xxmassdeveloper .mpchartexample .custom .MyMarkerView ;
37+ import com .xxmassdeveloper .mpchartexample .databinding .ActivityLinechartBinding ;
3838import com .xxmassdeveloper .mpchartexample .notimportant .DemoBase ;
3939
4040import java .util .ArrayList ;
4545 */
4646public class LineChartActivity1 extends DemoBase implements OnSeekBarChangeListener , OnChartValueSelectedListener {
4747
48- private LineChart chart1 ;
49- private SeekBar seekBarX , seekBarY ;
50- private TextView tvXMax , tvYMax ;
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- tvXMax = findViewById (R .id .tvXMax );
61- tvYMax = 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,15 +139,15 @@ 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 );
@@ -172,12 +165,12 @@ private void setData(int count, float range) {
172165
173166 LineDataSet lineDataSet0 ;
174167
175- if (chart1 .getData () != null && chart1 .getData ().getDataSetCount () > 0 ) {
176- lineDataSet0 = (LineDataSet ) chart1 .getData ().getDataSetByIndex (0 );
168+ if (binding . chart1 .getData () != null && binding . chart1 .getData ().getDataSetCount () > 0 ) {
169+ lineDataSet0 = (LineDataSet ) binding . chart1 .getData ().getDataSetByIndex (0 );
177170 lineDataSet0 .setEntries (values );
178171 lineDataSet0 .notifyDataSetChanged ();
179- chart1 .getData ().notifyDataChanged ();
180- chart1 .notifyDataSetChanged ();
172+ binding . chart1 .getData ().notifyDataChanged ();
173+ binding . chart1 .notifyDataSetChanged ();
181174 } else {
182175 // create a dataset and give it a type
183176 lineDataSet0 = new LineDataSet (values , "DataSet 1" );
@@ -211,7 +204,7 @@ private void setData(int count, float range) {
211204
212205 // set the filled area
213206 lineDataSet0 .setDrawFilled (true );
214- lineDataSet0 .setFillFormatter ((dataSet , dataProvider ) -> chart1 .getAxisLeft ().getAxisMinimum ());
207+ lineDataSet0 .setFillFormatter ((dataSet , dataProvider ) -> binding . chart1 .getAxisLeft ().getAxisMinimum ());
215208
216209 // set color of filled area
217210 if (Utils .getSDKInt () >= 18 ) {
@@ -229,7 +222,7 @@ private void setData(int count, float range) {
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- tvXMax .setText (String .valueOf (seekBarX .getProgress ()));
357- tvYMax .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