typescript - Angular highcharts 显示周数问题
我在我的 Angular 应用程序中使用 Highcharts 时遇到了一个问题,我就是无法解决。
我正在尝试在 x 轴上显示周数,x 轴的类型为 'datetime',如下所示。我找到了这个 JSFiddle,它应该可以解决这个问题,但当我尝试在我的代码中使用它时,会出现这个错误 Cannot assign to 'dateFormats' because it is a constant or a read-only property. 在本描述的底部,我已经包含了 dateFormats 的说明,难道不应该可以这样做吗?
我在这里忽视了什么 :)
以下代码第1行出现错误:
Highcharts.dateFormats = {
W: function (timestamp) {
console.log('timestamp', timestamp);
return 45;
}
};
Highcharts.setOptions({
xAxis = {
type: 'datetime',
labels: {
format: '{value:Week %W/%Y}'
},
minTickInterval: 1000 * 3600 * 24 * 7,
minRange: 1000 * 3600 * 24 * 7
};
});
来自 Highcharts 文件
/**
* A hook for defining additional date format specifiers. New specifiers are defined as key-value pairs by using the
* specifier as key, and a function which takes the timestamp as value. This function returns the formatted portion
* of the date.
*/
dateFormats: DateFormatSpecifiers;
答案#1
在对 Highcharts 版本 7.1.0 添加修复后,Highcharts.dateFormats 被初始化为空对象,因此你仍然无法覆盖它,但可以自由地向其中添加条目。条目需要是 Highcharts.TimeFormatCallbackFunction 类型,其中 type TimeFormatCallbackFunction=(timestamp: number) => string。
简而言之:下面的代码运行正常:
Highcharts.dateFormats.W = function (timestamp) {
console.log('timestamp', timestamp);
return '45'; // must return string
};
