简体   繁体   English

使用jQuery对Highcharts迭代JSON响应

[英]iterate JSON response with jQuery for Highcharts

I made a servlet which creates a Map Object: 我制作了一个Servlet,它创建了一个Map对象:

Map<String, Integer> data = new LinkedHashMap<String, Integer>();

fills in data and returns a response in JSON format using google JSON: 填写数据并使用Google JSON返回JSON格式的响应:

String json = new Gson().toJson(data);

All this works fine when retrieving data and iterating them into a table. 当检索数据并将其迭代到表中时,所有这些工作正常。 But I need it in special format for the Highcharts plugin: 但是我需要特殊格式的Highcharts插件:

series: [{
    name: 'Monday',
     data: [10]
    }, {
     name: 'Tuesday',
     data: [20]
  }, {
     name: 'Wednesday',
     data: [30]
    }, {
     name: 'Thursday',
     data: [40]          
    }, {
     name: 'Friday',
     data: [50]
    }, {
     name: 'Saturday',
     data: [60]            
    }, {
     name: 'Sunday',
     data: [70]             
}]

In order to achieve this you have to create the script as shown below: 为了实现这一点,您必须创建如下所示的脚本:

$(document).ready(function() {
    var options = {
        chart: {
            renderTo: 'container',
            defaultSeriesType: 'column',
            rightMargin: 80
        },

        title: {
            text: 'Weekdays'
        },
        subtitle: {
            text: 'Source: somewhere in a calendar'
        },

         xAxis: {
            labels: {
                enabled: false
        }
        },

        yAxis: [
            {
                min: 0,
                title: {
                    text: 'Amount'
                 }
            },
            {
                linkedTo: 0,
                opposite: true
            }
        ],

         series: []
    };

    $.getJSON('WeekdayServlet', function(data) {

        var series = [];

        $.each(data, function(key, value) {

            series.name = key;
            series.data = value;

        options.series.push(data);
    });

    // Create the chart
    var chart = new Highcharts.Chart(options);

 }); 

Anyhow, I am doing something wrong here. 无论如何,我在这里做错了。 Either in the iteration or how I "initialize" series . 要么在迭代中,要么我如何“初始化” 系列

Here are my sources for better understanding: 以下是我的资料,供您更好地理解:

The [] should map to a Java collection such as List or an array. []应该映射到Java集合,例如List或数组。 The {} should map to a Map or some Javabean. {}应该映射到Map或某些Javabean。

So, the desired JSON format can be translated/achieved as follows: 因此,所需的JSON格式可以按以下方式转换/实现:

public class Serie {
    private String name;
    private Integer[] data;

    public Serie() {
        // Keep default c'tor alive.
    }

    public Serie(String name, Integer... data) {
        this.name = name;
        this.data = data;
    }

    // Add/generate getters/setters/etc.
}

and

List<Serie> series = new ArrayList<Serie>();
series.add(new Serie("Monday", 10));
series.add(new Serie("Tuesday", 20));
series.add(new Serie("Wednesday", 30));
// ...
Map<String, Object> data = new HashMap<String, Object>();
data.put("series", series);
// ...
String json = new Gson().toJson(data);
// ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM