I am new to MP Android Chart and I cannot figure out how to fill a certain range of Y with different colors. Highlighted as point 2 in the screenshot.
Also, after reading the function documentation, I try to change the color of the line highlighted in step 1 and fill the area under the graph with some color point 3. But points 1 and 3 do not work, and I could not understand how point 2 can. 
public class LineChartActivity extends AppCompatActivity {
private static final String TAG = "Test";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_line_chart);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Log.i(TAG, "Line Chart Activity Created");
LineChart chart = (LineChart) findViewById(R.id.chart);
LineData data = new LineData(getXAxisValues(), getDataSet());
chart.setData(data);
setXAxis(chart);
chart.getAxisRight().setDrawLabels(false);
chart.setGridBackgroundColor(128);
chart.setBorderColor(255);
chart.setDrawGridBackground(false);
chart.getLegend().setEnabled(false);
chart.setPinchZoom(true);
chart.setDescription("");
chart.setTouchEnabled(true);
chart.setDoubleTapToZoomEnabled(true);
chart.animateXY(2000, 2000);
chart.invalidate();
}
private ArrayList<ILineDataSet> getDataSet() {
ArrayList<ILineDataSet> dataSets = null;
ArrayList<Entry> valueSet1 = new ArrayList<>();
Entry v1e1 = new Entry(110.000f, 0);
valueSet1.add(v1e1);
Entry v1e2 = new Entry(40.000f, 4);
valueSet1.add(v1e2);
Entry v1e3 = new Entry(60.000f, 5);
valueSet1.add(v1e3);
Entry v1e4 = new Entry(30.000f, 6);
valueSet1.add(v1e4);
Entry v1e5 = new Entry(90.000f, 7);
valueSet1.add(v1e5);
Entry v1e6 = new Entry(100.000f, 8);
valueSet1.add(v1e6);
LineDataSet lineDataSet1 = new LineDataSet(valueSet1, "Brand 1");
int[] colors = new int[] {R.color.red ,R.color.red, R.color.orange ,R.color.green, R.color.orange, R.color.red };
lineDataSet1.setCircleColors(colors, this);
lineDataSet1.setCircleRadius(8f);
lineDataSet1.setLineWidth(3f);
lineDataSet1.setValueTextSize(20f);
lineDataSet1.enableDashedLine(6f, 18f, 0);
dataSets = new ArrayList<>();
dataSets.add(lineDataSet1);
return dataSets;
}
private ArrayList<String> getXAxisValues() {
ArrayList<String> xAxis = new ArrayList<>();
xAxis.add("JAN");
xAxis.add("FEB");
xAxis.add("MAR");
xAxis.add("APR");
xAxis.add("MAY");
xAxis.add("JUN");
xAxis.add("JUL");
xAxis.add("AUG");
xAxis.add("SEPT");
xAxis.add("OCT");
xAxis.add("NOV");
xAxis.add("DEC");
return xAxis;
}
private void setXAxis(LineChart chart){
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setTextSize(10f);
xAxis.setTextColor(Color.BLUE);
xAxis.setDrawAxisLine(true);
}
}
Many thanks! Ankit
source
share