简体   繁体   中英

How to add gradient colors above chart line android?

在此处输入图像描述

How to add gradient above the line in mpandroidchart.Thanks in advance?

在此处输入图像描述

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM