简体   繁体   中英

database, servlet, json , javascript and highchart pie chart

i need to produce a dynamic 'highchart' pie chart using 'json' data from a servlet, the database method is running correctly but the json part in not passing any data to the pie chart.

'Below is my servlet code'

public class SubCountyAjaxApplications extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    PrintWriter out = response.getWriter();

    ArrayList<Applications> Rawpplications = AdminDB.sub_countyApplications();

    response.setContentType("text/html");
    response.setHeader("Cache-control", "no-cache, no-store");
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Expires", "-1");

    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "POST");
    response.setHeader("Access-Control-Allow-Headers", "Content-Type");
    response.setHeader("Access-Control-Max-Age", "86400");

    Gson gson = new Gson();
    JsonArray results = new JsonArray();

    for (Applications application : Rawpplications) {
        JsonObject myObj = new JsonObject();
        myObj.addProperty("key",application.getSub_county());
        System.out.println(application.getSub_county()+":"+application.getAmount_applied());
        myObj.addProperty("value",application.getAmount_applied());
        results.add(myObj);
    }
    out.println(results.getAsJsonArray());
    out.close();

}

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    doPost(request, response);
}

//Below is my javascript file, the title is appearing but no chart

        var chart;
        $(document).ready(
                function () {
                    chart = new Highcharts.Chart({
                        chart: {
                            renderTo: 'chart',
                        },
                        title: {
                            text: 'County Application Distribution'
                        },
                        tooltip: {
                            formatter: function () {
                                var s;
                                if (this.point.name) { // the pie chart
                                    s = '' + this.point.name + ': ' + this.y
                                            + ' Bugs';
                                } else {
                                    s = '' + this.x + ': ' + this.y;
                                }
                                return s;
                            }
                        },
                        labels: {
                            items: [{
                                    html: '',
                                    style: {
                                        left: '40px',
                                        top: '8px',
                                        color: 'black'
                                    }
                                }]
                        },
                        series: [{
                                type: 'pie',
                                name: 'Total Bugs',
                                data: [],
                                center: [70, 50],
                                size: 50,
                                showInLegend: false,
                                dataLabels: {
                                    enabled: false
                                },
                            }, ]

                    },function getdata(chart) {
                        var tmp = "";
                        var receivedData = "";

                        $.ajax({
                            url: 'SubCountyAjaxApplications',
                            dataType: 'json',
                            error: function () {
                                alert("error occured!!!");
                            },
                            success: function (data) {
                                var chartData = [];
                                $.each(data.jsonArray, function (index)
                                {
                                    $.each(data.jsonArray[index],
                                            function (key, value) {
                                                var point = [];
                                                point.push(key);
                                                point.push(value);
                                                chartData.push(point);
                                            });
                                });
                                chart.series[0].setData(chartData);
                            }
                        });
                    });

                });

where have i made a mistake

Assuming the data returned by your ajax call is in the format you specified above:

[{"key":"CHANGAMWE","label":"20,000"},{"key":"JOMVU","label":"55,000"},{"key":"Kathiani","label":"42,000"},{"key":"KISAUNI","label":"60,000"},{"key":"LIKONI","label":"20,000"},{"key":"Machakos Town","label":"15,000"},{"key":"MVITA","label":"15,000"},{"key":"NYALI","label":"35,000"}]

You can alter your parsing loop like this to return an appropriate data array for Highcharts:

var chartData = [];
$.each(data, function (i, node) {
    chartData.push( [ node.key, parseFloat(node.label) ] );
});

Example:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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