Im running round in circles trying to find a decent way to get week dates on the xaxis on a chart.
In the following fiddle it is easily added with the dateFormats hook, but using Angular I don't have access to any global "Highcharts" object. So how do I add a custom formatter if using Highcharts with Angular?
I've tried adding the formatter to the chart.options directly like so:
{
dateFormats : {
W: function (timestamp) {
var date = new Date(timestamp),
day = date.getUTCDay() === 0 ? 7 : date.getUTCDay(),
dayNumber;
date.setDate(date.getUTCDate() + 4 - day);
dayNumber = Math.floor((date.getTime() - new Date(date.getUTCFullYear(), 0, 1, -6)) / 86400000);
return 1 + Math.floor(dayNumber / 7);
}
},
title: {
text: 'Custom date format'
},
xAxis: {
type: 'datetime',
tickInterval: 7 * 24 * 36e5, // one week
labels: {
format: '{value:Week %W/%Y}',
align: 'right',
rotation: -30
}
},
series: [{
data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4],
pointInterval: 7 * 24 * 36e5,
pointStart: Date.UTC(2013, 0, 7)
}]
}
I've also tried importing highcharts in my app.comonent to get hold of the highcharts object as:
import * as highcharts from '../../node_modules/highcharts';
Something like: https://github.com/gevgeny/angular2-highcharts/blob/master/examples/webpack/src/comboMultiAxesExample.ts
But since I use typescript that results in a '--allowJs' is not set error, and just seems overly complicated.
EDIT:
Using the formatter you can do something like http://jsfiddle.net/dxyyzgar/
or:
{
series: data,
legend: {enabled: true},
xaxis: {
type: 'datetime',
formatter: () => {
return this.value
},
title: {
text: 0
}
}
}
But in Angular "this" reference the current component and not the Highcharts object.