Javascript 条件序列失败-Chart.js
我有一个基本的多系列折线图,我一直在努力。我想完成类似于下面代码的工作 这些在我的Javascript 条件序列失败-Chart.js,javascript,chart.js,Javascript,Chart.js,我有一个基本的多系列折线图,我一直在努力。我想完成类似于下面代码的工作 这些在我的数据集中[]: {label: 'Branch '+b7, data: B7, fill: false, backgroundColor: 'rgb(0, 0, 0)', borderColor: 'rgb(0, 0, 0)', }, if(b8){
数据集中[]
:
{label: 'Branch '+b7,
data: B7,
fill: false,
backgroundColor: 'rgb(0, 0, 0)',
borderColor: 'rgb(0, 0, 0)',
},
if(b8){
{label: 'Branch '+b8,
data: B8,
fill: false,
backgroundColor: 'rgb(0, 0, 0)',
borderColor: 'rgb(0, 0, 0)',
},
}
if(b9){
{label: 'Branch '+b9,
data: B9,
fill: false,
backgroundColor: 'rgb(0, 0, 0)',
borderColor: 'rgb(0, 0, 0)',
},
}
正如您可能看到的,我只希望在其b
变量存在时创建某些序列。由于数据集
括号的原因,此操作目前不起作用
我还没有找到一个解决方案,将所有可能的b
变量设置为零。这对我来说是不可接受的
编辑1
我想我会尝试创建多个数据集[]
,如下所示。但是现在我无法让条件语句工作。对于b0
它告诉我意想不到的typeof
if(typeof b0 !== 'undefined'){
datasets: [
{label: 'Branch '+b0,
data: B0,
fill: false,
backgroundColor: 'rgb(204, 0, 0)',
borderColor: 'rgb(204, 0, 0)',
}]
},
if(b1){
datasets: [
{label: 'Branch '+b1,
data: B1,
fill: false,
backgroundColor: 'rgb(82, 235, 52)',
borderColor: 'rgb(82, 235, 52)',
}]
},
编辑2
我尝试了许多方法来执行
if
语句。每一次尝试都尚未奏效。我尝试了if(typeof b!=='undefined)
以及将x
设置为true
或false
的type of b
结果,并尝试if(x==true) < p>您所尝试的不是有效的JavaScript语法,因为您不能在对象声明的中间放置<代码>如果语句。尝试类似以下内容:
var datasets = [
{
label: 'Branch '+b7,
data: B7,
fill: false,
backgroundColor: 'rgb(0, 0, 0)',
borderColor: 'rgb(0, 0, 0)'
}
];
if(b8) {
datasets.push({
label: 'Branch '+b8,
data: B8,
fill: false,
backgroundColor: 'rgb(0, 0, 0)',
borderColor: 'rgb(0, 0, 0)',
});
}
if(b9){
datasets.push({
label: 'Branch '+b9,
data: B9,
fill: false,
backgroundColor: 'rgb(0, 0, 0)',
borderColor: 'rgb(0, 0, 0)',
});
}
new Chart(document.getElementById('chart'),{
type: 'line',
data: {
labels: time,
datasets: datasets
}
});
这个解决方案非常适合我,谢谢你,本!