0

我正在使用一个图表库,它需要输入看起来像某种格式。这种格式看起来像 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
                }];

您可以在此链接中查看完整示例。在我的第一次测试中,我使用过,String.concat(anotherString)但我向你保证它不舒服。那么有没有提供这种格式的java API?提前致谢。

4

3 回答 3

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 调用进行路由。

于 2012-04-11T22:01:33.577 回答
0

解决方案很简单:

var chartData = eval(json_data);

此致

于 2012-04-25T10:00:15.793 回答
0

chk 此 API 的数据相关博客页面

http://blog.amcharts.com/2011/03/amcharts-javascript-tutorials-part-2.html

我认为本教程讲述了在这个 API 中处理数据的所有细节,其中包含大量示例和代码片段

于 2012-04-11T22:24:44.083 回答