如何在图表中将字符串值设置为 ChartDataEntry.init 的 x?
How to set String value as x of ChartDataEntry.init in Charts?
我目前正在使用 SwiftUI 开发应用程序。
我正在尝试在我的 SwiftUI
项目中使用 Charts
(Charts)。
我想将一些字符串值显示为 x 轴,但是当我调用 ChartDataEntry.init
时出现错误,因为无法接受字符串类型的数据。
所以我参考这篇文章尝试解决
但是我不能调用这个DateAxisValueFormatter()
并且仍然解决这个问题...
下面的代码如何解决?
代码如下:
import SwiftUI
import Charts
struct LineChartSwiftUI: UIViewRepresentable {
let lineChart = LineChartView()
func makeUIView(context: UIViewRepresentableContext<LineChartSwiftUI>) -> LineChartView {
setUpChart()
return lineChart
}
func updateUIView(_ uiView: LineChartView, context: UIViewRepresentableContext<LineChartSwiftUI>) {
}
func setUpChart() {
lineChart.noDataText = "No Data Available"
let dataSets = [getLineChartDataSet()]
let data = LineChartData(dataSets: dataSets)
data.setValueFont(.systemFont(ofSize: 7, weight: .light))
lineChart.data = data
}
func getChartDataPoints(sessions: [String], accuracy: [Double]) -> [ChartDataEntry] {
var dataPoints: [ChartDataEntry] = []
for count in (0..<sessions.count) {
dataPoints.append(ChartDataEntry.init(x: Double(sessions[count]), y: accuracy[count])) // I have an error here
}
return dataPoints
}
func getLineChartDataSet() -> LineChartDataSet {
let dataPoints = getChartDataPoints(sessions: ["1,Dec","5,Dec","10,Dec","15,Dec","20,Dec"], accuracy: [20.0, 40.0, 60.0, 80.0, 100.0])
let set = LineChartDataSet(entries: dataPoints, label: "DataSet")
set.lineWidth = 2.5
set.circleRadius = 4
set.circleHoleRadius = 2
let color = ChartColorTemplates.vordiplom()[0]
set.setColor(color)
set.setCircleColor(color)
return set
}
}
Xcode: 版本 12.0.1
图表:v3.6.0
你可以像这样实现委托方法
extension LineChartSwiftUI: IAxisValueFormatter {
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
if axis is XAxis {
// do something you need for X axis
return valueTransformedToString
}
}
}
并将值格式化程序设置为委托给自身的轴,如下所示:
lineChart.xAxis.valueFormatter = self
在委托方法中,您只需将值转换为您的格式即可。
我目前正在使用 SwiftUI 开发应用程序。
我正在尝试在我的 SwiftUI
项目中使用 Charts
(Charts)。
我想将一些字符串值显示为 x 轴,但是当我调用 ChartDataEntry.init
时出现错误,因为无法接受字符串类型的数据。
所以我参考这篇文章尝试解决
但是我不能调用这个DateAxisValueFormatter()
并且仍然解决这个问题...
下面的代码如何解决?
代码如下:
import SwiftUI
import Charts
struct LineChartSwiftUI: UIViewRepresentable {
let lineChart = LineChartView()
func makeUIView(context: UIViewRepresentableContext<LineChartSwiftUI>) -> LineChartView {
setUpChart()
return lineChart
}
func updateUIView(_ uiView: LineChartView, context: UIViewRepresentableContext<LineChartSwiftUI>) {
}
func setUpChart() {
lineChart.noDataText = "No Data Available"
let dataSets = [getLineChartDataSet()]
let data = LineChartData(dataSets: dataSets)
data.setValueFont(.systemFont(ofSize: 7, weight: .light))
lineChart.data = data
}
func getChartDataPoints(sessions: [String], accuracy: [Double]) -> [ChartDataEntry] {
var dataPoints: [ChartDataEntry] = []
for count in (0..<sessions.count) {
dataPoints.append(ChartDataEntry.init(x: Double(sessions[count]), y: accuracy[count])) // I have an error here
}
return dataPoints
}
func getLineChartDataSet() -> LineChartDataSet {
let dataPoints = getChartDataPoints(sessions: ["1,Dec","5,Dec","10,Dec","15,Dec","20,Dec"], accuracy: [20.0, 40.0, 60.0, 80.0, 100.0])
let set = LineChartDataSet(entries: dataPoints, label: "DataSet")
set.lineWidth = 2.5
set.circleRadius = 4
set.circleHoleRadius = 2
let color = ChartColorTemplates.vordiplom()[0]
set.setColor(color)
set.setCircleColor(color)
return set
}
}
Xcode: 版本 12.0.1
图表:v3.6.0
你可以像这样实现委托方法
extension LineChartSwiftUI: IAxisValueFormatter {
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
if axis is XAxis {
// do something you need for X axis
return valueTransformedToString
}
}
}
并将值格式化程序设置为委托给自身的轴,如下所示:
lineChart.xAxis.valueFormatter = self
在委托方法中,您只需将值转换为您的格式即可。