• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 package com.xxmassdeveloper.mpchartexample;
3 
4 import android.Manifest;
5 import android.content.Intent;
6 import android.content.pm.PackageManager;
7 import android.graphics.Color;
8 import android.net.Uri;
9 import android.os.Bundle;
10 import androidx.core.content.ContextCompat;
11 import android.util.Log;
12 import android.view.Menu;
13 import android.view.MenuItem;
14 import android.view.WindowManager;
15 import android.widget.Toast;
16 
17 import com.github.mikephil.charting.charts.LineChart;
18 import com.github.mikephil.charting.components.Legend;
19 import com.github.mikephil.charting.components.Legend.LegendForm;
20 import com.github.mikephil.charting.components.XAxis;
21 import com.github.mikephil.charting.components.YAxis;
22 import com.github.mikephil.charting.components.YAxis.AxisDependency;
23 import com.github.mikephil.charting.data.Entry;
24 import com.github.mikephil.charting.data.LineData;
25 import com.github.mikephil.charting.data.LineDataSet;
26 import com.github.mikephil.charting.highlight.Highlight;
27 import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
28 import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
29 import com.github.mikephil.charting.utils.ColorTemplate;
30 import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
31 
32 public class RealtimeLineChartActivity extends DemoBase implements
33         OnChartValueSelectedListener {
34 
35     private LineChart chart;
36 
37     @Override
onCreate(Bundle savedInstanceState)38     protected void onCreate(Bundle savedInstanceState) {
39         super.onCreate(savedInstanceState);
40         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
41                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
42         setContentView(R.layout.activity_realtime_linechart);
43 
44         setTitle("RealtimeLineChartActivity");
45 
46         chart = findViewById(R.id.chart1);
47         chart.setOnChartValueSelectedListener(this);
48 
49         // enable description text
50         chart.getDescription().setEnabled(true);
51 
52         // enable touch gestures
53         chart.setTouchEnabled(true);
54 
55         // enable scaling and dragging
56         chart.setDragEnabled(true);
57         chart.setScaleEnabled(true);
58         chart.setDrawGridBackground(false);
59 
60         // if disabled, scaling can be done on x- and y-axis separately
61         chart.setPinchZoom(true);
62 
63         // set an alternative background color
64         chart.setBackgroundColor(Color.LTGRAY);
65 
66         LineData data = new LineData();
67         data.setValueTextColor(Color.WHITE);
68 
69         // add empty data
70         chart.setData(data);
71 
72         // get the legend (only possible after setting data)
73         Legend l = chart.getLegend();
74 
75         // modify the legend ...
76         l.setForm(LegendForm.LINE);
77         l.setTypeface(tfLight);
78         l.setTextColor(Color.WHITE);
79 
80         XAxis xl = chart.getXAxis();
81         xl.setTypeface(tfLight);
82         xl.setTextColor(Color.WHITE);
83         xl.setDrawGridLines(false);
84         xl.setAvoidFirstLastClipping(true);
85         xl.setEnabled(true);
86 
87         YAxis leftAxis = chart.getAxisLeft();
88         leftAxis.setTypeface(tfLight);
89         leftAxis.setTextColor(Color.WHITE);
90         leftAxis.setAxisMaximum(100f);
91         leftAxis.setAxisMinimum(0f);
92         leftAxis.setDrawGridLines(true);
93 
94         YAxis rightAxis = chart.getAxisRight();
95         rightAxis.setEnabled(false);
96 
97     }
98 
addEntry()99     private void addEntry() {
100 
101         LineData data = chart.getData();
102 
103         if (data != null) {
104 
105             ILineDataSet set = data.getDataSetByIndex(0);
106             // set.addEntry(...); // can be called as well
107 
108             if (set == null) {
109                 set = createSet();
110                 data.addDataSet(set);
111             }
112 
113             data.addEntry(new Entry(set.getEntryCount(), (float) (Math.random() * 40) + 30f), 0);
114             data.notifyDataChanged();
115 
116             // let the chart know it's data has changed
117             chart.notifyDataSetChanged();
118 
119             // limit the number of visible entries
120             chart.setVisibleXRangeMaximum(120);
121             // chart.setVisibleYRange(30, AxisDependency.LEFT);
122 
123             // move to the latest entry
124             chart.moveViewToX(data.getEntryCount());
125 
126             // this automatically refreshes the chart (calls invalidate())
127             // chart.moveViewTo(data.getXValCount()-7, 55f,
128             // AxisDependency.LEFT);
129         }
130     }
131 
createSet()132     private LineDataSet createSet() {
133 
134         LineDataSet set = new LineDataSet(null, "Dynamic Data");
135         set.setAxisDependency(AxisDependency.LEFT);
136         set.setColor(ColorTemplate.getHoloBlue());
137         set.setCircleColor(Color.WHITE);
138         set.setLineWidth(2f);
139         set.setCircleRadius(4f);
140         set.setFillAlpha(65);
141         set.setFillColor(ColorTemplate.getHoloBlue());
142         set.setHighLightColor(Color.rgb(244, 117, 117));
143         set.setValueTextColor(Color.WHITE);
144         set.setValueTextSize(9f);
145         set.setDrawValues(false);
146         return set;
147     }
148 
149     private Thread thread;
150 
feedMultiple()151     private void feedMultiple() {
152 
153         if (thread != null)
154             thread.interrupt();
155 
156         final Runnable runnable = new Runnable() {
157 
158             @Override
159             public void run() {
160                 addEntry();
161             }
162         };
163 
164         thread = new Thread(new Runnable() {
165 
166             @Override
167             public void run() {
168                 for (int i = 0; i < 1000; i++) {
169 
170                     // Don't generate garbage runnables inside the loop.
171                     runOnUiThread(runnable);
172 
173                     try {
174                         Thread.sleep(25);
175                     } catch (InterruptedException e) {
176                         e.printStackTrace();
177                     }
178                 }
179             }
180         });
181 
182         thread.start();
183     }
184 
185     @Override
onCreateOptionsMenu(Menu menu)186     public boolean onCreateOptionsMenu(Menu menu) {
187         getMenuInflater().inflate(R.menu.realtime, menu);
188         return true;
189     }
190 
191     @Override
onOptionsItemSelected(MenuItem item)192     public boolean onOptionsItemSelected(MenuItem item) {
193 
194         switch (item.getItemId()) {
195             case R.id.viewGithub: {
196                 Intent i = new Intent(Intent.ACTION_VIEW);
197                 i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/RealtimeLineChartActivity.java"));
198                 startActivity(i);
199                 break;
200             }
201             case R.id.actionAdd: {
202                 addEntry();
203                 break;
204             }
205             case R.id.actionClear: {
206                 chart.clearValues();
207                 Toast.makeText(this, "Chart cleared!", Toast.LENGTH_SHORT).show();
208                 break;
209             }
210             case R.id.actionFeedMultiple: {
211                 feedMultiple();
212                 break;
213             }
214             case R.id.actionSave: {
215                 if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
216                     saveToGallery();
217                 } else {
218                     requestStoragePermission(chart);
219                 }
220                 break;
221             }
222         }
223         return true;
224     }
225 
226     @Override
saveToGallery()227     protected void saveToGallery() {
228         saveToGallery(chart, "RealtimeLineChartActivity");
229     }
230 
231     @Override
onValueSelected(Entry e, Highlight h)232     public void onValueSelected(Entry e, Highlight h) {
233         Log.i("Entry selected", e.toString());
234     }
235 
236     @Override
onNothingSelected()237     public void onNothingSelected() {
238         Log.i("Nothing selected", "Nothing selected.");
239     }
240 
241     @Override
onPause()242     protected void onPause() {
243         super.onPause();
244 
245         if (thread != null) {
246             thread.interrupt();
247         }
248     }
249 }
250