| |
| package com.example.mpchartexample; |
| |
| import android.app.Activity; |
| import android.graphics.Color; |
| 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.BarLineChartBase.BorderStyle; |
| import com.github.mikephil.charting.charts.LineChart; |
| import com.github.mikephil.charting.data.ChartData; |
| import com.github.mikephil.charting.data.DataSet; |
| import com.github.mikephil.charting.data.Entry; |
| 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.Highlight; |
| import com.github.mikephil.charting.utils.Legend; |
| import com.github.mikephil.charting.utils.Legend.LegendPosition; |
| |
| import java.util.ArrayList; |
| |
| public class LineChartActivity extends Activity 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); |
| mSeekBarX.setOnSeekBarChangeListener(this); |
| |
| mSeekBarY = (SeekBar) findViewById(R.id.seekBar2); |
| mSeekBarY.setOnSeekBarChangeListener(this); |
| |
| // create a color template for one dataset with only one color |
| ColorTemplate ct = new ColorTemplate(); |
| // ct.addColorsForDataSets(new int[] { |
| // R.color.colorful_1 |
| // }, this); |
| ct.addDataSetColors(new int[] { |
| R.color.colorful_1 |
| }, this); |
| |
| mChart = (LineChart) findViewById(R.id.chart1); |
| mChart.setOnChartValueSelectedListener(this); |
| mChart.setColorTemplate(ct); |
| |
| // if enabled, the chart will always start at zero on the y-axis |
| mChart.setStartAtZero(false); |
| |
| // disable the drawing of values into the chart |
| mChart.setDrawYValues(false); |
| |
| mChart.setLineWidth(4f); |
| mChart.setCircleSize(4f); |
| |
| mChart.setDrawBorder(true); |
| mChart.setBorderStyles(new BorderStyle[] { BorderStyle.BOTTOM }); |
| |
| // no description text |
| mChart.setDescription(""); |
| |
| // // enable / disable grid lines |
| // mChart.setDrawVerticalGrid(false); |
| // mChart.setDrawHorizontalGrid(false); |
| // |
| // // enable / disable grid background |
| // mChart.setDrawGridBackground(false); |
| // |
| // mChart.setDrawXLegend(false); |
| // mChart.setDrawYLegend(false); |
| |
| // set the number of y-legend entries the chart should have |
| mChart.setYLabelCount(6); |
| |
| // enable value highlighting |
| mChart.setHighlightEnabled(true); |
| |
| // enable touch gestures |
| mChart.setTouchEnabled(true); |
| |
| // enable scaling and dragging |
| mChart.setDragEnabled(true); |
| |
| // if disabled, scaling can be done on x- and y-axis separately |
| mChart.setPinchZoom(true); |
| |
| // create a custom MarkerView (extend MarkerView) and specify the layout |
| // to use for it |
| MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker_view); |
| |
| // define an offset to change the original position of the marker |
| // (optional) |
| mv.setOffsets(-mv.getMeasuredWidth() / 2, -mv.getMeasuredHeight()); |
| |
| // set the marker to the chart |
| mChart.setMarkerView(mv); |
| |
| // enable/disable highlight indicators (the lines that indicate the |
| // highlighted Entry) |
| mChart.setHighlightIndicatorEnabled(false); |
| |
| // set the line to be drawn like this "- - - - - -" |
| mChart.enableDashedLine(10f, 5f, 0f); |
| |
| // add data to the chart |
| mSeekBarX.setProgress(45); |
| mSeekBarY.setProgress(100); |
| |
| // get the legend |
| Legend l = mChart.getLegend(); |
| |
| // modify it |
| // l.setPosition(LegendPosition.LEFT_OF_CHART); |
| } |
| |
| @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: { |
| 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.actionToggleFilled: { |
| if (mChart.isDrawFilledEnabled()) |
| mChart.setDrawFilled(false); |
| else |
| mChart.setDrawFilled(true); |
| mChart.invalidate(); |
| break; |
| } |
| case R.id.actionToggleCircles: { |
| if (mChart.isDrawCirclesEnabled()) |
| mChart.setDrawCircles(false); |
| else |
| mChart.setDrawCircles(true); |
| mChart.invalidate(); |
| break; |
| } |
| case R.id.actionToggleStartzero: { |
| if (mChart.isStartAtZeroEnabled()) |
| mChart.setStartAtZero(false); |
| else |
| mChart.setStartAtZero(true); |
| |
| mChart.invalidate(); |
| break; |
| } |
| case R.id.actionTogglePinch: { |
| if (mChart.isPinchZoomEnabled()) |
| mChart.setPinchZoom(false); |
| else |
| mChart.setPinchZoom(true); |
| |
| mChart.invalidate(); |
| break; |
| } |
| case R.id.actionToggleAdjustXLegend: { |
| if (mChart.isAdjustXLabelsEnabled()) |
| mChart.setAdjustXLabels(false); |
| else |
| mChart.setAdjustXLabels(true); |
| |
| mChart.invalidate(); |
| 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.actionDashedLine: { |
| if (!mChart.isDashedLineEnabled()) { |
| mChart.enableDashedLine(10f, 5f, 0f); |
| } else { |
| mChart.disableDashedLine(); |
| } |
| mChart.invalidate(); |
| break; |
| } |
| case R.id.actionSave: { |
| // mChart.saveToGallery("title"+System.currentTimeMillis()); |
| mChart.saveToPath("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())); |
| |
| ArrayList<String> xVals = new ArrayList<String>(); |
| for (int i = 0; i < mSeekBarX.getProgress(); i++) { |
| xVals.add((i) + ""); |
| } |
| |
| ArrayList<Entry> yVals = new ArrayList<Entry>(); |
| |
| for (int i = 0; i < mSeekBarX.getProgress(); i++) { |
| float mult = (mSeekBarY.getProgress() + 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 |
| DataSet set1 = new DataSet(yVals, 0); |
| |
| ArrayList<DataSet> dataSets = new ArrayList<DataSet>(); |
| dataSets.add(set1); // add the datasets |
| |
| // create a data object with the datasets |
| ChartData data = new ChartData(xVals, dataSets); |
| |
| // set data |
| mChart.setData(data); |
| |
| // redraw |
| mChart.invalidate(); |
| } |
| |
| @Override |
| public void onValuesSelected(Entry[] values, Highlight[] highlights) { |
| Log.i("VALS SELECTED", |
| "Value: " + values[0].getVal() + ", xIndex: " + highlights[0].getXIndex() |
| + ", DataSet index: " + highlights[0].getDataSetIndex()); |
| } |
| |
| @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 |
| |
| } |
| } |