1

I'm using swift charts to make a line chart. I am drawing a circle on the last value, but I want to get the position of that circle so I can add an animation to it. Is this possible in charts?

My Chart

func setupIntradayLine(data: [ChartDataEntry], percentChange: Double) -> LineChartDataSet {
    let line = LineChartDataSet(entries: data)
    line.colors = [.white]
    line.lineWidth = 2.0
    line.mode = .linear
    line.drawHorizontalHighlightIndicatorEnabled = false
    line.highlightColor = .softWhite
    var colors = [UIColor]()
    for i in 0..<data.count {
        if(i == data.count - 1) {
            colors.append(UIColor.white)
        } else {
            colors.append(UIColor.clear)
        }
    }
    line.circleColors = colors
    line.circleHoleRadius = 0
    line.circleRadius = 3.5
    line.drawCirclesEnabled = true
    return line
}
Nick
  • 247
  • 2
  • 9

1 Answers1

2

first of all: I think this code is generating circles for every entry not only last entry, because of this line:

line.drawCirclesEnabled = true

now to answer your question, there is a method in ChartViewDelegate which calls when user select a value:

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

}

you can get position of circle (or actually highlighter that pointing to the selected value):

let x = highlight.xPx
let y = highlight.yPx

you can use them to center your custom view or anything related to that circle.

zzmasoud
  • 164
  • 1
  • 8
  • I think my code works for the circles, because they are not drawing every circle - only the last. My problem is that I am trying to get the X & Y of the last point so that I can put a view there. – Nick Apr 13 '20 at 20:47
  • just to select last point (to trigger delegate I described it), You should highlight last value programmatically. – zzmasoud Apr 13 '20 at 21:45