我正在使用图表库,需要输入看起来像某种格式。这种格式看起来像JSON,但它不是,在这里我分享一些数据来了解格式:
[{
year: 2005,
income: 23.5,
expenses: 18.1
}, {
year: 2006,
income: 26.2,
expenses: 22.8
}, {
year: 2007,
income: 30.1,
expenses: 23.9
}, {
year: 2008,
income: 29.5,
expenses: 25.1
}, {
year: 2009,
income: 24.6,
expenses: 25.0
}];
您可以在this link中看到完整的示例。
在我的第一次测试中,我使用了String.concat(anotherString)
,但我向你保证它不舒服。那么有没有提供这种格式的java API?
提前谢谢。
答案 0 :(得分:1)
嗯,该格式和JSON之间的唯一区别似乎只需要从字符串中删除双引号。所以data.replaceAll(“\”“,”“);应该完成这项工作。
但实际上,阅读该示例,它将接受有效的JSON。你应该尝试使用$ .ajax()方法,看起来像:
AmCharts.ready(function () {$.ajax({
url: '/mydata/',
dataType: 'json',
data: undefined,
success: function(chartData) {
// SERIAL CHART
chart = new AmCharts.AmSerialChart();
chart.dataProvider = chartData;
chart.categoryField = "year";
// AXES
// category
var categoryAxis = chart.categoryAxis;
categoryAxis.gridAlpha = 0.1;
categoryAxis.axisAlpha = 0;
categoryAxis.gridPosition = "start";
// value
var valueAxis = new AmCharts.ValueAxis();
valueAxis.stackType = "regular";
valueAxis.gridAlpha = 0.1;
valueAxis.axisAlpha = 0;
chart.addValueAxis(valueAxis);
// GRAPHS
// first graph
var graph = new AmCharts.AmGraph();
graph.title = "Europe";
graph.labelText = "[[value]]";
graph.valueField = "europe";
graph.type = "column";
graph.lineAlpha = 0;
graph.fillAlphas = 1;
graph.lineColor = "#C72C95";
chart.addGraph(graph);
// second graph
graph = new AmCharts.AmGraph();
graph.title = "North America";
graph.labelText = "[[value]]";
graph.valueField = "namerica";
graph.type = "column";
graph.lineAlpha = 0;
graph.fillAlphas = 1;
graph.lineColor = "#D8E0BD";
chart.addGraph(graph);
// third graph
graph = new AmCharts.AmGraph();
graph.title = "Asia-Pacific";
graph.labelText = "[[value]]";
graph.valueField = "asia";
graph.type = "column";
graph.lineAlpha = 0;
graph.fillAlphas = 1;
graph.lineColor = "#B3DBD4";
chart.addGraph(graph);
// LEGEND
var legend = new AmCharts.AmLegend();
chart.addLegend(legend);
// WRITE
chart.write("chartdiv");
});
}
});
(对不起,我远没有善于写javascript,但我认为这个想法是存在的)。此外,也许amCharts的API可以直接检查本地URL。然后,您只需将JSON数据路由为REST调用。
答案 1 :(得分:0)
查看此API的数据相关博客页面
http://blog.amcharts.com/2011/03/amcharts-javascript-tutorials-part-2.html
我认为本教程讲述了处理此API中数据的所有细节,包含大量示例和代码片段
答案 2 :(得分:0)
解决方案很简单:
var chartData = eval(json_data);
最好的问候