1

I would like to make a graph like this :

enter image description here

The problem is I don't know how to set a gradient color like this using MPAndroidChart. Maybe I should use an other library ?

Maybe it's better to use progressbar with transparent color (and gradient background) ?

This is my code :

val entries = ArrayList<BarEntry>()
    entries.add(BarEntry(1.toFloat(), 43.toFloat()))
    entries.add(BarEntry(2.toFloat(), 3.toFloat()))
    entries.add(BarEntry(3.toFloat(), 13.toFloat()))
    entries.add(BarEntry(4.toFloat(), 41.toFloat()))
    entries.add(BarEntry(5.toFloat(), 22.toFloat()))
    entries.add(BarEntry(6.toFloat(), 11.toFloat()))
    entries.add(BarEntry(7.toFloat(), 13.toFloat()))
    entries.add(BarEntry(8.toFloat(), 99.toFloat()))
    entries.add(BarEntry(9.toFloat(), 67.toFloat()))
    entries.add(BarEntry(10.toFloat(), 3.toFloat()))
    entries.add(BarEntry(11.toFloat(), 56.toFloat()))
    entries.add(BarEntry(12.toFloat(), 88.toFloat()))



    val dataSet = BarDataSet(entries, "Label")


    chart.data = BarData(dataSet)
Jéwôm'
  • 3,753
  • 5
  • 40
  • 73

2 Answers2

2

This worked for me:

dataset.setGradientColor(Color.parseColor("#00FF5722"),Color.parseColor("#FFFF5722"));
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
HAZEEM JOONUS
  • 483
  • 6
  • 9
-2

You can use setGradientFill() method which will accept n number of color.

int[] colors = { getResources().getColor(R.color.menu_text),
 getResources().getColor(android.R.color.white) };

float[] index = { 0, 1 };
dataset.setGradientFill(colors, index);

for more you can refer this. Hope it will help you!!

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
  • .setGradientFill is not a known method in v 3.1.0-alpha (latest as of today), so this solutions seems outdated. There are other methods; .setGradientColor() and .setGradientColors(), but these does not work (at least not on Horizontal Bar graph). – carl Apr 17 '21 at 09:10