【发布时间】:2016-05-18 07:47:41
【问题描述】:
我发现许多基于 jquery 和 javascript 的插件可以从表格中生成图表,但所有表格值都是硬编码的。但是我的表值是来自用户的输入,我需要动态生成图表。知道我该怎么做吗?
目前我正在使用 canvasjs。
<!DOCTYPE HTML>
<html>
<head>
<title>My Chart</title>
<script src="https://code.jquery.com/jquery-1.11.1.min.js" integrity="sha256-VAvG3sHdS5LqTT+5A/aeq/bZGa/Uj04xKxY8KM/w9EE=" crossorigin="anonymous"></script>
<script type="text/javascript" src="jquery.canvasjs.min.js"></script>
<script type="text/javascript">
window.onload = function hello() {
//Better to construct options first and then pass it as a parameter
var options = {
title: {
text: "My Chart"
},
animationEnabled: true,
data: [
{
type: "column", //change it to line, area, bar, pie, etc
dataPoints: [
{ x:25, y: 10 },
{ x: 20, y: 11 },
{ x: 30, y: 14 },
{ x: 40, y: 16 },
{ x: 50, y: 19 },
{ x: 60, y: 15 },
{ x: 70, y: 12 },
{ x: 80, y: 10 }
]
}
]
};
$("#chartContainer").CanvasJSChart(options);
}
</script>
</head>
<body>
<div id="chartContainer" style="height: 300px; width: 100%;"></div>
</body>
</html>
然后我尝试使用document.getElementById() 从输入中获取变量值并将其分配给hello() 函数中的变量。但它给出了错误。
帮助!! :-)
【问题讨论】:
标签: javascript jquery html