0

我有struct一些我正在尝试绘制的对象:

struct CustomHistoricalSample {
    var value: Double
    var date: Date
}

我正在使用SwiftChart(比图表更轻)来绘制值。该库采用[Double](or [TimeInterval]) 的 X 轴数组,然后我需要将其格式化回字符串以显示在图表下方。如何将我的日期转换为TimeIntervalthen 格式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
4

3 回答 3

1

您想将日期转换为时间间隔(相对于设定的时间点),然后在 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有助于构建格式)。

于 2018-06-22T02:38:16.087 回答
0

派对有点晚了,但您也可以使用 ChartAxisValueString 以字符串格式实际使用日期:

ChartPoint(x: ChartAxisValueString(dateStringYouWant, order:loopIndex, labelSettings: ChartDefaults.labelSettings), y: ChartAxisValueDouble(doubleValueYouWant))
于 2020-05-13T16:13:06.613 回答
0

在 danielgindi/Charts ios 中显示 x 轴值

在此处输入图像描述

 override func viewDidLoad() {
 super.viewDidLoad()

self.lineChart.delegate = self
self.lineChart.chartDescription?.textColor = UIColor.white

let months = ["Jan" , "Feb", "Mar"]
let dollars1 = [1453.0,2352,5431]
setChart(months, values: dollars1)
 }

func setChart(_ dataPoints: [String], values: [Double]) {

var dataEntries: [ChartDataEntry] = []

for i in 0 ..< dataPoints.count {
    dataEntries.append(ChartDataEntry(x: Double(i), y: values[i]))
}

let lineChartDataSet = LineChartDataSet(values: dataEntries, label: nil)
lineChartDataSet.axisDependency = .left
lineChartDataSet.setColor(UIColor.black)
lineChartDataSet.setCircleColor(UIColor.black) // our circle will be dark red
lineChartDataSet.lineWidth = 1.0
lineChartDataSet.circleRadius = 3.0 // the radius of the node circle
lineChartDataSet.fillAlpha = 1
lineChartDataSet.fillColor = UIColor.black
lineChartDataSet.highlightColor = UIColor.white
lineChartDataSet.drawCircleHoleEnabled = true

var dataSets = [LineChartDataSet]()
dataSets.append(lineChartDataSet)


let lineChartData = LineChartData(dataSets: dataSets)
lineChart.data = lineCh[![enter image description here][1]][1]artData
lineChart.rightAxis.enabled = false
lineChart.xAxis.drawGridLinesEnabled = false
lineChart.xAxis.labelPosition = .bottom
lineChart.xAxis.valueFormatter = IndexAxisValueFormatter(values: dataPoints)
}
于 2021-10-22T13:01:38.263 回答