Chart.js注释插件 – 方框未正确显示

huangapple go评论62阅读模式
英文:

Chart.js annotation plugin - Box not being displayed correctly

问题

I'm using chart.js@2.9.4 and chartjs-plugin-annotation@0.5.7 in a Vue 2 project. I was able to build a chart with a line but the box is always expanded out to the edges, I'm not sure if the issue is related with my x-axis, since the values are strings (months).

import Chart from 'chart.js';
import AnnotationPlugin from 'chartjs-plugin-annotation';

Chart.plugins.register(AnnotationPlugin);

new Chart(lineChart.value, {
    type: 'line',
    data: {
      labels: [
        'January',
        'February',
        'March',
        'April',
        'May',
        'June',
        'July',
      ],
      datasets: [
        {
          label: 'Dataset 1',
          data: [120, 125, 128, 121, 130, 125, 135],
          borderColor: '#018572',
          backgroundColor: 'green',
          lineTension: 0.1,
          fill: false,
        },
        {
          label: 'Dataset 2',
          data: [81, 85, 88, 90, 83, 82, 80],
          borderColor: '#9F642C',
          backgroundColor: 'brown',
          lineTension: 0.1,
          fill: false,
        },
      ],
    },
    options: {
      title: {
        display: true,
        text: 'Chart.js Line Chart',
      },
      legend: {
        display: true,
        position: 'top',
        labels: {
          boxWidth: 80,
          fontColor: 'black',
        },
      },
      annotation: {
        annotations: [
          {
            drawTime: 'afterDraw',
            id: 'a-line-1',
            type: 'line',
            mode: 'horizontal',
            scaleID: 'y-axis-0',
            value: '25',
            borderColor: 'red',
            borderWidth: 2,
          },
          {
            type: 'box',
            drawTime: 'beforeDatasetsDraw',
            id: 'a-box-1',
            xScaleID: 'x',
            xMax: 'July',
            xMin: 'January',
            yScaleID: 'y',
            yMax: 140,
            yMin: 120,
            backgroundColor: 'green',
          },
        ],
      },
    },
  });

This is the result that I'm always getting:

Chart.js注释插件 – 方框未正确显示

I need to decrease the box in the y-axis since I need to show two bands in the chart. I'll really appreciate any help and suggestions.

英文:

I'm using chart.js@2.9.4 and chartjs-plugin-annotation@0.5.7 in a Vue 2 project. I was able to build a chart with a line but the box is always expanded out to the edges, I'm not sure if the issue is related with my x-axis, since the values are strings (months).

import Chart from 'chart.js';
import AnnotationPlugin from 'chartjs-plugin-annotation';

Chart.plugins.register(AnnotationPlugin);

new Chart(lineChart.value, {
    type: 'line',
    data: {
      labels: [
        'January',
        'February',
        'March',
        'April',
        'May',
        'June',
        'July',
      ],
      datasets: [
        {
          label: 'Dataset 1',
          data: [120, 125, 128, 121, 130, 125, 135],
          borderColor: '#018572',
          backgroundColor: 'green',
          lineTension: 0.1,
          fill: false,
        },
        {
          label: 'Dataset 2',
          data: [81, 85, 88, 90, 83, 82, 80],
          borderColor: '#9F642C',
          backgroundColor: 'brown',
          lineTension: 0.1,
          fill: false,
        },
      ],
    },
    options: {
      title: {
        display: true,
        text: 'Chart.js Line Chart',
      },
      legend: {
        display: true,
        position: 'top',
        labels: {
          boxWidth: 80,
          fontColor: 'black',
        },
      },
      annotation: {
        annotations: [
          {
            drawTime: 'afterDraw',
            id: 'a-line-1',
            type: 'line',
            mode: 'horizontal',
            scaleID: 'y-axis-0',
            value: '25',
            borderColor: 'red',
            borderWidth: 2,
          },
          {
            type: 'box',
            drawTime: 'beforeDatasetsDraw',
            id: 'a-box-1',
            xScaleID: 'x',
            xMax: 'July',
            xMin: 'January',
            yScaleID: 'y',
            yMax: 140,
            yMin: 120,
            backgroundColor: 'green',
          },
        ],
      },
    },
  });

This is the result that I'm always getting:

Chart.js注释插件 – 方框未正确显示

I need to decrease the box in the y-axis since I need to show two bands in the chart.
I'll really appreciate any help and suggestions.

答案1

得分: 2

I think the issue is related to xScaleID and yScaleID values. You set x and y as values but in chart.js 2, the default scales IDs are different. In fact, in the line annotation, you are setting y-axis-0.
That said, I think setting xScaleID: x-axis-0 and yScaleID: y-axis-0 should work.

英文:

I think the issue is related to xScaleID and yScaleID values. You set 'x' and 'y' as values but in chart.js 2, the default scales IDs are different. In fact, in the line annotation, you are setting 'y-axis-0'.
That said, I think setting xScaleID: 'x-axis-0' and yScaleID: 'y-axis-0' should work.

huangapple
  • 本文由 发表于 2023年3月23日 08:47:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818409.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定