| |
| package com.xxmassdeveloper.mpchartexample; |
| |
| import android.graphics.Typeface; |
| import android.os.Bundle; |
| import android.util.Log; |
| import android.view.Menu; |
| import android.view.MenuItem; |
| import android.view.WindowManager; |
| import android.widget.SeekBar; |
| import android.widget.SeekBar.OnSeekBarChangeListener; |
| import android.widget.TextView; |
| |
| import com.github.mikephil.charting.charts.ScatterChart; |
| import com.github.mikephil.charting.charts.ScatterChart.ScatterShape; |
| import com.github.mikephil.charting.data.Entry; |
| import com.github.mikephil.charting.data.ScatterData; |
| import com.github.mikephil.charting.data.ScatterDataSet; |
| import com.github.mikephil.charting.data.filter.Approximator; |
| import com.github.mikephil.charting.data.filter.Approximator.ApproximatorType; |
| import com.github.mikephil.charting.interfaces.OnChartValueSelectedListener; |
| import com.github.mikephil.charting.utils.ColorTemplate; |
| import com.github.mikephil.charting.utils.Legend; |
| import com.github.mikephil.charting.utils.Legend.LegendPosition; |
| import com.github.mikephil.charting.utils.XLabels; |
| import com.github.mikephil.charting.utils.YLabels; |
| import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase; |
| |
| import java.util.ArrayList; |
| |
| public class ScatterChartActivity extends DemoBase implements OnSeekBarChangeListener, |
| OnChartValueSelectedListener { |
| |
| private ScatterChart mChart; |
| private SeekBar mSeekBarX, mSeekBarY; |
| private TextView tvX, tvY; |
| |
| @Override |
| protected void onCreate(Bundle savedInstanceState) { |
| super.onCreate(savedInstanceState); |
| getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, |
| WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| setContentView(R.layout.activity_scatterchart); |
| |
| tvX = (TextView) findViewById(R.id.tvXMax); |
| tvY = (TextView) findViewById(R.id.tvYMax); |
| |
| mSeekBarX = (SeekBar) findViewById(R.id.seekBar1); |
| mSeekBarX.setOnSeekBarChangeListener(this); |
| |
| mSeekBarY = (SeekBar) findViewById(R.id.seekBar2); |
| mSeekBarY.setOnSeekBarChangeListener(this); |
| |
| mChart = (ScatterChart) findViewById(R.id.chart1); |
| mChart.setDescription(""); |
| |
| Typeface tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf"); |
| mChart.setValueTypeface(tf); |
| |
| mChart.setOnChartValueSelectedListener(this); |
| |
| mChart.setDrawGridBackground(false); |
| |
| mChart.setTouchEnabled(true); |
| mChart.setHighlightEnabled(true); |
| mChart.setDrawYValues(false); |
| |
| mChart.setDragScaleEnabled(true); |
| |
| mChart.setMaxVisibleValueCount(200); |
| mChart.setPinchZoom(true); |
| |
| mSeekBarX.setProgress(45); |
| mSeekBarY.setProgress(100); |
| |
| Legend l = mChart.getLegend(); |
| l.setPosition(LegendPosition.RIGHT_OF_CHART); |
| l.setTypeface(tf); |
| |
| YLabels yl = mChart.getYLabels(); |
| yl.setTypeface(tf); |
| |
| XLabels xl = mChart.getXLabels(); |
| xl.setTypeface(tf); |
| } |
| |
| @Override |
| public boolean onCreateOptionsMenu(Menu menu) { |
| getMenuInflater().inflate(R.menu.scatter, menu); |
| return true; |
| } |
| |
| @Override |
| public boolean onOptionsItemSelected(MenuItem item) { |
| |
| switch (item.getItemId()) { |
| case R.id.actionToggleValues: { |
| if (mChart.isDrawYValuesEnabled()) |
| mChart.setDrawYValues(false); |
| else |
| mChart.setDrawYValues(true); |
| mChart.invalidate(); |
| break; |
| } |
| case R.id.actionToggleHighlight: { |
| if (mChart.isHighlightEnabled()) |
| mChart.setHighlightEnabled(false); |
| else |
| mChart.setHighlightEnabled(true); |
| mChart.invalidate(); |
| break; |
| } |
| case R.id.actionTogglePinch: { |
| if (mChart.isPinchZoomEnabled()) |
| mChart.setPinchZoom(false); |
| else |
| mChart.setPinchZoom(true); |
| |
| mChart.invalidate(); |
| break; |
| } |
| case R.id.actionToggleStartzero: { |
| if (mChart.isStartAtZeroEnabled()) |
| mChart.setStartAtZero(false); |
| else |
| mChart.setStartAtZero(true); |
| |
| mChart.invalidate(); |
| break; |
| } |
| case R.id.actionToggleAdjustXLegend: { |
| XLabels xLabels = mChart.getXLabels(); |
| |
| if (xLabels.isAdjustXLabelsEnabled()) |
| xLabels.setAdjustXLabels(false); |
| else |
| xLabels.setAdjustXLabels(true); |
| |
| mChart.invalidate(); |
| break; |
| } |
| case R.id.actionToggleFilter: { |
| |
| Approximator a = new Approximator(ApproximatorType.DOUGLAS_PEUCKER, 25); |
| |
| if (!mChart.isFilteringEnabled()) { |
| mChart.enableFiltering(a); |
| } else { |
| mChart.disableFiltering(); |
| } |
| mChart.invalidate(); |
| break; |
| } |
| case R.id.actionSave: { |
| // mChart.saveToGallery("title"+System.currentTimeMillis()); |
| mChart.saveToPath("title" + System.currentTimeMillis(), ""); |
| break; |
| } |
| case R.id.animateX: { |
| mChart.animateX(3000); |
| break; |
| } |
| case R.id.animateY: { |
| mChart.animateY(3000); |
| break; |
| } |
| case R.id.animateXY: { |
| |
| mChart.animateXY(3000, 3000); |
| break; |
| } |
| } |
| return true; |
| } |
| |
| @Override |
| public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { |
| |
| tvX.setText("" + (mSeekBarX.getProgress() + 1)); |
| tvY.setText("" + (mSeekBarY.getProgress())); |
| |
| ArrayList<String> xVals = new ArrayList<String>(); |
| for (int i = 0; i < mSeekBarX.getProgress(); i++) { |
| xVals.add((i) + ""); |
| } |
| |
| ArrayList<Entry> yVals1 = new ArrayList<Entry>(); |
| ArrayList<Entry> yVals2 = new ArrayList<Entry>(); |
| ArrayList<Entry> yVals3 = new ArrayList<Entry>(); |
| |
| for (int i = 0; i < mSeekBarX.getProgress(); i++) { |
| float val = (float) (Math.random() * mSeekBarY.getProgress()) + 3; |
| yVals1.add(new Entry(val, i)); |
| } |
| |
| for (int i = 0; i < mSeekBarX.getProgress(); i++) { |
| float val = (float) (Math.random() * mSeekBarY.getProgress()) + 3; |
| yVals2.add(new Entry(val, i)); |
| } |
| |
| for (int i = 0; i < mSeekBarX.getProgress(); i++) { |
| float val = (float) (Math.random() * mSeekBarY.getProgress()) + 3; |
| yVals3.add(new Entry(val, i)); |
| } |
| |
| // create a dataset and give it a type |
| ScatterDataSet set1 = new ScatterDataSet(yVals1, "DS 1"); |
| set1.setScatterShape(ScatterShape.SQUARE); |
| set1.setColor(ColorTemplate.COLORFUL_COLORS[0]); |
| ScatterDataSet set2 = new ScatterDataSet(yVals2, "DS 2"); |
| set2.setScatterShape(ScatterShape.CIRCLE); |
| set2.setColor(ColorTemplate.COLORFUL_COLORS[1]); |
| ScatterDataSet set3 = new ScatterDataSet(yVals3, "DS 3"); |
| set3.setScatterShape(ScatterShape.TRIANGLE); |
| set3.setColor(ColorTemplate.COLORFUL_COLORS[2]); |
| |
| set1.setScatterShapeSize(8f); |
| set2.setScatterShapeSize(8f); |
| set3.setScatterShapeSize(8f); |
| |
| ArrayList<ScatterDataSet> dataSets = new ArrayList<ScatterDataSet>(); |
| dataSets.add(set1); // add the datasets |
| dataSets.add(set2); |
| dataSets.add(set3); |
| |
| // create a data object with the datasets |
| ScatterData data = new ScatterData(xVals, dataSets); |
| |
| mChart.setData(data); |
| mChart.invalidate(); |
| } |
| |
| @Override |
| public void onValueSelected(Entry e, int dataSetIndex) { |
| Log.i("VAL SELECTED", |
| "Value: " + e.getVal() + ", xIndex: " + e.getXIndex() |
| + ", DataSet index: " + dataSetIndex); |
| } |
| |
| @Override |
| public void onNothingSelected() { |
| // TODO Auto-generated method stub |
| |
| } |
| |
| @Override |
| public void onStartTrackingTouch(SeekBar seekBar) { |
| // TODO Auto-generated method stub |
| |
| } |
| |
| @Override |
| public void onStopTrackingTouch(SeekBar seekBar) { |
| // TODO Auto-generated method stub |
| |
| } |
| } |