JSON data from web method for Donut chart

73 views
Skip to first unread message

Md Rehan Moazzam

unread,
Mar 23, 2016, 6:22:38 AM3/23/16
to Google Visualization API
Hi,

I am trying to provide a json string to googlepie chart but every time its saying that json format is invalid.

Please follow the code.

//ASP code

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 Method

[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;
}

//Error Message:

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}]}]


Please help!!

Sergey Grabkovsky

unread,
Mar 23, 2016, 9:35:54 AM3/23/16
to Google Visualization API
Hi,

A JSON string must have its properties double quoted, not single quoted. Let me know if you still experience issues after fixing this.

--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-visualizati...@googlegroups.com.
To post to this group, send email to google-visua...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-visualization-api.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-visualization-api/b5bcf61f-c75e-47a9-a906-bcc3a69d0417%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

unnamed.gif

Sergey Grabkovsky

Software Engineer

Google, Inc

gra...@google.com


Reply all
Reply to author
Forward
0 new messages