Chart.js v2 隐藏数据集标签
Chart.js v2 hide dataset labels
我有以下代码使用 Chart.js v2.1.3 创建图表:
var ctx = $('#gold_chart');
var goldChart = new Chart(ctx, {
type: 'line',
data: {
labels: dates,
datasets: [{
label: 'I want to remove this Label',
data: prices,
pointRadius: 0,
borderWidth: 1
}]
}
});
代码看起来很简单,但我无法从图中删除标签。我尝试了很多网上找到的解决方案,但大多数都使用 Chart.js v1.x.
如何删除数据集标签?
只需像这样设置 label
和 tooltip
选项
...
options: {
legend: {
display: false
},
tooltips: {
callbacks: {
label: function(tooltipItem) {
return tooltipItem.yLabel;
}
}
}
}
Fiddle - http://jsfiddle.net/g19220r6/
添加:
Chart.defaults.global.legend.display = false;
在脚本代码的开头;
您还可以通过删除 "title":
将工具提示放在一行中
this.chart = new Chart(ctx, {
type: this.props.horizontal ? 'horizontalBar' : 'bar',
options: {
legend: {
display: false,
},
tooltips: {
callbacks: {
label: tooltipItem => `${tooltipItem.yLabel}: ${tooltipItem.xLabel}`,
title: () => null,
}
},
},
});
就像添加这个一样简单:
legend: {
display: false,
}
或者,如果您愿意,可以使用其他应该也有效的选项:
Chart.defaults.global.legend.display = false;``
new Chart('idName', {
type: 'typeChar',
data: data,
options: {
legend: {
display: false
}
}
});
用此代码段替换选项,将修复 Vanilla JavaScript 开发人员
options: {
title: {
text: 'Hello',
display: true
},
scales: {
xAxes: [{
ticks: {
display: false
}
}]
},
legend: {
display: false
}
}
截至 2021 年,命名空间已从 options.legend
更改为 options.plugins.legend
。这个简单的代码对我有用 -
data{
...
},
options: {
plugins: {
legend: {
display: false
}
}
}
新解决方案 ChartJS v3.1.1
以上解决方案对于v3.1之前的chart js版本是正确的,对于v3.1.1使用如下
...
options: {
plugins:{
legend: {
display: false
}
}
}
对于那些想要在 2021 年删除实际轴标签而不仅仅是图例的人(Chart.js v.3.5.1)。注意:这也会删除轴。
const chartWrap = document.querySelector('.chart-wrap')
const canvas = document.createElement('canvas')
chartWrap.appendChild(canvas)
const ctx = canvas.getContext('2d')
const myChart = new Chart(ctx, {
type: 'line',
data: {
labels: ['Your', 'axis', 'labels'],
datasets: [
{
label: 'Your legend label',
data: [1, 3, 2],
backgroundColor: '#54ACEF'
}
]
},
options: {
maintainAspectRatio: false,
plugins: {
legend: false // Hide legend
},
scales: {
y: {
display: false // Hide Y axis labels
},
x: {
display: false // Hide X axis labels
}
}
}
})
<div class="chart-wrap" style="width: 200px; height: 100px;"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.1/chart.min.js" integrity="sha512-Wt1bJGtlnMtGP0dqNFH1xlkLBNpEodaiQ8ZN5JLA5wpc1sUlk/O5uuOMNgvzddzkpvZ9GLyYNa8w2s7rqiTk5Q==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
我有以下代码使用 Chart.js v2.1.3 创建图表:
var ctx = $('#gold_chart');
var goldChart = new Chart(ctx, {
type: 'line',
data: {
labels: dates,
datasets: [{
label: 'I want to remove this Label',
data: prices,
pointRadius: 0,
borderWidth: 1
}]
}
});
代码看起来很简单,但我无法从图中删除标签。我尝试了很多网上找到的解决方案,但大多数都使用 Chart.js v1.x.
如何删除数据集标签?
只需像这样设置 label
和 tooltip
选项
...
options: {
legend: {
display: false
},
tooltips: {
callbacks: {
label: function(tooltipItem) {
return tooltipItem.yLabel;
}
}
}
}
Fiddle - http://jsfiddle.net/g19220r6/
添加:
Chart.defaults.global.legend.display = false;
在脚本代码的开头;
您还可以通过删除 "title":
将工具提示放在一行中this.chart = new Chart(ctx, {
type: this.props.horizontal ? 'horizontalBar' : 'bar',
options: {
legend: {
display: false,
},
tooltips: {
callbacks: {
label: tooltipItem => `${tooltipItem.yLabel}: ${tooltipItem.xLabel}`,
title: () => null,
}
},
},
});
就像添加这个一样简单:
legend: {
display: false,
}
或者,如果您愿意,可以使用其他应该也有效的选项:
Chart.defaults.global.legend.display = false;``
new Chart('idName', {
type: 'typeChar',
data: data,
options: {
legend: {
display: false
}
}
});
用此代码段替换选项,将修复 Vanilla JavaScript 开发人员
options: {
title: {
text: 'Hello',
display: true
},
scales: {
xAxes: [{
ticks: {
display: false
}
}]
},
legend: {
display: false
}
}
截至 2021 年,命名空间已从 options.legend
更改为 options.plugins.legend
。这个简单的代码对我有用 -
data{
...
},
options: {
plugins: {
legend: {
display: false
}
}
}
新解决方案 ChartJS v3.1.1
以上解决方案对于v3.1之前的chart js版本是正确的,对于v3.1.1使用如下
...
options: {
plugins:{
legend: {
display: false
}
}
}
对于那些想要在 2021 年删除实际轴标签而不仅仅是图例的人(Chart.js v.3.5.1)。注意:这也会删除轴。
const chartWrap = document.querySelector('.chart-wrap')
const canvas = document.createElement('canvas')
chartWrap.appendChild(canvas)
const ctx = canvas.getContext('2d')
const myChart = new Chart(ctx, {
type: 'line',
data: {
labels: ['Your', 'axis', 'labels'],
datasets: [
{
label: 'Your legend label',
data: [1, 3, 2],
backgroundColor: '#54ACEF'
}
]
},
options: {
maintainAspectRatio: false,
plugins: {
legend: false // Hide legend
},
scales: {
y: {
display: false // Hide Y axis labels
},
x: {
display: false // Hide X axis labels
}
}
}
})
<div class="chart-wrap" style="width: 200px; height: 100px;"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.1/chart.min.js" integrity="sha512-Wt1bJGtlnMtGP0dqNFH1xlkLBNpEodaiQ8ZN5JLA5wpc1sUlk/O5uuOMNgvzddzkpvZ9GLyYNa8w2s7rqiTk5Q==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>