【问题标题】:Chartjs autocolors not working as expected with datasetChartjs autocolors 无法按预期使用数据集
【发布时间】:2022-11-30 03:59:14
【问题描述】:

Autocolors 插件无法按预期与 chartjs 一起使用。它不会分配不同的颜色。

const autocolors = window['chartjs-plugin-autocolors'];

var options = {
  type: 'line',
  data: {
    labels: ["0", "1", "2", "3", "4", "5"],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3]
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7]
      },
      {
        label: '# of People',
        data: [3, 1, 15, 4, 9, 12]
      }
    ]
  },
  options: {
    plugins: {
      autocolors: {
         mode: 'dataset'
      }
    }
  }
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.9.1/chart.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-autocolors"></script>
</body>

【问题讨论】:

    标签: chart.js


    【解决方案1】:

    它不起作用,因为您从未注册过该插件,如果您这样做,它就可以正常工作:

    const autocolors = window['chartjs-plugin-autocolors'];
    
    Chart.register(autocolors);
    
    var options = {
      type: 'line',
      data: {
        labels: ["0", "1", "2", "3", "4", "5"],
        datasets: [{
            label: '# of Votes',
            data: [12, 19, 3, 5, 2, 3]
          },
          {
            label: '# of Points',
            data: [7, 11, 5, 8, 3, 7]
          },
          {
            label: '# of People',
            data: [3, 1, 15, 4, 9, 12]
          }
        ]
      },
      options: {
        plugins: {
          autocolors: {
             mode: 'dataset'
          }
        }
      }
    }
    
    var ctx = document.getElementById('chartJSContainer').getContext('2d');
    new Chart(ctx, options);
    <body>
      <canvas id="chartJSContainer" width="600" height="400"></canvas>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.9.1/chart.js"></script>
      <script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-autocolors"></script>
    </body>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-22
      • 2022-09-02
      • 2020-03-20
      • 2018-06-22
      • 1970-01-01
      • 1970-01-01
      • 2019-01-05
      • 1970-01-01
      相关资源
      最近更新 更多