Chartjs:制作方形图例

问题描述

使用 react-chartjs-2,创建条形图如下。我正在尝试将图例设为方形而不是认矩形。

我应用 BoxWidth: 10 如下但它不起作用,是否有其他可能的替代方法生成方形图例

const sample_data = {
  labels: ['Item1','Item2'],datasets: [
    {
      data: [10,10,50],borderWidth: 1,backgroundColor: 'yellow',},{ data: [20,20,20],backgroundColor: 'green' 
    },],};
const sample_options = {
  responsive: true,legend: {
    BoxWidth: 10,// Also 0 doesn't make any change
  },...
};
<Bar data={sample_data} options={sample_options} />;

解决方法

您必须按照文档中的说明将 boxWidth 放在图例选项的标签部分:https://www.chartjs.org/docs/latest/configuration/legend.html#legend-label-configuration

var options = {
  type: 'line',data: {
    labels: ["Red","Blue","Yellow","Green","Purple","Orange"],datasets: [{
        label: '# of Votes',data: [12,19,3,5,2,3],borderWidth: 1
      },{
        label: '# of Points',data: [7,11,8,7],borderWidth: 1
      }
    ]
  },options: {
    legend: {
      labels: {
        boxWidth: 10
      }
    },scales: {
      yAxes: [{
        ticks: {
          reverse: false
        }
      }]
    }
  }
}

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/2.9.4/Chart.js" integrity="sha512-hZf9Qhp3rlDJBvAKvmiG+goaaKRZA6LKUO35oK6EsM0/kjPK32Yw7URqrq3Q+Nvbbt8Usss+IekL7CRn83dYmw==" crossorigin="anonymous"></script>
</body>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...