我有 struct
对象,我想绘制图表:
struct CustomHistoricalSample {
var value: Double
var date: Date
}
我正在使用 SwiftChart (比图表更轻)来绘制值。该库采用 [Double]
(或 [TimeInterval]
)的 X 轴数组,然后我需要将其格式化回字符串以显示在图表下方。如何将我的日期转换为 TimeInterval
然后格式化为 String
以便绘制它?下面也是我试图绘制的 CustomHistoricalSamples 示例。
override func viewDidLoad() {
super.viewDidLoad()
chart.delegate = self
let graphableVales = customHistoricalSamples.map { $0.value }
let graphableDatesAsDouble = customHistoricalSamples.map { $0.date }
let series = ChartSeries(graphableVales)
series.area = true
chart.xLabels = //?
chart.xLabelsFormatter = //String?
chart.add(series)
}
value = 15.0 and date = 2018-01-16 03:19:08 +0000
value = 26.0 and date = 2018-01-17 02:58:29 +0000
value = 27.0 and date = 2018-01-20 20:48:58 +0000
value = 22.0 and date = 2018-01-21 02:13:17 +0000
value = 18.0 and date = 2018-01-22 02:11:43 +0000
value = 14.0 and date = 2018-01-26 01:16:37 +0000
value = 20.0 and date = 2018-01-28 02:22:48 +0000
value = 16.0 and date = 2018-01-28 03:58:07 +0000
value = 15.0 and date = 2018-02-03 22:12:02 +0000
value = 18.0 and date = 2018-02-04 03:44:11 +0000
value = 22.0 and date = 2018-02-10 20:54:22 +0000
value = 19.0 and date = 2018-02-10 22:14:51 +0000
value = 14.0 and date = 2018-02-11 23:58:37 +0000
value = 25.0 and date = 2018-02-13 03:27:34 +0000
value = 22.7 and date = 2018-02-17 02:21:26 +0000
value = 15.8 and date = 2018-02-19 02:08:40 +0000
value = 16.2 and date = 2018-02-23 01:13:49 +0000
value = 16.6 and date = 2018-02-24 20:43:42 +0000
value = 24.2 and date = 2018-02-24 22:10:35 +0000
value = 16.4 and date = 2018-02-28 01:18:26 +0000
value = 13.8 and date = 2018-03-02 01:40:51 +0000
value = 21.9 and date = 2018-03-03 20:48:25 +0000
value = 13.7 and date = 2018-03-03 22:09:32 +0000
value = 15.1 and date = 2018-03-05 02:12:14 +0000
value = 14.3 and date = 2018-03-06 00:33:12 +0000
value = 16.4 and date = 2018-03-10 03:40:25 +0000
value = 13.5 and date = 2018-03-11 22:44:04 +0000
value = 16.2 and date = 2018-03-18 23:25:03 +0000
value = 19.7 and date = 2018-03-28 00:09:40 +0000
value = 14.5 and date = 2018-03-30 02:17:00 +0000
value = 17.6 and date = 2018-03-31 19:20:38 +0000
value = 15.8 and date = 2018-04-03 23:23:37 +0000
value = 15.0 and date = 2018-04-11 01:16:16 +0000
value = 18.4 and date = 2018-04-18 01:15:09 +0000
value = 14.7 and date = 2018-04-29 00:23:30 +0000
value = 12.7 and date = 2018-05-16 02:52:31 +0000
value = 13.7 and date = 2018-05-19 23:16:40 +0000
value = 14.0 and date = 2018-05-23 01:09:00 +0000
value = 13.7 and date = 2018-06-03 21:38:09 +0000
value = 13.5 and date = 2018-06-10 21:35:51 +0000
value = 14.3 and date = 2018-06-12 23:52:19 +0000
value = 17.2 and date = 2018-06-16 22:12:13 +0000
value = 13.4 and date = 2018-06-17 00:44:35 +0000
value = 13.4 and date = 2018-06-17 00:44:35 +0000
最佳答案
您想将日期转换为时间间隔(针对设定的时间点),然后在 xLabelsFormatter 中您可以使用该设定点将它们转回日期,并使用 NSDateFormatter 将日期字符串化。 NSDate 使用 1970 作为固定时间点使这变得容易。您的图表代码如下所示:
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short
let graphableDatesAsDouble = customHistoricalSamples.map { $0.date.timeIntervalSince1970 }
let series = ChartSeries(graphableVales)
series.area = true
chart.xLabels = graphableDatesAsDouble
chart.xLabelsFormatter = { dateFormatter.string(from: Date(timeIntervalSince1970: $1)) }
chart.add(series)
您可以将 NSDateFormatter 的 dateStyle
更改为其他内容,或者为其提供自定义 dateFormat
(如果您不熟悉 http://nsdateformatter.com 有助于构建格式).
关于ios - 如何将日期绘制为 X 轴 (Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50979448/