【发布时间】:2020-10-12 15:17:22
【问题描述】:
我在 Chart.js 中沿图表的 x 轴有一个日期时间序列,但已根据此处的方法将刻度/标签的位置更改为条形之间:
Chart.js : How I change the x axes ticks labels alignment in any sizes?
这导致日期时间移动到与它们相关的栏的右侧,而我需要它们在左侧。所以我将返回计算改为减去金额而不是加:
var TimeCenterScale = Chart.scaleService.getScaleConstructor('time').extend({
getPixelForTick: function(index) {
var ticks = this.getTicks();
if (index < 0 || index >= ticks.length) {
return null;
}
// Get the pixel value for the current tick.
var px = this.getPixelForOffset(ticks[index].value);
// Get the next tick's pixel value.
var nextPx = this.right;
var nextTick = ticks[index + 1];
if (nextTick) {
nextPx = this.getPixelForOffset(nextTick.value);
}
// Align the labels in the middle of the current and next tick.
return px - (nextPx - px) / 2;
},
});
除了最后一列之外,这大部分都有效。 如何正确对齐最后一列?
【问题讨论】:
标签: javascript datetime chart.js alignment