Reputation: 2146
How to add gradient above the line in mpandroidchart.Thanks in advance?
Upvotes: 0
Views: 425
Reputation: 2922
fade_red.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="-90"
android:startColor="#00ff0000"
android:endColor="#ffff0000" />
</shape>
CustomValueFormatter
class CustomValueFormatter extends ValueFormatter {
int maxValue;
public CustomValueFormatter(int maxValue) {
this.maxValue = maxValue;
}
@Override
public String getAxisLabel(float value, AxisBase axis) {
int newValue = (int) value + maxValue;
return newValue + "";
}
@Override
public String getPointLabel(Entry entry) {
int newValue = (int) entry.getY() + maxValue;
return newValue + "";
}
}
chart data & setup
chart.setDragEnabled(true);
chart.setScaleXEnabled(false);
chart.setScaleYEnabled(true);
chart.getAxisRight().setEnabled(false);
chart.getAxisLeft().setDrawAxisLine(false);
chart.getXAxis().setEnabled(false);
chart.getLegend().setEnabled(false);
chart.getDescription().setText("");
chart.setTouchEnabled(true);
ArrayList<Entry> list = new ArrayList<>();
int maxValue = 1000;
for (int i = 0; i < 10; i++) {
list.add(new Entry(i, (100 * i) - 1000));
}
chart.getAxisLeft().setAxisMaximum(0);
chart.getAxisLeft().setAxisMinimum(-maxValue);
chart.getAxisLeft().setLabelCount(5);
CustomValueFormatter valueFormatter = new CustomValueFormatter(maxValue);
chart.getAxisLeft().setValueFormatter(valueFormatter);
LineDataSet dataset = new LineDataSet(list, "");
int color = ContextCompat.getColor(this, R.color.orange);
dataset.setColor(color);
dataset.setDrawCircles(false);
dataset.setDrawFilled(true);
Drawable drawable = ContextCompat.getDrawable(this, R.drawable.fade_red);
dataset.setFillDrawable(drawable);
ArrayList<ILineDataSet> sets = new ArrayList<>();
sets.add(dataset);
LineData lineData = new LineData(sets);
lineData.setValueFormatter(valueFormatter);
chart.setData(lineData);
Upvotes: 0