iOS-Charts 如何将 UIImage 放在一个点旁边 [英] iOS-Charts How to put UIImage beside a point
问题描述
我想在 Swift 中使用 iOS-Charts 绘制一些折线图来显示数据.现在,我需要在每个点旁边放一个小图标.
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.
现在我可以在图形上添加 UIImage,但问题是如何正确获取每个点的 CGPoint 坐标.
Now I can add UIImage on the graph, but the problem is how can I get the CGPoint coordinates of each points correctly.
如果我改变设备的方向或设备类型,图像的位置仍然是固定的,不会相应地改变.这是 iPhone 6s 图片.
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.
这是我当前的代码:
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)
}
推荐答案
可以修改private func drawCircles(context context: CGContext)
在 LineChartRenderer.swift
内执行以下操作:在绘制圆圈的循环内使用 pt
var,它在那里计算并添加如下内容:
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))
显然,您需要使用 CGRect 来按照您需要的方式放置图像.每次设备方向发生变化时都会调用此方法.
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.
这篇关于iOS-Charts 如何将 UIImage 放在一个点旁边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!