Make dot on line that passing all the way horizontal line char

72 Views Asked by At

I draw a line chart and I have a vertical line
there is a way with this library to add a dot on vertical line and when I press and move the line, the dot move across that line chart (i add a picture)

I'm using this library https://github.com/danielgindi/Chart to get line charts

my function :

 func setData() {
        let dataSet = LineChartDataSet(entries: ChartLineHandler.getChartData())
        dataSet.mode = .cubicBezier
        dataSet.lineWidth = 1
        dataSet.drawHorizontalHighlightIndicatorEnabled = false
        dataSet.setColor(.systemOrange)
        dataSet.drawCirclesEnabled = false
        
        let data = LineChartData(dataSet: dataSet)
        data.setDrawValues(false)
    
        lineChatView.data = data
    }

this what i want to get

1

There are 1 best solutions below

0
berbaspin On

You need to create the Marker:

final 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()
    }
}

Then add it to your Chart View:

lineChatView.marker = CircleMarker(color: .systemOrange.withAlphaComponent(0.5))

Don't forget to enable highlightPerDragEnabled and highlightPerTapEnabled:

lineChatView.highlightPerDragEnabled = true
lineChatView.highlightPerTapEnabled = true