0

我正在尝试添加一个日期/字符串作为图表中的 xAxis 标签(标签之间需要有相等的空间)

我在下面做,但构造函数中的 x 只需要一个双倍。

任何想法?

for i in 0..<self.grahEntries.count {
     let x1 = self.grahEntries[i].x
     let dateFormatter = DateFormatter()
     dateFormatter.dateFormat = "YYY-MM-DD HH:mm:ss"
     let date = dateFormatter.date(from: x1)
     print(date)
     let dataEntry = ChartDataEntry(x: date, y: self.grahEntries[i].y)//HERE IS MY PROBLEM!! xAxis is only double
     dataEntries.append(dataEntry)
}

 let line1 = LineChartDataSet(values: dataEntries, label: "Units Consumed")
    line1.colors = [NSUIColor.blue]
    line1.mode = .cubicBezier
    line1.cubicIntensity = 0.2


    let gradient = getGradientFilling()
    line1.fill = Fill.fillWithLinearGradient(gradient, angle: 90.0)
    line1.drawFilledEnabled = true

    let data = LineChartData()
    data.addDataSet(line1)
    mChart.data = data
    mChart.setScaleEnabled(false)
    mChart.animate(xAxisDuration: 1.5)

    mChart.xAxis.valueFormatter = DateAxisValueFormatter()
    mChart.xAxis.granularity = 1.0


    mChart.drawGridBackgroundEnabled = false
    mChart.xAxis.drawAxisLineEnabled = false
    mChart.xAxis.drawGridLinesEnabled = false
    mChart.leftAxis.drawAxisLineEnabled = true
    mChart.leftAxis.drawGridLinesEnabled = true
    mChart.rightAxis.drawAxisLineEnabled = false
    mChart.rightAxis.drawGridLinesEnabled = false
    mChart.legend.enabled = false
    mChart.xAxis.enabled = true
    mChart.leftAxis.enabled = true
    mChart.rightAxis.enabled = false

    mChart.xAxis.labelPosition = .bottom

    mChart.xAxis.drawLabelsEnabled = true
    mChart.xAxis.drawLimitLinesBehindDataEnabled = true
    mChart.xAxis.avoidFirstLastClippingEnabled = true

更新 :

我按照@ces 所说的那样准确地传播它们,但我仍然看不到标签。我更新了上面的代码以显示表格 在此处输入图像描述

4

2 回答 2

2

如果您希望数据均匀分布(即不代表准确的时间线),那么答案会更简单。

for将循环更改为

var dates: [String] = []
for i in 0..<self.grahEntries.count {
     let x1 = self.grahEntries[i].x
     let dateFormatter = DateFormatter()
     dateFormatter.dateFormat = "YYY-MM-DD HH:mm:ss"
     let date = dateFormatter.date(from: x1)
     dates.append(date)
     let dataEntry = ChartDataEntry(x: Double(i), y: self.grahEntries[i].y)
     dataEntries.append(dataEntry)
}

和改变

mChart.xAxis.valueFormatter = DateAxisValueFormatter(dates)

然后更改DataAxisValueFormatter为在构造函数中接受一个字符串数组并存储它们。改变

func stringForValue(_ value: Double, axis: AxisBase?) -> String
{
    let i = Int(value)
    return dates[i]
}
于 2019-02-28T01:26:01.197 回答
0

正如您所发现的,您需要使用双精度(或数值)作为 x 的值。在我之前针对日期绘制的地方,我的方法是使用自纪元以来的秒数将日期转换为数字并转换为天数,使用:-

 date.timeIntervalSince1970 / secondsPerDay

其中(足够准确):-

let secondsPerDay = 24.0 * 3600.0

要为查看器格式化轴上的日期,然后我使用如下格式化程序:-

class DateAxisValueFormatter : NSObject, IAxisValueFormatter
{
  let dateFormatter = DateFormatter()

  override init()
  {
    super.init()
    dateFormatter.dateFormat = "dd MMM"
  }

  func stringForValue(_ value: Double, axis: AxisBase?) -> String
  {
    let date = Date(timeIntervalSince1970: value * secondsPerDay)
    return dateFormatter.string(from: date)
  }
}

它将我使用的值转换回正确的文本。如果你设置

lineChart.xAxis.valueFormatter = DateAxisValueFormatter()
lineChart.xAxis.granularity = 1.0

那么你只会在底部看到几天。

总而言之:

  • 查找日期的数字转换。
  • 使用这个值
  • 在格式化程序中转换回日期。
于 2019-02-27T22:21:31.570 回答