blob: 8aa1a1c5522cde2f190f15341f463ea37a5e4bdf [file] [log] [blame]
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.BarLineChartBase.BorderStyle;
import com.github.mikephil.charting.charts.LineChart;
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.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.LegendForm;
import com.github.mikephil.charting.utils.XLabels;
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
import java.util.ArrayList;
public class LineChartActivity 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);
// 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.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);
// 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);
// 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);
// add data
setData(45, 100);
// // 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: {
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: {
XLabels xLabels = mChart.getXLabels();
if (xLabels.isAdjustXLabelsEnabled())
xLabels.setAdjustXLabels(false);
else
xLabels.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.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 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
}
private void setData(int count, float range) {
ArrayList<String> xVals = new ArrayList<String>();
for (int i = 0; i < count; i++) {
xVals.add((i) + "");
}
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");
// set the line to be drawn like this "- - - - - -"
set1.enableDashedLine(10f, 5f, 0f);
set1.setLineWidth(1f);
set1.setCircleSize(4f);
ArrayList<LineDataSet> dataSets = new ArrayList<LineDataSet>();
dataSets.add(set1); // add the datasets
// create a data object with the datasets
LineData data = new LineData(xVals, dataSets);
// set data
mChart.setData(data);
}
}