|
| 1 | + |
| 2 | +package com.xxmassdeveloper.mpchartexample; |
| 3 | + |
| 4 | +import android.graphics.Color; |
| 5 | +import android.os.Bundle; |
| 6 | +import android.util.Log; |
| 7 | +import android.view.Menu; |
| 8 | +import android.view.MenuItem; |
| 9 | +import android.view.WindowManager; |
| 10 | +import android.widget.SeekBar; |
| 11 | +import android.widget.SeekBar.OnSeekBarChangeListener; |
| 12 | +import android.widget.TextView; |
| 13 | +import android.widget.Toast; |
| 14 | + |
| 15 | +import com.github.mikephil.charting.charts.LineChart; |
| 16 | +import com.github.mikephil.charting.components.Legend; |
| 17 | +import com.github.mikephil.charting.components.XAxis; |
| 18 | +import com.github.mikephil.charting.data.DataSet; |
| 19 | +import com.github.mikephil.charting.data.Entry; |
| 20 | +import com.github.mikephil.charting.data.LineData; |
| 21 | +import com.github.mikephil.charting.data.LineDataSet; |
| 22 | +import com.github.mikephil.charting.data.filter.Approximator; |
| 23 | +import com.github.mikephil.charting.data.filter.Approximator.ApproximatorType; |
| 24 | +import com.github.mikephil.charting.listener.OnChartValueSelectedListener; |
| 25 | +import com.github.mikephil.charting.utils.Highlight; |
| 26 | +import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase; |
| 27 | + |
| 28 | +import java.util.ArrayList; |
| 29 | + |
| 30 | +public class PerformanceLineChart extends DemoBase implements OnSeekBarChangeListener { |
| 31 | + |
| 32 | + private LineChart mChart; |
| 33 | + private SeekBar mSeekBarValues; |
| 34 | + private TextView mTvCount; |
| 35 | + |
| 36 | + @Override |
| 37 | + protected void onCreate(Bundle savedInstanceState) { |
| 38 | + super.onCreate(savedInstanceState); |
| 39 | + getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, |
| 40 | + WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| 41 | + setContentView(R.layout.activity_performance_linechart); |
| 42 | + |
| 43 | + mTvCount = (TextView) findViewById(R.id.tvValueCount); |
| 44 | + mSeekBarValues = (SeekBar) findViewById(R.id.seekbarValues); |
| 45 | + mTvCount.setText("500"); |
| 46 | + |
| 47 | + mSeekBarValues.setProgress(500); |
| 48 | + |
| 49 | + mSeekBarValues.setOnSeekBarChangeListener(this); |
| 50 | + |
| 51 | + mChart = (LineChart) findViewById(R.id.chart1); |
| 52 | + mChart.setDrawGridBackground(false); |
| 53 | + |
| 54 | + // no description text |
| 55 | + mChart.setDescription(""); |
| 56 | + mChart.setNoDataTextDescription("You need to provide data for the chart."); |
| 57 | + |
| 58 | + // disable value highlighting |
| 59 | + mChart.setHighlightEnabled(false); |
| 60 | + |
| 61 | + // enable touch gestures |
| 62 | + mChart.setTouchEnabled(true); |
| 63 | + |
| 64 | + // enable scaling and dragging |
| 65 | + mChart.setDragEnabled(true); |
| 66 | + mChart.setScaleEnabled(true); |
| 67 | + |
| 68 | + // if disabled, scaling can be done on x- and y-axis separately |
| 69 | + mChart.setPinchZoom(false); |
| 70 | + |
| 71 | + // enable/disable highlight indicators (the lines that indicate the |
| 72 | + // highlighted Entry) |
| 73 | + mChart.setHighlightIndicatorEnabled(false); |
| 74 | + |
| 75 | + mChart.getAxisLeft().setDrawGridLines(false); |
| 76 | + mChart.getAxisRight().setEnabled(false); |
| 77 | + mChart.getXAxis().setDrawGridLines(false); |
| 78 | + mChart.getXAxis().setDrawAxisLine(false); |
| 79 | + |
| 80 | + // dont forget to refresh the drawing |
| 81 | + mChart.invalidate(); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 86 | + getMenuInflater().inflate(R.menu.line, menu); |
| 87 | + return true; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 92 | + |
| 93 | + switch (item.getItemId()) { |
| 94 | + case R.id.actionToggleValues: { |
| 95 | + mChart.resetAverage(); |
| 96 | + break; |
| 97 | + } |
| 98 | + case R.id.actionToggleHighlight: { |
| 99 | + if (mChart.isHighlightEnabled()) |
| 100 | + mChart.setHighlightEnabled(false); |
| 101 | + else |
| 102 | + mChart.setHighlightEnabled(true); |
| 103 | + mChart.invalidate(); |
| 104 | + break; |
| 105 | + } |
| 106 | + case R.id.actionToggleFilled: { |
| 107 | + |
| 108 | + ArrayList<LineDataSet> sets = (ArrayList<LineDataSet>) mChart.getData() |
| 109 | + .getDataSets(); |
| 110 | + |
| 111 | + for (LineDataSet set : sets) { |
| 112 | + if (set.isDrawFilledEnabled()) |
| 113 | + set.setDrawFilled(false); |
| 114 | + else |
| 115 | + set.setDrawFilled(true); |
| 116 | + } |
| 117 | + mChart.invalidate(); |
| 118 | + break; |
| 119 | + } |
| 120 | + case R.id.actionToggleCircles: { |
| 121 | + ArrayList<LineDataSet> sets = (ArrayList<LineDataSet>) mChart.getData() |
| 122 | + .getDataSets(); |
| 123 | + |
| 124 | + for (LineDataSet set : sets) { |
| 125 | + if (set.isDrawCirclesEnabled()) |
| 126 | + set.setDrawCircles(false); |
| 127 | + else |
| 128 | + set.setDrawCircles(true); |
| 129 | + } |
| 130 | + mChart.invalidate(); |
| 131 | + break; |
| 132 | + } |
| 133 | + case R.id.actionToggleCubic: { |
| 134 | + ArrayList<LineDataSet> sets = (ArrayList<LineDataSet>) mChart.getData() |
| 135 | + .getDataSets(); |
| 136 | + |
| 137 | + for (LineDataSet set : sets) { |
| 138 | + if (set.isDrawCubicEnabled()) |
| 139 | + set.setDrawCubic(false); |
| 140 | + else |
| 141 | + set.setDrawCubic(true); |
| 142 | + } |
| 143 | + mChart.invalidate(); |
| 144 | + break; |
| 145 | + } |
| 146 | + case R.id.actionToggleStartzero: { |
| 147 | + mChart.getAxisLeft().setStartAtZero(!mChart.getAxisLeft().isStartAtZeroEnabled()); |
| 148 | + mChart.getAxisRight().setStartAtZero(!mChart.getAxisRight().isStartAtZeroEnabled()); |
| 149 | + mChart.invalidate(); |
| 150 | + break; |
| 151 | + } |
| 152 | + case R.id.actionTogglePinch: { |
| 153 | + if (mChart.isPinchZoomEnabled()) |
| 154 | + mChart.setPinchZoom(false); |
| 155 | + else |
| 156 | + mChart.setPinchZoom(true); |
| 157 | + |
| 158 | + mChart.invalidate(); |
| 159 | + break; |
| 160 | + } |
| 161 | + case R.id.animateX: { |
| 162 | + mChart.animateX(3000); |
| 163 | + break; |
| 164 | + } |
| 165 | + case R.id.animateY: { |
| 166 | + mChart.animateY(3000); |
| 167 | + break; |
| 168 | + } |
| 169 | + case R.id.animateXY: { |
| 170 | + mChart.animateXY(3000, 3000); |
| 171 | + break; |
| 172 | + } |
| 173 | + case R.id.actionToggleAdjustXLegend: { |
| 174 | + XAxis xLabels = mChart.getXAxis(); |
| 175 | + |
| 176 | + if (xLabels.isAdjustXLabelsEnabled()) |
| 177 | + xLabels.setAdjustXLabels(false); |
| 178 | + else |
| 179 | + xLabels.setAdjustXLabels(true); |
| 180 | + |
| 181 | + mChart.invalidate(); |
| 182 | + break; |
| 183 | + } |
| 184 | + case R.id.actionToggleFilter: { |
| 185 | + |
| 186 | + // the angle of filtering is 35° |
| 187 | + Approximator a = new Approximator(ApproximatorType.DOUGLAS_PEUCKER, 35); |
| 188 | + |
| 189 | + if (!mChart.isFilteringEnabled()) { |
| 190 | + mChart.enableFiltering(a); |
| 191 | + } else { |
| 192 | + mChart.disableFiltering(); |
| 193 | + } |
| 194 | + mChart.invalidate(); |
| 195 | + |
| 196 | + // |
| 197 | + // for(int i = 0; i < 10; i++) { |
| 198 | + // mChart.addEntry(new Entry((float) (Math.random() * 100), |
| 199 | + // i+2), 0); |
| 200 | + // mChart.invalidate(); |
| 201 | + // } |
| 202 | + // |
| 203 | + // Toast.makeText(getApplicationContext(), "valcount: " + |
| 204 | + // mChart.getDataOriginal().getYValCount() + ", valsum: " + |
| 205 | + // mChart.getDataOriginal().getYValueSum(), |
| 206 | + // Toast.LENGTH_SHORT).show(); |
| 207 | + // |
| 208 | + break; |
| 209 | + } |
| 210 | + case R.id.actionSave: { |
| 211 | + if (mChart.saveToPath("title" + System.currentTimeMillis(), "")) { |
| 212 | + Toast.makeText(getApplicationContext(), "Saving SUCCESSFUL!", |
| 213 | + Toast.LENGTH_SHORT).show(); |
| 214 | + } else |
| 215 | + Toast.makeText(getApplicationContext(), "Saving FAILED!", Toast.LENGTH_SHORT) |
| 216 | + .show(); |
| 217 | + |
| 218 | + // mChart.saveToGallery("title"+System.currentTimeMillis()) |
| 219 | + break; |
| 220 | + } |
| 221 | + } |
| 222 | + return true; |
| 223 | + } |
| 224 | + |
| 225 | + @Override |
| 226 | + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { |
| 227 | + |
| 228 | + mTvCount.setText("" + (mSeekBarValues.getProgress())); |
| 229 | + |
| 230 | + setData(mSeekBarValues.getProgress(), 500f); |
| 231 | + |
| 232 | + // redraw |
| 233 | + mChart.invalidate(); |
| 234 | + } |
| 235 | + |
| 236 | + @Override |
| 237 | + public void onStartTrackingTouch(SeekBar seekBar) { |
| 238 | + // TODO Auto-generated method stub |
| 239 | + |
| 240 | + } |
| 241 | + |
| 242 | + @Override |
| 243 | + public void onStopTrackingTouch(SeekBar seekBar) { |
| 244 | + // TODO Auto-generated method stub |
| 245 | + |
| 246 | + } |
| 247 | + |
| 248 | + private void setData(int count, float range) { |
| 249 | + |
| 250 | + ArrayList<String> xVals = new ArrayList<String>(); |
| 251 | + for (int i = 0; i < count; i++) { |
| 252 | + xVals.add((i) + ""); |
| 253 | + } |
| 254 | + |
| 255 | + ArrayList<Entry> yVals = new ArrayList<Entry>(); |
| 256 | + |
| 257 | + for (int i = 0; i < count; i++) { |
| 258 | + float mult = (range + 1); |
| 259 | + float val = (float) (Math.random() * mult) + 3;// + (float) |
| 260 | + // ((mult * |
| 261 | + // 0.1) / 10); |
| 262 | + yVals.add(new Entry(val, i)); |
| 263 | + } |
| 264 | + |
| 265 | + // create a dataset and give it a type |
| 266 | + LineDataSet set1 = new LineDataSet(yVals, "DataSet 1"); |
| 267 | + |
| 268 | + set1.setColor(Color.BLACK); |
| 269 | + set1.setLineWidth(1f); |
| 270 | + set1.setDrawValues(false); |
| 271 | + set1.setDrawCircles(false); |
| 272 | + |
| 273 | + // create a data object with the datasets |
| 274 | + LineData data = new LineData(xVals, set1); |
| 275 | + |
| 276 | + // set data |
| 277 | + mChart.setData(data); |
| 278 | + |
| 279 | + // get the legend (only possible after setting data) |
| 280 | + Legend l = mChart.getLegend(); |
| 281 | + l.setEnabled(false); |
| 282 | + } |
| 283 | +} |
0 commit comments