我正在尝试通过调用ajax api(以json格式响应)来创建amcharts,通过调用post api我在console.log中获取所需的数据但是图表没有显示。请检查代码,并建议我缺少什么。我只使用jquery和HTML,如果api不起作用,那么请向我解释一下东西数据。
我可以通过使用amcharts的数据加载器插件来实现这一目标吗?如果是,那么请通过使用dataLoader解释它将如何实现。请检查我的代码并更新我如何通过json响应首先绘制图表。
这是我的工作代码
$(function () {
$("[id*=btnok]").click(function () {
var obj = {};
obj = $.trim($("[id*=nodays]").val());
console.log(obj)
$.ajax({
type: "POST",
"headers": {
"content-type": "application/json",
"cache-control": "no-cache"
},
url: "http://10.26.32.4/api/rating-service/rate/ridecount/days/",
data: JSON.stringify(obj),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
console.log(r)
AmCharts.makeChart("rides", {
"type": "serial",
"fixedColumnWidth": '10px',
"legend": {
"horizontalGap": 10,
"maxColumns": 1,
"position": "right",
"useGraphSettings": true,
"markerSize": 10
},
"valueAxes": [{
"gridColor": "#FFFFFF",
"gridAlpha": 0.2,
"dashLength": 0
}],
"gridAboveGraphs": true,
"startDuration": 1,
"valueAxes": [{
"stackType": "regular",
"axisAlpha": 0.3,
"gridAlpha": 0,
"minimum": 0,
"maximum": 50,
"gridCount": 1
}],
"graphs": [{
"balloonText": "<b>[[title]]</b><br><span style='font-size:14px'>[[category]]: <b>[[value]]</b></span>",
"fillColors": "#47b012",
"lineColor": "#47b012",
"fillAlphas": 0.8,
"labelText": "[[value]]",
"lineAlpha": 0.3,
"title": "Completed Rides",
"type": "column",
"color": "#000000",
"valueField": "completedTrip",
"fixedColumnWidth": 25
}, {
"balloonText": "<b>[[title]]</b><br><span style='font-size:14px'>[[category]]: <b>[[value]]</b></span>",
"fillColors": "#fff138",
"lineColor": "#fff138",
"fillAlphas": 0.8,
"labelText": "[[value]]",
"lineAlpha": 0.3,
"title": "Not Ended",
"type": "column",
"color": "#000000",
"valueField": "activeTrip",
"fixedColumnWidth": 25
}, {
"balloonText": "<b>[[title]]</b><br><span style='font-size:14px'>[[category]]: <b>[[value]]</b></span>",
"fillColors": "#dd111b",
"lineColor": "#dd111b",
"fillAlphas": 0.8,
"labelText": "[[value]]",
"lineAlpha": 0.3,
"title": "Canceled Rides",
"type": "column",
"color": "#000000",
"valueField": "cancelledTrip",
"fixedColumnWidth": 25
}],
"chartCursor": {
"categoryBalloonEnabled": false,
"cursorAlpha": 0,
"zoomable": false
},
"categoryField": "creationDate",
"categoryAxis": {
"gridPosition": "start",
"gridAlpha": 0,
"tickPosition": "start",
"tickLength": 20,
"labelRotation": 60
}
});
}
});
return false;
});
});
dataLoader仅支持GET请求,因此它不是一个选项。
我唯一看错你的代码是你没有将你的回复分配给图表的dataProvider
。假设您的响应格式正确(对象数组):
$.ajax({
type: "POST",
"headers": {
"content-type": "application/json",
"cache-control": "no-cache"
},
url: "http://10.26.32.4/api/rating-service/rate/ridecount/days/",
data: JSON.stringify(obj),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
console.log(r)
AmCharts.makeChart("rides", {
"type": "serial",
"fixedColumnWidth": '10px',
"dataProvider": r, //if your response is an array of objects, e.g. [{"completedTrip": .., "activeTrip: .., "cancelledTrip": .., "creationDate": ..}, ...]
"legend": {
"horizontalGap": 10,
"maxColumns": 1,
"position": "right",
"useGraphSettings": true,
"markerSize": 10
},
"valueAxes": [{
"gridColor": "#FFFFFF",
"gridAlpha": 0.2,
"dashLength": 0
}],
"gridAboveGraphs": true,
"startDuration": 1,
"valueAxes": [{
"stackType": "regular",
"axisAlpha": 0.3,
"gridAlpha": 0,
"minimum": 0,
"maximum": 50,
"gridCount": 1
}],
"graphs": [{
"balloonText": "<b>[[title]]</b><br><span style='font-size:14px'>[[category]]: <b>[[value]]</b></span>",
"fillColors": "#47b012",
"lineColor": "#47b012",
"fillAlphas": 0.8,
"labelText": "[[value]]",
"lineAlpha": 0.3,
"title": "Completed Rides",
"type": "column",
"color": "#000000",
"valueField": "completedTrip",
"fixedColumnWidth": 25
}, {
"balloonText": "<b>[[title]]</b><br><span style='font-size:14px'>[[category]]: <b>[[value]]</b></span>",
"fillColors": "#fff138",
"lineColor": "#fff138",
"fillAlphas": 0.8,
"labelText": "[[value]]",
"lineAlpha": 0.3,
"title": "Not Ended",
"type": "column",
"color": "#000000",
"valueField": "activeTrip",
"fixedColumnWidth": 25
}, {
"balloonText": "<b>[[title]]</b><br><span style='font-size:14px'>[[category]]: <b>[[value]]</b></span>",
"fillColors": "#dd111b",
"lineColor": "#dd111b",
"fillAlphas": 0.8,
"labelText": "[[value]]",
"lineAlpha": 0.3,
"title": "Canceled Rides",
"type": "column",
"color": "#000000",
"valueField": "cancelledTrip",
"fixedColumnWidth": 25
}],
"chartCursor": {
"categoryBalloonEnabled": false,
"cursorAlpha": 0,
"zoomable": false
},
"categoryField": "creationDate",
"categoryAxis": {
"gridPosition": "start",
"gridAlpha": 0,
"tickPosition": "start",
"tickLength": 20,
"labelRotation": 60
}
});