0

I have a list of Entry points which I have plotted on LineChart. Now I want to set icon to a particular point. How to set or draw icons in LineChart to my selected position in MPAndroidChart?

Sandeep Yohans
  • 929
  • 1
  • 14
  • 36

2 Answers2

1

You can add icon to your selected point using 3 parameters Entry constructor where third parameter is a Drawable:

public Entry(float x, float y, android.graphics.drawable.Drawable icon)

Parameters: x - the x value y - the y value (the actual value of the entry) icon - icon image

Ref: MPAndroidChart v3.0.3 JavaDoc

Example:

    ArrayList<Entry> values = new ArrayList<Entry>();
    values.add(new Entry (x, y, ContextCompat.getDrawable(getApplicationContext(),R.drawable.star)));

You can mark a single point with icon from entire LineDataSet by adding Drawable in Entry constructor.

Sandeep Yohans
  • 929
  • 1
  • 14
  • 36
  • it doesn't work for me. I added entries with icon but it's not shown. Should I add something else like : setDrawIcons(true) ? – Badr Yousfi Jul 28 '21 at 11:44
1

I know this is an old question but This is my solution in case anyone still needs it.

First, you need to call setDrawIcons(true) on your data set. Then You need to define your entry as a member inside your class to be able to reset it later.

private var selectedEntry: Entry? = null

And Inside onValueSelected you need to set selectedEntry's icon as null if it exists and set the icon for the new selectedEntry point.

override fun onValueSelected(e: Entry?, h: Highlight?) {
    if (selectedEntry != null) {
        selectedEntry?.icon = null
    }
    for (set in chart.data.dataSets) {
        selectedEntry = set.getEntryForIndex(set.getEntryIndex(e))
        selectedEntry?.icon = ContextCompat.getDrawable(this,R.drawable.star)
    }
    chart.invalidate()
}
Mohamed Nageh
  • 1,963
  • 1
  • 19
  • 27