| |
| 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.BarChart; |
| import com.github.mikephil.charting.components.AxisBase; |
| 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.XAxis.XAxisPosition; |
| import com.github.mikephil.charting.components.YAxis; |
| import com.github.mikephil.charting.components.YAxis.AxisDependency; |
| import com.github.mikephil.charting.components.YAxis.YAxisLabelPosition; |
| 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.Entry; |
| import com.github.mikephil.charting.formatter.AxisValueFormatter; |
| import com.github.mikephil.charting.highlight.Highlight; |
| import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; |
| import com.github.mikephil.charting.interfaces.datasets.IDataSet; |
| import com.github.mikephil.charting.listener.OnChartValueSelectedListener; |
| import com.github.mikephil.charting.utils.ColorTemplate; |
| import com.xxmassdeveloper.mpchartexample.custom.MyAxisValueFormatter; |
| import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase; |
| |
| import java.util.ArrayList; |
| |
| public class BarChartActivity extends DemoBase implements OnSeekBarChangeListener, |
| OnChartValueSelectedListener { |
| |
| protected BarChart mChart; |
| private SeekBar mSeekBarX, mSeekBarY; |
| private TextView tvX, tvY; |
| |
| private Typeface mTf; |
| |
| @Override |
| protected void onCreate(Bundle savedInstanceState) { |
| super.onCreate(savedInstanceState); |
| getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, |
| WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| setContentView(R.layout.activity_barchart); |
| |
| 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 = (BarChart) findViewById(R.id.chart1); |
| mChart.setOnChartValueSelectedListener(this); |
| |
| 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 xPx- and yPx-axis separately |
| mChart.setPinchZoom(false); |
| |
| mChart.setDrawGridBackground(false); |
| // mChart.setDrawYLabels(false); |
| |
| mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf"); |
| |
| XAxis xAxis = mChart.getXAxis(); |
| xAxis.setPosition(XAxisPosition.BOTTOM); |
| xAxis.setTypeface(mTf); |
| xAxis.setDrawGridLines(false); |
| xAxis.setGranularity(1f); // only intervals of 1 day |
| xAxis.setValueFormatter(new DayAxisFormatter()); |
| |
| AxisValueFormatter custom = new MyAxisValueFormatter(); |
| |
| YAxis leftAxis = mChart.getAxisLeft(); |
| leftAxis.setTypeface(mTf); |
| leftAxis.setLabelCount(8, false); |
| leftAxis.setValueFormatter(custom); |
| leftAxis.setPosition(YAxisLabelPosition.OUTSIDE_CHART); |
| leftAxis.setSpaceTop(15f); |
| leftAxis.setAxisMinValue(0f); // this replaces setStartAtZero(true) |
| |
| YAxis rightAxis = mChart.getAxisRight(); |
| rightAxis.setDrawGridLines(false); |
| rightAxis.setTypeface(mTf); |
| rightAxis.setLabelCount(8, false); |
| rightAxis.setValueFormatter(custom); |
| rightAxis.setSpaceTop(15f); |
| rightAxis.setAxisMinValue(0f); // this replaces setStartAtZero(true) |
| |
| Legend l = mChart.getLegend(); |
| l.setPosition(LegendPosition.BELOW_CHART_LEFT); |
| l.setForm(LegendForm.SQUARE); |
| l.setFormSize(9f); |
| l.setTextSize(11f); |
| l.setXEntrySpace(4f); |
| // l.setExtra(ColorTemplate.VORDIPLOM_COLORS, new String[] { "abc", |
| // "def", "ghj", "ikl", "mno" }); |
| // l.setCustom(ColorTemplate.VORDIPLOM_COLORS, new String[] { "abc", |
| // "def", "ghj", "ikl", "mno" }); |
| |
| setData(12, 50); |
| |
| // setting data |
| mSeekBarY.setProgress(50); |
| mSeekBarX.setProgress(12); |
| |
| mSeekBarY.setOnSeekBarChangeListener(this); |
| mSeekBarX.setOnSeekBarChangeListener(this); |
| |
| // 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 (IDataSet 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.actionToggleHighlightArrow: { |
| if (mChart.isDrawHighlightArrowEnabled()) |
| mChart.setDrawHighlightArrow(false); |
| else |
| mChart.setDrawHighlightArrow(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.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() + 1, 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) { |
| |
| float start = 0.5f; |
| |
| mChart.getXAxis().setAxisMinValue(start); |
| mChart.getXAxis().setAxisMaxValue(start + count); |
| |
| ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>(); |
| |
| for (int i = (int) start; i < start + count; i++) { |
| float mult = (range + 1); |
| float val = (float) (Math.random() * mult); |
| yVals1.add(new BarEntry(i + 1f, val)); |
| } |
| |
| BarDataSet set1; |
| |
| if (mChart.getData() != null && |
| mChart.getData().getDataSetCount() > 0) { |
| set1 = (BarDataSet) mChart.getData().getDataSetByIndex(0); |
| set1.setYVals(yVals1); |
| mChart.getData().notifyDataChanged(); |
| mChart.notifyDataSetChanged(); |
| } else { |
| set1 = new BarDataSet(yVals1, "The year 2017"); |
| set1.setColors(ColorTemplate.MATERIAL_COLORS); |
| |
| ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>(); |
| dataSets.add(set1); |
| |
| BarData data = new BarData(dataSets); |
| data.setValueTextSize(10f); |
| data.setValueTypeface(mTf); |
| data.setBarWidth(0.9f); |
| |
| mChart.setData(data); |
| } |
| } |
| |
| @SuppressLint("NewApi") |
| @Override |
| public void onValueSelected(Entry e, int dataSetIndex, Highlight h) { |
| |
| 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()); |
| |
| Log.i("xPx-index", |
| "low: " + mChart.getLowestVisibleX() + ", high: " |
| + mChart.getHighestVisibleX()); |
| } |
| |
| public void onNothingSelected() { |
| } |
| |
| private class DayAxisFormatter implements AxisValueFormatter { |
| |
| protected String[] mMonths = new String[]{ |
| "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec" |
| }; |
| |
| @Override |
| public String getFormattedValue(float value, AxisBase axis) { |
| |
| int dayOfYear = (int) value; |
| |
| int month = determineMonth(dayOfYear); |
| int dayOfMonth = determineDayOfMonth(dayOfYear, month); |
| |
| String appendix = "th"; |
| |
| switch (dayOfMonth) { |
| case 1: |
| appendix = "st"; |
| break; |
| case 2: |
| appendix = "nd"; |
| break; |
| case 3: |
| appendix = "rd"; |
| break; |
| case 21: |
| appendix = "st"; |
| break; |
| case 22: |
| appendix = "nd"; |
| break; |
| case 23: |
| appendix = "rd"; |
| break; |
| case 31: |
| appendix = "st"; |
| break; |
| } |
| |
| return dayOfMonth + appendix + " " + mMonths[month % mMonths.length]; |
| } |
| |
| private int getDaysForMonth(int month) { |
| |
| if (month == 1) { |
| return 28; |
| } |
| |
| if (month == 3 || month == 5 || month == 8 || month == 10) |
| return 30; |
| else |
| return 31; |
| } |
| |
| private int determineMonth(int dayOfYear) { |
| |
| int month = -1; |
| int days = 0; |
| |
| while (days < dayOfYear) { |
| month++; |
| days += getDaysForMonth(month); |
| } |
| |
| return month; |
| } |
| |
| private int determineDayOfMonth(int dayOfYear, int month) { |
| |
| int count = 0; |
| int days = 0; |
| |
| while (count < month) { |
| days += getDaysForMonth(count); |
| count++; |
| } |
| |
| return dayOfYear - days; |
| } |
| |
| @Override |
| public int getDecimalDigits() { |
| return 0; |
| } |
| } |
| } |