iOS-Charts How to put UIImage beside a point

自闭症网瘾萝莉.ら 提交于 2020-01-01 19:37:34

问题


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.



来源:https://stackoverflow.com/questions/36929413/ios-charts-how-to-put-uiimage-beside-a-point

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!