| |
| package com.xxmassdeveloper.mpchartexample; |
| |
| import android.graphics.Color; |
| import android.os.Bundle; |
| import android.util.Log; |
| import android.view.Menu; |
| import android.view.MenuItem; |
| import android.view.MotionEvent; |
| 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.BorderPosition; |
| 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.OnChartGestureListener; |
| import com.github.mikephil.charting.interfaces.OnChartValueSelectedListener; |
| import com.github.mikephil.charting.utils.Legend; |
| import com.github.mikephil.charting.utils.Legend.LegendForm; |
| import com.github.mikephil.charting.utils.LimitLine; |
| import com.github.mikephil.charting.utils.LimitLine.LimitLabelPosition; |
| import com.github.mikephil.charting.utils.XLabels; |
| import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase; |
| |
| import java.util.ArrayList; |
| |
| public class LineChartActivity extends DemoBase implements OnSeekBarChangeListener, |
| OnChartGestureListener, 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.setOnChartGestureListener(this); |
| mChart.setOnChartValueSelectedListener(this); |
| |
| mChart.setUnit(" $"); |
| mChart.setDrawUnitsInChart(true); |
| |
| // 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.setBorderPositions(new BorderPosition[] { |
| BorderPosition.BOTTOM |
| }); |
| |
| // no description text |
| mChart.setDescription(""); |
| mChart.setNoDataTextDescription("You need to provide data for the chart."); |
| |
| // // enable / disable grid lines |
| // mChart.setDrawVerticalGrid(false); |
| // mChart.setDrawHorizontalGrid(false); |
| // |
| // // enable / disable grid background |
| // mChart.setDrawGridBackground(false); |
| // |
| // mChart.setDrawXLegend(false); |
| // mChart.setDrawYLegend(false); |
| |
| // enable value highlighting |
| mChart.setHighlightEnabled(true); |
| |
| // 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); |
| |
| // 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); |
| |
| mChart.animateX(2500); |
| |
| // // 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: { |
| |
| ArrayList<LineDataSet> sets = (ArrayList<LineDataSet>) mChart.getDataCurrent() |
| .getDataSets(); |
| |
| for (LineDataSet set : sets) { |
| if (set.isDrawFilledEnabled()) |
| set.setDrawFilled(false); |
| else |
| set.setDrawFilled(true); |
| } |
| mChart.invalidate(); |
| break; |
| } |
| case R.id.actionToggleCircles: { |
| ArrayList<LineDataSet> sets = (ArrayList<LineDataSet>) mChart.getDataCurrent() |
| .getDataSets(); |
| |
| for (LineDataSet set : sets) { |
| if (set.isDrawCirclesEnabled()) |
| set.setDrawCircles(false); |
| else |
| set.setDrawCircles(true); |
| } |
| mChart.invalidate(); |
| break; |
| } |
| case R.id.actionToggleCubic: { |
| ArrayList<LineDataSet> sets = (ArrayList<LineDataSet>) mChart.getDataCurrent() |
| .getDataSets(); |
| |
| for (LineDataSet set : sets) { |
| if (set.isDrawCubicEnabled()) |
| set.setDrawCubic(false); |
| else |
| set.setDrawCubic(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.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.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(); |
| |
| // |
| // for(int i = 0; i < 10; i++) { |
| // mChart.addEntry(new Entry((float) (Math.random() * 100), |
| // i+2), 0); |
| // mChart.invalidate(); |
| // } |
| // |
| // Toast.makeText(getApplicationContext(), "valcount: " + |
| // mChart.getDataOriginal().getYValCount() + ", valsum: " + |
| // mChart.getDataOriginal().getYValueSum(), |
| // Toast.LENGTH_SHORT).show(); |
| // |
| 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 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"); |
| // set1.setFillAlpha(110); |
| // set1.setFillColor(Color.RED); |
| |
| // set the line to be drawn like this "- - - - - -" |
| set1.enableDashedLine(10f, 5f, 0f); |
| set1.setColor(Color.BLACK); |
| set1.setCircleColor(Color.BLACK); |
| set1.setLineWidth(1f); |
| set1.setCircleSize(4f); |
| set1.setFillAlpha(65); |
| set1.setFillColor(Color.BLACK); |
| // set1.setShader(new LinearGradient(0, 0, 0, mChart.getHeight(), |
| // Color.BLACK, Color.WHITE, Shader.TileMode.MIRROR)); |
| |
| 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); |
| |
| LimitLine ll1 = new LimitLine(130f); |
| ll1.setLineWidth(4f); |
| ll1.enableDashedLine(10f, 10f, 0f); |
| ll1.setDrawValue(true); |
| ll1.setLabelPosition(LimitLabelPosition.RIGHT); |
| |
| LimitLine ll2 = new LimitLine(-30f); |
| ll2.setLineWidth(4f); |
| ll2.enableDashedLine(10f, 10f, 0f); |
| ll2.setDrawValue(true); |
| ll2.setLabelPosition(LimitLabelPosition.RIGHT); |
| |
| data.addLimitLine(ll1); |
| data.addLimitLine(ll2); |
| |
| // set data |
| mChart.setData(data); |
| } |
| |
| @Override |
| public void onChartLongPressed(MotionEvent me) { |
| Log.i("LongPress", "Chart longpressed."); |
| } |
| |
| @Override |
| public void onChartDoubleTapped(MotionEvent me) { |
| Log.i("DoubleTap", "Chart double-tapped."); |
| } |
| |
| @Override |
| public void onChartSingleTapped(MotionEvent me) { |
| Log.i("SingleTap", "Chart single-tapped."); |
| } |
| |
| @Override |
| public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) { |
| Log.i("Fling", "Chart flinged. VeloX: " + velocityX + ", VeloY: " + velocityY); |
| } |
| |
| @Override |
| public void onValueSelected(Entry e, int dataSetIndex) { |
| Log.i("Entry selected", e.toString()); |
| } |
| |
| @Override |
| public void onNothingSelected() { |
| Log.i("Nothing selected", "Nothing selected."); |
| } |
| } |