iosswiftios-charts

iOS-Chart: Select the data point programmatically but get the highLight is NAN


I'd like to select the last data point programmatically and draw gradient background, but I got the highLight is NAN so I can't get the coordinate to draw the gradient.I used :

chartView.highlightValue(Highlight(x: lastEntry.x, y: lastEntry.y, dataSetIndex: 0), callDelegate: true)

to select the last entry, and draw the gradient in the delegate function:

func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight)

but the highlight is NAN and then crash I also tried using "valueToPixelMatrix" way to get the coordinate, but the results are different from the highlight.xPx and highlight.yPx, i would like to ask how can i get the correct coordinate of data point on Chart View. Thank you.


Solution

  • I think like you use wrong way for customization of highlighting. You don't need to use this delegate function for drawing.

    You need to create your own renderer class and override drawHighlighted(context: CGContext, indices: [Highlight]) method. In this place you will get access to correct coordinates. Keep in your mind, that you need instantiate from properly parent class in depend of type of your chart view. (E.g. CandleStickChartRenderer, BarChartRenderer, LineChartRenderer, etc.)

    Don't forget set instance of your custom renderer class to renderer property of your chart view.

    Good luck.