ChartJs - 无法绘制注释线

问题描述 投票:0回答:1

我试图绘制平均价格注释但没有任何反应..价格条也被绘制,但注释没有任何反应。我也检查了控制台,没有错误或警告

const Chart = require("chart.js");

const ctx = document.querySelector("#canvas").getContext("2d");

new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ["iPhone", "Samsung", "Xiaomi"],
        datasets: [
            {
                data: [1000, 700, 900]
            },
        ]
    },
    options: {
        title: {
            display: true,
            text: "Price Comparison",
            fontSize: 22,
            fontColor: "white",
        },
        legend: {
            display: false
        },

        responsive: true,
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero: true,
                    display: false
                }
            }],
        }
    },
    plugins: {
        annotation: {
            drawTime: 'afterDatasetsDraw',

            annotations: [{
                type: 'line',
                mode: 'horizontal',
                scaleID: 'y-axis-0',
                value: 750,
                borderColor: 'rgb(75, 192, 192)',
                borderWidth: 4,
                label: {
                    enabled: true,
                    content: 'Average Price'
                }
            }]
        }
    }
});
javascript chart.js
1个回答
0
投票

我找到了。我忘了注册插件。在我使用另一个不需要手动广告的插件之前。但似乎有些插件并未全球添加。我们应该手动添加它们..

const Chart = require("chart.js");
const Annotation = require("chartjs-plugin-annotation");
Chart.plugins.register(Annotation);
© www.soinside.com 2019 - 2024. All rights reserved.