ChartJS 图表Js注解插件未显示,但无错误

0ve6wy6x  于 2022-11-29  发布在  Chart.js
关注(0)|答案(1)|浏览(173)

我试图复制其他解决方案,但不起作用。图表显示,但没有注解行。显示我的代码如下。非常感谢您的关注。集成在base.html上

<script src="path/to/chartjs/dist/chart.min.js"></script>
<script src="path/to/chartjs-plugin-annotation/dist/chartjs-plugin-annotation.min.js"></script>

在扩展base.html之后的另一个页面上:

<script>

  // jquery function
  $(document).ready(function(){
    const ctx = document.getElementById('myChart');

    const labels = [{% for item in query_set%}'{{item.analysis_date}}',{% endfor %}];

    const data = {
      labels: labels,
      datasets: [
      //Если добавить еще один - будет сдвоенный график
        {
          label: '{{analysis_info.a_name}} - {{analysis_info.a_measur}}',
          data: [{% for item in query_set%}'{{item.analysis_data}}',{% endfor %}],
          borderColor: 'rgb(75, 192, 192)',
          borderWidth: 4,
          
        },
      ]
    };

    const options = {
      scales: {
        yAxes: [{
          ticks: {
            beginAtZero: true,
            min: 0
          }
        }]
      },
      animation: {
            duration: 1500,
        },
      plugins: {
        autocolors: false,
        annotation: {
          annotations: {
            box1: {
              // Indicates the type of annotation
              type: 'line',
              yMin: 10,
              yMax: 10,
              backgroundColor: 'rgba(255, 99, 132, 0.25)'
            }
          }
        }
      }
    }
    const config = {
      type: 'line',
      data,
      options,
      };

    const chart = new Chart(ctx, config);

  });
</script>
{% endblock scripts %}

但没有React
我相信解决办法很简单,但我被卡住了

o4tp2gmn

o4tp2gmn1#

正如您在评论中重复的那样,“通过源库/Chart. js/2.8.0/Chart. min. js库/chartjs-plugin-annotation/2.1.0导入”,您使用的是2个不兼容的版本。
请参阅插件库的Redame:https://github.com/chartjs/chartjs-plugin-annotation#readme

An annotation plugin for Chart.js >= 3.7.0

For Chart.js 3.0.0 to 3.6.2 support, use version 1.4.0 of this plugin For Chart.js 2.4.0 to 2.9.x support, use version 0.5.7 of this plugin

相关问题