iOS图表-显示CandleStick图表的最高和最低价值

时间:2018-09-04 12:03:22

标签: ios-charts

我正在尝试使用Charts

创建烛台图

正如你们从我的屏幕快照中注意到的那样,该图表仅显示最高和最低值,而不显示所有蜡烛的值。有什么方法可以通过Charts框架实现呢?

谢谢。

enter image description here

1 个答案:

答案 0 :(得分:0)

如果只想显示最高和最低值,则需要实现自己的继承自labels: <%- JSON.stringify(country)%>, 的渲染器。实际上,您只需要重写一个功能CandleStickChartRenderer

我已经举了一个包含一百行代码的示例,但是实际上我的自定义代码包含约三十行。

drawValues(context: CGContext)

不要忘记为图表使用自定义渲染器。 ;)

class MyCandleStickChartRenderer: CandleStickChartRenderer {

    private var _xBounds = XBounds() // Reusable XBounds object

    private var minValue: Double
    private var maxValue: Double

    // New constructor
    init (view: CandleStickChartView, minValue: Double, maxValue: Double) {
        self.minValue = minValue
        self.maxValue = maxValue

        super.init(dataProvider: view, animator: view.chartAnimator, viewPortHandler: view.viewPortHandler)
    }

    // Override draw function 
    override func drawValues(context: CGContext)
    {
        guard
            let dataProvider = dataProvider,
            let candleData = dataProvider.candleData
            else { return }

        guard isDrawingValuesAllowed(dataProvider: dataProvider) else { return }

        var dataSets = candleData.dataSets

        let phaseY = animator.phaseY

        var pt = CGPoint()

        for i in 0 ..< dataSets.count
        {
            guard let dataSet = dataSets[i] as? IBarLineScatterCandleBubbleChartDataSet
                else { continue }

            let valueFont = dataSet.valueFont
            let trans = dataProvider.getTransformer(forAxis: dataSet.axisDependency)
            let valueToPixelMatrix = trans.valueToPixelMatrix

            _xBounds.set(chart: dataProvider, dataSet: dataSet, animator: animator)

            let lineHeight = valueFont.lineHeight
            let yOffset: CGFloat = lineHeight + 5.0

            for j in stride(from: _xBounds.min, through: _xBounds.range + _xBounds.min, by: 1)
            {
                guard let e = dataSet.entryForIndex(j) as? CandleChartDataEntry else { break }

                guard e.high == maxValue || e.low == minValue else { continue }

                pt.x = CGFloat(e.x)
                if e.high == maxValue {
                    pt.y = CGFloat(e.high * phaseY)
                } else if e.low == minValue {
                    pt.y = CGFloat(e.low * phaseY)
                }
                pt = pt.applying(valueToPixelMatrix)

                if (!viewPortHandler.isInBoundsRight(pt.x))
                {
                    break
                }

                if (!viewPortHandler.isInBoundsLeft(pt.x) || !viewPortHandler.isInBoundsY(pt.y))
                {
                    continue
                }

                if dataSet.isDrawValuesEnabled
                {
                    // In this part we draw min and max values
                    var textValue: String?
                    var align: NSTextAlignment = .center
                    if e.high == maxValue {
                        pt.y -= yOffset
                        textValue = "←  " + String(maxValue)
                        align = .left
                    } else if e.low == minValue {
                        pt.y += yOffset / 5
                        textValue = String(minValue) + "  →"
                        align = .right
                    }

                    if let textValue = textValue {
                        ChartUtils.drawText(
                            context: context,
                            text: textValue,
                            point: CGPoint(
                                x: pt.x,
                                y: pt.y ),
                            align: align,
                            attributes: [NSAttributedStringKey.font: valueFont, NSAttributedStringKey.foregroundColor: dataSet.valueTextColorAt(j)])
                    }
                }
            }
        }
    }
}

Ned Rockson