gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 02:08:13 29 4
gpt4 key购买 nike

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

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

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

结果应该类似于: Desired Result

How it looks right now

提前谢谢你。

最佳答案

您可以简单地更新 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;
}

您可以根据需要在此处更新 widthheight

工作演示:

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>

关于javascript - 隐藏 Chart.js 中的所有标签和工具提示并使其非常小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61101942/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com