我在从 $(document).ready
调用的函数中包含此代码。问题是在我的网站中您可以看到轴,但没有显示条形。我不知道问题出在哪里:
var chart = new CanvasJS.Chart("chartContainer", {
axisY: {
title: "percent",
gridThickness: 0
},
legend: {
visible: false,
},
toolTip: {
enabled: false,
},
animationEnabled: true,
data: [],
});
var data = [];
percentGraph.forEach(function(el, i) {
var objeto1 = {
type: "stackedBar100",
showInLegend: false,
name: el.code,
dataPoints: []
}
el.percents.forEach(function(el2, j) {
if (i === j) {
var objeto2 = {
y: el2.percent,
label: el2.name,
color: colores[el2.code]
}
} else {
var objeto2 = {
y: el2.percent,
label: el2.name,
color: 'white'
}
}
objeto1.dataPoints.push(objeto2);
});
data.push(objeto1);
});
chart.options.data = data;
chart.render();
使用 console.log(chart.options.data)
我得到以下结果:
dataPoints: Array[4]
0: Object
color: "white"
label: "Direccion "
x: 0
y: "10"
__proto__: Object
1: Object
color: "white"
label: "Talento"
x: 1
y: "60"
__proto__: Object
2: Object
color: "white"
label: "Ingenieria"
x: 2
y: "15"
__proto__: Object
3: Object
color: "rgb(45,204,205)"
label: "Ingenieria"
x: 3
y: "15"
__proto__: Object
length: 4
__proto__: Array[0]
name: "04"
showInLegend: false
type: "stackedBar100"
这是每个 chart.options.data
对象的数据结构
最佳答案
抱歉,我发现问题是 el2.percent 是一个字符串,而 canvasjs 需要一个数字。
关于javascript - Canvas 绘图问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42947509/