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 15 import com.github.mikephil.charting.animation.Easing; 16 import com.github.mikephil.charting.charts.RadarChart; 17 import com.github.mikephil.charting.components.AxisBase; 18 import com.github.mikephil.charting.components.Legend; 19 import com.github.mikephil.charting.components.MarkerView; 20 import com.github.mikephil.charting.components.XAxis; 21 import com.github.mikephil.charting.components.YAxis; 22 import com.github.mikephil.charting.data.RadarData; 23 import com.github.mikephil.charting.data.RadarDataSet; 24 import com.github.mikephil.charting.data.RadarEntry; 25 import com.github.mikephil.charting.formatter.IAxisValueFormatter; 26 import com.github.mikephil.charting.interfaces.datasets.IDataSet; 27 import com.github.mikephil.charting.interfaces.datasets.IRadarDataSet; 28 import com.xxmassdeveloper.mpchartexample.custom.RadarMarkerView; 29 import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase; 30 31 import java.util.ArrayList; 32 33 public class RadarChartActivity extends DemoBase { 34 35 private RadarChart chart; 36 37 @Override onCreate(Bundle savedInstanceState)38 protected void onCreate(Bundle savedInstanceState) { 39 super.onCreate(savedInstanceState); 40 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 41 WindowManager.LayoutParams.FLAG_FULLSCREEN); 42 setContentView(R.layout.activity_radarchart); 43 44 setTitle("RadarChartActivity"); 45 46 chart = findViewById(R.id.chart1); 47 chart.setBackgroundColor(Color.rgb(60, 65, 82)); 48 49 chart.getDescription().setEnabled(false); 50 51 chart.setWebLineWidth(1f); 52 chart.setWebColor(Color.LTGRAY); 53 chart.setWebLineWidthInner(1f); 54 chart.setWebColorInner(Color.LTGRAY); 55 chart.setWebAlpha(100); 56 57 // create a custom MarkerView (extend MarkerView) and specify the layout 58 // to use for it 59 MarkerView mv = new RadarMarkerView(this, R.layout.radar_markerview); 60 mv.setChartView(chart); // For bounds control 61 chart.setMarker(mv); // Set the marker to the chart 62 63 setData(); 64 65 chart.animateXY(1400, 1400, Easing.EaseInOutQuad); 66 67 XAxis xAxis = chart.getXAxis(); 68 xAxis.setTypeface(tfLight); 69 xAxis.setTextSize(9f); 70 xAxis.setYOffset(0f); 71 xAxis.setXOffset(0f); 72 xAxis.setValueFormatter(new IAxisValueFormatter() { 73 74 private final String[] mActivities = new String[]{"Burger", "Steak", "Salad", "Pasta", "Pizza"}; 75 76 @Override 77 public String getFormattedValue(float value, AxisBase axis) { 78 return mActivities[(int) value % mActivities.length]; 79 } 80 }); 81 xAxis.setTextColor(Color.WHITE); 82 83 YAxis yAxis = chart.getYAxis(); 84 yAxis.setTypeface(tfLight); 85 yAxis.setLabelCount(5, false); 86 yAxis.setTextSize(9f); 87 yAxis.setAxisMinimum(0f); 88 yAxis.setAxisMaximum(80f); 89 yAxis.setDrawLabels(false); 90 91 Legend l = chart.getLegend(); 92 l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); 93 l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER); 94 l.setOrientation(Legend.LegendOrientation.HORIZONTAL); 95 l.setDrawInside(false); 96 l.setTypeface(tfLight); 97 l.setXEntrySpace(7f); 98 l.setYEntrySpace(5f); 99 l.setTextColor(Color.WHITE); 100 } 101 setData()102 private void setData() { 103 104 float mul = 80; 105 float min = 20; 106 int cnt = 5; 107 108 ArrayList<RadarEntry> entries1 = new ArrayList<>(); 109 ArrayList<RadarEntry> entries2 = new ArrayList<>(); 110 111 // NOTE: The order of the entries when being added to the entries array determines their position around the center of 112 // the chart. 113 for (int i = 0; i < cnt; i++) { 114 float val1 = (float) (Math.random() * mul) + min; 115 entries1.add(new RadarEntry(val1)); 116 117 float val2 = (float) (Math.random() * mul) + min; 118 entries2.add(new RadarEntry(val2)); 119 } 120 121 RadarDataSet set1 = new RadarDataSet(entries1, "Last Week"); 122 set1.setColor(Color.rgb(103, 110, 129)); 123 set1.setFillColor(Color.rgb(103, 110, 129)); 124 set1.setDrawFilled(true); 125 set1.setFillAlpha(180); 126 set1.setLineWidth(2f); 127 set1.setDrawHighlightCircleEnabled(true); 128 set1.setDrawHighlightIndicators(false); 129 130 RadarDataSet set2 = new RadarDataSet(entries2, "This Week"); 131 set2.setColor(Color.rgb(121, 162, 175)); 132 set2.setFillColor(Color.rgb(121, 162, 175)); 133 set2.setDrawFilled(true); 134 set2.setFillAlpha(180); 135 set2.setLineWidth(2f); 136 set2.setDrawHighlightCircleEnabled(true); 137 set2.setDrawHighlightIndicators(false); 138 139 ArrayList<IRadarDataSet> sets = new ArrayList<>(); 140 sets.add(set1); 141 sets.add(set2); 142 143 RadarData data = new RadarData(sets); 144 data.setValueTypeface(tfLight); 145 data.setValueTextSize(8f); 146 data.setDrawValues(false); 147 data.setValueTextColor(Color.WHITE); 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.radar, 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/RadarChartActivity.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.actionToggleHighlight: { 177 if (chart.getData() != null) { 178 chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled()); 179 chart.invalidate(); 180 } 181 break; 182 } 183 case R.id.actionToggleRotate: { 184 if (chart.isRotationEnabled()) 185 chart.setRotationEnabled(false); 186 else 187 chart.setRotationEnabled(true); 188 chart.invalidate(); 189 break; 190 } 191 case R.id.actionToggleFilled: { 192 193 ArrayList<IRadarDataSet> sets = (ArrayList<IRadarDataSet>) chart.getData() 194 .getDataSets(); 195 196 for (IRadarDataSet set : sets) { 197 if (set.isDrawFilledEnabled()) 198 set.setDrawFilled(false); 199 else 200 set.setDrawFilled(true); 201 } 202 chart.invalidate(); 203 break; 204 } 205 case R.id.actionToggleHighlightCircle: { 206 207 ArrayList<IRadarDataSet> sets = (ArrayList<IRadarDataSet>) chart.getData() 208 .getDataSets(); 209 210 for (IRadarDataSet set : sets) { 211 set.setDrawHighlightCircleEnabled(!set.isDrawHighlightCircleEnabled()); 212 } 213 chart.invalidate(); 214 break; 215 } 216 case R.id.actionToggleXLabels: { 217 chart.getXAxis().setEnabled(!chart.getXAxis().isEnabled()); 218 chart.notifyDataSetChanged(); 219 chart.invalidate(); 220 break; 221 } 222 case R.id.actionToggleYLabels: { 223 224 chart.getYAxis().setEnabled(!chart.getYAxis().isEnabled()); 225 chart.invalidate(); 226 break; 227 } 228 case R.id.animateX: { 229 chart.animateX(1400); 230 break; 231 } 232 case R.id.animateY: { 233 chart.animateY(1400); 234 break; 235 } 236 case R.id.animateXY: { 237 chart.animateXY(1400, 1400); 238 break; 239 } 240 case R.id.actionToggleSpin: { 241 chart.spin(2000, chart.getRotationAngle(), chart.getRotationAngle() + 360, Easing.EaseInOutCubic); 242 break; 243 } 244 case R.id.actionSave: { 245 if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { 246 saveToGallery(); 247 } else { 248 requestStoragePermission(chart); 249 } 250 break; 251 } 252 } 253 return true; 254 } 255 256 @Override saveToGallery()257 protected void saveToGallery() { 258 saveToGallery(chart, "RadarChartActivity"); 259 } 260 } 261