圆环图的Web方法的JSON数据

时间:2016-03-23 10:30:09

标签: json charts

我试图提供一个json字符串到谷歌饼图,但每次它说json格式无效。我已经尝试了很多方法来提供json字符串。

请遵守代码。

// ASP代码

function drawDonutChart(chartType) {
            //alert('Under Donut Chart');

            var objCombo = {};
            objCombo.siteURL = window.location.protocol + "//" + window.location.host + _spPageContextInfo.siteServerRelativeUrl;
            objCombo.chartType = chartType;

            var jsonData = $.ajax({
                type: "POST",
                contentType: 'application/json',
                url: 'Dashboard.aspx/GetDonutChartData',
                data: JSON.stringify(objCombo),
                //beforeSend: function () { alert("before send"); },
                //complete: function () { alert("complete"); },
                success: function (data) {
                    var donutChartData = new google.visualization.DataTable(data.d);
                    var options = {
                        legend: 'right',
                        pieSliceText: 'value',
                        pieHole: 0.4,
                        chartArea: {
                            width: 300, left: 20, top: 20, right: 20, bottom: 10
                        }
                    };

                    if (donutChartData.getNumberOfRows() > 1) {
                        var donutChart = new google.visualization.PieChart(document.getElementById('programmatic_DonutChart'));
                        donutChart.draw(donutChartData, options);
                    }
                    else {
                        document.getElementById('programmatic_DonutChart').innerHTML = "No data to display!";
                    }
                },
                error: function (data) {
                    if ($("meta[name=debug]").attr("content") == "true") {
                        //Full Error when debugging
                        var errDoc = window.open();
                        errDoc.document.write(data.responseText);
                        errDoc.document.close();
                    }
                    else {
                        // generic error message for production use
                        alert("An unexpected error occurred.");
                    } return false;
                }
            });
            return false;
        }

// Web方法

[WebMethod]
        public static string GetDonutChartData(string siteURL, string chartType)
{
string jsonData = "{'cols': [{'id':'','label':'Topping','pattern':'','type':'string'},"+
        "{'id':'','label':'Slices','pattern':'','type':'number'}"+
      "],'rows': [{'c':[{'v':'Mushrooms','f':null},{'v':3,'f':null}]},{'c':[{'v':'Onions','f':null},{'v':1,'f':null}]},{'c':[{'v':'Olives','f':null},{'v':1,'f':null}]},{'c':[{'v':'Zucchini','f':null},{'v':1,'f':null}]},"+
        "{'c':[{'v':'Pepperoni','f':null},{'v':2,'f':null}]}]}";
            //return list;
            return jsonData;
}

//错误讯息:

Uncaught Error: Invalid JSON string: "{'cols': [{'id':'','label':'Topping','pattern':'','type':'string'},{'id':'','label':'Slices','pattern':'','type':'number'}],'rows': [{'c':[{'v':'Mushrooms','f':null},{'v':3,'f':null}]},{'c':[{'v':'Onions','f':null},{'v':1,'f':null}]},{'c':[{'v':'Olives','f':null},{'v':1,'f':null}]},{'c':[{'v':'Zucchini','f':null},{'v':1,'f':null}]},{'c':[{'v':'Pepperoni','f':null},{'v':2,'f':null}]}]}"

请帮助!!

0 个答案:

没有答案