Loading an external JSON into ChartJs(将外部 JSON 加载到 ChartJs)
问题描述
在使用库在 JavaScript 中绘制图表时,我是一个新手/我刚开始尝试使用 Chartjs,但我无法使用 getJson 或其他任何东西来加载我的 json 对象并替换标签和数据.我以前使用过 HighCharts,与此相比它非常简单.另外,我将如何将其放入 Angular 的指令中并显示它.
I am a newbie when it comes to using libraries for drawing charts in JavaScript/ I just started experimenting with Chartjs and I have been unable to how to use getJson or anything else to load my json object and replace the labels and data. I have used HighCharts before and its quite simple compared to this. Also, how would I go about putting this into a directive in Angular and displaying it.
https://jsfiddle.net/0u9Lpttx/1/
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div style="width: 100%; height: 100%;">
<canvas id="myChart" style="width: 100%; height: auto;"></canvas>
</div>
<div id="js-legend" class="chart-legend"></div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.1/Chart.min.js"></script>
<script src="js/chartJsControl.js"></script>
</body>
</html>
data.json
[
{
"timestamp": "Monday",
"original_tweet": "756",
"retweets": "345",
"shared": "34",
"quoted": "14"
},
{
"timestamp": "Tuesday",
"original_tweet": "756",
"retweets": "345",
"shared": "34",
"quoted": "14"
},
{
"timestamp": "Wednesday",
"original_tweet": "756",
"retweets": "345",
"shared": "34",
"quoted": "14"
}
]
chartJsControl.js
var test = [];
$.getJSON("data.json", function (json) {
test.push(json[i].timestamp);
});
var data = {
labels: test,
datasets: [
{
label: "My First dataset",
fillColor: "rgba(220,220,220,0.5)",
strokeColor: "rgba(220,220,220,0.8)",
highlightFill: "rgba(220,220,220,0.75)",
highlightStroke: "rgba(220,220,220,1)",
data: [65, 59, 80, 81, 56, 55, 40]
},
{
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: [28, 48, 40, 19, 86, 27, 90]
}
]
};
var ctx = document.getElementById("myChart").getContext("2d");
ctx.canvas.width = 1000;
ctx.canvas.height = 800;
var myChart = new Chart(ctx).Bar(data);
推荐答案
如果你想使用从 data.json 返回的 JSON,你需要在回调函数中这样做:
If you want to use the returned JSON from data.json you need to do this in the callback function like this:
$.getJSON("data.json", function (json) {
// will generate array with ['Monday', 'Tuesday', 'Wednesday']
var labels = json.map(function(item) {
return item.timestamp;
});
var data = {
labels: labels,
datasets: [
{
label: "My First dataset",
fillColor: "rgba(220,220,220,0.5)",
strokeColor: "rgba(220,220,220,0.8)",
highlightFill: "rgba(220,220,220,0.75)",
highlightStroke: "rgba(220,220,220,1)",
data: [65, 59, 80, 81, 56, 55, 40]
},
{
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: [28, 48, 40, 19, 86, 27, 90]
}
]
};
var ctx = document.getElementById("myChart").getContext("2d");
ctx.canvas.width = 1000;
ctx.canvas.height = 800;
var myChart = new Chart(ctx).Bar(data);
});
如果您将它与 Angular 一起使用,我建议您使用 angular chart.js 版本,请参阅:http://jtblin.github.io/angular-chart.js/
If you are using it with Angular I would recommend using the angular chart.js version, see: http://jtblin.github.io/angular-chart.js/
那么你已经有了一个可以使用的角度指令!
Then you already have an angular directive, which you can use!
这篇关于将外部 JSON 加载到 ChartJs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将外部 JSON 加载到 ChartJs
基础教程推荐
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 直接将值设置为滑块 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01