我想动态设置chart.js的数据集值来创建一个条形图,我试图用以下代码
getmths();
mthlen=marr.length;
xdistid=$('#mdistid').val();
mdistid=xdistid.split(',');
mdilen=mdistid.length;
var distdata=[];
for(i=0;i<mdilen;i++)
{
glc=mdistid[i];
for(j=0;j<mthlen;j++)
{
mth=marr[j];
switch(mth)
{
case '1':
distdata[j]=$('#qty1'+glc).val();
break;
case '2':
distdata[j]=$('#qty2'+glc).val();
break;
case '3':
distdata[j]=$('#qty3'+glc).val();
break;
case '4':
distdata[j]=$('#qty4'+glc).val();
break;
case '5':
distdata[j]=$('#qty5'+glc).val();
break;
case '6':
distdata[j]=$('#qty6'+glc).val();
break;
case '7':
distdata[j]=$('#qty7'+glc).val();
break;
case '8':
distdata[j]=$('#qty8'+glc).val();
break;
case '9':
distdata[j]=$('#qty9'+glc).val();
break;
case '10':
distdata[j]=$('#qty10'+glc).val();
break;
case '11':
distdata[j]=$('#qty11'+glc).val();
break;
case '12':
distdata[j]=$('#qty12'+glc).val();
break;
}
}
//Month Loop Ends here;
alert("Distributor data:"+distdata);
if(i==0)
{
datasetValue[0]=
{
label: "My Second dataset",
fillColor: "rgba(151,187,205,0.5)",
strokeColor: "rgba(151,187,205,0.8)",
highlightFill: "rgba(151,187,205,0.75)",
highlightStroke: "rgba(151,187,205,1)",
data: distdata
};
}
if(i==1)
{
datasetValue[1]=
{
label: "My Second dataset",
fillColor: "rgba(151,187,205,0.5)",
strokeColor: "rgba(151,187,205,0.8)",
highlightFill: "rgba(151,187,205,0.75)",
highlightStroke: "rgba(151,187,205,1)",
data: distdata
};
}
}
//Distributor Loop Ends here;
alert("Datasets: "+datasetValue[0].data);
alert(datasetValue.length);
mkchart();
mklinechart();
我的问题是,在循环完成后,当我检查datasetValue [0] .data的值时,我得到的值与datatValue [1]相同。数据请帮我解决这个问题.... .....提前致谢
答案 0 :(得分:0)
我的问题是循环结束后我正在检查 datasetValue [0] .data的值得到的值与sholud相同 是datasetValue [1] .data
您的问题是您将两个图表数据集的data
设置为同一个数组对象(distdata
)。更新数组时,两个数据集的data
对象都会发生变化(因为它们指向同一个对象的2个变量)
当您将distdata
分配给数据集的data
属性时,可以通过创建slice()
的副本来轻松解决此问题。最简单的方法是使用.slice()
(有关更多方法,请参阅Javascript fastest way to duplicate an Array - slice vs for loop)。
简而言之,将数据集分配更改为(请注意额外的...
if (i == 0) {
datasetValue[0] =
{
label: "My Second dataset",
fillColor: "rgba(151,187,205,0.5)",
strokeColor: "rgba(151,187,205,0.8)",
highlightFill: "rgba(151,187,205,0.75)",
highlightStroke: "rgba(151,187,205,1)",
data: distdata.slice()
};
}
if (i == 1) {
datasetValue[1] =
{
label: "My Second dataset",
fillColor: "rgba(151,187,205,0.5)",
strokeColor: "rgba(151,187,205,0.8)",
highlightFill: "rgba(151,187,205,0.75)",
highlightStroke: "rgba(151,187,205,1)",
data: distdata.slice()
};
}
...
)
SELECT DISTINCT c1.PageId
FROM Categories AS c1
INNER JOIN Rules AS r ON c1.ItemNumber = r.WhenViewingCategoryNumber
INNER JOIN Categories AS c2 ON c1.PageId = c2.PageId
AND r.HideEverythingWithCategoryNumber = c2.ItemNumber