iOS-Charts How to put UIImage beside a point

2019-04-16 01:51发布

问题:

I want to use iOS-Charts with Swift to draw some line charts to show the data. Now, I have a requirement that put a small icon beside each point.

Now I can add UIImage on the graph, but the problem is how can I get the CGPoint coordinates of each points correctly.

If I change the orientation of device or device type, the position of images are still fixed, not change correspondingly. Here is iPhone 6s picture.

Here is my current code:

for i in 0..<dataPoints.count {
        let pointLeft = lineChartView.getPosition(dataEntries[i], axis: .Left)

        let image = UIImage(named: numbers[i])
        let imageView = UIImageView(image:image!)
        imageView.frame = CGRect(x: pointLeft.x, y: pointLeft.y, width: 16, height: 16)
        lineChartView.addSubview(imageView)

    }

回答1:

You can modify private func drawCircles(context context: CGContext) inside LineChartRenderer.swift to do the following: inside the loop where the circles are drawing use pt var which is calculated there and add something like this:

let image = UIImage(named: numbers[i])
image?.drawInRect(CGRect(x: pt.x, y: pt.y, width: 16, height: 16))

Obviously, you need to play with CGRect to place images the way you need. This method will be called every time device orientation will be changing etc.