Draw horizontal line on chart in chart.js on v2

后端 未结 4 1114
慢半拍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 05:00

    Here's an example of getting it working in a Rails view if you're using it with the Chartkick gem:

    <%=
      line_chart profit_per_day_chart_path(staff), xtitle: 'Day', ytitle: 'Profit',
        library: {
          annotation: {
            annotations: [
              {
                type: 'line',
                mode: 'horizontal',
                scaleID: 'y-axis-0',
                value: 20,
                label: {
                  content: 'My Horizontal Line',
                  enabled: true
                }
              }
            ]
          }
        }
    %>
    

    Ensure that you've registered the chartjs-plugin-annotation.js plugin with Chart.js first:

    import ChartAnnotationsPlugin from 'chartjs-plugin-annotation';
    Chart.plugins.register(ChartAnnotationsPlugin);
    

提交回复
热议问题