我对jquery有点新鲜。
我在jquery中有一个图表,图表静态获取数据。
我想动态
这是静态数据。
var data = [
{ label: "New Request", data: 1.7, color: "#68BC31"},
{ label: "On-Going", data: 24.5, color: "#2091CF"},
{ label: "Deadlines", data: 8.2, color: "red"},
]
我有一个返回此值的ajax。
{"标签":"有效","数据":" 4",颜色:"#68BC31&#34 ;}
这是我的ajax。
jQuery.ajax({
type: "POST",
url: "dashboard/graph_data/",
success:function(response){
return response;
// the response returns this value
// {"label":"active","data":"4", color: "#68BC31"}
}
});
如何将静态数据替换或转换为我的ajax函数。
提前致谢
答案 0 :(得分:1)
var data = [
{ label: "New Request", data: 1.7, color: "#68BC31"},
{ label: "On-Going", data: 24.5, color: "#2091CF"},
{ label: "Deadlines", data: 8.2, color: "red"},
];
jQuery.ajax({
type: "POST", // Are you sure you want POST and not GET ?
url: "dashboard/graph_data/",
dataType: "json", // If you know the return value type, explicitely type it
success: function(response){
data.push(response); // Maybe you'll need to JSON.parse() the response, but not sure
console.log(data); // Your data array has been updated asynchronously
}
});
答案 1 :(得分:0)
您必须重新填充图表的数据源,然后重新绑定它。例如
案例1:添加新数据项
jQuery.ajax({
type: "POST",
url: "dashboard/graph_data/",
success:function(response){
data.push(response);
// Rebind function for graph
// the response returns this value
// {"label":"active","data":"4", color: "#68BC31"}
}
});
案例2:新鲜数据源
jQuery.ajax({
type: "POST",
url: "dashboard/graph_data/",
success:function(response){
data.removeAll();
data.push(response);
// Rebind function for graph
// the response returns this value
// {"label":"active","data":"4", color: "#68BC31"}
}
});
确保在响应中获取json,否则在推入数据之前转换为json