1 2 package com.xxmassdeveloper.mpchartexample; 3 4 import android.content.Intent; 5 import android.graphics.Color; 6 import android.graphics.Typeface; 7 import android.net.Uri; 8 import android.os.Bundle; 9 import android.view.Menu; 10 import android.view.MenuItem; 11 import android.view.WindowManager; 12 13 import com.github.mikephil.charting.charts.LineChart; 14 import com.github.mikephil.charting.components.Legend; 15 import com.github.mikephil.charting.data.Entry; 16 import com.github.mikephil.charting.data.LineData; 17 import com.github.mikephil.charting.data.LineDataSet; 18 import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase; 19 20 import java.util.ArrayList; 21 22 @SuppressWarnings("SameParameterValue") 23 public class LineChartActivityColored extends DemoBase { 24 25 private final LineChart[] charts = new LineChart[4]; 26 27 @Override onCreate(Bundle savedInstanceState)28 protected void onCreate(Bundle savedInstanceState) { 29 super.onCreate(savedInstanceState); 30 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 31 WindowManager.LayoutParams.FLAG_FULLSCREEN); 32 setContentView(R.layout.activity_colored_lines); 33 34 setTitle("LineChartActivityColored"); 35 36 charts[0] = findViewById(R.id.chart1); 37 charts[1] = findViewById(R.id.chart2); 38 charts[2] = findViewById(R.id.chart3); 39 charts[3] = findViewById(R.id.chart4); 40 41 Typeface mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Bold.ttf"); 42 43 for (int i = 0; i < charts.length; i++) { 44 45 LineData data = getData(36, 100); 46 data.setValueTypeface(mTf); 47 48 // add some transparency to the color with "& 0x90FFFFFF" 49 setupChart(charts[i], data, colors[i % colors.length]); 50 } 51 } 52 53 private final int[] colors = new int[] { 54 Color.rgb(137, 230, 81), 55 Color.rgb(240, 240, 30), 56 Color.rgb(89, 199, 250), 57 Color.rgb(250, 104, 104) 58 }; 59 setupChart(LineChart chart, LineData data, int color)60 private void setupChart(LineChart chart, LineData data, int color) { 61 62 ((LineDataSet) data.getDataSetByIndex(0)).setCircleHoleColor(color); 63 64 // no description text 65 chart.getDescription().setEnabled(false); 66 67 // chart.setDrawHorizontalGrid(false); 68 // 69 // enable / disable grid background 70 chart.setDrawGridBackground(false); 71 // chart.getRenderer().getGridPaint().setGridColor(Color.WHITE & 0x70FFFFFF); 72 73 // enable touch gestures 74 chart.setTouchEnabled(true); 75 76 // enable scaling and dragging 77 chart.setDragEnabled(true); 78 chart.setScaleEnabled(true); 79 80 // if disabled, scaling can be done on x- and y-axis separately 81 chart.setPinchZoom(false); 82 83 chart.setBackgroundColor(color); 84 85 // set custom chart offsets (automatic offset calculation is hereby disabled) 86 chart.setViewPortOffsets(10, 0, 10, 0); 87 88 // add data 89 chart.setData(data); 90 91 // get the legend (only possible after setting data) 92 Legend l = chart.getLegend(); 93 l.setEnabled(false); 94 95 chart.getAxisLeft().setEnabled(false); 96 chart.getAxisLeft().setSpaceTop(40); 97 chart.getAxisLeft().setSpaceBottom(40); 98 chart.getAxisRight().setEnabled(false); 99 100 chart.getXAxis().setEnabled(false); 101 102 // animate calls invalidate()... 103 chart.animateX(2500); 104 } 105 getData(int count, float range)106 private LineData getData(int count, float range) { 107 108 ArrayList<Entry> values = new ArrayList<>(); 109 110 for (int i = 0; i < count; i++) { 111 float val = (float) (Math.random() * range) + 3; 112 values.add(new Entry(i, val)); 113 } 114 115 // create a dataset and give it a type 116 LineDataSet set1 = new LineDataSet(values, "DataSet 1"); 117 // set1.setFillAlpha(110); 118 // set1.setFillColor(Color.RED); 119 120 set1.setLineWidth(1.75f); 121 set1.setCircleRadius(5f); 122 set1.setCircleHoleRadius(2.5f); 123 set1.setColor(Color.WHITE); 124 set1.setCircleColor(Color.WHITE); 125 set1.setHighLightColor(Color.WHITE); 126 set1.setDrawValues(false); 127 128 // create a data object with the data sets 129 return new LineData(set1); 130 } 131 132 @Override onCreateOptionsMenu(Menu menu)133 public boolean onCreateOptionsMenu(Menu menu) { 134 getMenuInflater().inflate(R.menu.only_github, menu); 135 return true; 136 } 137 138 @Override onOptionsItemSelected(MenuItem item)139 public boolean onOptionsItemSelected(MenuItem item) { 140 141 switch (item.getItemId()) { 142 case R.id.viewGithub: { 143 Intent i = new Intent(Intent.ACTION_VIEW); 144 i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivityColored.java")); 145 startActivity(i); 146 break; 147 } 148 } 149 150 return true; 151 } 152 153 @Override saveToGallery()154 public void saveToGallery() { /* Intentionally left empty */ } 155 } 156