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