|
| 1 | + |
| 2 | +package com.xxmassdeveloper.mpchartexample; |
| 3 | + |
| 4 | +import android.graphics.Color; |
| 5 | +import android.graphics.Typeface; |
| 6 | +import android.os.Bundle; |
| 7 | +import android.util.Log; |
| 8 | +import android.view.Menu; |
| 9 | +import android.view.MenuItem; |
| 10 | +import android.view.WindowManager; |
| 11 | +import android.widget.SeekBar; |
| 12 | +import android.widget.SeekBar.OnSeekBarChangeListener; |
| 13 | +import android.widget.TextView; |
| 14 | +import android.widget.Toast; |
| 15 | + |
| 16 | +import com.github.mikephil.charting.charts.LineChart; |
| 17 | +import com.github.mikephil.charting.components.Legend; |
| 18 | +import com.github.mikephil.charting.components.Legend.LegendForm; |
| 19 | +import com.github.mikephil.charting.components.Legend.LegendPosition; |
| 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.data.XAxisValue; |
| 27 | +import com.github.mikephil.charting.highlight.Highlight; |
| 28 | +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; |
| 29 | +import com.github.mikephil.charting.listener.OnChartValueSelectedListener; |
| 30 | +import com.github.mikephil.charting.utils.ColorTemplate; |
| 31 | +import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase; |
| 32 | + |
| 33 | +import java.util.ArrayList; |
| 34 | +import java.util.Date; |
| 35 | +import java.util.List; |
| 36 | + |
| 37 | +public class LineChartTime extends DemoBase implements OnSeekBarChangeListener { |
| 38 | + |
| 39 | + private LineChart mChart; |
| 40 | + private SeekBar mSeekBarX, mSeekBarY; |
| 41 | + private TextView tvX, tvY; |
| 42 | + |
| 43 | + @Override |
| 44 | + protected void onCreate(Bundle savedInstanceState) { |
| 45 | + super.onCreate(savedInstanceState); |
| 46 | + getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, |
| 47 | + WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| 48 | + setContentView(R.layout.activity_linechart); |
| 49 | + |
| 50 | + tvX = (TextView) findViewById(R.id.tvXMax); |
| 51 | + tvY = (TextView) findViewById(R.id.tvYMax); |
| 52 | + mSeekBarX = (SeekBar) findViewById(R.id.seekBar1); |
| 53 | + mSeekBarY = (SeekBar) findViewById(R.id.seekBar2); |
| 54 | + |
| 55 | + mSeekBarX.setProgress(45); |
| 56 | + mSeekBarY.setProgress(100); |
| 57 | + |
| 58 | + mSeekBarY.setOnSeekBarChangeListener(this); |
| 59 | + mSeekBarX.setOnSeekBarChangeListener(this); |
| 60 | + |
| 61 | + mChart = (LineChart) findViewById(R.id.chart1); |
| 62 | + |
| 63 | + // no description text |
| 64 | + mChart.setDescription(""); |
| 65 | + mChart.setNoDataTextDescription("You need to provide data for the chart."); |
| 66 | + |
| 67 | + // enable touch gestures |
| 68 | + mChart.setTouchEnabled(true); |
| 69 | + |
| 70 | + mChart.setDragDecelerationFrictionCoef(0.9f); |
| 71 | + |
| 72 | + // enable scaling and dragging |
| 73 | + mChart.setDragEnabled(true); |
| 74 | + mChart.setScaleEnabled(true); |
| 75 | + mChart.setDrawGridBackground(false); |
| 76 | + mChart.setHighlightPerDragEnabled(true); |
| 77 | + |
| 78 | + // if disabled, scaling can be done on x- and y-axis separately |
| 79 | + mChart.setPinchZoom(true); |
| 80 | + |
| 81 | + // set an alternative background color |
| 82 | + mChart.setBackgroundColor(Color.LTGRAY); |
| 83 | + |
| 84 | + // add data |
| 85 | + setData(20, 30); |
| 86 | + |
| 87 | + mChart.animateX(2500); |
| 88 | + |
| 89 | + Typeface tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf"); |
| 90 | + |
| 91 | + // get the legend (only possible after setting data) |
| 92 | + Legend l = mChart.getLegend(); |
| 93 | + |
| 94 | + // modify the legend ... |
| 95 | + // l.setPosition(LegendPosition.LEFT_OF_CHART); |
| 96 | + l.setForm(LegendForm.LINE); |
| 97 | + l.setTypeface(tf); |
| 98 | + l.setTextSize(11f); |
| 99 | + l.setTextColor(Color.WHITE); |
| 100 | + l.setPosition(LegendPosition.BELOW_CHART_LEFT); |
| 101 | +// l.setYOffset(11f); |
| 102 | + |
| 103 | + XAxis xAxis = mChart.getXAxis(); |
| 104 | + xAxis.setTypeface(tf); |
| 105 | + xAxis.setTextSize(12f); |
| 106 | + xAxis.setTextColor(Color.WHITE); |
| 107 | + xAxis.setDrawAxisLine(false); |
| 108 | + xAxis.setSpaceBetweenLabels(1); |
| 109 | + |
| 110 | + YAxis leftAxis = mChart.getAxisLeft(); |
| 111 | + leftAxis.setTypeface(tf); |
| 112 | + leftAxis.setTextColor(ColorTemplate.getHoloBlue()); |
| 113 | + leftAxis.setAxisMaxValue(200f); |
| 114 | + leftAxis.setAxisMinValue(0f); |
| 115 | + leftAxis.setDrawGridLines(true); |
| 116 | + leftAxis.setGranularityEnabled(true); |
| 117 | + |
| 118 | + YAxis rightAxis = mChart.getAxisRight(); |
| 119 | + rightAxis.setEnabled(false); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 124 | + getMenuInflater().inflate(R.menu.line, menu); |
| 125 | + return true; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 130 | + |
| 131 | + switch (item.getItemId()) { |
| 132 | + case R.id.actionToggleValues: { |
| 133 | + List<ILineDataSet> sets = mChart.getData() |
| 134 | + .getDataSets(); |
| 135 | + |
| 136 | + for (ILineDataSet iSet : sets) { |
| 137 | + |
| 138 | + LineDataSet set = (LineDataSet) iSet; |
| 139 | + set.setDrawValues(!set.isDrawValuesEnabled()); |
| 140 | + } |
| 141 | + |
| 142 | + mChart.invalidate(); |
| 143 | + break; |
| 144 | + } |
| 145 | + case R.id.actionToggleHighlight: { |
| 146 | + if (mChart.getData() != null) { |
| 147 | + mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled()); |
| 148 | + mChart.invalidate(); |
| 149 | + } |
| 150 | + break; |
| 151 | + } |
| 152 | + case R.id.actionToggleFilled: { |
| 153 | + |
| 154 | + List<ILineDataSet> sets = mChart.getData() |
| 155 | + .getDataSets(); |
| 156 | + |
| 157 | + for (ILineDataSet iSet : sets) { |
| 158 | + |
| 159 | + LineDataSet set = (LineDataSet) iSet; |
| 160 | + if (set.isDrawFilledEnabled()) |
| 161 | + set.setDrawFilled(false); |
| 162 | + else |
| 163 | + set.setDrawFilled(true); |
| 164 | + } |
| 165 | + mChart.invalidate(); |
| 166 | + break; |
| 167 | + } |
| 168 | + case R.id.actionToggleCircles: { |
| 169 | + List<ILineDataSet> sets = mChart.getData() |
| 170 | + .getDataSets(); |
| 171 | + |
| 172 | + for (ILineDataSet iSet : sets) { |
| 173 | + |
| 174 | + LineDataSet set = (LineDataSet) iSet; |
| 175 | + if (set.isDrawCirclesEnabled()) |
| 176 | + set.setDrawCircles(false); |
| 177 | + else |
| 178 | + set.setDrawCircles(true); |
| 179 | + } |
| 180 | + mChart.invalidate(); |
| 181 | + break; |
| 182 | + } |
| 183 | + case R.id.actionToggleCubic: { |
| 184 | + List<ILineDataSet> sets = mChart.getData() |
| 185 | + .getDataSets(); |
| 186 | + |
| 187 | + for (ILineDataSet iSet : sets) { |
| 188 | + |
| 189 | + LineDataSet set = (LineDataSet) iSet; |
| 190 | + if (set.isDrawCubicEnabled()) |
| 191 | + set.setDrawCubic(false); |
| 192 | + else |
| 193 | + set.setDrawCubic(true); |
| 194 | + } |
| 195 | + mChart.invalidate(); |
| 196 | + break; |
| 197 | + } |
| 198 | + case R.id.actionToggleStepped: { |
| 199 | + List<ILineDataSet> sets = mChart.getData() |
| 200 | + .getDataSets(); |
| 201 | + |
| 202 | + for (ILineDataSet iSet : sets) { |
| 203 | + |
| 204 | + LineDataSet set = (LineDataSet) iSet; |
| 205 | + if (set.isDrawSteppedEnabled()) |
| 206 | + set.setDrawStepped(false); |
| 207 | + else |
| 208 | + set.setDrawStepped(true); |
| 209 | + } |
| 210 | + mChart.invalidate(); |
| 211 | + break; |
| 212 | + } |
| 213 | + case R.id.actionTogglePinch: { |
| 214 | + if (mChart.isPinchZoomEnabled()) |
| 215 | + mChart.setPinchZoom(false); |
| 216 | + else |
| 217 | + mChart.setPinchZoom(true); |
| 218 | + |
| 219 | + mChart.invalidate(); |
| 220 | + break; |
| 221 | + } |
| 222 | + case R.id.actionToggleAutoScaleMinMax: { |
| 223 | + mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled()); |
| 224 | + mChart.notifyDataSetChanged(); |
| 225 | + break; |
| 226 | + } |
| 227 | + case R.id.animateX: { |
| 228 | + mChart.animateX(3000); |
| 229 | + break; |
| 230 | + } |
| 231 | + case R.id.animateY: { |
| 232 | + mChart.animateY(3000); |
| 233 | + break; |
| 234 | + } |
| 235 | + case R.id.animateXY: { |
| 236 | + mChart.animateXY(3000, 3000); |
| 237 | + break; |
| 238 | + } |
| 239 | + |
| 240 | + case R.id.actionSave: { |
| 241 | + if (mChart.saveToPath("title" + System.currentTimeMillis(), "")) { |
| 242 | + Toast.makeText(getApplicationContext(), "Saving SUCCESSFUL!", |
| 243 | + Toast.LENGTH_SHORT).show(); |
| 244 | + } else |
| 245 | + Toast.makeText(getApplicationContext(), "Saving FAILED!", Toast.LENGTH_SHORT) |
| 246 | + .show(); |
| 247 | + |
| 248 | + // mChart.saveToGallery("title"+System.currentTimeMillis()) |
| 249 | + break; |
| 250 | + } |
| 251 | + } |
| 252 | + return true; |
| 253 | + } |
| 254 | + |
| 255 | + @Override |
| 256 | + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { |
| 257 | + |
| 258 | + tvX.setText("" + (mSeekBarX.getProgress() + 1)); |
| 259 | + tvY.setText("" + (mSeekBarY.getProgress())); |
| 260 | + |
| 261 | + setData(mSeekBarX.getProgress() + 1, mSeekBarY.getProgress()); |
| 262 | + |
| 263 | + // redraw |
| 264 | + mChart.invalidate(); |
| 265 | + } |
| 266 | + |
| 267 | + private void setData(int count, float range) { |
| 268 | + |
| 269 | + ArrayList<XAxisValue> xVals = new ArrayList<XAxisValue>(); |
| 270 | + for (int i = 0; i < count; i++) { |
| 271 | + xVals.add(new XAxisValue(i, i + "")); |
| 272 | + } |
| 273 | + |
| 274 | + ArrayList<Entry> yVals1 = new ArrayList<Entry>(); |
| 275 | + |
| 276 | + for (int i = 0; i < count; i++) { |
| 277 | + float mult = range / 2f; |
| 278 | + float val = (float) (Math.random() * mult) + 50;// + (float) |
| 279 | + // ((mult * |
| 280 | + // 0.1) / 10); |
| 281 | + yVals1.add(new Entry(val, i)); |
| 282 | + } |
| 283 | + |
| 284 | + // create a dataset and give it a type |
| 285 | + LineDataSet set1 = new LineDataSet(yVals1, "DataSet 1"); |
| 286 | + set1.setAxisDependency(AxisDependency.LEFT); |
| 287 | + set1.setColor(ColorTemplate.getHoloBlue()); |
| 288 | + set1.setCircleColor(Color.WHITE); |
| 289 | + set1.setLineWidth(2f); |
| 290 | + set1.setCircleRadius(3f); |
| 291 | + set1.setFillAlpha(65); |
| 292 | + set1.setFillColor(ColorTemplate.getHoloBlue()); |
| 293 | + set1.setHighLightColor(Color.rgb(244, 117, 117)); |
| 294 | + set1.setDrawCircleHole(false); |
| 295 | + |
| 296 | + // create a data object with the datasets |
| 297 | + LineData data = new LineData(xVals, set1); |
| 298 | + data.setValueTextColor(Color.WHITE); |
| 299 | + data.setValueTextSize(9f); |
| 300 | + |
| 301 | + // set data |
| 302 | + mChart.setData(data); |
| 303 | + } |
| 304 | + |
| 305 | + @Override |
| 306 | + public void onStartTrackingTouch(SeekBar seekBar) { |
| 307 | + // TODO Auto-generated method stub |
| 308 | + |
| 309 | + } |
| 310 | + |
| 311 | + @Override |
| 312 | + public void onStopTrackingTouch(SeekBar seekBar) { |
| 313 | + // TODO Auto-generated method stub |
| 314 | + |
| 315 | + } |
| 316 | +} |
0 commit comments