|
| 1 | + |
| 2 | +package com.xxmassdeveloper.mpchartexample; |
| 3 | + |
| 4 | +import android.graphics.Color; |
| 5 | +import android.os.Bundle; |
| 6 | +import android.util.Log; |
| 7 | +import android.view.Menu; |
| 8 | +import android.view.MenuItem; |
| 9 | +import android.view.WindowManager; |
| 10 | +import android.widget.SeekBar; |
| 11 | +import android.widget.SeekBar.OnSeekBarChangeListener; |
| 12 | +import android.widget.TextView; |
| 13 | +import android.widget.Toast; |
| 14 | + |
| 15 | +import com.github.mikephil.charting.charts.BarChart; |
| 16 | +import com.github.mikephil.charting.charts.HorizontalBarChart; |
| 17 | +import com.github.mikephil.charting.components.Legend; |
| 18 | +import com.github.mikephil.charting.components.Legend.LegendPosition; |
| 19 | +import com.github.mikephil.charting.components.XAxis; |
| 20 | +import com.github.mikephil.charting.components.XAxis.XAxisPosition; |
| 21 | +import com.github.mikephil.charting.components.YAxis; |
| 22 | +import com.github.mikephil.charting.data.BarData; |
| 23 | +import com.github.mikephil.charting.data.BarDataSet; |
| 24 | +import com.github.mikephil.charting.data.BarEntry; |
| 25 | +import com.github.mikephil.charting.data.DataSet; |
| 26 | +import com.github.mikephil.charting.data.Entry; |
| 27 | +import com.github.mikephil.charting.data.filter.Approximator; |
| 28 | +import com.github.mikephil.charting.data.filter.Approximator.ApproximatorType; |
| 29 | +import com.github.mikephil.charting.listener.OnChartValueSelectedListener; |
| 30 | +import com.github.mikephil.charting.utils.ColorTemplate; |
| 31 | +import com.github.mikephil.charting.utils.Highlight; |
| 32 | +import com.github.mikephil.charting.utils.ValueFormatter; |
| 33 | +import com.xxmassdeveloper.mpchartexample.custom.MyValueFormatter; |
| 34 | +import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase; |
| 35 | + |
| 36 | +import java.text.DecimalFormat; |
| 37 | +import java.util.ArrayList; |
| 38 | + |
| 39 | +public class StackedBarActivityNegative extends DemoBase implements |
| 40 | + OnChartValueSelectedListener { |
| 41 | + |
| 42 | + private HorizontalBarChart mChart; |
| 43 | + |
| 44 | + @Override |
| 45 | + protected void onCreate(Bundle savedInstanceState) { |
| 46 | + super.onCreate(savedInstanceState); |
| 47 | + getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, |
| 48 | + WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| 49 | + setContentView(R.layout.activity_age_distribution); |
| 50 | + |
| 51 | + setTitle("Age Distribution Austria"); |
| 52 | + |
| 53 | + mChart = (HorizontalBarChart) findViewById(R.id.chart1); |
| 54 | + mChart.setOnChartValueSelectedListener(this); |
| 55 | + mChart.setDrawGridBackground(false); |
| 56 | + mChart.setDescription(""); |
| 57 | + |
| 58 | + // if false values are only drawn for the stack sum, else each value is |
| 59 | + // drawn |
| 60 | + mChart.setDrawValuesForWholeStack(true); |
| 61 | + // scaling can now only be done on x- and y-axis separately |
| 62 | + mChart.setPinchZoom(false); |
| 63 | + |
| 64 | + mChart.setDrawBarShadow(false); |
| 65 | + mChart.setDrawValueAboveBar(true); |
| 66 | + |
| 67 | + mChart.getAxisLeft().setEnabled(false); |
| 68 | + mChart.getAxisRight().setStartAtZero(false); |
| 69 | + mChart.getAxisRight().setAxisMaxValue(25f); |
| 70 | + mChart.getAxisRight().setAxisMinValue(-25f); |
| 71 | + mChart.getAxisRight().setLabelCount(7); |
| 72 | + mChart.getAxisRight().setValueFormatter(new CustomFormatter()); |
| 73 | + mChart.getAxisRight().setTextSize(9f); |
| 74 | + |
| 75 | + XAxis xAxis = mChart.getXAxis(); |
| 76 | + xAxis.setPosition(XAxisPosition.BOTH_SIDED); |
| 77 | + xAxis.setDrawGridLines(false); |
| 78 | + xAxis.setDrawAxisLine(false); |
| 79 | + xAxis.setTextSize(9f); |
| 80 | + |
| 81 | + Legend l = mChart.getLegend(); |
| 82 | + l.setPosition(LegendPosition.BELOW_CHART_RIGHT); |
| 83 | + l.setFormSize(8f); |
| 84 | + l.setFormToTextSpace(4f); |
| 85 | + l.setXEntrySpace(6f); |
| 86 | + |
| 87 | + ArrayList<BarEntry> yValues = new ArrayList<BarEntry>(); |
| 88 | + yValues.add(new BarEntry(new float[]{ -10, 10 }, 0)); |
| 89 | + yValues.add(new BarEntry(new float[]{ -12, 13 }, 1)); |
| 90 | + yValues.add(new BarEntry(new float[]{ -15, 15 }, 2)); |
| 91 | + yValues.add(new BarEntry(new float[]{ -17, 17 }, 3)); |
| 92 | + yValues.add(new BarEntry(new float[]{ -19, 20 }, 4)); |
| 93 | + yValues.add(new BarEntry(new float[]{ -19, 19 }, 5)); |
| 94 | + yValues.add(new BarEntry(new float[]{ -16, 16 }, 6)); |
| 95 | + yValues.add(new BarEntry(new float[]{ -13, 14 }, 7)); |
| 96 | + yValues.add(new BarEntry(new float[]{ -10, 11 }, 8)); |
| 97 | + yValues.add(new BarEntry(new float[]{ -5, 6 }, 9)); |
| 98 | + yValues.add(new BarEntry(new float[]{ -1, 2 }, 10)); |
| 99 | + |
| 100 | + BarDataSet set = new BarDataSet(yValues, "Age Distribution"); |
| 101 | + set.setValueFormatter(new CustomFormatter()); |
| 102 | + set.setValueTextSize(7f); |
| 103 | + set.setAxisDependency(YAxis.AxisDependency.RIGHT); |
| 104 | + set.setBarSpacePercent(50f); |
| 105 | + set.setColors(new int[] {Color.rgb(67,67,72), Color.rgb(124,181,236)}); |
| 106 | + set.setStackLabels(new String[]{ |
| 107 | + "Men", "Women" |
| 108 | + }); |
| 109 | + |
| 110 | + String []xVals = new String[]{"0-10", "10-20", "20-30", "30-40", "40-50", "50-60", "60-70", "70-80", "80-90", "90-100", "100+"}; |
| 111 | + |
| 112 | + BarData data = new BarData(xVals, set); |
| 113 | + mChart.setData(data); |
| 114 | + mChart.invalidate(); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 119 | + getMenuInflater().inflate(R.menu.bar, menu); |
| 120 | + return true; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 125 | + |
| 126 | + switch (item.getItemId()) { |
| 127 | + case R.id.actionToggleValues: { |
| 128 | + for (DataSet<?> set : mChart.getData().getDataSets()) |
| 129 | + set.setDrawValues(!set.isDrawValuesEnabled()); |
| 130 | + |
| 131 | + mChart.invalidate(); |
| 132 | + break; |
| 133 | + } |
| 134 | + case R.id.actionToggleHighlight: { |
| 135 | + if (mChart.isHighlightEnabled()) |
| 136 | + mChart.setHighlightEnabled(false); |
| 137 | + else |
| 138 | + mChart.setHighlightEnabled(true); |
| 139 | + mChart.invalidate(); |
| 140 | + break; |
| 141 | + } |
| 142 | + case R.id.actionTogglePinch: { |
| 143 | + if (mChart.isPinchZoomEnabled()) |
| 144 | + mChart.setPinchZoom(false); |
| 145 | + else |
| 146 | + mChart.setPinchZoom(true); |
| 147 | + |
| 148 | + mChart.invalidate(); |
| 149 | + break; |
| 150 | + } |
| 151 | + case R.id.actionToggleAutoScaleMinMax: { |
| 152 | + mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled()); |
| 153 | + mChart.notifyDataSetChanged(); |
| 154 | + break; |
| 155 | + } |
| 156 | + case R.id.actionToggleHighlightArrow: { |
| 157 | + if (mChart.isDrawHighlightArrowEnabled()) |
| 158 | + mChart.setDrawHighlightArrow(false); |
| 159 | + else |
| 160 | + mChart.setDrawHighlightArrow(true); |
| 161 | + mChart.invalidate(); |
| 162 | + break; |
| 163 | + } |
| 164 | + case R.id.actionToggleStartzero: { |
| 165 | + mChart.getAxisLeft().setStartAtZero(!mChart.getAxisLeft().isStartAtZeroEnabled()); |
| 166 | + mChart.getAxisRight().setStartAtZero(!mChart.getAxisRight().isStartAtZeroEnabled()); |
| 167 | + mChart.invalidate(); |
| 168 | + break; |
| 169 | + } |
| 170 | + case R.id.animateX: { |
| 171 | + mChart.animateX(3000); |
| 172 | + break; |
| 173 | + } |
| 174 | + case R.id.animateY: { |
| 175 | + mChart.animateY(3000); |
| 176 | + break; |
| 177 | + } |
| 178 | + case R.id.animateXY: { |
| 179 | + |
| 180 | + mChart.animateXY(3000, 3000); |
| 181 | + break; |
| 182 | + } |
| 183 | + case R.id.actionToggleFilter: { |
| 184 | + |
| 185 | + Approximator a = new Approximator(ApproximatorType.DOUGLAS_PEUCKER, 25); |
| 186 | + |
| 187 | + if (!mChart.isFilteringEnabled()) { |
| 188 | + mChart.enableFiltering(a); |
| 189 | + } else { |
| 190 | + mChart.disableFiltering(); |
| 191 | + } |
| 192 | + mChart.invalidate(); |
| 193 | + break; |
| 194 | + } |
| 195 | + case R.id.actionSave: { |
| 196 | + if (mChart.saveToGallery("title" + System.currentTimeMillis(), 50)) { |
| 197 | + Toast.makeText(getApplicationContext(), "Saving SUCCESSFUL!", |
| 198 | + Toast.LENGTH_SHORT).show(); |
| 199 | + } else |
| 200 | + Toast.makeText(getApplicationContext(), "Saving FAILED!", Toast.LENGTH_SHORT) |
| 201 | + .show(); |
| 202 | + break; |
| 203 | + } |
| 204 | + } |
| 205 | + return true; |
| 206 | + } |
| 207 | + |
| 208 | + @Override |
| 209 | + public void onValueSelected(Entry e, int dataSetIndex, Highlight h) { |
| 210 | + |
| 211 | + BarEntry entry = (BarEntry) e; |
| 212 | + Log.i("VAL SELECTED", |
| 213 | + "Value: " + entry.getVals()[h.getStackIndex()]); |
| 214 | + } |
| 215 | + |
| 216 | + @Override |
| 217 | + public void onNothingSelected() { |
| 218 | + // TODO Auto-generated method stub |
| 219 | + |
| 220 | + } |
| 221 | + |
| 222 | + private class CustomFormatter implements ValueFormatter { |
| 223 | + |
| 224 | + private DecimalFormat mFormat; |
| 225 | + |
| 226 | + public CustomFormatter() { |
| 227 | + mFormat = new DecimalFormat("###"); |
| 228 | + } |
| 229 | + |
| 230 | + @Override |
| 231 | + public String getFormattedValue(float value) { |
| 232 | + return mFormat.format(Math.abs(value)) + "m"; |
| 233 | + } |
| 234 | + } |
| 235 | +} |
0 commit comments