• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // TODO: Finish and add to main activity list
2 package com.xxmassdeveloper.mpchartexample;
3 
4 import android.Manifest;
5 import android.content.pm.PackageManager;
6 import android.os.Bundle;
7 import androidx.core.content.ContextCompat;
8 import android.util.Log;
9 import android.view.Menu;
10 import android.view.MenuItem;
11 import android.view.WindowManager;
12 
13 import com.github.mikephil.charting.charts.Chart;
14 import com.github.mikephil.charting.charts.LineChart;
15 import com.github.mikephil.charting.components.XAxis;
16 import com.github.mikephil.charting.components.YAxis;
17 import com.github.mikephil.charting.data.DataSet;
18 import com.github.mikephil.charting.data.Entry;
19 import com.github.mikephil.charting.data.LineData;
20 import com.github.mikephil.charting.data.LineDataSet;
21 import com.github.mikephil.charting.highlight.Highlight;
22 import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
23 import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
24 import com.github.mikephil.charting.listener.OnDrawListener;
25 import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
26 
27 import java.util.ArrayList;
28 import java.util.List;
29 
30 /**
31  * This Activity demonstrates drawing into the Chart with the finger. Both line,
32  * bar and scatter charts can be used for drawing.
33  *
34  * @author Philipp Jahoda
35  */
36 public class DrawChartActivity extends DemoBase implements OnChartValueSelectedListener,
37         OnDrawListener {
38 
39     private LineChart chart;
40 
41     @Override
onCreate(Bundle savedInstanceState)42     protected void onCreate(Bundle savedInstanceState) {
43         super.onCreate(savedInstanceState);
44         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
45                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
46         setContentView(R.layout.activity_draw_chart);
47 
48         setTitle("DrawChartActivity");
49 
50         chart = findViewById(R.id.chart1);
51 
52         // listener for selecting and drawing
53         chart.setOnChartValueSelectedListener(this);
54         chart.setOnDrawListener(this);
55 
56         // if disabled, drawn data sets with the finger will not be automatically
57         // finished
58         // chart.setAutoFinish(true);
59         chart.setDrawGridBackground(false);
60 
61         // add dummy-data to the chart
62         initWithDummyData();
63 
64         XAxis xl = chart.getXAxis();
65         xl.setTypeface(tfRegular);
66         xl.setAvoidFirstLastClipping(true);
67 
68         YAxis yl = chart.getAxisLeft();
69         yl.setTypeface(tfRegular);
70 
71         chart.getLegend().setEnabled(false);
72 
73         // chart.setYRange(-40f, 40f, true);
74         // call this to reset the changed y-range
75         // chart.resetYRange(true);
76     }
77 
initWithDummyData()78     private void initWithDummyData() {
79 
80         ArrayList<Entry> values = new ArrayList<>();
81 
82         // create a dataset and give it a type (0)
83         LineDataSet set1 = new LineDataSet(values, "DataSet");
84         set1.setLineWidth(3f);
85         set1.setCircleRadius(5f);
86 
87         // create a data object with the data sets
88         LineData data = new LineData(set1);
89 
90         chart.setData(data);
91     }
92 
93     @Override
onCreateOptionsMenu(Menu menu)94     public boolean onCreateOptionsMenu(Menu menu) {
95         getMenuInflater().inflate(R.menu.draw, menu);
96         return true;
97     }
98 
99     @Override
onOptionsItemSelected(MenuItem item)100     public boolean onOptionsItemSelected(MenuItem item) {
101 
102         switch (item.getItemId()) {
103             case R.id.actionToggleValues: {
104                 List<ILineDataSet> sets = chart.getData()
105                         .getDataSets();
106 
107                 for (ILineDataSet iSet : sets) {
108 
109                     LineDataSet set = (LineDataSet) iSet;
110                     set.setDrawValues(!set.isDrawValuesEnabled());
111                 }
112 
113                 chart.invalidate();
114                 break;
115             }
116             case R.id.actionToggleHighlight: {
117                 if(chart.getData() != null) {
118                     chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled());
119                     chart.invalidate();
120                 }
121                 break;
122             }
123             case R.id.actionTogglePinch: {
124                 if (chart.isPinchZoomEnabled())
125                     chart.setPinchZoom(false);
126                 else
127                     chart.setPinchZoom(true);
128 
129                 chart.invalidate();
130                 break;
131             }
132             case R.id.actionToggleAutoScaleMinMax: {
133                 chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled());
134                 chart.notifyDataSetChanged();
135                 break;
136             }
137             case R.id.actionSave: {
138                 if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
139                     saveToGallery();
140                 } else {
141                     requestStoragePermission(chart);
142                 }
143                 break;
144             }
145         }
146         return true;
147     }
148 
149     @Override
saveToGallery()150     protected void saveToGallery() {
151         saveToGallery(chart, "DrawChartActivity");
152     }
153 
154     @Override
onValueSelected(Entry e, Highlight h)155     public void onValueSelected(Entry e, Highlight h) {
156         Log.i("VAL SELECTED",
157                 "Value: " + e.getY() + ", xIndex: " + e.getX()
158                         + ", DataSet index: " + h.getDataSetIndex());
159     }
160 
161     @Override
onNothingSelected()162     public void onNothingSelected() {
163     }
164 
165     /** callback for each new entry drawn with the finger */
166     @Override
onEntryAdded(Entry entry)167     public void onEntryAdded(Entry entry) {
168         Log.i(Chart.LOG_TAG, entry.toString());
169     }
170 
171     /** callback when a DataSet has been drawn (when lifting the finger) */
172     @Override
onDrawFinished(DataSet<?> dataSet)173     public void onDrawFinished(DataSet<?> dataSet) {
174         Log.i(Chart.LOG_TAG, "DataSet drawn. " + dataSet.toSimpleString());
175 
176         // prepare the legend again
177         chart.getLegendRenderer().computeLegend(chart.getData());
178     }
179 
180     @Override
onEntryMoved(Entry entry)181     public void onEntryMoved(Entry entry) {
182         Log.i(Chart.LOG_TAG, "Point moved " + entry.toString());
183     }
184 }
185