|
| 1 | + |
| 2 | +package com.xxmassdeveloper.mpchartexample; |
| 3 | + |
| 4 | +import android.annotation.SuppressLint; |
| 5 | +import android.graphics.RectF; |
| 6 | +import android.os.Bundle; |
| 7 | +import android.util.Log; |
| 8 | +import android.view.Menu; |
| 9 | +import android.view.MenuItem; |
| 10 | +import android.view.WindowManager; |
| 11 | +import android.widget.SeekBar; |
| 12 | +import android.widget.SeekBar.OnSeekBarChangeListener; |
| 13 | +import android.widget.TextView; |
| 14 | +import android.widget.Toast; |
| 15 | + |
| 16 | +import com.github.mikephil.charting.charts.HorizontalBarChart; |
| 17 | +import com.github.mikephil.charting.components.Legend; |
| 18 | +import com.github.mikephil.charting.components.XAxis; |
| 19 | +import com.github.mikephil.charting.components.XAxis.XAxisPosition; |
| 20 | +import com.github.mikephil.charting.components.YAxis; |
| 21 | +import com.github.mikephil.charting.data.BarData; |
| 22 | +import com.github.mikephil.charting.data.BarDataSet; |
| 23 | +import com.github.mikephil.charting.data.BarEntry; |
| 24 | +import com.github.mikephil.charting.data.Entry; |
| 25 | +import com.github.mikephil.charting.highlight.Highlight; |
| 26 | +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; |
| 27 | +import com.github.mikephil.charting.listener.OnChartValueSelectedListener; |
| 28 | +import com.github.mikephil.charting.utils.MPPointF; |
| 29 | +import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase; |
| 30 | + |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.List; |
| 33 | + |
| 34 | +public class HorizontalBarNegativeChartActivity extends DemoBase implements OnSeekBarChangeListener, |
| 35 | + OnChartValueSelectedListener { |
| 36 | + |
| 37 | + protected HorizontalBarChart mChart; |
| 38 | + private SeekBar mSeekBarX, mSeekBarY; |
| 39 | + private TextView tvX, tvY; |
| 40 | + |
| 41 | + @Override |
| 42 | + protected void onCreate(Bundle savedInstanceState) { |
| 43 | + super.onCreate(savedInstanceState); |
| 44 | + getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, |
| 45 | + WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| 46 | + setContentView(R.layout.activity_horizontalbarchart); |
| 47 | + |
| 48 | + tvX = findViewById(R.id.tvXMax); |
| 49 | + tvY = (TextView) findViewById(R.id.tvYMax); |
| 50 | + |
| 51 | + mSeekBarX = (SeekBar) findViewById(R.id.seekBar1); |
| 52 | + mSeekBarY = (SeekBar) findViewById(R.id.seekBar2); |
| 53 | + |
| 54 | + mChart = (HorizontalBarChart) findViewById(R.id.chart1); |
| 55 | + mChart.setOnChartValueSelectedListener(this); |
| 56 | + // mChart.setHighlightEnabled(false); |
| 57 | + |
| 58 | + mChart.setDrawBarShadow(false); |
| 59 | + |
| 60 | + mChart.setDrawValueAboveBar(true); |
| 61 | + |
| 62 | + mChart.getDescription().setEnabled(false); |
| 63 | + |
| 64 | + // if more than 60 entries are displayed in the chart, no values will be |
| 65 | + // drawn |
| 66 | + mChart.setMaxVisibleValueCount(60); |
| 67 | + |
| 68 | + // scaling can now only be done on x- and y-axis separately |
| 69 | + mChart.setPinchZoom(false); |
| 70 | + |
| 71 | + // draw shadows for each bar that show the maximum value |
| 72 | + // mChart.setDrawBarShadow(true); |
| 73 | + |
| 74 | + mChart.setDrawGridBackground(false); |
| 75 | + |
| 76 | + XAxis xl = mChart.getXAxis(); |
| 77 | + xl.setPosition(XAxisPosition.BOTTOM); |
| 78 | + xl.setTypeface(mTfLight); |
| 79 | + xl.setDrawAxisLine(true); |
| 80 | + xl.setDrawGridLines(false); |
| 81 | + xl.setGranularity(10f); |
| 82 | + |
| 83 | + YAxis yl = mChart.getAxisLeft(); |
| 84 | + yl.setTypeface(mTfLight); |
| 85 | + yl.setDrawAxisLine(true); |
| 86 | + yl.setDrawGridLines(true); |
| 87 | + yl.setDrawZeroLine(true); // draw a zero line |
| 88 | +// yl.setInverted(true); |
| 89 | + |
| 90 | + YAxis yr = mChart.getAxisRight(); |
| 91 | + yr.setTypeface(mTfLight); |
| 92 | + yr.setDrawAxisLine(true); |
| 93 | + yr.setDrawGridLines(false); |
| 94 | +// yr.setInverted(true); |
| 95 | + |
| 96 | + setData(12, 50); |
| 97 | + mChart.setFitBars(true); |
| 98 | + mChart.animateY(2500); |
| 99 | + |
| 100 | + // setting data |
| 101 | + mSeekBarY.setProgress(50); |
| 102 | + mSeekBarX.setProgress(12); |
| 103 | + |
| 104 | + mSeekBarY.setOnSeekBarChangeListener(this); |
| 105 | + mSeekBarX.setOnSeekBarChangeListener(this); |
| 106 | + |
| 107 | + Legend l = mChart.getLegend(); |
| 108 | + l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM); |
| 109 | + l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT); |
| 110 | + l.setOrientation(Legend.LegendOrientation.HORIZONTAL); |
| 111 | + l.setDrawInside(false); |
| 112 | + l.setFormSize(8f); |
| 113 | + l.setXEntrySpace(4f); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 118 | + getMenuInflater().inflate(R.menu.bar, menu); |
| 119 | + return true; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 124 | + |
| 125 | + switch (item.getItemId()) { |
| 126 | + case R.id.actionToggleValues: { |
| 127 | + List<IBarDataSet> sets = mChart.getData() |
| 128 | + .getDataSets(); |
| 129 | + |
| 130 | + for (IBarDataSet iSet : sets) { |
| 131 | + |
| 132 | + IBarDataSet set = (BarDataSet) iSet; |
| 133 | + set.setDrawValues(!set.isDrawValuesEnabled()); |
| 134 | + } |
| 135 | + |
| 136 | + mChart.invalidate(); |
| 137 | + break; |
| 138 | + } |
| 139 | + case R.id.actionToggleIcons: { |
| 140 | + List<IBarDataSet> sets = mChart.getData() |
| 141 | + .getDataSets(); |
| 142 | + |
| 143 | + for (IBarDataSet iSet : sets) { |
| 144 | + |
| 145 | + IBarDataSet set = (BarDataSet) iSet; |
| 146 | + set.setDrawIcons(!set.isDrawIconsEnabled()); |
| 147 | + } |
| 148 | + |
| 149 | + mChart.invalidate(); |
| 150 | + break; |
| 151 | + } |
| 152 | + case R.id.actionToggleHighlight: { |
| 153 | + if(mChart.getData() != null) { |
| 154 | + mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled()); |
| 155 | + mChart.invalidate(); |
| 156 | + } |
| 157 | + break; |
| 158 | + } |
| 159 | + case R.id.actionTogglePinch: { |
| 160 | + if (mChart.isPinchZoomEnabled()) |
| 161 | + mChart.setPinchZoom(false); |
| 162 | + else |
| 163 | + mChart.setPinchZoom(true); |
| 164 | + |
| 165 | + mChart.invalidate(); |
| 166 | + break; |
| 167 | + } |
| 168 | + case R.id.actionToggleAutoScaleMinMax: { |
| 169 | + mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled()); |
| 170 | + mChart.notifyDataSetChanged(); |
| 171 | + break; |
| 172 | + } |
| 173 | + case R.id.actionToggleBarBorders: { |
| 174 | + for (IBarDataSet set : mChart.getData().getDataSets()) |
| 175 | + ((BarDataSet)set).setBarBorderWidth(set.getBarBorderWidth() == 1.f ? 0.f : 1.f); |
| 176 | + |
| 177 | + mChart.invalidate(); |
| 178 | + break; |
| 179 | + } |
| 180 | + case R.id.animateX: { |
| 181 | + mChart.animateX(3000); |
| 182 | + break; |
| 183 | + } |
| 184 | + case R.id.animateY: { |
| 185 | + mChart.animateY(3000); |
| 186 | + break; |
| 187 | + } |
| 188 | + case R.id.animateXY: { |
| 189 | + |
| 190 | + mChart.animateXY(3000, 3000); |
| 191 | + break; |
| 192 | + } |
| 193 | + case R.id.actionSave: { |
| 194 | + if (mChart.saveToGallery("title" + System.currentTimeMillis(), 50)) { |
| 195 | + Toast.makeText(getApplicationContext(), "Saving SUCCESSFUL!", |
| 196 | + Toast.LENGTH_SHORT).show(); |
| 197 | + } else |
| 198 | + Toast.makeText(getApplicationContext(), "Saving FAILED!", Toast.LENGTH_SHORT) |
| 199 | + .show(); |
| 200 | + break; |
| 201 | + } |
| 202 | + } |
| 203 | + return true; |
| 204 | + } |
| 205 | + |
| 206 | + @Override |
| 207 | + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { |
| 208 | + |
| 209 | + tvX.setText("" + (mSeekBarX.getProgress() + 1)); |
| 210 | + tvY.setText("" + (mSeekBarY.getProgress())); |
| 211 | + |
| 212 | + setData(mSeekBarX.getProgress() + 1, mSeekBarY.getProgress()); |
| 213 | + mChart.setFitBars(true); |
| 214 | + mChart.invalidate(); |
| 215 | + } |
| 216 | + |
| 217 | + @Override |
| 218 | + public void onStartTrackingTouch(SeekBar seekBar) { |
| 219 | + // TODO Auto-generated method stub |
| 220 | + |
| 221 | + } |
| 222 | + |
| 223 | + @Override |
| 224 | + public void onStopTrackingTouch(SeekBar seekBar) { |
| 225 | + // TODO Auto-generated method stub |
| 226 | + |
| 227 | + } |
| 228 | + |
| 229 | + private void setData(int count, float range) { |
| 230 | + |
| 231 | + float barWidth = 9f; |
| 232 | + float spaceForBar = 10f; |
| 233 | + ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>(); |
| 234 | + |
| 235 | + for (int i = 0; i < count; i++) { |
| 236 | + float val = (float) (Math.random() * range - range / 2); |
| 237 | + yVals1.add(new BarEntry(i * spaceForBar, val, |
| 238 | + getResources().getDrawable(R.drawable.star))); |
| 239 | + } |
| 240 | + |
| 241 | + BarDataSet set1; |
| 242 | + |
| 243 | + if (mChart.getData() != null && |
| 244 | + mChart.getData().getDataSetCount() > 0) { |
| 245 | + set1 = (BarDataSet)mChart.getData().getDataSetByIndex(0); |
| 246 | + set1.setValues(yVals1); |
| 247 | + mChart.getData().notifyDataChanged(); |
| 248 | + mChart.notifyDataSetChanged(); |
| 249 | + } else { |
| 250 | + set1 = new BarDataSet(yVals1, "DataSet 1"); |
| 251 | + |
| 252 | + set1.setDrawIcons(false); |
| 253 | + |
| 254 | + ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>(); |
| 255 | + dataSets.add(set1); |
| 256 | + |
| 257 | + BarData data = new BarData(dataSets); |
| 258 | + data.setValueTextSize(10f); |
| 259 | + data.setValueTypeface(mTfLight); |
| 260 | + data.setBarWidth(barWidth); |
| 261 | + mChart.setData(data); |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + protected RectF mOnValueSelectedRectF = new RectF(); |
| 266 | + @SuppressLint("NewApi") |
| 267 | + @Override |
| 268 | + public void onValueSelected(Entry e, Highlight h) { |
| 269 | + |
| 270 | + if (e == null) |
| 271 | + return; |
| 272 | + |
| 273 | + RectF bounds = mOnValueSelectedRectF; |
| 274 | + mChart.getBarBounds((BarEntry) e, bounds); |
| 275 | + |
| 276 | + MPPointF position = mChart.getPosition(e, mChart.getData().getDataSetByIndex(h.getDataSetIndex()) |
| 277 | + .getAxisDependency()); |
| 278 | + |
| 279 | + Log.i("bounds", bounds.toString()); |
| 280 | + Log.i("position", position.toString()); |
| 281 | + |
| 282 | + MPPointF.recycleInstance(position); |
| 283 | + } |
| 284 | + |
| 285 | + @Override |
| 286 | + public void onNothingSelected() { |
| 287 | + }; |
| 288 | +} |
0 commit comments