尝试在 ChartDataEntry 中输入 Date/String
Trying to enter Date/String in ChartDataEntry
我正在尝试添加 Date/String 作为 Charts 中的 xAxis 标签(标签之间需要相等的 space)
我在下面做,但构造函数中的 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 所说的那样进行了准确传播,但我仍然看不到标签。我更新了上面的代码以显示 table
如您所见,您需要使用双精度值(或数值)作为 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
那么你只会在底部看到天数。
总而言之:
- 查找日期的数字转换。
- 使用这个值
- 在格式化程序中转换回日期。
如果您希望数据间隔均匀(即不代表准确的时间线),那么答案就更简单了。
将您的 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]
}
我正在尝试添加 Date/String 作为 Charts 中的 xAxis 标签(标签之间需要相等的 space)
我在下面做,但构造函数中的 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 所说的那样进行了准确传播,但我仍然看不到标签。我更新了上面的代码以显示 table
如您所见,您需要使用双精度值(或数值)作为 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
那么你只会在底部看到天数。
总而言之:
- 查找日期的数字转换。
- 使用这个值
- 在格式化程序中转换回日期。
如果您希望数据间隔均匀(即不代表准确的时间线),那么答案就更简单了。
将您的 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]
}