我从来没有进入过 Node ,所以我很确定我在这里做错了什么,因为我通过谷歌搜索根本找不到任何信息。
我有一个 django 网站,我想要一个 JS 图表库,我选择了 chart.js。
我安装并喜欢文档,但之后我不确定该怎么做,所以我尝试填写空白并尽可能遵循他们的指南。这是我的 html 的样子....
<script src="/public/node_modules/chart.js/dist/Chart.js"></script>
<canvas id="myChart" width="400" height="400"></canvas>
<script>
var ctx = document.getElementById("myChart");
console.log(ctx);
var options = {}
var data = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [
{
label: "My First dataset",
fill: false,
lineTension: 0.1,
backgroundColor: "rgba(75,192,192,0.4)",
borderColor: "rgba(75,192,192,1)",
borderCapStyle: 'butt',
borderDash: [],
borderDashOffset: 0.0,
borderJoinStyle: 'miter',
pointBorderColor: "rgba(75,192,192,1)",
pointBackgroundColor: "#fff",
pointBorderWidth: 1,
pointHoverRadius: 5,
pointHoverBackgroundColor: "rgba(75,192,192,1)",
pointHoverBorderColor: "rgba(220,220,220,1)",
pointHoverBorderWidth: 2,
pointRadius: 1,
pointHitRadius: 10,
data: [65, 59, 80, 81, 56, 55, 40],
spanGaps: false,
}
]
};
var myChart = new Chart({
type: 'line',
data: data,
options: options
})
</script>
我只是想让一个例子工作。我将通过 npm 下载的 node_modules 目录放在我的服务器将为它们提供服务的某个地方......并验证它们正在被提供服务,但是当我尝试构建图表时出现此错误。我从他们的文档中获取了所有图表代码,所以我很确定那部分是正确的,但我不明白为什么会出现这个错误。
最佳答案
出现同样错误的另一个原因是,如果 id 引用的元素不是 <canvas>
.我有一个 <div>
我的 HTML 源代码中的元素,当然它不起作用。
关于javascript - chart.js 创建图表失败 : can't acquire context from the given item,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41280857/