gpt4 book ai didi

javascript - 使用外部 JSON 绘制 chart.js

转载 作者:行者123 更新时间:2023-11-30 17:05:21 26 4
gpt4 key购买 nike

我有这个问题,我似乎无法独自解决。我无法使用带有外部 JSON 文件的 chart.js 绘制图表。当我使用 console.log(data) 时,它在数组中正确显示数据,但 Canvas 上没有显示任何内容。我已经设法使用以下代码绘制了一个包含静态 JSON 数据的图表:

var myData2 = [

{
"name": "1. The Wolf of Wall Street",
"gross": 116.901
},
{
"name": "2. Frozen",
"gross": 400.738
},
{
"name": "3. RoboCop (1987)",
"gross": 58.607
},
{
"name": "4. Gravity",
"gross": 274.092
},
{
"name": "5. The Hobbit: The Desolation of Smaug",
"gross": 258.366
},
{
"name": "6. Thor: The Dark World",
"gross": 206.362
},
{
"name": "7. Captain America: The Winter Soldier",
"gross": 259.766
},
{
"name": "8. The Legend of Hercules",
"gross": 18.848
},
{
"name": "9. X-Men: Days of Future Past",
"gross": 233.921
},
{
"name": "10. 12 Years a Slave",
"gross": 56.671
}
]
Array.prototype.mapProperty = function(property) {
return this.map(function (obj) {
return obj[property];
});

};
console.log(myData);


// Example: myData.mapProperty('rank') to get an array of all ranks
lineChartData = {
labels : myData.mapProperty('name'),
datasets : [
{
label: "My First dataset",
fillColor : "rgba(220,220,220,0.2)",
strokeColor : "rgba(220,220,220,1)",
pointColor : "rgba(220,220,220,1)",
pointStrokeColor : "#fff",
pointHighlightFill : "#fff",
pointHighlightStroke : "rgba(220,220,220,1)",
data : myData.mapProperty('downloads')
}
]
};
lineChartData2 = {
labels : myData2.mapProperty('name'),
datasets : [
{
label: "My First dataset",
fillColor : "rgba(220,220,220,0.2)",
strokeColor : "rgba(220,220,220,1)",
pointColor : "rgba(220,220,220,1)",
pointStrokeColor : "#fff",
pointHighlightFill : "#fff",
pointHighlightStroke : "rgba(220,220,220,1)",
data : myData2.mapProperty('gross')
}
]
};


window.onload = function()
{
var ctx = document.getElementById("canvas").getContext("2d");
window.myLine = new Chart(ctx).Line(lineChartData);
var ctx2 = document.getElementById("canvas2").getContext("2d");
window.myLine2 = new Chart(ctx2).Line(lineChartData2);
}

然而,当我尝试使用以下代码(使用外部 JSON)时,它不会绘制图表,但控制台日志显示数组完全相同,没有任何其他错误或问题。

 <script src="Chart.js"></script>
</head>
<body>
<div id="chartCanvas">
<canvas id="canvas"></canvas>
<br>
</div>


<script type="text/javascript">
$.getJSON("js/dataverkeer.json",function(data){
var myData = (data);

Array.prototype.mapProperty = function(property) {
return this.map(function (obj) {
return obj[property];
});

};

console.log(myData);
// Example: myData.mapProperty('rank') to get an array of all ranks
lineChartData = {
labels : myData.mapProperty('jaar'),
datasets : [
{
label: "My First dataset",
fillColor : "rgba(220,220,220,0.2)",
strokeColor : "rgba(220,220,220,1)",
pointColor : "rgba(220,220,220,1)",
pointStrokeColor : "#fff",
pointHighlightFill : "#fff",
pointHighlightStroke : "rgba(220,220,220,1)",
data : myData.mapProperty('verbruikInGB')
}
]
};


window.onload = function()
{
var ctx = document.getElementById("canvas").getContext("2d");
window.myLine = new Chart(ctx).Line(lineChartData);
}




});
</script>

和外部 JSON

  [
{
"jaar": "1999",
"verbruikInGB": 1074.44
},
{
"jaar": "2000",
"verbruikInGB": 1095.89
},
{
"jaar": "2001",
"verbruikInGB": 1117.88
},
{
"jaar": "2002",
"verbruikInGB": 1140.24
},
{
"jaar": "2003",
"verbruikInGB": 1163.02
},
{
"jaar": "2004",
"verbruikInGB": 1186.31
},
{
"jaar": "2005",
"verbruikInGB": 1210
},
{
"jaar": "2006",
"verbruikInGB": 1234
},
{
"jaar": "2007",
"verbruikInGB": 1259
},
{
"jaar": "2008",
"verbruikInGB": 1309.6
},
{
"jaar": "2009",
"verbruikInGB": 1387.5
},
{
"jaar": "2010",
"verbruikInGB": 1387.5
},
{
"jaar": "2011",
"verbruikInGB": 1415.24
}
]

我真的找不到问题,希望这里有人能看到我的错误是什么。提前致谢!

最佳答案

我弄乱了它并让它工作(至少对我来说)

我更改的显着部分是 ajax:

$.ajax({url:"data.json",dataType:"json"})
.fail(function(){alert("Im a failure")})
.done(function(data){
var myData = (data);

我从“window.onload”中取出了结束部分,这不是必需的,因为您已经声明了 Canvas 。

我想我会转储我使用的所有代码

<script src="Chart.js"></script>
<script src="jquery-1.11.2.min.js"></script>
</head>
<body>
<div id="chartCanvas">
<canvas id="canvas"></canvas>
<br>
</div>


<script type="text/javascript">
$.ajax({url:"data.json",dataType:"json"})
.fail(function(){alert("Im a failure")})
.done(function(data){
var myData = (data);
console.log(myData[0].jaar);
Array.prototype.mapProperty = function(property) {
return this.map(function (obj) {
return obj[property];
});

};

// Example: myData.mapProperty('rank') to get an array of all ranks
lineChartData = {
labels : myData.mapProperty('jaar'),
datasets : [
{
label: "My First dataset",
fillColor : "rgba(220,220,220,0.2)",
strokeColor : "rgba(220,220,220,1)",
pointColor : "rgba(220,220,220,1)",
pointStrokeColor : "#fff",
pointHighlightFill : "#fff",
pointHighlightStroke : "rgba(220,220,220,1)",
data : myData.mapProperty('verbruikInGB')
}
]
};

//window.onload = function(){
console.log("cheerio")
var ctx = document.getElementById("canvas").getContext("2d");
window.myLine = new Chart(ctx).Line(lineChartData);
//}

});

关于javascript - 使用外部 JSON 绘制 chart.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28142383/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com