Skip to content

Retain cycle if we use ChartBarsLayer for custom bars #434

@khawararbi

Description

@khawararbi

Am using ChartBarsLayer to draw custom bars on graph.
Code to generate custom bars is written below.

I have debugged each and every aspect of my app and Chart. And after spending almost 8 hours I figured it out the actual issue. I have confirmed retain cycle by disabling this barsLayer, everything worked smoothly without these barsLayer. If we enable this then none of our viewModels are getting deallocated due to some retain cycle.

Am using this

        let barsLayer = ChartBarsLayer(xAxis: xAxisLayer.axis,
                                       yAxis: yAxisLayer.axis,
                                       bars: bars,
                                       horizontal: false,
                                       barWidth: barWidth,
                                       settings: barViewSettings, viewGenerator: { p1, p2, width, bgColor, settings, model, index in
            // p1: Start point of the bar
            // p2: End point of the bar
            // width: Width of the bar
            // bgColor: Background color of the bar
            // settings: Bar view settings
            // model: The data model of the bar
            // index: The index of the current bar
            var p1 = p1
            var p2 = p2
            
            if !p1.y.isFinite {
                p1 = CGPoint(x: p1.x, y: 0)
            }
            
            if !p2.y.isFinite {
                p2 = CGPoint(x: p2.x, y: 0)
            }
            let barFrame = CGRect(
                x: p1.x - width / 2, // Center the bar
                y: p2.y,             // Top of the bar
                width: width,        // Width of the bar
                height: p1.y - p2.y  // Height of the bar
            )
            
            // Create a ChartPointViewBar
            let bar = ChartPointViewBar(p1: p1, p2: p2, width: width, bgColor: bgColor, settings: settings)
            
            // Set the frame for the bar
            bar.frame = barFrame
            
            // Customize the bar (add rounded top corners)
            let cornerRadius: CGFloat = 10.5
            let roundedPath = UIBezierPath(
                roundedRect: bar.bounds,
                byRoundingCorners: p1.y > p2.y ? [.topLeft, .topRight] : [.bottomLeft, .bottomRight],
                cornerRadii: CGSize(width: cornerRadius, height: cornerRadius)
            )
            
            // Apply the rounded path as a mask
            let maskLayer = CAShapeLayer()
            maskLayer.path = roundedPath.cgPath
            bar.layer.mask = maskLayer
            
            return bar
        })

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions