简体   繁体   English

Chart.js - x 轴时间序列的更改刻度 / label 位置

[英]Chart.js - changing tick / label positions for x axis time series

I have a datetime series along the x axis of a chart in Chart.js but have changed the position of the tick / labels to be between the bars according to the method here:我在 Chart.js 中的图表的 x 轴上有一个日期时间序列,但根据此处的方法将刻度 / 标签的 position 更改为条形之间:

Chart.js: How I change the x axes ticks labels alignment in any sizes? Chart.js:如何更改任何尺寸的 x 轴刻度标签 alignment?

This resulted in the datetimes moving to the right of the bar they related to, whereas I needed them on the left.这导致日期时间移动到与它们相关的栏的右侧,而我需要它们在左侧。 So I changed the return calculation to subtract the amount instead of add:因此,我更改了返回计算以减去金额而不是添加:

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;
    },
});

This mostly works except for the last column.除了最后一列之外,这大部分都有效。 How can I align this last column correctly?如何正确对齐最后一列?

在此处输入图像描述

I think the problem is at these lines我认为问题出在这些方面

    var nextPx = this.right;
    var nextTick = ticks[index + 1];
    if (nextTick) {
       nextPx = this.getPixelForOffset(nextTick.value);
       return px - (nextPx - px) / 2;
    }
    else{
       var prevTick = ticks[index - 1];
       prevPx = this.getPixelForOffset(prevTick .value);
       return px - (px - prevPx ) / 2;
//       return px + (px - prevPx ) / 2;   if the above statement don't work

    }

When nextTick is null, nextPx takes the value of this.right which creates problem.当 nextTick 为 null 时,nextPx 取 this.right 的值,这会产生问题。 You have to add an else block for nextTick to handle the rightmost label.您必须为 nextTick 添加一个 else 块来处理最右边的 label。 I updated the code for else block .我更新了 else 块的代码 Its just an idea, you may find a better way.这只是一个想法,您可能会找到更好的方法。

You could simply define offset: true on your x-axes as follows:您可以简单地在 x 轴上定义offset: true ,如下所示:

options: {
  scales: {
    xAxes: [{
      offset: true,
      ...

Please have a look at below runnable code snippet.请查看下面的可运行代码片段。

 new Chart("chart", { type: 'bar', data: { labels: ["2020-05-13", "2020-05-11", "2020-05-12", "2020-05-14", "2020-05-09", "2020-05-10"], datasets: [{ label: "My Dataset", backgroundColor: "#02a499", borderColor: "#ffffff", borderWidth: 1, hoverBackgroundColor: "#02a499", hoverBorderColor: "#02a499", data: [20, 11, 9, 22, 11, 9] }] }, options: { scales: { xAxes: [{ offset: true, type: 'time', time: { unit: 'day', source: 'data', tooltipFormat: 'MMM DD' }, gridLines: { display: false } }], yAxes: [{ ticks: { beginAtZero: true }, gridLines: { display: false } }] } } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js"></script> <canvas id="chart" height="120"></canvas>

Based on @MuhammadUmarFarooq answer I implemented the following code to check for the previous tick if there isn't a next one.基于@MuhammadUmarFarooq 的回答,如果没有下一个,我实现了以下代码来检查上一个刻度。 I am using distribution: 'series' so all the ticks on the x axis should be the same distance so I just to calculate the distance between any two.我正在使用distribution: 'series'所以 x 轴上的所有刻度都应该是相同的距离,所以我只是计算任意两个之间的距离。

// Get the next tick's pixel value.
        var nextPx = this.right;
        var nextTick = ticks[index + 1];
        if (nextTick) {
            nextPx = this.getPixelForOffset(nextTick.value);
        } else {
            var previousPx = this.left;
            var previousTick = ticks[index - 1];
            previousPx = this.getPixelForOffset(previousTick.value);
            return previousPx + (px - previousPx) / 2;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM