全部,
我正在尝试更改一个Javascript var,我用它来基于单击元素在图表上填充数据,似乎无法让它工作。有人可以帮忙吗?
我有下面的变量,每当我点击一个html元素时,我想在我的图表上使用下面的数组。
var monthActualLWDC = [12,54,65,74,23,47,75,23,57,65,45,33]
元素,
<a id="btn2" onclick="changeTheVariable()" value="monthActualLWDC" class="ui labeled button"></a>
功能,
function changeTheVariable() {
var chartdata = document.getElementById("btn2").value;
}
因此,当将"chartdata"
插入图表时,它应显示"monthActualLWDC"
new Chart(document.getElementById("mixed-chart"), {
type: 'bar',
data: {
labels: ["Oct", "Nov", "Dec", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "YTD", "P.FY"],
datasets: [{
label: "Month Actual",
type: "line",
borderColor: "#002db3",
data: chartdata,
fill: false
}, {
label: "Prev. Y. Month Actual",
type: "line",
borderColor: "#000d33",
data: monthActualPYLWD,
fill: false
}, {
label: "Goal",
type: "line",
borderColor: "#e60000",
data: goalLWD,
fill: false
}, {
label: "YTD & P.Fiscal Year",
type: "bar",
backgroundColor: "rgba(0,0,0,0.5)",
data: ytdpfyLWD,
},
]
},
options: {
title: {
display: true,
text: 'CDC - Champaign Distribution Center - KPI'
},
legend: { display: true }
}
});
我不知道我做错了什么......需要帮助。
答案 0 :(得分:0)
输入的值只是一个字符串,它不会自动作为变量进行评估。如果要使用字符串名称访问数据,则应将其放在属性名称为字符串的对象中。
var myData = {
monthActualLWDC = [12,54,65,74,23,47,75,23,57,65,45,33]
}
然后您可以将该值用作对象的索引:
function changeTheVariable() {
var chartdata = mYData[document.getElementById("btn2").value];
// code here that uses chartdata to display the chart
}