Mpandroidchart: Help, the realization of this effect,MPAndroidChart setDrawFilled can not achieve

Created on 24 Oct 2016  ·  5Comments  ·  Source: PhilJay/MPAndroidChart

Thanks to PhilJay for developing the library!!
Hope to get help,
To achieve the following effects

2

Most helpful comment

Relevant xkcd

All 5 comments

3

This is what I am currently implementing.Fill color outside the line,There are some problems
The code is as follows:Help

private void initChart() {
lineChart.setDescription("");
lineChart.setNoDataTextDescription(getResources().getString(R.string.data_is_empty));
lineChart.setTouchEnabled(false);
lineChart.setDragDecelerationFrictionCoef(0.9f);

    // enable scaling and dragging
    lineChart.setDragEnabled(false);
    lineChart.setScaleEnabled(false);
    lineChart.setDrawGridBackground(false);
    lineChart.setHighlightPerDragEnabled(false);

    // if disabled, scaling can be done on x- and y-axis separately
    lineChart.setPinchZoom(false);
    lineChart.setBackgroundColor(Color.TRANSPARENT);

    // add data
    setChartData();
    lineChart.animateX(2000);
    Legend l = lineChart.getLegend();
    l.setEnabled(false);

    XAxis xAxis = lineChart.getXAxis();
    xAxis.setTypeface(mTfLight);
    xAxis.setTextSize(8f);
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setDrawGridLines(false);
    xAxis.setDrawAxisLine(false);
    xAxis.setValueFormatter(new IAxisValueFormatter() {
        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            return String.valueOf((int)value);
        }

        @Override
        public int getDecimalDigits() {
            return 0;
        }
    });


    YAxis leftAxis = lineChart.getAxisLeft();
    leftAxis.setTypeface(mTfLight);
    leftAxis.setTextSize(8f);
    leftAxis.setDrawGridLines(true);
    leftAxis.enableGridDashedLine(25f, 25f, 0f);
    leftAxis.setGridLineWidth(1f);
    leftAxis.setGranularityEnabled(true);
    leftAxis.setDrawAxisLine(false);
    leftAxis.setValueFormatter(new IAxisValueFormatter() {
        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            return "¥" + (int) value;
        }

        @Override
        public int getDecimalDigits() {
            return 0;
        }
    });

    YAxis rightAxis = lineChart.getAxisRight();
    rightAxis.setEnabled(false);
}


private void setChartData( ) {
    ArrayList<Entry> yValues1 = new ArrayList<>();
    yValues1.add(new Entry(1990, 58));
    yValues1.add(new Entry(1991, 63));
    yValues1.add(new Entry(1992, 68));
    yValues1.add(new Entry(1993, 73));
    yValues1.add(new Entry(1994, 109));

    ArrayList<Entry> yValues2 = new ArrayList<>();
    yValues2.add(new Entry(1990, 55));
    yValues2.add(new Entry(1991, 56));
    yValues2.add(new Entry(1992, 57));
    yValues2.add(new Entry(1993, 58));
    yValues2.add(new Entry(1994, 60));





    ArrayList<Entry> yValues3 = new ArrayList<>();
    yValues3.add(new Entry(1990, 58));
    yValues3.add(new Entry(1991, 62));
    yValues3.add(new Entry(1992, 66));
    yValues3.add(new Entry(1993, 71));
    yValues3.add(new Entry(1994, 76));

    ArrayList<Entry> yValues4 = new ArrayList<>();
    yValues4.add(new Entry(1990, 56));
    yValues4.add(new Entry(1991, 58));
    yValues4.add(new Entry(1992, 61));
    yValues4.add(new Entry(1993, 63));
    yValues4.add(new Entry(1994, 66));

    ArrayList<Entry> yValues5 = new ArrayList<>();
    yValues5.add(new Entry(1990, 57));
    yValues5.add(new Entry(1991, 61));
    yValues5.add(new Entry(1992, 64));
    yValues5.add(new Entry(1993, 68));
    yValues5.add(new Entry(1994, 72));


    LineDataSet set1, set2, set3, set4, set5;
    if (lineChart.getData() != null && lineChart.getData().getDataSetCount() > 0) {
        set1 = (LineDataSet) lineChart.getData().getDataSetByIndex(0);
        set2 = (LineDataSet) lineChart.getData().getDataSetByIndex(1);
        set3 = (LineDataSet) lineChart.getData().getDataSetByIndex(2);
        set4 = (LineDataSet) lineChart.getData().getDataSetByIndex(3);
        set5 = (LineDataSet) lineChart.getData().getDataSetByIndex(4);
        set1.setValues(yValues1);
        set2.setValues(yValues2);
        set3.setValues(yValues3);
        set4.setValues(yValues4);
        set5.setValues(yValues5);
        lineChart.getData().notifyDataChanged();
        lineChart.notifyDataSetChanged();
        lineChart.invalidate();
    } else {
        set1 = getLineDataSet(yValues1, "", Color.RED, yValues1.get(0).getY(), true);
        set2 = getLineDataSet(yValues2, "", Color.RED, yValues1.get(0).getY(), true);

        set3 = getLineDataSet(yValues3, "", Color.GREEN, yValues1.get(0).getY(), true);
        set4 = getLineDataSet(yValues4, "", Color.GREEN, yValues1.get(0).getY(), true);

        set5 = getLineDataSet(yValues5, "", R.color.black, 0, false);

        ArrayList<ILineDataSet> dataSets = new ArrayList<>();
        dataSets.add(set1);
        dataSets.add(set2);
        dataSets.add(set3);
        dataSets.add(set4);
        dataSets.add(set5);

        // create a data object with the datasets
        LineData data = new LineData(dataSets);
        data.setValueTypeface(mTfLight);
        data.setValueTextSize(9f);
        data.setDrawValues(false);


        // set data
        lineChart.setData(data);

    }
}


private LineDataSet getLineDataSet(ArrayList<Entry> yValues, String label, int color, final float fillLinePosition, boolean isFillColor) {
    LineDataSet lineDataSet = new LineDataSet(yValues, label);
    lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
    lineDataSet.setCubicIntensity(0.2f);
    if (isFillColor) {
        lineDataSet.setDrawFilled(true);
        lineDataSet.setFillAlpha(65);
        lineDataSet.setFillColor(color);
        lineDataSet.setFillFormatter(new IFillFormatter() {
            @Override
            public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
                return fillLinePosition;
            }
        });
    }
    lineDataSet.setDrawCircles(false);
    lineDataSet.setLineWidth(2f);
    lineDataSet.setColor(color);
    return lineDataSet;
}

@Pitel @nielsz @dseider @mikemonteith @matiash help

solved

Relevant xkcd

There is a need to contact me

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thanhcly920 picture thanhcly920  ·  3Comments

JungYongWook picture JungYongWook  ·  3Comments

andreyfel picture andreyfel  ·  3Comments

SutharRohit picture SutharRohit  ·  3Comments

manucheri picture manucheri  ·  3Comments