如何在标记中显示除图表点(x,y)以外的数据?

时间:2018-04-16 14:34:45

标签: swift ios-charts

我已经想出如何修改标记中出现的文本和x,y值,但我无法弄清楚如何在标记中显示其他数据。

这是我目前的标记:

enter image description here

我想显示两个用于计算y值的值,而不是x和y值:

“5次重复@ 200磅”

更新:换句话说,图表上显示的点是233.3(y值)。但我需要让标签显示5和200,用于计算图表上点的233.3的结果。

看起来我会使用ChartDataEntry,但这需要ChartDataEntry,我要显示的值是不是数据点(ChartDataEntry)。

我已经查看了Github项目中的所有问题,而我能找到的最接近解决方案的是this thread,但答案并没有真正帮助初学者级别的开发人员。

有人可以教我如何在标记中显示非数据点数据吗?

更新:感谢@RazibMollick指导我使用ChartDataEntry(x:y:data:)初始值设定项的解决方案,该初始值设定项在{{1}中使用了另一个参数(generatedLineData)方法:

let numberFormatter = NumberFormatter()
let repetitions = numberFormatter.string(from: liftEvents[index].repetitions)
let weightLifted = numberFormatter.string(from: liftEvents[index].weightLifted)
let liftData = LiftData(repetitions: repetitions!, weightLifted: weightLifted!)

return ChartDataEntry(x: (thisDateInSeconds! - beginningDateInSeconds) / (3600.0 * 24.0), y: yValue, data: (liftData as AnyObject))

refreshContent(entry:highlight)我现在有了这个:

open override func refreshContent(entry: ChartDataEntry, highlight: Highlight)
{
    let unit = UserDefaults.weightUnit()
    let liftData = entry.data as! LiftData
    setLabel(String(format: "%@ reps @ %@ \(unit)", liftData.repetitions, liftData.weightLifted))
}

现在我有了这个:

enter image description here

现在,如果我可以让BalloonMarker上的角落变得圆润......

1 个答案:

答案 0 :(得分:2)

[Swift 4],我使用了here中的助手类。

我修改了下面的类(在你想要的 refreshContent 中设置你想要的字符串):

import Foundation
import Charts
open class BalloonMarker: MarkerImage
{
    open var color: UIColor?
    open var arrowSize = CGSize(width: 15, height: 11)
    open var font: UIFont?
    open var textColor: UIColor?
    open var insets = UIEdgeInsets()
    open var minimumSize = CGSize()

    fileprivate var labelns: NSString?
    fileprivate var _labelSize: CGSize = CGSize()
    fileprivate var _paragraphStyle: NSMutableParagraphStyle?
    fileprivate var _drawAttributes = [NSAttributedStringKey : Any]()

    public init(color: UIColor, font: UIFont, textColor: UIColor, insets: UIEdgeInsets)
    {
        super.init()

        self.color = color
        self.font = font
        self.textColor = textColor
        self.insets = insets

        _paragraphStyle = NSParagraphStyle.default.mutableCopy() as? NSMutableParagraphStyle
        _paragraphStyle?.alignment = .center
    }

    open override func offsetForDrawing(atPoint point: CGPoint) -> CGPoint
    {
        var offset = self.offset

        let chart = self.chartView

        var size = self.size

        if size.width == 0.0 && image != nil
        {
            size.width = image?.size.width ?? 0.0
        }
        if size.height == 0.0 && image != nil
        {
            size.height = image?.size.height ?? 0.0
        }

        let width = size.width
        let height = size.height
        let padding = CGFloat(8.0)
        var origin = point;
        origin.x -= width / 2;
        origin.y -= height;
        if origin.x + offset.x < 0.0
        {
            offset.x = -origin.x + padding
        }
        else if chart != nil && origin.x + width + offset.x > chart!.bounds.size.width
        {
            offset.x = chart!.bounds.size.width - origin.x - width - padding
        }

        if origin.y + offset.y < 0
        {
            offset.y = height + padding;
        }
        else if chart != nil && origin.y + height + offset.y > chart!.bounds.size.height
        {
            offset.y = chart!.bounds.size.height - origin.y - height - padding
        }

        return offset
    }

    open override func draw(context: CGContext, point: CGPoint)
    {
        if labelns == nil
        {
            return
        }
        let offset = self.offsetForDrawing(atPoint: point)
        let size = self.size

        var rect = CGRect(
            origin: CGPoint(
                x: point.x + offset.x,
                y: point.y + offset.y),
            size: size)
        rect.origin.x -= size.width / 2.0
        rect.origin.y -= size.height
        context.saveGState()

        if let color = color
        {
            context.setFillColor(color.cgColor)
            if(offset.y > 0) {
                context.beginPath()
                context.move(to: CGPoint(
                    x: rect.origin.x,
                    y: rect.origin.y + arrowSize.height))
                context.addLine(to: CGPoint(
                    x: rect.origin.x + (rect.size.width - arrowSize.width) / 2.0,
                    y: rect.origin.y + arrowSize.height))
                //arrow vertex
                context.addLine(to: CGPoint(
                    x: point.x,
                    y: point.y))
                context.addLine(to: CGPoint(
                    x: rect.origin.x + (rect.size.width + arrowSize.width) / 2.0,
                    y: rect.origin.y + arrowSize.height))
                context.addLine(to: CGPoint(
                    x: rect.origin.x + rect.size.width,
                    y: rect.origin.y + arrowSize.height))
                context.addLine(to: CGPoint(
                    x: rect.origin.x + rect.size.width,
                    y: rect.origin.y + rect.size.height))
                context.addLine(to: CGPoint(
                    x: rect.origin.x,
                    y: rect.origin.y + rect.size.height))
                context.addLine(to: CGPoint(
                    x: rect.origin.x,
                    y: rect.origin.y + arrowSize.height))
                context.fillPath()
            } else {
                context.beginPath()
                context.move(to: CGPoint(
                    x: rect.origin.x,
                    y: rect.origin.y))
                context.addLine(to: CGPoint(
                    x: rect.origin.x + rect.size.width,
                    y: rect.origin.y))
                context.addLine(to: CGPoint(
                    x: rect.origin.x + rect.size.width,
                    y: rect.origin.y + rect.size.height - arrowSize.height))
                context.addLine(to: CGPoint(
                    x: rect.origin.x + (rect.size.width + arrowSize.width) / 2.0,
                    y: rect.origin.y + rect.size.height - arrowSize.height))
                //arrow vertex
                context.addLine(to: CGPoint(
                    x: point.x,
                    y: point.y))
                context.addLine(to: CGPoint(
                    x: rect.origin.x + (rect.size.width - arrowSize.width) / 2.0,
                    y: rect.origin.y + rect.size.height - arrowSize.height))
                context.addLine(to: CGPoint(
                    x: rect.origin.x,
                    y: rect.origin.y + rect.size.height - arrowSize.height))
                context.addLine(to: CGPoint(
                    x: rect.origin.x,
                    y: rect.origin.y))
                context.fillPath()
            }
        }
        if (offset.y > 0) {
            rect.origin.y += self.insets.top + arrowSize.height
        } else {
            rect.origin.y += self.insets.top
        }
        rect.size.height -= self.insets.top + self.insets.bottom

        UIGraphicsPushContext(context)

        labelns?.draw(in: rect, withAttributes: _drawAttributes)

        UIGraphicsPopContext()

        context.restoreGState()
    }

    open override func refreshContent(entry: ChartDataEntry, highlight: Highlight)
    {
        //setLabel(String(entry.y))
        setLabel(String(format: "%.1f reps @ %.01f lb", entry.x, entry.y))
    }

    open func setLabel(_ label: String)
    {
        labelns = label as NSString

        _drawAttributes.removeAll()
        _drawAttributes[NSAttributedStringKey.font] = self.font
        _drawAttributes[NSAttributedStringKey.paragraphStyle] = _paragraphStyle
        _drawAttributes[NSAttributedStringKey.foregroundColor] = self.textColor

        _labelSize = labelns?.size(withAttributes: _drawAttributes) ?? CGSize.zero

        var size = CGSize()
        size.width = _labelSize.width + self.insets.left + self.insets.right
        size.height = _labelSize.height + self.insets.top + self.insets.bottom
        size.width = max(minimumSize.width, size.width)
        size.height = max(minimumSize.height, size.height)
        self.size = size
    }
}

然后我添加如下气球标记:

let marker:BalloonMarker = BalloonMarker(color: UIColor.black, font: UIFont(name: "Helvetica", size: 12)!, textColor: UIColor.white, insets: UIEdgeInsets(top: 7.0, left: 7.0, bottom: 7.0, right: 7.0))
        marker.minimumSize = CGSize(width: 75.0, height: 35.0)
        chartView.marker = marker

[修改1 ]

还有另一种方法,如下所示,您可以考虑将其他数据添加为 AnyObject &amp;获取 refreshContent 中的数据值。或者您可以使用字典映射修改BalloonMarker类。

 ChartDataEntry(x:Double, y: Double, data: AnyObject?)