• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 package com.xxmassdeveloper.mpchartexample;
3 
4 import android.Manifest;
5 import android.content.Intent;
6 import android.content.pm.PackageManager;
7 import android.graphics.Color;
8 import android.net.Uri;
9 import android.os.Bundle;
10 import androidx.core.content.ContextCompat;
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 
19 import com.github.mikephil.charting.charts.BubbleChart;
20 import com.github.mikephil.charting.components.Legend;
21 import com.github.mikephil.charting.components.XAxis;
22 import com.github.mikephil.charting.components.YAxis;
23 import com.github.mikephil.charting.data.BubbleData;
24 import com.github.mikephil.charting.data.BubbleDataSet;
25 import com.github.mikephil.charting.data.BubbleEntry;
26 import com.github.mikephil.charting.data.Entry;
27 import com.github.mikephil.charting.highlight.Highlight;
28 import com.github.mikephil.charting.interfaces.datasets.IBubbleDataSet;
29 import com.github.mikephil.charting.interfaces.datasets.IDataSet;
30 import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
31 import com.github.mikephil.charting.utils.ColorTemplate;
32 import com.github.mikephil.charting.utils.MPPointF;
33 import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
34 
35 import java.util.ArrayList;
36 
37 public class BubbleChartActivity extends DemoBase implements OnSeekBarChangeListener,
38         OnChartValueSelectedListener {
39 
40     private BubbleChart chart;
41     private SeekBar seekBarX, seekBarY;
42     private TextView tvX, tvY;
43 
44     @Override
onCreate(Bundle savedInstanceState)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_bubblechart);
50 
51         setTitle("BubbleChartActivity");
52 
53         tvX = findViewById(R.id.tvXMax);
54         tvY = findViewById(R.id.tvYMax);
55 
56         seekBarX = findViewById(R.id.seekBar1);
57         seekBarX.setOnSeekBarChangeListener(this);
58 
59         seekBarY = findViewById(R.id.seekBar2);
60         seekBarY.setOnSeekBarChangeListener(this);
61 
62         chart = findViewById(R.id.chart1);
63         chart.getDescription().setEnabled(false);
64 
65         chart.setOnChartValueSelectedListener(this);
66 
67         chart.setDrawGridBackground(false);
68 
69         chart.setTouchEnabled(true);
70 
71         // enable scaling and dragging
72         chart.setDragEnabled(true);
73         chart.setScaleEnabled(true);
74 
75         chart.setMaxVisibleValueCount(200);
76         chart.setPinchZoom(true);
77 
78         seekBarX.setProgress(10);
79         seekBarY.setProgress(50);
80 
81         Legend l = chart.getLegend();
82         l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
83         l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
84         l.setOrientation(Legend.LegendOrientation.VERTICAL);
85         l.setDrawInside(false);
86         l.setTypeface(tfLight);
87 
88         YAxis yl = chart.getAxisLeft();
89         yl.setTypeface(tfLight);
90         yl.setSpaceTop(30f);
91         yl.setSpaceBottom(30f);
92         yl.setDrawZeroLine(false);
93 
94         chart.getAxisRight().setEnabled(false);
95 
96         XAxis xl = chart.getXAxis();
97         xl.setPosition(XAxis.XAxisPosition.BOTTOM);
98         xl.setTypeface(tfLight);
99     }
100 
101     @Override
onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)102     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
103 
104         int count = seekBarX.getProgress();
105         int range = seekBarY.getProgress();
106 
107         tvX.setText(String.valueOf(count));
108         tvY.setText(String.valueOf(range));
109 
110         ArrayList<BubbleEntry> values1 = new ArrayList<>();
111         ArrayList<BubbleEntry> values2 = new ArrayList<>();
112         ArrayList<BubbleEntry> values3 = new ArrayList<>();
113 
114         for (int i = 0; i < count; i++) {
115             values1.add(new BubbleEntry(i, (float) (Math.random() * range), (float) (Math.random() * range), getResources().getDrawable(R.drawable.star)));
116             values2.add(new BubbleEntry(i, (float) (Math.random() * range), (float) (Math.random() * range), getResources().getDrawable(R.drawable.star)));
117             values3.add(new BubbleEntry(i, (float) (Math.random() * range), (float) (Math.random() * range)));
118         }
119 
120         // create a dataset and give it a type
121         BubbleDataSet set1 = new BubbleDataSet(values1, "DS 1");
122         set1.setDrawIcons(false);
123         set1.setColor(ColorTemplate.COLORFUL_COLORS[0], 130);
124         set1.setDrawValues(true);
125 
126         BubbleDataSet set2 = new BubbleDataSet(values2, "DS 2");
127         set2.setDrawIcons(false);
128         set2.setIconsOffset(new MPPointF(0, 15));
129         set2.setColor(ColorTemplate.COLORFUL_COLORS[1], 130);
130         set2.setDrawValues(true);
131 
132         BubbleDataSet set3 = new BubbleDataSet(values3, "DS 3");
133         set3.setColor(ColorTemplate.COLORFUL_COLORS[2], 130);
134         set3.setDrawValues(true);
135 
136         ArrayList<IBubbleDataSet> dataSets = new ArrayList<>();
137         dataSets.add(set1); // add the data sets
138         dataSets.add(set2);
139         dataSets.add(set3);
140 
141         // create a data object with the data sets
142         BubbleData data = new BubbleData(dataSets);
143         data.setDrawValues(false);
144         data.setValueTypeface(tfLight);
145         data.setValueTextSize(8f);
146         data.setValueTextColor(Color.WHITE);
147         data.setHighlightCircleWidth(1.5f);
148 
149         chart.setData(data);
150         chart.invalidate();
151     }
152 
153     @Override
onCreateOptionsMenu(Menu menu)154     public boolean onCreateOptionsMenu(Menu menu) {
155         getMenuInflater().inflate(R.menu.bubble, menu);
156         return true;
157     }
158 
159     @Override
onOptionsItemSelected(MenuItem item)160     public boolean onOptionsItemSelected(MenuItem item) {
161 
162         switch (item.getItemId()) {
163             case R.id.viewGithub: {
164                 Intent i = new Intent(Intent.ACTION_VIEW);
165                 i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BubbleChartActivity.java"));
166                 startActivity(i);
167                 break;
168             }
169             case R.id.actionToggleValues: {
170                 for (IDataSet set : chart.getData().getDataSets())
171                     set.setDrawValues(!set.isDrawValuesEnabled());
172 
173                 chart.invalidate();
174                 break;
175             }
176             case R.id.actionToggleIcons: {
177                 for (IDataSet set : chart.getData().getDataSets())
178                     set.setDrawIcons(!set.isDrawIconsEnabled());
179 
180                 chart.invalidate();
181                 break;
182             }
183             case R.id.actionToggleHighlight: {
184                 if(chart.getData() != null) {
185                     chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled());
186                     chart.invalidate();
187                 }
188                 break;
189             }
190             case R.id.actionTogglePinch: {
191                 if (chart.isPinchZoomEnabled())
192                     chart.setPinchZoom(false);
193                 else
194                     chart.setPinchZoom(true);
195 
196                 chart.invalidate();
197                 break;
198             }
199             case R.id.actionToggleAutoScaleMinMax: {
200                 chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled());
201                 chart.notifyDataSetChanged();
202                 break;
203             }
204             case R.id.actionSave: {
205                 if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
206                     saveToGallery();
207                 } else {
208                     requestStoragePermission(chart);
209                 }
210                 break;
211             }
212             case R.id.animateX: {
213                 chart.animateX(2000);
214                 break;
215             }
216             case R.id.animateY: {
217                 chart.animateY(2000);
218                 break;
219             }
220             case R.id.animateXY: {
221                 chart.animateXY(2000, 2000);
222                 break;
223             }
224         }
225         return true;
226     }
227 
228     @Override
saveToGallery()229     protected void saveToGallery() {
230         saveToGallery(chart, "BubbleChartActivity");
231     }
232 
233     @Override
onValueSelected(Entry e, Highlight h)234     public void onValueSelected(Entry e, Highlight h) {
235         Log.i("VAL SELECTED",
236                 "Value: " + e.getY() + ", xIndex: " + e.getX()
237                         + ", DataSet index: " + h.getDataSetIndex());
238     }
239 
240     @Override
onNothingSelected()241     public void onNothingSelected() {}
242 
243     @Override
onStartTrackingTouch(SeekBar seekBar)244     public void onStartTrackingTouch(SeekBar seekBar) {}
245 
246     @Override
onStopTrackingTouch(SeekBar seekBar)247     public void onStopTrackingTouch(SeekBar seekBar) {}
248 }
249