|
| 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.BarLineChartBase.BorderPosition; |
| 17 | +import com.github.mikephil.charting.charts.LineChart; |
| 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.data.filter.Approximator; |
| 22 | +import com.github.mikephil.charting.data.filter.Approximator.ApproximatorType; |
| 23 | +import com.github.mikephil.charting.interfaces.OnChartValueSelectedListener; |
| 24 | +import com.github.mikephil.charting.utils.ColorTemplate; |
| 25 | +import com.github.mikephil.charting.utils.Legend; |
| 26 | +import com.github.mikephil.charting.utils.Legend.LegendForm; |
| 27 | +import com.github.mikephil.charting.utils.XLabels; |
| 28 | +import com.github.mikephil.charting.utils.YLabels; |
| 29 | +import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase; |
| 30 | + |
| 31 | +import java.util.ArrayList; |
| 32 | + |
| 33 | +public class LineChartActivity2 extends DemoBase implements OnSeekBarChangeListener, |
| 34 | + OnChartValueSelectedListener { |
| 35 | + |
| 36 | + private LineChart mChart; |
| 37 | + private SeekBar mSeekBarX, mSeekBarY; |
| 38 | + private TextView tvX, tvY; |
| 39 | + |
| 40 | + @Override |
| 41 | + protected void onCreate(Bundle savedInstanceState) { |
| 42 | + super.onCreate(savedInstanceState); |
| 43 | + getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, |
| 44 | + WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| 45 | + setContentView(R.layout.activity_linechart); |
| 46 | + |
| 47 | + tvX = (TextView) findViewById(R.id.tvXMax); |
| 48 | + tvY = (TextView) findViewById(R.id.tvYMax); |
| 49 | + mSeekBarX = (SeekBar) findViewById(R.id.seekBar1); |
| 50 | + mSeekBarY = (SeekBar) findViewById(R.id.seekBar2); |
| 51 | + |
| 52 | + mSeekBarX.setProgress(45); |
| 53 | + mSeekBarY.setProgress(100); |
| 54 | + |
| 55 | + mSeekBarY.setOnSeekBarChangeListener(this); |
| 56 | + mSeekBarX.setOnSeekBarChangeListener(this); |
| 57 | + |
| 58 | + mChart = (LineChart) findViewById(R.id.chart1); |
| 59 | + mChart.setOnChartValueSelectedListener(this); |
| 60 | + |
| 61 | + mChart.setUnit(" $"); |
| 62 | + mChart.setDrawUnitsInChart(true); |
| 63 | + |
| 64 | + // if enabled, the chart will always start at zero on the y-axis |
| 65 | + mChart.setStartAtZero(false); |
| 66 | + |
| 67 | + // disable the drawing of values into the chart |
| 68 | + mChart.setDrawYValues(false); |
| 69 | + |
| 70 | + mChart.setDrawBorder(true); |
| 71 | + mChart.setBorderPositions(new BorderPosition[] { |
| 72 | + BorderPosition.BOTTOM |
| 73 | + }); |
| 74 | + |
| 75 | + // no description text |
| 76 | + mChart.setDescription(""); |
| 77 | + mChart.setNoDataTextDescription("You need to provide data for the chart."); |
| 78 | + |
| 79 | + // enable value highlighting |
| 80 | + mChart.setHighlightEnabled(true); |
| 81 | + |
| 82 | + // enable touch gestures |
| 83 | + mChart.setTouchEnabled(true); |
| 84 | + |
| 85 | + // enable scaling and dragging |
| 86 | + mChart.setDragEnabled(true); |
| 87 | + mChart.setScaleEnabled(true); |
| 88 | + mChart.setDrawGridBackground(false); |
| 89 | + mChart.setDrawVerticalGrid(false); |
| 90 | + mChart.setDrawHorizontalGrid(false); |
| 91 | + |
| 92 | + // if disabled, scaling can be done on x- and y-axis separately |
| 93 | + mChart.setPinchZoom(true); |
| 94 | + |
| 95 | + // set an alternative background color |
| 96 | + mChart.setBackgroundColor(Color.GRAY); |
| 97 | + |
| 98 | + // add data |
| 99 | + setData(45, 100); |
| 100 | + |
| 101 | + mChart.animateX(2500); |
| 102 | + |
| 103 | + Typeface tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf"); |
| 104 | + |
| 105 | + // get the legend (only possible after setting data) |
| 106 | + Legend l = mChart.getLegend(); |
| 107 | + |
| 108 | + // modify the legend ... |
| 109 | + // l.setPosition(LegendPosition.LEFT_OF_CHART); |
| 110 | + l.setForm(LegendForm.LINE); |
| 111 | + l.setTypeface(tf); |
| 112 | + l.setTextColor(Color.WHITE); |
| 113 | + |
| 114 | + XLabels xl = mChart.getXLabels(); |
| 115 | + xl.setTypeface(tf); |
| 116 | + xl.setTextColor(Color.WHITE); |
| 117 | + |
| 118 | + YLabels yl = mChart.getYLabels(); |
| 119 | + yl.setTypeface(tf); |
| 120 | + yl.setTextColor(Color.WHITE); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 125 | + getMenuInflater().inflate(R.menu.line, menu); |
| 126 | + return true; |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 131 | + |
| 132 | + switch (item.getItemId()) { |
| 133 | + case R.id.actionToggleValues: { |
| 134 | + if (mChart.isDrawYValuesEnabled()) |
| 135 | + mChart.setDrawYValues(false); |
| 136 | + else |
| 137 | + mChart.setDrawYValues(true); |
| 138 | + mChart.invalidate(); |
| 139 | + break; |
| 140 | + } |
| 141 | + case R.id.actionToggleHighlight: { |
| 142 | + if (mChart.isHighlightEnabled()) |
| 143 | + mChart.setHighlightEnabled(false); |
| 144 | + else |
| 145 | + mChart.setHighlightEnabled(true); |
| 146 | + mChart.invalidate(); |
| 147 | + break; |
| 148 | + } |
| 149 | + case R.id.actionToggleFilled: { |
| 150 | + |
| 151 | + ArrayList<LineDataSet> sets = (ArrayList<LineDataSet>) mChart.getData() |
| 152 | + .getDataSets(); |
| 153 | + |
| 154 | + for (LineDataSet set : sets) { |
| 155 | + if (set.isDrawFilledEnabled()) |
| 156 | + set.setDrawFilled(false); |
| 157 | + else |
| 158 | + set.setDrawFilled(true); |
| 159 | + } |
| 160 | + mChart.invalidate(); |
| 161 | + break; |
| 162 | + } |
| 163 | + case R.id.actionToggleCircles: { |
| 164 | + ArrayList<LineDataSet> sets = (ArrayList<LineDataSet>) mChart.getData() |
| 165 | + .getDataSets(); |
| 166 | + |
| 167 | + for (LineDataSet set : sets) { |
| 168 | + if (set.isDrawCirclesEnabled()) |
| 169 | + set.setDrawCircles(false); |
| 170 | + else |
| 171 | + set.setDrawCircles(true); |
| 172 | + } |
| 173 | + mChart.invalidate(); |
| 174 | + break; |
| 175 | + } |
| 176 | + case R.id.actionToggleCubic: { |
| 177 | + ArrayList<LineDataSet> sets = (ArrayList<LineDataSet>) mChart.getData() |
| 178 | + .getDataSets(); |
| 179 | + |
| 180 | + for (LineDataSet set : sets) { |
| 181 | + if (set.isDrawCubicEnabled()) |
| 182 | + set.setDrawCubic(false); |
| 183 | + else |
| 184 | + set.setDrawCubic(true); |
| 185 | + } |
| 186 | + mChart.invalidate(); |
| 187 | + break; |
| 188 | + } |
| 189 | + case R.id.actionToggleStartzero: { |
| 190 | + if (mChart.isStartAtZeroEnabled()) |
| 191 | + mChart.setStartAtZero(false); |
| 192 | + else |
| 193 | + mChart.setStartAtZero(true); |
| 194 | + |
| 195 | + mChart.invalidate(); |
| 196 | + break; |
| 197 | + } |
| 198 | + case R.id.actionTogglePinch: { |
| 199 | + if (mChart.isPinchZoomEnabled()) |
| 200 | + mChart.setPinchZoom(false); |
| 201 | + else |
| 202 | + mChart.setPinchZoom(true); |
| 203 | + |
| 204 | + mChart.invalidate(); |
| 205 | + break; |
| 206 | + } |
| 207 | + case R.id.animateX: { |
| 208 | + mChart.animateX(3000); |
| 209 | + break; |
| 210 | + } |
| 211 | + case R.id.animateY: { |
| 212 | + mChart.animateY(3000); |
| 213 | + break; |
| 214 | + } |
| 215 | + case R.id.animateXY: { |
| 216 | + mChart.animateXY(3000, 3000); |
| 217 | + break; |
| 218 | + } |
| 219 | + case R.id.actionToggleAdjustXLegend: { |
| 220 | + XLabels xLabels = mChart.getXLabels(); |
| 221 | + |
| 222 | + if (xLabels.isAdjustXLabelsEnabled()) |
| 223 | + xLabels.setAdjustXLabels(false); |
| 224 | + else |
| 225 | + xLabels.setAdjustXLabels(true); |
| 226 | + |
| 227 | + mChart.invalidate(); |
| 228 | + break; |
| 229 | + } |
| 230 | + case R.id.actionToggleFilter: { |
| 231 | + |
| 232 | + // the angle of filtering is 35° |
| 233 | + Approximator a = new Approximator(ApproximatorType.DOUGLAS_PEUCKER, 35); |
| 234 | + |
| 235 | + if (!mChart.isFilteringEnabled()) { |
| 236 | + mChart.enableFiltering(a); |
| 237 | + } else { |
| 238 | + mChart.disableFiltering(); |
| 239 | + } |
| 240 | + mChart.invalidate(); |
| 241 | + break; |
| 242 | + } |
| 243 | + case R.id.actionSave: { |
| 244 | + if (mChart.saveToPath("title" + System.currentTimeMillis(), "")) { |
| 245 | + Toast.makeText(getApplicationContext(), "Saving SUCCESSFUL!", |
| 246 | + Toast.LENGTH_SHORT).show(); |
| 247 | + } else |
| 248 | + Toast.makeText(getApplicationContext(), "Saving FAILED!", Toast.LENGTH_SHORT) |
| 249 | + .show(); |
| 250 | + |
| 251 | + // mChart.saveToGallery("title"+System.currentTimeMillis()) |
| 252 | + break; |
| 253 | + } |
| 254 | + } |
| 255 | + return true; |
| 256 | + } |
| 257 | + |
| 258 | + @Override |
| 259 | + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { |
| 260 | + |
| 261 | + tvX.setText("" + (mSeekBarX.getProgress() + 1)); |
| 262 | + tvY.setText("" + (mSeekBarY.getProgress())); |
| 263 | + |
| 264 | + setData(mSeekBarX.getProgress() + 1, mSeekBarY.getProgress()); |
| 265 | + |
| 266 | + // redraw |
| 267 | + mChart.invalidate(); |
| 268 | + } |
| 269 | + |
| 270 | + private void setData(int count, float range) { |
| 271 | + |
| 272 | + ArrayList<String> xVals = new ArrayList<String>(); |
| 273 | + for (int i = 0; i < count; i++) { |
| 274 | + xVals.add((i) + ""); |
| 275 | + } |
| 276 | + |
| 277 | + ArrayList<Entry> yVals = new ArrayList<Entry>(); |
| 278 | + |
| 279 | + for (int i = 0; i < count; i++) { |
| 280 | + float mult = (range + 1); |
| 281 | + float val = (float) (Math.random() * mult) + 3;// + (float) |
| 282 | + // ((mult * |
| 283 | + // 0.1) / 10); |
| 284 | + yVals.add(new Entry(val, i)); |
| 285 | + } |
| 286 | + |
| 287 | + // create a dataset and give it a type |
| 288 | + LineDataSet set1 = new LineDataSet(yVals, "DataSet 1"); |
| 289 | + set1.setColor(ColorTemplate.getHoloBlue()); |
| 290 | + set1.setCircleColor(ColorTemplate.getHoloBlue()); |
| 291 | + set1.setLineWidth(2f); |
| 292 | + set1.setCircleSize(4f); |
| 293 | + set1.setFillAlpha(65); |
| 294 | + set1.setFillColor(ColorTemplate.getHoloBlue()); |
| 295 | + set1.setHighLightColor(Color.rgb(244, 117, 117)); |
| 296 | + |
| 297 | + ArrayList<LineDataSet> dataSets = new ArrayList<LineDataSet>(); |
| 298 | + dataSets.add(set1); // add the datasets |
| 299 | + |
| 300 | + // create a data object with the datasets |
| 301 | + LineData data = new LineData(xVals, dataSets); |
| 302 | + |
| 303 | + // set data |
| 304 | + mChart.setData(data); |
| 305 | + } |
| 306 | + |
| 307 | + @Override |
| 308 | + public void onValueSelected(Entry e, int dataSetIndex) { |
| 309 | + Log.i("Entry selected", e.toString()); |
| 310 | + } |
| 311 | + |
| 312 | + @Override |
| 313 | + public void onNothingSelected() { |
| 314 | + Log.i("Nothing selected", "Nothing selected."); |
| 315 | + } |
| 316 | + |
| 317 | + @Override |
| 318 | + public void onStartTrackingTouch(SeekBar seekBar) { |
| 319 | + // TODO Auto-generated method stub |
| 320 | + |
| 321 | + } |
| 322 | + |
| 323 | + @Override |
| 324 | + public void onStopTrackingTouch(SeekBar seekBar) { |
| 325 | + // TODO Auto-generated method stub |
| 326 | + |
| 327 | + } |
| 328 | +} |
0 commit comments