adann96d
adann96d

Reputation: 41

MPAndroidChart - ways of creating colorful line chart with gradient

enter image description here

I am looking for any simple code example with explanation how to color a blue part of the line chart below. I need to implement a gradient consisting of three colors: gold, red and dark. Here is my code which creates the chart:

            LineDataSet lineDataSet = new LineDataSet(currRates,"Last 5 days rates");
            lineDataSet.setValues(currRates);

            lineDataSet.setDrawIcons(false);
            lineDataSet.enableDashedLine(10f, 5f, 0f);
            lineDataSet.enableDashedHighlightLine(10f, 5f, 0f);
            lineDataSet.setColor(Color.DKGRAY);
            lineDataSet.setCircleColor(Color.DKGRAY);
            lineDataSet.setLineWidth(1f);
            lineDataSet.setCircleRadius(3f);
            lineDataSet.setDrawCircleHole(false);
            lineDataSet.setDrawFilled(true);

            lineDataSet.setFormLineWidth(1f);
            lineDataSet.setFormLineDashEffect(new DashPathEffect(new float[]{10f, 5f}, 0f));
            lineDataSet.setFormSize(15.f);
            lineDataSet.setValueTextColor(Color.BLACK);
            lineDataSet.setValueTextSize(18f);

            YAxis rightYAxis = lineChart.getAxisRight();
            XAxis topXAxis = lineChart.getXAxis();
            rightYAxis.setEnabled(false);
            topXAxis.setEnabled(false);

            ArrayList<ILineDataSet> dataSets = new ArrayList<>();
            dataSets.add(lineDataSet);
            LineData data = new LineData(dataSets);
            XAxis xAxis = lineChart.getXAxis();
            xAxis.setSpaceMin(0.5f);
            xAxis.setSpaceMax(0.5f);
            lineChart.setData(data);
            lineChart.animateY(1000);

Is anyone able to help me?

Upvotes: 0

Views: 336

Answers (1)

aiwiguna
aiwiguna

Reputation: 2922

create a xml gradient

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

<shape    xmlns:android="http://schemas.android.com/apk/res/android">

<gradient android:startColor="#9A0C0C"
          android:centerColor="#CE9908"
          android:endColor="#3091FF"
          android:angle="270"/> </shape>

and set the setFillDrawable of the dataset

Drawable drawable = ContextCompat.getDrawable(this, R.drawable.your_xml_name);
dataset.setFillDrawable(drawable);

Upvotes: 1

Related Questions