chartjs-在整数x轴值上绘制垂直线

mag*_*ged 3 chart.js

在下面的示例中,chartjs批注适用于字符串值("MAR"),但不适用于整数值。如何在一些整数x轴值上绘制一条垂直线。

var chartData = {
  labels: ["JAN", "FEB", "MAR"],
  datasets: [
    {
      data: [12, 3, 2]
    }
  ]
};

window.onload = function() {
  var ctx = document.getElementById("canvas").getContext("2d");
  new Chart(ctx, {
    type: "line",
    data: chartData,
    options: {
      annotation: {
        annotations: [
          {
            type: "line",
            mode: "vertical",
            scaleID: "x-axis-0",
            value: 2,
            borderColor: "red",
            label: {
              content: "TODAY",
              enabled: true,
              position: "top"
            }
          }
        ]
      }
    }
  });
};
Run Code Online (Sandbox Code Playgroud)

参见小提琴:https : //codepen.io/anon/pen/QaQWba

bea*_*ver 5

您可以实现将数据作为传递,配置xAxes线性创建自定义刻度格式的目标

数据:

var chartData = {
  labels: ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"],
  datasets: [
    {
      data: [{x: 1, y: 12}, {x: 2, y: 3}, {x: 3, y: 2}, {x: 4, y: 1}, {x: 5, y: 8}, {x: 6, y: 8}, {x: 7, y: 2}, {x: 8, y: 2}, {x: 9, y: 3}, {x: 10, y: 5}, {x: 11, y: 11}, {x: 12, y: 1}];
    }
  ]
};
Run Code Online (Sandbox Code Playgroud)

xAxes配置:

xAxes: [{
  type: 'linear',
  position: 'bottom',
  ticks: {
        max: 12,
        min: 1,
        stepSize: 1,
        callback: function(value, index, values) {
             return chartData.labels[index];
        }
   }
}]
Run Code Online (Sandbox Code Playgroud)

检查更新的CodePen:https ://codepen.io/beaver71/pen/XVZXOM