如何在 iOS-图表中添加最大值和最小值的箭头?
How can I add the arrow of the maximum and minimum value in iOS-charts?
我想像下图一样显示最大值和最小值的箭头。
有人知道如何使用 iOS-Charts 实现它吗?非常感谢。
您需要为 CandleStickChartView
编写自己的自定义渲染器。
幸运的是,在您的情况下,您只需要覆盖一种方法。
因此,从 CandleStickChartRenderer
继承 class 并覆盖方法 drawValues(context: CGContext)
。您可以 copy/paste 父方法中的大部分代码,只进行必要的更改。
class MyCandleStickChartRenderer: CandleStickChartRenderer {
internal var _xBounds = XBounds()
var minValue: Double
var maxValue: Double
init (view: CandleStickChartView, minValue: Double, maxValue: Double) {
self.minValue = minValue
self.maxValue = maxValue
super.init(dataProvider: view, animator: view.chartAnimator, viewPortHandler: view.viewPortHandler)
}
override func drawValues(context: CGContext)
{
// ... I remove some code that was not changed ...
for j in stride(from: _xBounds.min, through: _xBounds.range + _xBounds.min, by: 1)
{
guard let e = dataSet.entryForIndex(j) as? CandleChartDataEntry else { break }
// need to show only min and max values
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)
// ... I remove some code that was not changed ...
if dataSet.isDrawValuesEnabled
{
var textValue: String?
var align: NSTextAlignment = .center
// customize position for min/max value
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)])
}
}
}
}
}
}
您还需要创建渲染器实例并将其设置为图表视图中的 属性。
myCandleStickChartView.renderer = MyCandleStickChartRenderer(view: candleStickChartView, minValue: 400, maxValue: 1450)
最后...
我想像下图一样显示最大值和最小值的箭头。
有人知道如何使用 iOS-Charts 实现它吗?非常感谢。
您需要为 CandleStickChartView
编写自己的自定义渲染器。
幸运的是,在您的情况下,您只需要覆盖一种方法。
因此,从 CandleStickChartRenderer
继承 class 并覆盖方法 drawValues(context: CGContext)
。您可以 copy/paste 父方法中的大部分代码,只进行必要的更改。
class MyCandleStickChartRenderer: CandleStickChartRenderer {
internal var _xBounds = XBounds()
var minValue: Double
var maxValue: Double
init (view: CandleStickChartView, minValue: Double, maxValue: Double) {
self.minValue = minValue
self.maxValue = maxValue
super.init(dataProvider: view, animator: view.chartAnimator, viewPortHandler: view.viewPortHandler)
}
override func drawValues(context: CGContext)
{
// ... I remove some code that was not changed ...
for j in stride(from: _xBounds.min, through: _xBounds.range + _xBounds.min, by: 1)
{
guard let e = dataSet.entryForIndex(j) as? CandleChartDataEntry else { break }
// need to show only min and max values
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)
// ... I remove some code that was not changed ...
if dataSet.isDrawValuesEnabled
{
var textValue: String?
var align: NSTextAlignment = .center
// customize position for min/max value
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)])
}
}
}
}
}
}
您还需要创建渲染器实例并将其设置为图表视图中的 属性。
myCandleStickChartView.renderer = MyCandleStickChartRenderer(view: candleStickChartView, minValue: 400, maxValue: 1450)
最后...