Nick
Nick

Reputation: 237

Get the position of a circle in swift charts

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
}

Upvotes: 1

Views: 1402

Answers (1)

zzmasoud
zzmasoud

Reputation: 164

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.

Upvotes: 2

Related Questions