|
| 1 | +package com.example.mpchartexample; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.view.Menu; |
| 6 | +import android.view.MenuItem; |
| 7 | +import android.view.WindowManager; |
| 8 | +import android.widget.SeekBar; |
| 9 | +import android.widget.SeekBar.OnSeekBarChangeListener; |
| 10 | +import android.widget.TextView; |
| 11 | + |
| 12 | +import com.github.mikephil.charting.BarChart; |
| 13 | +import com.github.mikephil.charting.ColorTemplate; |
| 14 | + |
| 15 | +import java.util.ArrayList; |
| 16 | + |
| 17 | +public class BarChartActivity extends Activity implements OnSeekBarChangeListener { |
| 18 | + |
| 19 | + private BarChart mChart; |
| 20 | + private SeekBar mSeekBarX, mSeekBarY; |
| 21 | + private TextView tvX, tvY; |
| 22 | + |
| 23 | + @Override |
| 24 | + protected void onCreate(Bundle savedInstanceState) { |
| 25 | + super.onCreate(savedInstanceState); |
| 26 | + getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, |
| 27 | + WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| 28 | + setContentView(R.layout.activity_barchart); |
| 29 | + |
| 30 | + tvX = (TextView) findViewById(R.id.tvXMax); |
| 31 | + tvY = (TextView) findViewById(R.id.tvYMax); |
| 32 | + |
| 33 | + mSeekBarX = (SeekBar) findViewById(R.id.seekBar1); |
| 34 | + mSeekBarX.setOnSeekBarChangeListener(this); |
| 35 | + |
| 36 | + |
| 37 | + mSeekBarY = (SeekBar) findViewById(R.id.seekBar2); |
| 38 | + mSeekBarY.setOnSeekBarChangeListener(this); |
| 39 | + |
| 40 | + mChart = (BarChart) findViewById(R.id.chart1); |
| 41 | + mChart.setColorTemplate(new ColorTemplate(ColorTemplate.getColors(this, ColorTemplate.FRESH_COLORS))); |
| 42 | + |
| 43 | +// mChart.setDrawFilled(true); |
| 44 | +// mChart.setRoundedYLegend(false); |
| 45 | +// mChart.setStartAtZero(true); |
| 46 | + mChart.setDrawValues(false); |
| 47 | + mChart.set3DEnabled(false); |
| 48 | + mChart.setDrawAdditional(true); |
| 49 | +// mChart.setSpacePercent(20, 10); |
| 50 | + mChart.setYLegendCount(5); |
| 51 | + mChart.setTouchEnabled(true); |
| 52 | + |
| 53 | + ArrayList<String> xVals = new ArrayList<String>(); |
| 54 | + for (int i = 1; i <= 50; i++) { |
| 55 | + xVals.add(Integer.toString(i - 1)); |
| 56 | + } |
| 57 | + |
| 58 | + ArrayList<Float> yVals = new ArrayList<Float>(); |
| 59 | + |
| 60 | + for (int i = 1; i <= 50; i++) { |
| 61 | + float val = (float) (Math.random() * 10); |
| 62 | + yVals.add(val); |
| 63 | + } |
| 64 | + |
| 65 | + mSeekBarX.setProgress(50); |
| 66 | + mSeekBarY.setProgress(100); |
| 67 | + |
| 68 | + mChart.setData(xVals, yVals); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 73 | + getMenuInflater().inflate(R.menu.bar, menu); |
| 74 | + return true; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 79 | + |
| 80 | + switch (item.getItemId()) { |
| 81 | + case R.id.actionToggleRound: { |
| 82 | + if (mChart.isYLegendRounded()) |
| 83 | + mChart.setRoundedYLegend(false); |
| 84 | + else |
| 85 | + mChart.setRoundedYLegend(true); |
| 86 | + mChart.invalidate(); |
| 87 | + break; |
| 88 | + } |
| 89 | + case R.id.actionToggleValues: { |
| 90 | + if (mChart.isDrawValuesEnabled()) |
| 91 | + mChart.setDrawValues(false); |
| 92 | + else |
| 93 | + mChart.setDrawValues(true); |
| 94 | + mChart.invalidate(); |
| 95 | + break; |
| 96 | + } |
| 97 | + case R.id.actionToggle3D: { |
| 98 | + if (mChart.is3DEnabled()) |
| 99 | + mChart.set3DEnabled(false); |
| 100 | + else |
| 101 | + mChart.set3DEnabled(true); |
| 102 | + mChart.invalidate(); |
| 103 | + break; |
| 104 | + } |
| 105 | + case R.id.actionToggleStartzero: { |
| 106 | + if (mChart.isStartAtZeroEnabled()) |
| 107 | + mChart.setStartAtZero(false); |
| 108 | + else |
| 109 | + mChart.setStartAtZero(true); |
| 110 | + |
| 111 | + mChart.invalidate(); |
| 112 | + break; |
| 113 | + } |
| 114 | + case R.id.actionToggleAdjustXLegend: { |
| 115 | + if (mChart.isAdjustXLegendEnabled()) |
| 116 | + mChart.setAdjustXLegend(false); |
| 117 | + else |
| 118 | + mChart.setAdjustXLegend(true); |
| 119 | + |
| 120 | + mChart.invalidate(); |
| 121 | + break; |
| 122 | + } |
| 123 | + case R.id.actionSave: { |
| 124 | +// mChart.saveToGallery("title"+System.currentTimeMillis()); |
| 125 | + mChart.saveToPath("title"+System.currentTimeMillis(), ""); |
| 126 | + break; |
| 127 | + } |
| 128 | + } |
| 129 | + return true; |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { |
| 134 | + |
| 135 | + ArrayList<String> xVals = new ArrayList<String>(); |
| 136 | + for (int i = 1; i <= mSeekBarX.getProgress()+1; i++) { |
| 137 | + xVals.add((i - 1)+""); |
| 138 | + } |
| 139 | + |
| 140 | + ArrayList<Float> yVals = new ArrayList<Float>(); |
| 141 | + |
| 142 | + for (int i = 1; i <= mSeekBarX.getProgress()+1; i++) { |
| 143 | + float mult = (mSeekBarY.getProgress()+1); |
| 144 | + float val = (float) (Math.random() * mult * 0.1) + 3;// + (float) ((mult * 0.1) / 10); |
| 145 | + yVals.add(val); |
| 146 | + } |
| 147 | + |
| 148 | + tvX.setText(""+ (mSeekBarX.getProgress() + 1)); |
| 149 | + tvY.setText(""+ (mSeekBarY.getProgress() / 10)); |
| 150 | + |
| 151 | + mChart.setData(xVals, yVals); |
| 152 | + mChart.invalidate(); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public void onStartTrackingTouch(SeekBar seekBar) { |
| 157 | + // TODO Auto-generated method stub |
| 158 | + |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public void onStopTrackingTouch(SeekBar seekBar) { |
| 163 | + // TODO Auto-generated method stub |
| 164 | + |
| 165 | + } |
| 166 | +} |
0 commit comments