ios - 如何在 ios-charts 上添加标记?

标签 ios objective-c ios-charts

我正在使用 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/

相关文章:

ios - 如何在 iOS 中将照片保存到 google drive 的文件夹中

swift - 条形图中条形的不同颜色取决于值

ios - 如何在 MPAndroidChart 中缩小时隐藏值标签?

objective-c - 永恒滚动的 UITableView

swift - 水平条形图标签

ios - 为非 gailbreak 设备中已安装的应用下载和安装 .IPA 文件(开发应用商店)

iphone - slider /文本框交换数据

c++ - 这个网络库可以在 iOS 上运行吗?

objective-c - 从 Dropbox 下载文件并保存到路径

ios - 绘制带孔饼图