如何删除彩色标签方块

时间:2019-01-30 20:25:15

标签: chart.js

我在标签旁边有一个红色正方形:

enter image description here

如何删除它?

这是我的图表实施

var ctx = document.getElementById(chart').getContext("2d");

        var myChart = new Chart(ctx, {
            type: 'bar',
            responsive: true,
            data: {
                labels: ["test"]
                datasets: [{
                    label: "Sfarzoso",
                    fill: false,
                    borderColor: '#000',
                    borderWidth: 2,
                    borderDash: [],
                    borderDashOffset: 0.0,
                    data: ["1"]
                    backgroundColor: ["#fff5f7"]
                }]
            },
        });

我如何获得这样的结果?

1 个答案:

答案 0 :(得分:1)

您需要添加options对象,并指示不显示图例

var options = {
        legend: {
            display: false
        }
    };

然后使用图表构造函数中的选项

var myChart = new Chart(ctx, {
  type: 'bar',
  responsive: true,
  /* include options in the constructor */
  options: options,
  ... }

请参见下面的示例

有关可用选项的文档位于其网站上:https://www.chartjs.org/docs/latest/getting-started/

var ctx = document.getElementById('chart').getContext("2d");

// add options object - indicate not to display the legend
var options = {
        legend: {
            display: false
        }
    };

var myChart = new Chart(ctx, {
  type: 'bar',
  responsive: true,
  /* include options in the constructor */
  options: options,
  data: {
    labels: ["test"],
    datasets: [{
      label: "Sfarzoso",
      fill: false,
      borderColor: '#000',
      borderWidth: 2,
      borderDash: [],
      borderDashOffset: 0.0,
      data: ["1"],
      backgroundColor: ["#fff5f7"]
    }]
  },
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js"></script>


<canvas id="chart"></canvas>