How to make rectangle in chart.js

前端 未结 1 1385
盖世英雄少女心
盖世英雄少女心 2021-01-23 07:43

I am trying to put rectangle to make a upper-lower range/level in chart.js, like in image

Although I am able to make it by drawing two line in this example

1条回答
  •  抹茶落季
    2021-01-23 08:08

    There is already a Chart.js plugin exists called chartjs-plugin-annotation, by which you can achieve that much easily.

    Using that plugin, you'll need to create a box annotation (rectangle), as such :

    options: { //your chart options
       annotation: {
          annotations: [{
             type: 'box',
             drawTime: 'beforeDatasetsDraw',
             yScaleID: 'y-axis-0',
             yMin: 40,
             yMax: 50,
             backgroundColor: 'rgba(0, 255, 0, 0.1)'
          }]
       }
    }
    

    note: this is the minimum options required to draw that rectangle, and you can find more options here.

    Here is a working fiddle.

    0 讨论(0)
提交回复
热议问题