我正在使用 Objective-C 创建一个 iOS 应用程序,我需要使用 ios-charts。
现在我遇到了一个问题,我找不到将标记添加到我的 graphView 的方法。
此外,我需要通过用户操作更改 YAxis 数据集,但我不知道如何实现此功能。
感谢您的帮助。
最佳答案
不幸的是,库本身没有显示带有文本的标记的类。 github 上提供的示例中有这个 BalloonMarker 类,但它不包含在库中。因此,您可以使用 github 上示例中的 BalloonMarker,或者下面是另一个您可以使用的简单标记类。我认为它可能比 BallonMarker 更容易理解和定制:
class ChartMarker: MarkerView {
var text = ""
override func refreshContent(entry: ChartDataEntry, highlight: Highlight) {
super.refreshContent(entry: entry, highlight: highlight)
text = String(entry.y)
}
override func draw(context: CGContext, point: CGPoint) {
super.draw(context: context, point: point)
var drawAttributes = [NSAttributedStringKey : Any]()
drawAttributes[.font] = UIFont.systemFont(ofSize: 15)
drawAttributes[.foregroundColor] = UIColor.white
drawAttributes[.backgroundColor] = UIColor.darkGray
self.bounds.size = (" \(text) " as NSString).size(withAttributes: drawAttributes)
self.offset = CGPoint(x: 0, y: -self.bounds.size.height - 2)
let offset = self.offsetForDrawing(atPoint: point)
drawText(text: " \(text) " as NSString, rect: CGRect(origin: CGPoint(x: point.x + offset.x, y: point.y + offset.y), size: self.bounds.size), withAttributes: drawAttributes)
}
func drawText(text: NSString, rect: CGRect, withAttributes attributes: [NSAttributedStringKey : Any]? = nil) {
let size = text.size(withAttributes: attributes)
let centeredRect = CGRect(x: rect.origin.x + (rect.size.width - size.width) / 2.0, y: rect.origin.y + (rect.size.height - size.height) / 2.0, width: size.width, height: size.height)
text.draw(in: centeredRect, withAttributes: attributes)
}
}
并将其与给定图表一起使用:
let marker = ChartMarker()
marker.chartView = chartView
chartView.marker = marker
关于ios - 如何在 ios-charts 上添加标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34981568/