简体   繁体   English

从.NET WebHandler传递JSON数据以使用新系列数据重绘Pie Highcharts

[英]Pass JSON data from a .NET WebHandler to redraw Pie Highcharts with new series data

I want to create a page displaying a default pie chart and a few buttons. 我想创建一个显示默认饼图和一些按钮的页面。 When the user clicks on the buttons, the page will get new JSON data from a WebHandler and redraw the chart. 当用户单击按钮时,页面将从WebHandler获取新的JSON数据并重新绘制图表。 I'm using .net 2.0, C#, web forms. 我正在使用.net 2.0,C#,Web表单。

I have this WebHandler json.ashx generating the JSON: 我有这个WebHandler json.ashx生成JSON:

public void ProcessRequest (HttpContext context) {        
    String data = String.Empty;
    data += "[{\"Male\": 80.0}, {\"Female\": 20.0}]";
    context.Response.ContentType = "application/json";
    context.Response.Write(data);
}

Here is my script on the aspx page: 这是我在aspx页面上的脚本:

    var chart = null;

    $(document).ready(function () {

        var options = {
            chart: {
                renderTo: 'container',
                defaultSeriesType: 'pie'
            },
            tooltip: {
                formatter: function () {
                    return '<b>' + this.point.name + '</b>: ' + this.y + ' (' + this.percentage + '%)';
                }
            },
            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: true,
                        color: '#000000',
                        connectorColor: '#000000',
                        formatter: function () {
                            return '<b>' + this.point.name + '</b>: ' + this.y + ' (' + this.percentage + '%)';
                        }
                    }
                }
            },
            series: [{
                data: [['Yes', 60], ['No', 40]]
            }]

        };

        var chart = new Highcharts.Chart(options);


        $("#Gender").click(function () {
            $.get('json.ashx', function (data) {
                options.series = data;
                var chart = new Highcharts.chart(options);
            });
        });

    });

when I click on the Gender button, it does not generate the new chart. 当我单击“ Gender按钮时,它不会生成新图表。 How do I properly parse the JSON data and feed it to the series.data of the new pie chart? 如何正确解析JSON数据并将其提供给新饼图的series.data

Nvm, I got the problem resolved. Nvm,问题得到解决。 I had to pass in the Json format that highcharts want. 我必须采用highcharts想要的Json格式。

eg: [{"type" : "pie","name" : "Fruits","data" : [["Apple",43.0],["Pear",57.0]]}] 例如:[{“ type”:“ pie”,“ name”:“水果”,“ data”:[[“ Apple”,43.0],[“ Pear”,57.0]]}]]

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

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