【发布时间】:2016-02-18 04:16:42
【问题描述】:
我使用 amcharts(折线图)。使用 ajax 动态设置数据,并以 JSON 格式从 php 代码中获取数据。
问题是字符串没有动态地变成数组。
如果我在 jquery 代码中添加了这个静态字符串
代码:
[{"country":"17 Feb","visits":"4","color":"#EA5759"},{"country":"16 Feb","visits":"1","color":"#EA5759"},{"country":"15 Feb","visits":"3","color":"#EA5759"}];
这很好。
如果我已动态设置此代码。折线图显示未定义。
我用过这个功能
ajax 返回码
[{"country":"2 月 17 日","visits":"4","color":"#EA5759"},{"country":"2 月 16 日","visits":"1", "color":"#EA5759"},{"country":"15 Feb","visits":"3","color":"#EA5759"}]
我刚刚通过这个函数。喜欢
var ajaxString = [{"country":"2 月 17 日","visits":"4","color":"#EA5759"},{"country":"2 月 16 日","visits":" 1","color":"#EA5759"},{"country":"15 Feb","visits":"3","color":"#EA5759"}];
并调用这个函数
lineChart(ajaxString);
函数线图(数据){ 变量图表;
var chartData=data;
// SERIAL CHART
chart = new AmCharts.AmSerialChart();
chart.dataProvider = chartData;
chart.categoryField = "country";
chart.startDuration = 1;
// AXES
// category
var categoryAxis = chart.categoryAxis;
categoryAxis.labelRotation = 45; // this line makes category values to be rotated
categoryAxis.gridPosition = "start";
// value
var valueAxis = new AmCharts.ValueAxis();
valueAxis.dashLength = 5;
// valueAxis.title = "Visitors from country";
valueAxis.axisAlpha = 0;
chart.addValueAxis(valueAxis);
// GRAPH
var graph = new AmCharts.AmGraph();
graph.valueField = "visits";
graph.colorField = "color";
graph.balloonText = "<b>[[category]]: [[value]] Users</b>";
graph.type = "column";
graph.lineAlpha = 0.2;
graph.fillAlphas = 0.9;
chart.addGraph(graph);
// CURSOR
var chartCursor = new AmCharts.ChartCursor();
chartCursor.cursorAlpha = 0;
chartCursor.zoomable = false;
chartCursor.categoryBalloonEnabled = false;
chart.addChartCursor(chartCursor);
chart.creditsPosition = "top-right";
// WRITE
chart.write("chartdiv");
}[![undefined message][1]][1]
但这不起作用。 提前谢谢你
【问题讨论】:
-
你是如何动态设置字符串的?
-
会在加载时从ajax获取数据。那是我从 ajax 得到的,从 php 代码返回
-
你能告诉我们你静态设置字符串的代码吗?
-
静态字符串 var chartData =[{"country":"2 月 17 日","visits":"4","color":"#EA5759"},{"country":"2 月 16 日","visits":"1","color":"#EA5759"},{"country":"2 月 15 日","visits":"3","color":"#EA5759"}];我正在动态使用这个字符串。那字符串来自ajax [{"country":"17 Feb","visits":"4","color":"#EA5759"},{"country":"16 Feb","visits":"1 ","color":"#EA5759"},{"country":"15 Feb","visits":"3","color":"#EA5759"}] var chartData=dataelement;
-
你应该在问题中更新这个,而不是在 cmets 部分