| |
| package com.xxmassdeveloper.mpchartexample; |
| |
| import android.annotation.SuppressLint; |
| import android.graphics.PointF; |
| import android.graphics.RectF; |
| import android.graphics.Typeface; |
| 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.HorizontalBarChart; |
| import com.github.mikephil.charting.components.Legend; |
| import com.github.mikephil.charting.components.Legend.LegendPosition; |
| import com.github.mikephil.charting.components.XAxis; |
| import com.github.mikephil.charting.components.XAxis.XAxisPosition; |
| import com.github.mikephil.charting.components.YAxis; |
| import com.github.mikephil.charting.components.YAxis.AxisDependency; |
| 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.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.listener.OnChartValueSelectedListener; |
| import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase; |
| |
| import java.util.ArrayList; |
| |
| public class HorizontalBarChartActivity extends DemoBase implements OnSeekBarChangeListener, |
| OnChartValueSelectedListener { |
| |
| protected HorizontalBarChart mChart; |
| private SeekBar mSeekBarX, mSeekBarY; |
| private TextView tvX, tvY; |
| |
| private Typeface tf; |
| |
| @Override |
| protected void onCreate(Bundle savedInstanceState) { |
| super.onCreate(savedInstanceState); |
| getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, |
| WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| setContentView(R.layout.activity_horizontalbarchart); |
| |
| tvX = (TextView) findViewById(R.id.tvXMax); |
| tvY = (TextView) findViewById(R.id.tvYMax); |
| |
| mSeekBarX = (SeekBar) findViewById(R.id.seekBar1); |
| mSeekBarY = (SeekBar) findViewById(R.id.seekBar2); |
| |
| mChart = (HorizontalBarChart) findViewById(R.id.chart1); |
| mChart.setOnChartValueSelectedListener(this); |
| // mChart.setHighlightEnabled(false); |
| |
| 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); |
| |
| tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf"); |
| |
| XAxis xl = mChart.getXAxis(); |
| xl.setPosition(XAxisPosition.BOTTOM); |
| xl.setTypeface(tf); |
| xl.setDrawAxisLine(true); |
| xl.setDrawGridLines(true); |
| // xl.setEnabled(false); |
| |
| YAxis yl = mChart.getAxisLeft(); |
| yl.setTypeface(tf); |
| yl.setDrawAxisLine(true); |
| yl.setDrawGridLines(true); |
| |
| YAxis yr = mChart.getAxisRight(); |
| yr.setTypeface(tf); |
| yr.setDrawAxisLine(true); |
| yr.setDrawGridLines(true); |
| |
| setData(12, 50); |
| mChart.animateY(2500); |
| |
| // setting data |
| mSeekBarY.setProgress(50); |
| mSeekBarX.setProgress(12); |
| |
| mSeekBarY.setOnSeekBarChangeListener(this); |
| mSeekBarX.setOnSeekBarChangeListener(this); |
| |
| Legend l = mChart.getLegend(); |
| l.setPosition(LegendPosition.BELOW_CHART_LEFT); |
| l.setFormSize(8f); |
| l.setXEntrySpace(4f); |
| |
| // mChart.setDrawLegend(false); |
| } |
| |
| @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 (DataSet<?> set : mChart.getData().getDataSets()) |
| set.setDrawValues(!set.isDrawValuesEnabled()); |
| |
| mChart.invalidate(); |
| break; |
| } |
| case R.id.actionToggleHighlight: { |
| if (mChart.isHighlightEnabled()) |
| mChart.setHighlightEnabled(false); |
| else |
| mChart.setHighlightEnabled(true); |
| mChart.invalidate(); |
| break; |
| } |
| case R.id.actionTogglePinch: { |
| if (mChart.isPinchZoomEnabled()) |
| mChart.setPinchZoom(false); |
| else |
| mChart.setPinchZoom(true); |
| |
| mChart.invalidate(); |
| break; |
| } |
| case R.id.actionToggleHighlightArrow: { |
| if (mChart.isDrawHighlightArrowEnabled()) |
| mChart.setDrawHighlightArrow(false); |
| else |
| mChart.setDrawHighlightArrow(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.actionToggleAdjustXLegend: { |
| XAxis xLabels = mChart.getXAxis(); |
| |
| if (xLabels.isAdjustXLabelsEnabled()) |
| xLabels.setAdjustXLabels(false); |
| else |
| xLabels.setAdjustXLabels(true); |
| |
| mChart.invalidate(); |
| break; |
| } |
| case R.id.actionToggleFilter: { |
| |
| Approximator a = new Approximator(ApproximatorType.DOUGLAS_PEUCKER, 25); |
| |
| if (!mChart.isFilteringEnabled()) { |
| mChart.enableFiltering(a); |
| } else { |
| mChart.disableFiltering(); |
| } |
| mChart.invalidate(); |
| 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() + 1)); |
| tvY.setText("" + (mSeekBarY.getProgress())); |
| |
| setData(mSeekBarX.getProgress(), mSeekBarY.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, float range) { |
| |
| ArrayList<String> xVals = new ArrayList<String>(); |
| for (int i = 0; i < count; i++) { |
| xVals.add(mMonths[i % 12]); |
| } |
| |
| ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>(); |
| |
| for (int i = 0; i < count; i++) { |
| float mult = (range + 1); |
| float val = (float) (Math.random() * mult); |
| yVals1.add(new BarEntry(val, i)); |
| } |
| |
| BarDataSet set1 = new BarDataSet(yVals1, "DataSet"); |
| set1.setBarSpacePercent(35f); |
| |
| ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>(); |
| dataSets.add(set1); |
| |
| BarData data = new BarData(xVals, dataSets); |
| data.setValueTextSize(10f); |
| data.setValueTypeface(tf); |
| |
| mChart.setData(data); |
| } |
| |
| @SuppressLint("NewApi") |
| @Override |
| public void onValueSelected(Entry e, int dataSetIndex) { |
| |
| if (e == null) |
| return; |
| |
| RectF bounds = mChart.getBarBounds((BarEntry) e); |
| PointF position = mChart.getPosition(e, AxisDependency.LEFT); |
| |
| Log.i("bounds", bounds.toString()); |
| Log.i("position", position.toString()); |
| } |
| |
| public void onNothingSelected() { |
| }; |
| } |