Mar*_*rta 5 charts highlight ios ios-charts
我只想在点击 iOS 图表时突出显示和显示价值。我启用了突出显示而不是值,因为我只在点击和突出显示时想要它们
lineChartDataSet.drawValuesEnabled = false
lineChartDataSet.highlightEnabled = true
Run Code Online (Sandbox Code Playgroud)
我需要这个功能吗?
func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {}
Run Code Online (Sandbox Code Playgroud)
这是一个老问题,但我认为对于一些开发人员来说仍然是现实的。
如果您只想在用户触摸图表视图时显示值、气球或突出显示栏,您可以使用 捕获触摸事件UILongPressGestureRecognizer。
我实例化了TappableLineChartView来自 的新类LineChartView。BarChartView但你可以用同样的方式工作。另外,如果您不想实例化新类,您可以在视图控制器中合并addTapRecognizer和函数。chartTapped
在我的示例中,我显示和隐藏值,但以同样的方式,您可以显示和隐藏气球或其他标记。
class TappableLineChartView: LineChartView {
public override init(frame: CGRect)
{
super.init(frame: frame)
addTapRecognizer()
}
public required init?(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)
addTapRecognizer()
}
func addTapRecognizer() {
let tapRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(chartTapped))
tapRecognizer.minimumPressDuration = 0.1
self.addGestureRecognizer(tapRecognizer)
}
@objc func chartTapped(_ sender: UITapGestureRecognizer) {
if sender.state == .began || sender.state == .changed {
// show
let position = sender.location(in: self)
let highlight = self.getHighlightByTouchPoint(position)
let dataSet = self.getDataSetByTouchPoint(point: position)
dataSet?.drawValuesEnabled = true
highlightValue(highlight)
} else {
// hide
data?.dataSets.forEach{ $0.drawValuesEnabled = false }
highlightValue(nil)
}
}
}
Run Code Online (Sandbox Code Playgroud)