1

I was looking everywhere for this and wasn't able to find any solution or any hint on how to achieve this. I want to draw an indicator (circle) only for selected/highlighted point. Right now I have

dataSet.drawCircleHoleEnabled = false
dataSet.drawCirclesEnabled = false

So my line chart looks simple, but as soon as I start dragging my finder and seeing highlight indicator, I also want to see a circle showing me the data entry. I want something which looks like this:

enter image description here

So far the only thing coming to my mind was creating another data set and configure it on the fly using delegates, but it seems like overkill for something so common.

cojoj
  • 6,405
  • 4
  • 30
  • 52

1 Answers1

9

you need to create a Custom Marker

class CircleMarker: MarkerImage {
    
    @objc var color: UIColor
    @objc var radius: CGFloat = 4
    
    @objc public init(color: UIColor) {
        self.color = color
        super.init()
    }
    
    override func draw(context: CGContext, point: CGPoint) {
        let circleRect = CGRect(x: point.x - radius, y: point.y - radius, width: radius * 2, height: radius * 2)
        context.setFillColor(color.cgColor)
        context.fillEllipse(in: circleRect)
        
        context.restoreGState()
    }
}

and use it

let marker = CircleMarker(color: .red)
chart.marker = marker

enter image description here

based on BallonMarker

aiwiguna
  • 2,852
  • 2
  • 15
  • 26
  • I wonder that why variables are described with `@objc`. if you make it with dynamic dispatch or something, can I learn the goal of this? – eemrah Mar 01 '21 at 14:14
  • this answer is based on BallonMarker.swift in the demo of the library, the demo has code in obj-c and swift so it needs @objc for obj-c class to access attribute in this swift class – aiwiguna Mar 01 '21 at 23:00
  • thanks @aiwiguna so this is a must to describe objc flag when you have to use a variable from swift to objective-c – eemrah Mar 02 '21 at 06:21