Charts下的MakerView和ValueFormatter

上次我们说到了动态的刷新数据, 这次我们说下 MarkerView 和 ValueFormatter

由于公司之前用的是 Charts 2.x, 更新之后很多方法都变了, 刚好需要修改这部分的代码

Charts下的MakerView和ValueFormatter_第1张图片

先上代码

    open var xAxisValueFormatter: IAxisValueFormatter?
    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 = [String : AnyObject]()
    
    public init(color: UIColor,colorStock: UIColor, font: UIFont, insets: UIEdgeInsets, xAxisValueFormatter: IAxisValueFormatter)
    {
        super.init()
        
        self.color = color
        self.font = font
        self.textColor = colorStock
        self.insets = insets
        self.xAxisValueFormatter = xAxisValueFormatter
        
        _paragraphStyle = NSParagraphStyle.default.mutableCopy() as? NSMutableParagraphStyle
        _paragraphStyle?.alignment = .center
    }
    
    open override func offsetForDrawing(atPoint point: CGPoint) -> CGPoint
    {
        let size = self.size
        var point = point
        point.x -= size.width / 2.0
        point.y -= size.height
        return super.offsetForDrawing(atPoint: point)
    }
    
    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)
            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))
            context.addLine(to: CGPoint(
                x: rect.origin.x + rect.size.width / 2.0,
                y: rect.origin.y + rect.size.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))
            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()
        }
        
        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(entry)
    }
    
    open func setLabel(_ entry: ChartDataEntry)
    {
        
        labelns = String(format: "7日年化收益率: %.2f%%\n时间: %@", Float(entry.y) * 100, xAxisValueFormatter!.stringForValue(entry.x, axis: nil)) as NSString?
        
        _drawAttributes.removeAll()
        _drawAttributes[NSFontAttributeName] = self.font
        _drawAttributes[NSParagraphStyleAttributeName] = _paragraphStyle
        _drawAttributes[NSForegroundColorAttributeName] = UIColor.white
        
        _labelSize = labelns?.size(attributes: _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
    }

代码大部分都是我直接从 Charts 的官方 Demo 上 copy 下来的, 只有 setLabel 的部分地方重新修改了下, 之前 Charts 2.x 的时候

public override func refreshContent(entry entry: ChartDataEntry, highlight: ChartHighlight, XAxisValue: String)

是有 XAxisValue 的参数的, 并且参数是 String 类型, 3.x 之后在refreshContent 方法里去掉了 XAxisValue 参数, 而且我查看 ChartDataEntryHighlight 下属性都是 Double 类型
关于上图的实现我想到了先传入秒数, 用 ValueFormatter 来计算日期格式显示到 x 轴, 在 markerView 下就可以取到秒数, 在换算成日期就可以, 这个就是换算方面的问题了
还有一个方法就是把 ValueFormatter 当做参数传进去, 让内部进行换算之类的, 可以省去几行代码
关于 MarkerView 使用方法也很简单, chartView.marker = [CustomMarkerView new] 即可

下面在说一下自定义 ValueFormatter, 只需要继承 NSObject, 遵循IChartAxisValueFormatter 代理, 然后在
- (NSString *)stringForValue:(double)value axis:(ChartAxisBase *)axis
协议写你需要的格式或者其他特定坐标值就可以了, 使用时```chartView.xAxis.valueFormatter = [CustomValueFormatter new]


由于 Charts 封装的功能已经十分具体, 详细, 详细代码就不放出来了, 在 Charts 的官方 Demo 还是可以找到的, 如果在使用当中遇到什么问题或者难以解决的需求, 可以给我留言共同探讨

你可能感兴趣的:(Charts下的MakerView和ValueFormatter)