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.view.Menu; 12 import android.view.MenuItem; 13 import android.view.WindowManager; 14 import android.widget.SeekBar; 15 import android.widget.SeekBar.OnSeekBarChangeListener; 16 import android.widget.TextView; 17 18 import com.github.mikephil.charting.charts.BarChart; 19 import com.github.mikephil.charting.components.Legend; 20 import com.github.mikephil.charting.components.Legend.LegendForm; 21 import com.github.mikephil.charting.components.XAxis; 22 import com.github.mikephil.charting.components.YAxis; 23 import com.github.mikephil.charting.data.BarData; 24 import com.github.mikephil.charting.data.BarDataSet; 25 import com.github.mikephil.charting.data.BarEntry; 26 import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; 27 import com.github.mikephil.charting.utils.FileUtils; 28 import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase; 29 30 import java.util.ArrayList; 31 import java.util.List; 32 33 public class BarChartActivitySinus extends DemoBase implements OnSeekBarChangeListener { 34 35 private BarChart chart; 36 private SeekBar seekBarX; 37 private TextView tvX; 38 39 private List<BarEntry> data; 40 41 @Override onCreate(Bundle savedInstanceState)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_barchart_sinus); 47 48 setTitle("BarChartActivitySinus"); 49 50 data = FileUtils.loadBarEntriesFromAssets(getAssets(), "othersine.txt"); 51 52 tvX = findViewById(R.id.tvValueCount); 53 54 seekBarX = findViewById(R.id.seekbarValues); 55 56 chart = findViewById(R.id.chart1); 57 58 chart.setDrawBarShadow(false); 59 chart.setDrawValueAboveBar(true); 60 61 chart.getDescription().setEnabled(false); 62 63 // if more than 60 entries are displayed in the chart, no values will be 64 // drawn 65 chart.setMaxVisibleValueCount(60); 66 67 // scaling can now only be done on x- and y-axis separately 68 chart.setPinchZoom(false); 69 70 // draw shadows for each bar that show the maximum value 71 // chart.setDrawBarShadow(true); 72 73 // chart.setDrawXLabels(false); 74 75 chart.setDrawGridBackground(false); 76 // chart.setDrawYLabels(false); 77 78 XAxis xAxis = chart.getXAxis(); 79 xAxis.setEnabled(false); 80 81 YAxis leftAxis = chart.getAxisLeft(); 82 leftAxis.setTypeface(tfLight); 83 leftAxis.setLabelCount(6, false); 84 leftAxis.setAxisMinimum(-2.5f); 85 leftAxis.setAxisMaximum(2.5f); 86 leftAxis.setGranularityEnabled(true); 87 leftAxis.setGranularity(0.1f); 88 89 YAxis rightAxis = chart.getAxisRight(); 90 rightAxis.setDrawGridLines(false); 91 rightAxis.setTypeface(tfLight); 92 rightAxis.setLabelCount(6, false); 93 rightAxis.setAxisMinimum(-2.5f); 94 rightAxis.setAxisMaximum(2.5f); 95 rightAxis.setGranularity(0.1f); 96 97 seekBarX.setOnSeekBarChangeListener(this); 98 seekBarX.setProgress(150); // set data 99 100 Legend l = chart.getLegend(); 101 l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM); 102 l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT); 103 l.setOrientation(Legend.LegendOrientation.HORIZONTAL); 104 l.setDrawInside(false); 105 l.setForm(LegendForm.SQUARE); 106 l.setFormSize(9f); 107 l.setTextSize(11f); 108 l.setXEntrySpace(4f); 109 110 chart.animateXY(1500, 1500); 111 } 112 setData(int count)113 private void setData(int count) { 114 115 ArrayList<BarEntry> entries = new ArrayList<>(); 116 117 for (int i = 0; i < count; i++) { 118 entries.add(data.get(i)); 119 } 120 121 BarDataSet set; 122 123 if (chart.getData() != null && 124 chart.getData().getDataSetCount() > 0) { 125 set = (BarDataSet) chart.getData().getDataSetByIndex(0); 126 set.setValues(entries); 127 chart.getData().notifyDataChanged(); 128 chart.notifyDataSetChanged(); 129 } else { 130 set = new BarDataSet(entries, "Sinus Function"); 131 set.setColor(Color.rgb(240, 120, 124)); 132 } 133 134 BarData data = new BarData(set); 135 data.setValueTextSize(10f); 136 data.setValueTypeface(tfLight); 137 data.setDrawValues(false); 138 data.setBarWidth(0.8f); 139 140 chart.setData(data); 141 } 142 143 @Override onCreateOptionsMenu(Menu menu)144 public boolean onCreateOptionsMenu(Menu menu) { 145 getMenuInflater().inflate(R.menu.bar, menu); 146 return true; 147 } 148 149 @Override onOptionsItemSelected(MenuItem item)150 public boolean onOptionsItemSelected(MenuItem item) { 151 152 switch (item.getItemId()) { 153 case R.id.viewGithub: { 154 Intent i = new Intent(Intent.ACTION_VIEW); 155 i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivitySinus.java")); 156 startActivity(i); 157 break; 158 } 159 case R.id.actionToggleValues: { 160 for (IBarDataSet set : chart.getData().getDataSets()) 161 set.setDrawValues(!set.isDrawValuesEnabled()); 162 163 chart.invalidate(); 164 break; 165 } 166 case R.id.actionToggleHighlight: { 167 if (chart.getData() != null) { 168 chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled()); 169 chart.invalidate(); 170 } 171 break; 172 } 173 case R.id.actionTogglePinch: { 174 if (chart.isPinchZoomEnabled()) 175 chart.setPinchZoom(false); 176 else 177 chart.setPinchZoom(true); 178 179 chart.invalidate(); 180 break; 181 } 182 case R.id.actionToggleAutoScaleMinMax: { 183 chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled()); 184 chart.notifyDataSetChanged(); 185 break; 186 } 187 case R.id.actionToggleBarBorders: { 188 for (IBarDataSet set : chart.getData().getDataSets()) 189 ((BarDataSet) set).setBarBorderWidth(set.getBarBorderWidth() == 1.f ? 0.f : 1.f); 190 191 chart.invalidate(); 192 break; 193 } 194 case R.id.animateX: { 195 chart.animateX(2000); 196 break; 197 } 198 case R.id.animateY: { 199 chart.animateY(2000); 200 break; 201 } 202 case R.id.animateXY: { 203 204 chart.animateXY(2000, 2000); 205 break; 206 } 207 case R.id.actionSave: { 208 if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { 209 saveToGallery(); 210 } else { 211 requestStoragePermission(chart); 212 } 213 break; 214 } 215 } 216 return true; 217 } 218 219 @Override onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)220 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 221 222 tvX.setText(String.valueOf(seekBarX.getProgress())); 223 224 setData(seekBarX.getProgress()); 225 chart.invalidate(); 226 } 227 228 @Override saveToGallery()229 protected void saveToGallery() { 230 saveToGallery(chart, "BarChartActivitySinus"); 231 } 232 233 @Override onStartTrackingTouch(SeekBar seekBar)234 public void onStartTrackingTouch(SeekBar seekBar) {} 235 236 @Override onStopTrackingTouch(SeekBar seekBar)237 public void onStopTrackingTouch(SeekBar seekBar) {} 238 239 } 240