|
| 1 | + |
| 2 | +package com.example.mpchartexample; |
| 3 | + |
| 4 | +import android.app.Activity; |
| 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 | + |
| 14 | +import com.github.mikephil.charting.charts.ScatterChart; |
| 15 | +import com.github.mikephil.charting.data.ChartData; |
| 16 | +import com.github.mikephil.charting.data.DataSet; |
| 17 | +import com.github.mikephil.charting.data.Entry; |
| 18 | +import com.github.mikephil.charting.data.filter.Approximator.ApproximatorType; |
| 19 | +import com.github.mikephil.charting.listener.OnChartValueSelectedListener; |
| 20 | +import com.github.mikephil.charting.utils.ColorTemplate; |
| 21 | +import com.github.mikephil.charting.utils.Highlight; |
| 22 | + |
| 23 | +import java.util.ArrayList; |
| 24 | + |
| 25 | +public class ScatterChartActivity extends Activity implements OnSeekBarChangeListener, |
| 26 | + OnChartValueSelectedListener { |
| 27 | + |
| 28 | + private ScatterChart mChart; |
| 29 | + private SeekBar mSeekBarX, mSeekBarY; |
| 30 | + private TextView tvX, tvY; |
| 31 | + |
| 32 | + @Override |
| 33 | + protected void onCreate(Bundle savedInstanceState) { |
| 34 | + super.onCreate(savedInstanceState); |
| 35 | + getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, |
| 36 | + WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| 37 | + setContentView(R.layout.activity_scatterchart); |
| 38 | + |
| 39 | + tvX = (TextView) findViewById(R.id.tvXMax); |
| 40 | + tvY = (TextView) findViewById(R.id.tvYMax); |
| 41 | + |
| 42 | + mSeekBarX = (SeekBar) findViewById(R.id.seekBar1); |
| 43 | + mSeekBarX.setOnSeekBarChangeListener(this); |
| 44 | + |
| 45 | + mSeekBarY = (SeekBar) findViewById(R.id.seekBar2); |
| 46 | + mSeekBarY.setOnSeekBarChangeListener(this); |
| 47 | + |
| 48 | + // create a color template for one dataset with only one color |
| 49 | + ColorTemplate ct = new ColorTemplate(); |
| 50 | + ct.addColorsForDataSets(new int[] { |
| 51 | + R.color.colorful_1, R.color.colorful_2, R.color.colorful_3 |
| 52 | + }, this); |
| 53 | + |
| 54 | + mChart = (ScatterChart) findViewById(R.id.chart1); |
| 55 | + mChart.setOnChartValueSelectedListener(this); |
| 56 | + mChart.setColorTemplate(ct); |
| 57 | + |
| 58 | + // mChart.setDrawFilled(true); |
| 59 | + // mChart.setRoundedYLegend(false); |
| 60 | + // mChart.setStartAtZero(true); |
| 61 | + // mChart.setSpacePercent(20, 10); |
| 62 | + mChart.setYLegendCount(6); |
| 63 | + mChart.setTouchEnabled(true); |
| 64 | + mChart.setHighlightEnabled(true); |
| 65 | + |
| 66 | + // highlight index 2 and 6 in dataset 0 |
| 67 | + // mChart.highlightValues(new Highlight[] {new Highlight(2, 0), new |
| 68 | + // Highlight(6, 0)}); |
| 69 | + mChart.setDragEnabled(true); |
| 70 | + |
| 71 | + mChart.setMaxVisibleValueCount(300); |
| 72 | + |
| 73 | + mSeekBarX.setProgress(45); |
| 74 | + mSeekBarY.setProgress(100); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 79 | + getMenuInflater().inflate(R.menu.scatter, menu); |
| 80 | + return true; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 85 | + |
| 86 | + switch (item.getItemId()) { |
| 87 | + case R.id.actionToggleValues: { |
| 88 | + if (mChart.isDrawYValuesEnabled()) |
| 89 | + mChart.setDrawYValues(false); |
| 90 | + else |
| 91 | + mChart.setDrawYValues(true); |
| 92 | + mChart.invalidate(); |
| 93 | + break; |
| 94 | + } |
| 95 | + case R.id.actionToggleHighlight: { |
| 96 | + if (mChart.isHighlightEnabled()) |
| 97 | + mChart.setHighlightEnabled(false); |
| 98 | + else |
| 99 | + mChart.setHighlightEnabled(true); |
| 100 | + mChart.invalidate(); |
| 101 | + break; |
| 102 | + } |
| 103 | + case R.id.actionToggleStartzero: { |
| 104 | + if (mChart.isStartAtZeroEnabled()) |
| 105 | + mChart.setStartAtZero(false); |
| 106 | + else |
| 107 | + mChart.setStartAtZero(true); |
| 108 | + |
| 109 | + mChart.invalidate(); |
| 110 | + break; |
| 111 | + } |
| 112 | + case R.id.actionToggleAdjustXLegend: { |
| 113 | + if (mChart.isAdjustXLegendEnabled()) |
| 114 | + mChart.setAdjustXLegend(false); |
| 115 | + else |
| 116 | + mChart.setAdjustXLegend(true); |
| 117 | + |
| 118 | + mChart.invalidate(); |
| 119 | + break; |
| 120 | + } |
| 121 | + case R.id.actionToggleFilter: |
| 122 | + if (mChart.isFilterSet()) { |
| 123 | + mChart.setFilter(ApproximatorType.NONE, 0); |
| 124 | + } else { |
| 125 | + mChart.setFilter(ApproximatorType.DOUGLAS_PEUCKER, 2); |
| 126 | + } |
| 127 | + mChart.invalidate(); |
| 128 | + break; |
| 129 | + case R.id.actionSave: { |
| 130 | + // mChart.saveToGallery("title"+System.currentTimeMillis()); |
| 131 | + mChart.saveToPath("title" + System.currentTimeMillis(), ""); |
| 132 | + break; |
| 133 | + } |
| 134 | + } |
| 135 | + return true; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { |
| 140 | + |
| 141 | + ArrayList<String> xVals = new ArrayList<String>(); |
| 142 | + for (int i = 0; i < mSeekBarX.getProgress(); i++) { |
| 143 | + xVals.add((i) + ""); |
| 144 | + } |
| 145 | + |
| 146 | + ArrayList<Entry> yVals1 = new ArrayList<Entry>(); |
| 147 | + ArrayList<Entry> yVals2 = new ArrayList<Entry>(); |
| 148 | + ArrayList<Entry> yVals3 = new ArrayList<Entry>(); |
| 149 | + |
| 150 | + for (int i = 0; i < mSeekBarX.getProgress(); i++) { |
| 151 | + float mult = (mSeekBarY.getProgress() + 1); |
| 152 | + float val = (float) (Math.random() * mult * 0.1) + 3;// + (float) |
| 153 | + // ((mult * |
| 154 | + // 0.1) / 10); |
| 155 | + yVals1.add(new Entry(val, i)); |
| 156 | + } |
| 157 | + |
| 158 | + for (int i = 0; i < mSeekBarX.getProgress(); i++) { |
| 159 | + float mult = (mSeekBarY.getProgress() + 1); |
| 160 | + float val = (float) (Math.random() * mult * 0.1) + 3;// + (float) |
| 161 | + // ((mult * |
| 162 | + // 0.1) / 10); |
| 163 | + yVals2.add(new Entry(val, i)); |
| 164 | + } |
| 165 | + |
| 166 | + for (int i = 0; i < mSeekBarX.getProgress(); i++) { |
| 167 | + float mult = (mSeekBarY.getProgress() + 1); |
| 168 | + float val = (float) (Math.random() * mult * 0.1) + 3;// + (float) |
| 169 | + // ((mult * |
| 170 | + // 0.1) / 10); |
| 171 | + yVals3.add(new Entry(val, i)); |
| 172 | + } |
| 173 | + |
| 174 | + tvX.setText("" + (mSeekBarX.getProgress() + 1)); |
| 175 | + tvY.setText("" + (mSeekBarY.getProgress() / 10)); |
| 176 | + |
| 177 | + // create a dataset and give it a type (0) |
| 178 | + DataSet set1 = new DataSet(yVals1, 0); |
| 179 | + DataSet set2 = new DataSet(yVals2, 1); |
| 180 | + DataSet set3 = new DataSet(yVals3, 2); |
| 181 | + |
| 182 | + ArrayList<DataSet> dataSets = new ArrayList<DataSet>(); |
| 183 | + dataSets.add(set1); // add the datasets |
| 184 | + dataSets.add(set2); |
| 185 | + dataSets.add(set3); |
| 186 | + |
| 187 | + // create a data object with the datasets |
| 188 | + ChartData data = new ChartData(xVals, dataSets); |
| 189 | + |
| 190 | + mChart.setData(data); |
| 191 | + mChart.invalidate(); |
| 192 | + } |
| 193 | + |
| 194 | + @Override |
| 195 | + public void onValuesSelected(Entry[] values, Highlight[] highlights) { |
| 196 | + Log.i("VALS SELECTED", |
| 197 | + "Value: " + values[0].getVal() + ", xIndex: " + highlights[0].getXIndex() |
| 198 | + + ", DataSet index: " + highlights[0].getDataSetIndex()); |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public void onNothingSelected() { |
| 203 | + // TODO Auto-generated method stub |
| 204 | + |
| 205 | + } |
| 206 | + |
| 207 | + @Override |
| 208 | + public void onStartTrackingTouch(SeekBar seekBar) { |
| 209 | + // TODO Auto-generated method stub |
| 210 | + |
| 211 | + } |
| 212 | + |
| 213 | + @Override |
| 214 | + public void onStopTrackingTouch(SeekBar seekBar) { |
| 215 | + // TODO Auto-generated method stub |
| 216 | + |
| 217 | + } |
| 218 | +} |
0 commit comments