Home > database >  HighCharts : xAxis Categories Date format not working
HighCharts : xAxis Categories Date format not working

Time:02-17

When I set Date format for XAxis. It's not working Below is my code :

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]

It display original date from array only which I have set in xAxis.categories

CodePudding user response:

First of all I know this is not the best answer because this has nothing to do with highcharts. There should be a proper way of achieving this using highcharts.

Until someone post the correct way you can try this.

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)"
    }
}

use this as 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()}

CodePudding user response:

Let's simplified your example, do you want achieve something like this?

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

Maybe the format of your date is incorrect, have you tried giving values in milliseconds?

  • Related