gpt4 book ai didi

javascript - Zing Feed 从 rest api 获取数据

转载 作者:搜寻专家 更新时间:2023-11-01 05:10:59 26 4
gpt4 key购买 nike

我正在从 rest api 获取 json 数据,我想将其用作 ZingFeed 的输入。

<!DOCTYPE html>
<html>

<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script src='http://cdn.zingchart.com/zingchart.min.js'></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</head>

<body>
<script>
function getNewData()
{
$.ajax({
type: "GET",
dataType: "json",
headers: {
Accept:"application/json",
"Access-Control-Allow-Origin": "*"
},
url: "/PerformanceMonitor/showProcessUsage/chrome",
success: function(data){
var mem = data.mem.size/10000;
return mem/10000;
//$("#processInfo").append(data.mem.size);
//$("#processInfo").append(" ")

}
});
//return parseInt(memSize);
}

var chartData = {
"type":"line",
"refresh": {
"type": "feed",
"transport": "js",
"url": "feed()",
"interval": 200
},
"series":[
{
"values":[]
}
]
};


window.onload = function() {
zingchart.render({
id: "chartDiv",
data: chartData,
height: 600,
width: "100%"
});
};

window.feed = function(callback) {
var tick = {};
// tick.plot0 = parseInt(10 + 900 * Math.random(), 10);
tick.plot0 = parseInt(getNewData());
//tick.plot0 = parseInt(1);
callback(JSON.stringify(tick));
};
</script>
<div id="processInfo"></div>
<div id='chartDiv'></div>

</body>

</html>

在 firebug 中看到它工作正常。数据(即本例中的 mem 真的很大,所以我在将它分配给 tick.plot0 之前将它划分了两次)。分配给 tick.plot0 后 .. 在开发人员工具中悬停时显示 Nan。你能帮我在 ZingFeed 图表中绘制出这些巨大的值(value)吗

提前致谢

最佳答案

这里的问题是 Javascript 中异步函数的性质。从 AJAX 返回数据并不像您在上面尝试的那样工作。 You can read more about it here.

这是一个可行的解决方案。

我在 ZingChart 团队工作。如果您对 ZingChart 库有其他问题,请告诉我。

<!DOCTYPE html>
<html>

<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script src='http://cdn.zingchart.com/zingchart.min.js'></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</head>

<body>
<script>
var chartData = {
"type":"line",
"refresh": {
"type": "feed",
"transport": "js",
"url": "feed()",
"interval": 200
},
"series":[
{
"values":[]
}
]
};

window.onload = function() {
zingchart.render({
id: "chartDiv",
data: chartData,
height: 600,
width: "100%"
});
};

window.feed = function(callback) {
$.ajax({
type: "GET",
dataType: "json",
headers: {
Accept: "application/json",
"Access-Control-Allow-Origin": "*"
},
url: "/PerformanceMonitor/showProcessUsage/chrome",
success: function (data) {
var mem = data.mem.size/10000;
var tick = {
plot0: parseInt(mem)
};
callback(JSON.stringify(tick));
}
});
};
</script>
<div id="processInfo"></div>
<div id='chartDiv'></div>

</body>

关于javascript - Zing Feed 从 rest api 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29894115/

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