SwiftUI iOS 图表:如何获得突出显示的值
SwiftUI iOS CHARTS: How to get highlighted value
使用 iOS 图表(作者 Daniel Gindi https://github.com/danielgindi/Charts)和 SwiftUI,我想从折线图中获取选定的值,当用户点击它时突出显示的值。突出显示有效,我可以看到该值的附加行。
经过一些研究,我发现我们必须创建这个函数:chartValueSelected() 但在这种情况下从未调用过。我想我必须处理委托,但我没有找到任何解决方案或 SwiftUI 示例。有人可以帮忙吗?谢谢
LineChart
import Foundation
import Charts
import SwiftUI
struct GraphMeterLine : UIViewRepresentable {
var xArray:[Float] = [0,1,2,3,4]
var yArray:[Float] = [0,3,6,2,12]
var data = LineChartData()
let chartline = LineChartView()
func makeUIView(context: Context) -> LineChartView {
return chartline
}
func updateUIView(_ uiView: LineChartView, context: Context) {
//when data changes chartd.data update is required
uiView.data = createGraph()
}
func createGraph() -> LineChartData {
var lineChartEntry = [ChartDataEntry]()
for i in 0..<yArray.count {
let value = ChartDataEntry(x: Double(xArray[i]), y: Double(yArray[i]))
lineChartEntry.append(value)
}
let line1 = LineChartDataSet(entries: lineChartEntry, label: "Number")
data.addDataSet(line1)
data.setDrawValues(false)
return data
}
public func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
print("To Display Values X/Y Values here")
}
typealias UIViewType = LineChartView
}
将尝试另一个想法。我现在无法对其进行测试,所以不确定它是否会起作用,但它已成功构建,请在您身边尝试一下。我在您的代码中添加了代码:
var xArray:[Float] = [0,1,2,3,4]
var yArray:[Float] = [0,3,6,2,12]
var data = LineChartData()
let chartline = LineChartView()
class ChartDelegete: ChartViewDelegate {
func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
print("To Display Values X/Y Values here")
}
}
let chartDelegete = ChartDelegete()
func makeUIView(context: Context) -> LineChartView {
chartline.delegate = chartDelegete
return chartline
}
评论后更新
旧答案
将委托添加到定义中
struct GraphMeterLine : UIViewRepresentable, ChartViewDelegate {
并设置委托
chartline.delegate = self
在 makeUIView(context: 例如调用。它应该工作。
使用 iOS 图表(作者 Daniel Gindi https://github.com/danielgindi/Charts)和 SwiftUI,我想从折线图中获取选定的值,当用户点击它时突出显示的值。突出显示有效,我可以看到该值的附加行。
经过一些研究,我发现我们必须创建这个函数:chartValueSelected() 但在这种情况下从未调用过。我想我必须处理委托,但我没有找到任何解决方案或 SwiftUI 示例。有人可以帮忙吗?谢谢 LineChart
import Foundation
import Charts
import SwiftUI
struct GraphMeterLine : UIViewRepresentable {
var xArray:[Float] = [0,1,2,3,4]
var yArray:[Float] = [0,3,6,2,12]
var data = LineChartData()
let chartline = LineChartView()
func makeUIView(context: Context) -> LineChartView {
return chartline
}
func updateUIView(_ uiView: LineChartView, context: Context) {
//when data changes chartd.data update is required
uiView.data = createGraph()
}
func createGraph() -> LineChartData {
var lineChartEntry = [ChartDataEntry]()
for i in 0..<yArray.count {
let value = ChartDataEntry(x: Double(xArray[i]), y: Double(yArray[i]))
lineChartEntry.append(value)
}
let line1 = LineChartDataSet(entries: lineChartEntry, label: "Number")
data.addDataSet(line1)
data.setDrawValues(false)
return data
}
public func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
print("To Display Values X/Y Values here")
}
typealias UIViewType = LineChartView
}
将尝试另一个想法。我现在无法对其进行测试,所以不确定它是否会起作用,但它已成功构建,请在您身边尝试一下。我在您的代码中添加了代码:
var xArray:[Float] = [0,1,2,3,4]
var yArray:[Float] = [0,3,6,2,12]
var data = LineChartData()
let chartline = LineChartView()
class ChartDelegete: ChartViewDelegate {
func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
print("To Display Values X/Y Values here")
}
}
let chartDelegete = ChartDelegete()
func makeUIView(context: Context) -> LineChartView {
chartline.delegate = chartDelegete
return chartline
}
评论后更新
旧答案
将委托添加到定义中
struct GraphMeterLine : UIViewRepresentable, ChartViewDelegate {
并设置委托
chartline.delegate = self
在 makeUIView(context: 例如调用。它应该工作。