0

I'm returning a JSON string with an Ajax call in jQuery, I'd like to pump that data into a bar chart using jqPlot.

I got the JSON conversion code from another Stack-Overflow post, but can't understand why this isn't working. My code:

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify(DTO), //JSON.stringify(AnDParms), combined, 
    url: "GetAdmitsDischarges.asmx/GetAandD",
    dataType: "json",
    success: function (data) {
        //do chart stuff here.
        var line1 = [];
        for (var prop_name in data.d) {
            line1.push([prop_name, data[prop_name]])
        }
        var ticks = ['Admits', 'Discharges'];

        var plot1 = $.jqplot('chartdiv', [line1], {
            title: 'Admits & Discharges',
            series: [{ renderer: $.jqplot.BarRenderer}],
            axesDefaults: {
                tickRenderer: $.jqplot.CanvasAxisTickRenderer
            },
            axes: {
                xaxis: {
                    renderer: $.jqplot.CategoryAxisRenderer
                }
            }
        });
        //to prove the flow is working...
        //alert("Data: " + data.d);

    }, //end of success
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        alert(textStatus + ' ' + errorThrown + ' ' + XMLHttpRequest);
    } //end of error
});   //end of ajax call

In Firebug, the value of line1 is (going from 0 to 32):

[["0", undefined],["1", undefined],...["31", undefined],["32", undefined]]

While the value of data is:

Object { d="{"Admits":"35","Discharges":"36"}" }

Thanks for any help you can offer...

2 Answers 2

1

The problem is your JSON structure:

{
    "Admits": "35",
    "Discharges": "36"
}

You are providing a JSON object, but jqplot needs array instead:

[
  ["Admits", 35],
  ["Discharges", 36]
]
Sign up to request clarification or add additional context in comments.

7 Comments

Any ideas how to change this? is there a search and replace function that I could use on a JSON string, to change the ":" into a "," and the "," into "],[", and the outer "{" into "[["? I'd like this to be able to work on larger JSON strings, as I plan to create more complex charts down stream...this simple one is just a proof of concept...thanks.
you change this in your service, change the way you are encoding your json data
Can we just get jqPlot to accept JSON strings? :) I used Objects in my service, so making this kind of change will be a LARGE undertaking...if I can't do it in the javascript/jquery...
unless you wanna hack their source code, if you dont want to change it on the server level then do it in your js, check this out stackoverflow.com/questions/6693579/json-for-jqplot
Thanks, I will try it in the javascript
|
0

I finally figured it out with the help of Dave Ward of Encosia.com...if you've not checked out his blog, head straight there right now...it's great for all your .Net/jQuery needs.

Here is my javascript:

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(DTO),
        url: "GetAdmitsDischarges.asmx/GetAandD",
        dataType: "json",
        success: function (data) {
            var jqPlotData = $.map(data.d, function (value, key) {
                if (key != "__type") {
                    return [value]; //was key, value
                }
            });
            var ticks = ['Admits', 'Discharges'];
            var plot1 = $.jqplot('chartdiv', [jqPlotData], {
                title: 'Admits & Discharges',
                seriesDefaults: {
                    renderer: $.jqplot.BarRenderer,
                    rendererOptions: { varyBarColor: true },
                    pointLabels: { show: true }
                },
                axes: {
                    xaxis: {
                        renderer: $.jqplot.CategoryAxisRenderer,
                        ticks: ticks
                    }
                },
                highlighter: { show: false }

            });

Also, I removed the JSON serialization from my web service, and just returned the object. Hopefully this will help others.

3 Comments

Why did this get a "-1"? It's the solution.
can you tell me how to use the json returned data in the "series " as well? for example using the admits and discharge returned from the data as the series
something like series: some_array and some_array = [{label:"json returned data},{label: "json returned data"}]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.