• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 package com.xxmassdeveloper.mpchartexample;
3 
4 import android.content.Intent;
5 import android.graphics.Color;
6 import android.net.Uri;
7 import android.os.Bundle;
8 import android.view.Menu;
9 import android.view.MenuItem;
10 import android.view.WindowManager;
11 
12 import com.github.mikephil.charting.charts.LineChart;
13 import com.github.mikephil.charting.components.Legend;
14 import com.github.mikephil.charting.components.XAxis;
15 import com.github.mikephil.charting.components.YAxis;
16 import com.github.mikephil.charting.data.Entry;
17 import com.github.mikephil.charting.data.LineData;
18 import com.github.mikephil.charting.data.LineDataSet;
19 import com.github.mikephil.charting.formatter.IFillFormatter;
20 import com.github.mikephil.charting.interfaces.dataprovider.LineDataProvider;
21 import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
22 import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
23 
24 import java.util.ArrayList;
25 
26 /**
27  * This works by inverting the background and desired "fill" color. First, we draw the fill color
28  * that we want between the lines as the actual background of the chart. Then, we fill the area
29  * above the highest line and the area under the lowest line with the desired background color.
30  *
31  * This method makes it look like we filled the area between the lines, but really we are filling
32  * the area OUTSIDE the lines!
33  */
34 @SuppressWarnings("SameParameterValue")
35 public class FilledLineActivity extends DemoBase {
36 
37     private LineChart chart;
38     private final int fillColor = Color.argb(150, 51, 181, 229);
39 
40     @Override
onCreate(Bundle savedInstanceState)41     protected void onCreate(Bundle savedInstanceState) {
42         super.onCreate(savedInstanceState);
43         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
44                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
45         setContentView(R.layout.activity_linechart_noseekbar);
46 
47         setTitle("FilledLineActivity");
48 
49         chart = findViewById(R.id.chart1);
50         chart.setBackgroundColor(Color.WHITE);
51         chart.setGridBackgroundColor(fillColor);
52         chart.setDrawGridBackground(true);
53 
54         chart.setDrawBorders(true);
55 
56         // no description text
57         chart.getDescription().setEnabled(false);
58 
59         // if disabled, scaling can be done on x- and y-axis separately
60         chart.setPinchZoom(false);
61 
62         Legend l = chart.getLegend();
63         l.setEnabled(false);
64 
65         XAxis xAxis = chart.getXAxis();
66         xAxis.setEnabled(false);
67 
68         YAxis leftAxis = chart.getAxisLeft();
69         leftAxis.setAxisMaximum(900f);
70         leftAxis.setAxisMinimum(-250f);
71         leftAxis.setDrawAxisLine(false);
72         leftAxis.setDrawZeroLine(false);
73         leftAxis.setDrawGridLines(false);
74 
75         chart.getAxisRight().setEnabled(false);
76 
77         // add data
78         setData(100, 60);
79 
80         chart.invalidate();
81     }
82 
setData(int count, float range)83     private void setData(int count, float range) {
84 
85         ArrayList<Entry> values1 = new ArrayList<>();
86 
87         for (int i = 0; i < count; i++) {
88             float val = (float) (Math.random() * range) + 50;
89             values1.add(new Entry(i, val));
90         }
91 
92         ArrayList<Entry> values2 = new ArrayList<>();
93 
94         for (int i = 0; i < count; i++) {
95             float val = (float) (Math.random() * range) + 450;
96             values2.add(new Entry(i, val));
97         }
98 
99         LineDataSet set1, set2;
100 
101         if (chart.getData() != null &&
102                 chart.getData().getDataSetCount() > 0) {
103             set1 = (LineDataSet) chart.getData().getDataSetByIndex(0);
104             set2 = (LineDataSet) chart.getData().getDataSetByIndex(1);
105             set1.setValues(values1);
106             set2.setValues(values2);
107             chart.getData().notifyDataChanged();
108             chart.notifyDataSetChanged();
109         } else {
110             // create a dataset and give it a type
111             set1 = new LineDataSet(values1, "DataSet 1");
112 
113             set1.setAxisDependency(YAxis.AxisDependency.LEFT);
114             set1.setColor(Color.rgb(255, 241, 46));
115             set1.setDrawCircles(false);
116             set1.setLineWidth(2f);
117             set1.setCircleRadius(3f);
118             set1.setFillAlpha(255);
119             set1.setDrawFilled(true);
120             set1.setFillColor(Color.WHITE);
121             set1.setHighLightColor(Color.rgb(244, 117, 117));
122             set1.setDrawCircleHole(false);
123             set1.setFillFormatter(new IFillFormatter() {
124                 @Override
125                 public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
126                     // change the return value here to better understand the effect
127                     // return 0;
128                     return chart.getAxisLeft().getAxisMinimum();
129                 }
130             });
131 
132             // create a dataset and give it a type
133             set2 = new LineDataSet(values2, "DataSet 2");
134             set2.setAxisDependency(YAxis.AxisDependency.LEFT);
135             set2.setColor(Color.rgb(255, 241, 46));
136             set2.setDrawCircles(false);
137             set2.setLineWidth(2f);
138             set2.setCircleRadius(3f);
139             set2.setFillAlpha(255);
140             set2.setDrawFilled(true);
141             set2.setFillColor(Color.WHITE);
142             set2.setDrawCircleHole(false);
143             set2.setHighLightColor(Color.rgb(244, 117, 117));
144             set2.setFillFormatter(new IFillFormatter() {
145                 @Override
146                 public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
147                     // change the return value here to better understand the effect
148                     // return 600;
149                     return chart.getAxisLeft().getAxisMaximum();
150                 }
151             });
152 
153             ArrayList<ILineDataSet> dataSets = new ArrayList<>();
154             dataSets.add(set1); // add the data sets
155             dataSets.add(set2);
156 
157             // create a data object with the data sets
158             LineData data = new LineData(dataSets);
159             data.setDrawValues(false);
160 
161             // set data
162             chart.setData(data);
163         }
164     }
165 
166     @Override
onCreateOptionsMenu(Menu menu)167     public boolean onCreateOptionsMenu(Menu menu) {
168         getMenuInflater().inflate(R.menu.only_github, menu);
169         return true;
170     }
171 
172     @Override
onOptionsItemSelected(MenuItem item)173     public boolean onOptionsItemSelected(MenuItem item) {
174 
175         switch (item.getItemId()) {
176             case R.id.viewGithub: {
177                 Intent i = new Intent(Intent.ACTION_VIEW);
178                 i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/FilledLineActivity.java"));
179                 startActivity(i);
180                 break;
181             }
182         }
183 
184         return true;
185     }
186 
187     @Override
saveToGallery()188     public void saveToGallery() { /* Intentionally left empty */ }
189 }
190