Как добавить градиентные цвета над линией графика android? - PullRequest
0 голосов
/ 03 августа 2020

введите описание изображения здесь

Как добавить градиент над линией в mpandroidchart. Заранее спасибо?

1 Ответ

0 голосов
/ 04 августа 2020

enter image description here

fade_red.xml

<?xml version="1.0" encoding="utf-8"?>
  

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