隐藏 Chart.js 中的所有标签和工具提示并使其非常小

Hide all labels and tooltips in Chart.js and make it very small size

所以我正在尝试在我的 React 应用程序中使用 react-chartjs-2 创建一些简约图表。

我想要实现的是在我创建的小卡片中显示一个非常小的图表,没有任何类型的标签。我想隐藏标签、图例,甚至图表网格。

我在文档或我看过的其他堆栈溢出问题中找不到如何让它像这样工作。

结果应与此类似: Desired Result

How it looks right now

提前致谢。

您可以使用选项对象自定义图表:

options = { title: { display: false }, legend: { display: false } };

您可以像这样简单地更新 options 属性:

options: {
    tooltips: {
      enabled: false,
    },
    legend: {
      display: false
    },
    scales: {
      xAxes: [{display: false}],
      yAxes: [{display: false}],
    }
  }

为了让它变得非常小,您可以将 canvas 放入容器 div 中,例如:

<div class="chart-container">
    <canvas id="myChart"></canvas>
</div>

并添加一些 CSS,例如:

.chart-container {
   width: 200px;
}
#myChart {
  display: block; 
  width: 200px; 
  height: 50px;
}

您可以根据需要在此处更新 width & height

工作演示:

var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
  // The type of chart we want to create
  type: 'line',

  // The data for our dataset
  data: {
    labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
    datasets: [{
      label: 'My First dataset',
      backgroundColor: 'rgb(255, 99, 132)',
      borderColor: 'rgb(255, 99, 132)',
      data: [0, 10, 5, 15, 20, 30, 45]
    }]
  },

  // Configuration options go here
  options: {
    tooltips: {
      enabled: false,
    },
    legend: {
      display: false
    },
    scales: {
      xAxes: [{
        display: false
      }],
      yAxes: [{
        display: false
      }],
    }
  }
});
.chart-container {
   width: 200px;
}
#myChart {
  display: block; 
  width: 200px; 
  height: 50px;
}
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>

<div class="chart-container">
    <canvas id="myChart"></canvas>
</div>