highcharts 高排行榜:xAxis类别日期格式不工作

jobtbby3  于 2022-11-11  发布在  Highcharts
关注(0)|答案(2)|浏览(184)

当我设置日期格式为XAxis .它不工作下面是我的代码:

let options = HIOptions()

xAxis.type = "datetime"
xAxis.categories = ["2022-01-17T00:00:00","2022-01-18T00:00:00","2022-01-19T00:00:00","2022-01-20T00:00:00","2022-01-21T00:00:00"]

xAxis.dateTimeLabelFormats = HIDateTimeLabelFormats() 

xAxis.dateTimeLabelFormats.month = HIMonth()

xAxis.dateTimeLabelFormats.month.main = "%e. %b"

options.xAxis = [xAxis]

它只显示我在xAxis.categories中设置的数组中的原始日期

rseugnpd

rseugnpd1#

首先,我知道这不是最好的答案,因为这与highcharts无关。应该有一个使用highcharts的正确方法来实现这一点。
直到有人张贴正确的方法,你可以尝试这个。

extension String {

    func formatStringDate() -> String{

        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy'-'MM'-'dd'T'hh':'mm':'ss"
        let date = dateFormatter.date(from: self)

        let calendar = Calendar.current
        let components = calendar.dateComponents([.year, .month, .day, .hour], from: date!)

        return "\(components.month ?? 0).\(components.day ?? 0)"
    }
}

将其用作xAxis.categories = ["2022-01-17T00:00:00","2022-01-18T00:00:00","2022-01-19T00:00:00","2022-01-20T00:00:00"].map{ $0.formatStringDate()}

ozxc1zmp

ozxc1zmp2#

让我们简化一下你的example,你想实现这样的东西吗?

dateTimeLabelFormats: {
  minute: '%e. %b',
},

也许你的日期格式不正确,你有没有试过以毫秒为单位给出值?

相关问题