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

问题描述 投票:2回答:1

我已经读过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))°"
      }
    }
ios swift ios-charts
1个回答
1
投票

您的假设不正确。在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))°"
      }
}
© www.soinside.com 2019 - 2024. All rights reserved.