gpt4 book ai didi

ios - 如何在iOS图表折线图中将不相关的数组添加到Datapoint标签?

转载 作者:行者123 更新时间:2023-12-01 16:13:25 26 4
gpt4 key购买 nike

我已阅读How to add Strings on X Axis in iOS-charts?,但无法回答我的问题。

我用iOS图表绘制了一个折线图,没有x / y轴标签only datapoint labels, like this。此图表在图表ChartDataEntry中将温度值显示为Y,间隔为0。
var temperaturevalues: [Double] = [47.0, 48.0, 49.0, 50.0, 51.0, 50.0, 49.0]
但是,我想向数据点标签添加不相关的时间数组,其中times [i]匹配i处的temperatureValues。
var times: [String] = ["7 AM", "8 AM", "11 AM", "2 PM", "5 PM", "8 PM", "11 PM"]
我在像这样的单独类中扩展了IValueFormatter:

class ChartsFormatterToDegrees: IValueFormatter {    
func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
return "\(Int(value))°"
}

但它只允许我使用ChartDataEntry中的值(即用来在lineChart中映射Y的temperatureValue)中的值来自定义dataPoint标签。如何在indexValue处添加另一个数组,以使dataPoints标签看起来像这样?
    return """
\(Int(value))°
\(timesOfDay)
"""

这是我的图表方法:
private func setupChartView(temperatureValues: [Double], timesDataPoints: [String]) {
var tempDataEntries: [ChartDataEntry] = []
let chartsFormatterToDegrees = ChartsFormatterToDegrees(time: timeDataPoints)


for eachTemp in 0..<temperatureValues.count {
let tempEntry = ChartDataEntry(x: Double(eachTemp), y: temperatureValues[eachTemp])
tempDataEntries.append(tempEntry)
}

let lineChartDataSet = LineChartDataSet(entries: tempDataEntries, label: "nil")
lineChartDataSet.valueFormatter = chartsFormatterToDegrees

let chartData = LineChartData(dataSets: [lineChartDataSet])
chartData.addDataSet(lineChartDataSet)
lineChartView.data = chartData
}

据我所知,IValueFormatter扩展仅允许您修改用于绘制图表的值,而不能在索引处添加其他字符串数组。当我尝试以下操作时,它仅在dataSetIndex上打印timesOfDay,仅在所有dataPoint标签上打印 timesOfDay[0]
class ChartsFormatterToDegrees: IValueFormatter {
init(time: [String]) {
timesOfDay = time
}
var timesOfDay = [String]()

func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
return """
\(Int(value))°
\(timesOfDay[dataSetIndex])
"""
}
}

这是我最新的打印方法:
class ChartsFormatterToDegrees: IValueFormatter {

var timesOfDay = [String]()
var values = [Double]()

init(values: [Double], time: [String]) {
timesOfDay = time
//print(values, "are the values") //prints values correctly
}

func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
//print(value, "at", dataSetIndex) //prints as 37.0 at 0, 39.0 at 0, 40.0 at 0 etc

if let index = values.firstIndex(of: value) {
return "\(Int(value))° \(timesOfDay[index])"
}

let i = values.firstIndex(of: value)
//print(i as Any, "is index of where value shows up") //prints as nil is index of where value shows up for each i

return "\(Int(value))°"
}
}

最佳答案

您的假设是不正确的。在IValueFormatter中,您可以为相应的值构造任何字符串。您只看到value或timeOfDay的原因是因为您正在使用String表示法构造""",该表示法添加了多行,而用于入口点的标签可能因此无法正确计算其大小。您应该按如下所示创建String并查看所获得的内容

class ChartsFormatterToDegrees: IValueFormatter {

var timesOfDay = [String]()
var values = [Double]()

init(values: [Double], time: [String]) {
timesOfDay = time
values = values
}

func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
if let index = values.firstIndex(of: value) {
return "\(Int(value))° \(timesOfDay[index])"
}
return "\(Int(value))°"
}
}

关于ios - 如何在iOS图表折线图中将不相关的数组添加到Datapoint标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58619685/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com