| |
| package com.xxmassdeveloper.mpchartexample; |
| |
| import android.graphics.Color; |
| import android.os.Bundle; |
| 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.BarChart; |
| import com.github.mikephil.charting.components.Legend; |
| import com.github.mikephil.charting.components.Legend.LegendForm; |
| import com.github.mikephil.charting.components.Legend.LegendPosition; |
| import com.github.mikephil.charting.components.XAxis; |
| import com.github.mikephil.charting.components.YAxis; |
| import com.github.mikephil.charting.data.BarData; |
| import com.github.mikephil.charting.data.BarDataSet; |
| import com.github.mikephil.charting.data.BarEntry; |
| import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; |
| import com.github.mikephil.charting.utils.FileUtils; |
| import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase; |
| |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| public class BarChartActivitySinus extends DemoBase implements OnSeekBarChangeListener { |
| |
| protected BarChart mChart; |
| private SeekBar mSeekBarX; |
| private TextView tvX; |
| |
| private List<BarEntry> mSinusData; |
| |
| @Override |
| protected void onCreate(Bundle savedInstanceState) { |
| super.onCreate(savedInstanceState); |
| getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, |
| WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| setContentView(R.layout.activity_barchart_sinus); |
| |
| mSinusData = FileUtils.loadBarEntriesFromAssets(getAssets(), "othersine.txt"); |
| |
| tvX = (TextView) findViewById(R.id.tvValueCount); |
| |
| mSeekBarX = (SeekBar) findViewById(R.id.seekbarValues); |
| |
| mChart = (BarChart) findViewById(R.id.chart1); |
| |
| mChart.setDrawBarShadow(false); |
| mChart.setDrawValueAboveBar(true); |
| |
| mChart.setDescription(""); |
| |
| // if more than 60 entries are displayed in the chart, no values will be |
| // drawn |
| mChart.setMaxVisibleValueCount(60); |
| |
| // scaling can now only be done on x- and y-axis separately |
| mChart.setPinchZoom(false); |
| |
| // draw shadows for each bar that show the maximum value |
| // mChart.setDrawBarShadow(true); |
| |
| // mChart.setDrawXLabels(false); |
| |
| mChart.setDrawGridBackground(false); |
| // mChart.setDrawYLabels(false); |
| |
| XAxis xAxis = mChart.getXAxis(); |
| xAxis.setEnabled(false); |
| |
| YAxis leftAxis = mChart.getAxisLeft(); |
| leftAxis.setTypeface(mTfLight); |
| leftAxis.setLabelCount(6, false); |
| leftAxis.setAxisMinValue(-2.5f); |
| leftAxis.setAxisMaxValue(2.5f); |
| leftAxis.setGranularityEnabled(true); |
| leftAxis.setGranularity(0.1f); |
| |
| YAxis rightAxis = mChart.getAxisRight(); |
| rightAxis.setDrawGridLines(false); |
| rightAxis.setTypeface(mTfLight); |
| rightAxis.setLabelCount(6, false); |
| rightAxis.setAxisMinValue(-2.5f); |
| rightAxis.setAxisMaxValue(2.5f); |
| rightAxis.setGranularity(0.1f); |
| |
| mSeekBarX.setOnSeekBarChangeListener(this); |
| mSeekBarX.setProgress(150); // set data |
| |
| Legend l = mChart.getLegend(); |
| l.setPosition(LegendPosition.BELOW_CHART_LEFT); |
| l.setForm(LegendForm.SQUARE); |
| l.setFormSize(9f); |
| l.setTextSize(11f); |
| l.setXEntrySpace(4f); |
| |
| mChart.animateXY(2000, 2000); |
| } |
| |
| @Override |
| public boolean onCreateOptionsMenu(Menu menu) { |
| getMenuInflater().inflate(R.menu.bar, menu); |
| return true; |
| } |
| |
| @Override |
| public boolean onOptionsItemSelected(MenuItem item) { |
| |
| switch (item.getItemId()) { |
| case R.id.actionToggleValues: { |
| for (IBarDataSet set : mChart.getData().getDataSets()) |
| 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.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.actionToggleBarBorders: { |
| for (IBarDataSet set : mChart.getData().getDataSets()) |
| ((BarDataSet) set).setBarBorderWidth(set.getBarBorderWidth() == 1.f ? 0.f : 1.f); |
| |
| mChart.invalidate(); |
| break; |
| } |
| case R.id.animateX: { |
| mChart.animateX(1500); |
| break; |
| } |
| case R.id.animateY: { |
| mChart.animateY(1500); |
| break; |
| } |
| case R.id.animateXY: { |
| |
| mChart.animateXY(2000, 2000); |
| break; |
| } |
| case R.id.actionSave: { |
| if (mChart.saveToGallery("title" + System.currentTimeMillis(), 50)) { |
| Toast.makeText(getApplicationContext(), "Saving SUCCESSFUL!", |
| Toast.LENGTH_SHORT).show(); |
| } else |
| Toast.makeText(getApplicationContext(), "Saving FAILED!", Toast.LENGTH_SHORT) |
| .show(); |
| break; |
| } |
| } |
| return true; |
| } |
| |
| @Override |
| public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { |
| |
| tvX.setText("" + (mSeekBarX.getProgress())); |
| |
| setData(mSeekBarX.getProgress()); |
| 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) { |
| |
| ArrayList<BarEntry> entries = new ArrayList<BarEntry>(); |
| |
| for (int i = 0; i < count; i++) { |
| entries.add(mSinusData.get(i)); |
| } |
| |
| BarDataSet set; |
| |
| if (mChart.getData() != null && |
| mChart.getData().getDataSetCount() > 0) { |
| set = (BarDataSet) mChart.getData().getDataSetByIndex(0); |
| set.setValues(entries); |
| mChart.getData().notifyDataChanged(); |
| mChart.notifyDataSetChanged(); |
| } else { |
| set = new BarDataSet(entries, "Sinus Function"); |
| set.setColor(Color.rgb(240, 120, 124)); |
| } |
| |
| BarData data = new BarData(set); |
| data.setValueTextSize(10f); |
| data.setValueTypeface(mTfLight); |
| data.setDrawValues(false); |
| data.setBarWidth(0.8f); |
| |
| mChart.setData(data); |
| } |
| } |