Draw horizontal line on chart in chart.js on v2

后端 未结 4 1119
慢半拍i
慢半拍i 2020-12-13 04:05

I have drawn a line chart using chart.js. For the labels and datasets i am getting values from the database. I am new to chart.js and its very powerful library, yet i am una

4条回答
  •  臣服心动
    2020-12-13 04:43

    If you are using the NPM package chartjs-plugin-annotation.js, the important thing - which you may forget, is to register the plugin.

    So first of all you installed the npm packages (here for React):

    npm i react-chartjs-2                (depends on your framework)
    npm i chartjs-plugin-annotation      (always required)
    

    See Vue.js or Angular for their framework depending packages.

    Option 1: Global plugin registration

    import { Line } from 'react-chartjs-2';
    import Chart from 'chart.js';
    import * as ChartAnnotation from 'chartjs-plugin-annotation';
    
    Chart.plugins.register([ChartAnnotation]); // Global
    
    // ...
    render() {
        return (
            
        )
    }
    

    Option 2: Per chart plugin registration

    import { Line } from 'react-chartjs-2';
    import * as ChartAnnotation from 'chartjs-plugin-annotation';
    
    // ...
    render() {
        return (
                                                       {/* per chart */}
            
        )
    }
    

    chartData is equivalent to the data: { section and chartOpts to options: { from jordanwillis answer. See this github post for further information.

    There are many other plugins available for chart.js.

提交回复
热议问题