admin管理员组文章数量:1201410
Chart.js isn't showing legends. I'm using version 2.4.0, and I've included the Chart.bundle.min.js
script in my website.
All of the variables like lineaRoja
, puntos
, MuPu
were defined and the data is shown correctly.
The problem is the legends with Falla balanceada
, Mu y Pu aplicados
and Diagrama M-P
with their respective colours aren't showing. Only the tooltips when I hover on the dots show.
var canvas = document.getElementById(domId);
var ctx = canvas.getContext("2d");
var lineChart = new Chart(ctx, {
type: 'line',
data: {
datasets: [{
label: 'Diagrama M-P',
data: puntos
}, {
label: 'Falla balanceada',
backgroundColor: 'rgba(0,0,0,0)',
borderColor: 'rgba(130,0,0,0.6)',
data: [{ x: 0, y: 0}, { x: lineaRoja[0], y: lineaRoja[1] }],
borderDash: [10, 5]
}, {
type: 'scatter',
data: MuPu,
fill: false,
showLine: false,
backgroundColor: 'rgba(0,130,0,0.6)',
label: 'Mu y Pu aplicados',
pointRadius: 6
}]
},
options: {
animation: { duration: 0 },
scales: {
xAxes: [{
type: 'linear',
position: 'bottom',
scaleLabel: {
display: true,
labelString: 'Ton'
}
}],
yAxes: [{
scaleLabel: {
display: true,
labelString: 'Ton m'
},
ticks: {
min: 0,
beginAtZero: true
}
}]
}
}
});
Chart.js isn't showing legends. I'm using version 2.4.0, and I've included the Chart.bundle.min.js
script in my website.
All of the variables like lineaRoja
, puntos
, MuPu
were defined and the data is shown correctly.
The problem is the legends with Falla balanceada
, Mu y Pu aplicados
and Diagrama M-P
with their respective colours aren't showing. Only the tooltips when I hover on the dots show.
var canvas = document.getElementById(domId);
var ctx = canvas.getContext("2d");
var lineChart = new Chart(ctx, {
type: 'line',
data: {
datasets: [{
label: 'Diagrama M-P',
data: puntos
}, {
label: 'Falla balanceada',
backgroundColor: 'rgba(0,0,0,0)',
borderColor: 'rgba(130,0,0,0.6)',
data: [{ x: 0, y: 0}, { x: lineaRoja[0], y: lineaRoja[1] }],
borderDash: [10, 5]
}, {
type: 'scatter',
data: MuPu,
fill: false,
showLine: false,
backgroundColor: 'rgba(0,130,0,0.6)',
label: 'Mu y Pu aplicados',
pointRadius: 6
}]
},
options: {
animation: { duration: 0 },
scales: {
xAxes: [{
type: 'linear',
position: 'bottom',
scaleLabel: {
display: true,
labelString: 'Ton'
}
}],
yAxes: [{
scaleLabel: {
display: true,
labelString: 'Ton m'
},
ticks: {
min: 0,
beginAtZero: true
}
}]
}
}
});
Share
Improve this question
asked Oct 13, 2017 at 22:11
Chris VilchesChris Vilches
1,1873 gold badges11 silver badges28 bronze badges
1
- Looks like newer versions of ChartJS require you to register the individual elements you're going to use. You need to register the legend specifically. stackoverflow.com/questions/70753316/… – Ivan Rubinson Commented Jan 18, 2022 at 11:11
4 Answers
Reset to default 11Next to adding the legend config into options Legend - Chart.js docs :
const chart = new Chart(ctx, {
type: 'bar',
data: data,
options: {
plugins: {
legend: {
display: true,
}
}
}
});
You have to import and register the module when using build tools like Webpack or Rollup Integration - Chart.js docs:
Chart.register(
Legend,
)
Or use one of the auto-register methods :
import { Chart, registerables } from 'chart.js';
Chart.register(...registerables);
or:
import Chart from 'chart.js/auto';
you can add this in options.
legend: {
display: true
}
Solved, the Bootstrap template I was using had this code somewhere in its .js
Chart.defaults.global.legend = {
enabled: false
};
To show the legend label the chart, add this to your configuration.
legend: {
display: true
}
本文标签: javascriptChartjs not showing legendsStack Overflow
版权声明:本文标题:javascript - Chart.js not showing legends - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738628633a2103612.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论