| |
| package com.xxmassdeveloper.mpchartexample; |
| |
| 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 android.widget.Toast; |
| |
| import com.github.mikephil.charting.charts.LineChart; |
| import com.github.mikephil.charting.components.Legend; |
| import com.github.mikephil.charting.components.Legend.LegendForm; |
| import com.github.mikephil.charting.components.XAxis; |
| import com.github.mikephil.charting.components.YAxis; |
| import com.github.mikephil.charting.data.Entry; |
| import com.github.mikephil.charting.data.LineData; |
| import com.github.mikephil.charting.data.LineDataSet; |
| import com.github.mikephil.charting.data.filter.Approximator; |
| import com.github.mikephil.charting.data.filter.Approximator.ApproximatorType; |
| import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; |
| import com.github.mikephil.charting.listener.OnChartValueSelectedListener; |
| import com.github.mikephil.charting.highlight.Highlight; |
| import com.xxmassdeveloper.mpchartexample.custom.MyMarkerView; |
| import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase; |
| |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| public class InvertedLineChartActivity extends DemoBase implements OnSeekBarChangeListener, |
| OnChartValueSelectedListener { |
| |
| private LineChart 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_linechart); |
| |
| tvX = (TextView) findViewById(R.id.tvXMax); |
| tvY = (TextView) findViewById(R.id.tvYMax); |
| |
| mSeekBarX = (SeekBar) findViewById(R.id.seekBar1); |
| mSeekBarY = (SeekBar) findViewById(R.id.seekBar2); |
| |
| mSeekBarX.setProgress(45); |
| mSeekBarY.setProgress(100); |
| |
| mSeekBarY.setOnSeekBarChangeListener(this); |
| mSeekBarX.setOnSeekBarChangeListener(this); |
| |
| mChart = (LineChart) findViewById(R.id.chart1); |
| mChart.setOnChartValueSelectedListener(this); |
| mChart.setDrawGridBackground(false); |
| |
| // no description text |
| mChart.setDescription(""); |
| |
| // enable touch gestures |
| mChart.setTouchEnabled(true); |
| |
| // enable scaling and dragging |
| mChart.setDragEnabled(true); |
| mChart.setScaleEnabled(true); |
| |
| // if disabled, scaling can be done on x- and y-axis separately |
| mChart.setPinchZoom(true); |
| |
| // set an alternative background color |
| // mChart.setBackgroundColor(Color.GRAY); |
| |
| // create a custom MarkerView (extend MarkerView) and specify the layout |
| // to use for it |
| MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker_view); |
| |
| // set the marker to the chart |
| mChart.setMarkerView(mv); |
| |
| XAxis xl = mChart.getXAxis(); |
| xl.setAvoidFirstLastClipping(true); |
| |
| YAxis leftAxis = mChart.getAxisLeft(); |
| leftAxis.setInverted(true); |
| |
| YAxis rightAxis = mChart.getAxisRight(); |
| rightAxis.setEnabled(false); |
| |
| // add data |
| setData(25, 50); |
| |
| // // restrain the maximum scale-out factor |
| // mChart.setScaleMinima(3f, 3f); |
| // |
| // // center the view to a specific position inside the chart |
| // mChart.centerViewPort(10, 50); |
| |
| // get the legend (only possible after setting data) |
| Legend l = mChart.getLegend(); |
| |
| // modify the legend ... |
| // l.setPosition(LegendPosition.LEFT_OF_CHART); |
| l.setForm(LegendForm.LINE); |
| |
| // dont forget to refresh the drawing |
| mChart.invalidate(); |
| } |
| |
| @Override |
| public boolean onCreateOptionsMenu(Menu menu) { |
| getMenuInflater().inflate(R.menu.line, menu); |
| return true; |
| } |
| |
| @Override |
| public boolean onOptionsItemSelected(MenuItem item) { |
| |
| switch (item.getItemId()) { |
| case R.id.actionToggleValues: { |
| List<ILineDataSet> sets = mChart.getData() |
| .getDataSets(); |
| |
| for (ILineDataSet iSet : sets) { |
| |
| LineDataSet set = (LineDataSet) iSet; |
| set.setDrawValues(!set.isDrawValuesEnabled()); |
| } |
| |
| mChart.invalidate(); |
| break; |
| } |
| case R.id.actionToggleHighlight: { |
| if(mChart.getData() != null) { |
| mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled()); |
| mChart.invalidate(); |
| } |
| break; |
| } |
| case R.id.actionToggleFilled: { |
| |
| List<ILineDataSet> sets = mChart.getData() |
| .getDataSets(); |
| |
| for (ILineDataSet iSet : sets) { |
| |
| LineDataSet set = (LineDataSet) iSet; |
| if (set.isDrawFilledEnabled()) |
| set.setDrawFilled(false); |
| else |
| set.setDrawFilled(true); |
| } |
| mChart.invalidate(); |
| break; |
| } |
| case R.id.actionToggleCircles: { |
| List<ILineDataSet> sets = mChart.getData() |
| .getDataSets(); |
| |
| for (ILineDataSet iSet : sets) { |
| |
| LineDataSet set = (LineDataSet) iSet; |
| if (set.isDrawCirclesEnabled()) |
| set.setDrawCircles(false); |
| else |
| set.setDrawCircles(true); |
| } |
| mChart.invalidate(); |
| break; |
| } |
| case R.id.actionToggleStartzero: { |
| mChart.getAxisLeft().setStartAtZero(!mChart.getAxisLeft().isStartAtZeroEnabled()); |
| mChart.getAxisRight().setStartAtZero(!mChart.getAxisRight().isStartAtZeroEnabled()); |
| mChart.invalidate(); |
| 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; |
| } |
| case R.id.actionTogglePinch: { |
| if (mChart.isPinchZoomEnabled()) |
| mChart.setPinchZoom(false); |
| else |
| mChart.setPinchZoom(true); |
| |
| mChart.invalidate(); |
| break; |
| } |
| case R.id.actionToggleAutoScaleMinMax: { |
| mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled()); |
| mChart.notifyDataSetChanged(); |
| break; |
| } |
| case R.id.actionToggleFilter: { |
| |
| // the angle of filtering is 35° |
| Approximator a = new Approximator(ApproximatorType.DOUGLAS_PEUCKER, 35); |
| |
| if (!mChart.isFilteringEnabled()) { |
| mChart.enableFiltering(a); |
| } else { |
| mChart.disableFiltering(); |
| } |
| mChart.invalidate(); |
| break; |
| } |
| case R.id.actionSave: { |
| if (mChart.saveToPath("title" + System.currentTimeMillis(), "")) { |
| Toast.makeText(getApplicationContext(), "Saving SUCCESSFUL!", |
| Toast.LENGTH_SHORT).show(); |
| } else |
| Toast.makeText(getApplicationContext(), "Saving FAILED!", Toast.LENGTH_SHORT) |
| .show(); |
| |
| // mChart.saveToGallery("title"+System.currentTimeMillis()) |
| break; |
| } |
| } |
| return true; |
| } |
| |
| @Override |
| public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { |
| |
| tvX.setText("" + (mSeekBarX.getProgress() + 1)); |
| tvY.setText("" + (mSeekBarY.getProgress())); |
| |
| setData(mSeekBarX.getProgress() + 1, mSeekBarY.getProgress()); |
| |
| // redraw |
| mChart.invalidate(); |
| } |
| |
| @Override |
| public void onValueSelected(Entry e, int dataSetIndex, Highlight h) { |
| 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 |
| |
| } |
| |
| private void setData(int count, float range) { |
| |
| ArrayList<String> xVals = new ArrayList<String>(); |
| for (int i = 0; i < count; i++) { |
| xVals.add((i % 30) + "/" + (i % 12) + "/14"); |
| } |
| |
| ArrayList<Entry> yVals = new ArrayList<Entry>(); |
| |
| for (int i = 0; i < count; i++) { |
| float mult = (range + 1); |
| float val = (float) (Math.random() * mult) + 3;// + (float) |
| // ((mult * |
| // 0.1) / 10); |
| yVals.add(new Entry(val, i)); |
| } |
| |
| // create a dataset and give it a type |
| LineDataSet set1 = new LineDataSet(yVals, "DataSet 1"); |
| |
| set1.setLineWidth(1.5f); |
| set1.setCircleRadius(4f); |
| |
| // create a data object with the datasets |
| LineData data = new LineData(xVals, set1); |
| |
| // set data |
| mChart.setData(data); |
| } |
| } |