ios - 如何将日期绘制为 X 轴 (Swift)
问题描述
我有struct
一些我正在尝试绘制的对象:
struct CustomHistoricalSample {
var value: Double
var date: Date
}
我正在使用SwiftChart(比图表更轻)来绘制值。该库采用[Double]
(or [TimeInterval]
) 的 X 轴数组,然后我需要将其格式化回字符串以显示在图表下方。如何将我的日期转换为TimeInterval
then 格式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)
您可以dateStyle
将 NSDateFormatter 更改为其他内容,或者给它一个自定义dateFormat
(如果您不熟悉它们, http: //nsdateformatter.com有助于构建格式)。
推荐阅读
- sql-server - 有没有办法告诉 SQL Server 在插入每个新行之前检查表中的重复项?
- java - 如何从 Maven 依赖项中获取类文件到 target\classes 文件夹
- javascript - kinesis 视频流 webrtc 403 在预签名的 websocket wss url 上被禁止
- python - Dockerized Python 项目 - 管理依赖项的最佳方式:Dockerfile 或 requirements.txt
- excel - Google 地图未显示在 Microsoft Excel 数据输入表单中
- vagrant - Vagrantfile - 以相同主机名显示的多台虚拟机
- c++ - 从 C 可执行文件加载 C++ 动态库时收到错误“未定义符号”
- java - Launch4J 创建的 EXE 如何与需要外部数据(例如游戏中的图像或精灵)的 java 程序一起工作?
- twitter-bootstrap - 我刚刚安装了 twitter bootstrap,我的导航栏似乎被禁用了,我不知道为什么
- javascript - javascript:通过某个id比较两个对象数组的内容并返回true或false