Fel*_*hes 7 javascript chart.js
Chart.js 不显示图例。我使用的是 2.4.0 版本,并且我已将该Chart.bundle.min.js脚本包含在我的网站中。
所有变量如lineaRoja、puntos、MuPu均已定义并且数据正确显示。
问题是Falla balanceada、Mu y Pu aplicados和Diagrama M-P各自颜色的图例没有显示。当我将鼠标悬停在点上时,仅显示工具提示。
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
}
}]
}
}
});
Run Code Online (Sandbox Code Playgroud)
接下来将图例配置添加到选项图例 - Chart.js 文档中:
const chart = new Chart(ctx, {
type: 'bar',
data: data,
options: {
plugins: {
legend: {
display: true,
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
使用 Webpack 或 Rollup Integration 等构建工具时,必须导入并注册模块- Chart.js 文档:
Chart.register(
Legend,
)
Run Code Online (Sandbox Code Playgroud)
或者使用自动注册方法之一:
import { Chart, registerables } from 'chart.js';
Chart.register(...registerables);
Run Code Online (Sandbox Code Playgroud)
或者:
import Chart from 'chart.js/auto';
Run Code Online (Sandbox Code Playgroud)
已解决,我使用的 Bootstrap 模板在其 .js 中的某个位置有此代码
Chart.defaults.global.legend = {
enabled: false
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13659 次 |
| 最近记录: |