1

I'm using jqPlot and since I can't find one decent place to find out how to send multiple series to jqplot through JSON, I will try to work around it.

so here's a little background:

Right now, I can call my servlet and return a JSON array with the data that I'm going to display in the chart.

AJAX CALL

$.ajax({
            type:   'POST',
            cache:  'false',
            data:   params,             
            url:    '/miloWeb/PlotChartServlet',
            async:  false,
            dataType: 'json',
            success: function(series){                  
                coordinates =  [series] ;
            },
            error: function (xhr, ajaxOptions, thrownError){
                alert(ajaxOptions);
            }   
        });

SERVLET

    private void generateCoordinates(HttpServletRequest request, HttpServletResponse response) throws IOException{

    JSONArray coordinates = new JSONArray();
    try {
        coordinates = findChartCoordinatesByPatientPK();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    response.getOutputStream().print(coordinates.toString());

}

What this does is return the string:

[["07/06/2000","22.0"],["08/06/2000","20.0"],["08/06/2003","15.0"],["08/06/2005","35.0"],["08/06/2007","12.0"],["08/06/2010","10.0"],["08/06/2012","10.0"]]

So I store that in the variable 'coordinates' and use those to plot the jqPlot graph using:

var plot10 = $.jqplot ('chartdiv', coordinates);

Up To this point everything works great, now comes what I'm trying to achieve:

If I hardcode a String that symbolizes the two arrays inside another array, like so:

[[["07/06/2000","22.0"],["08/06/2000","20.0"],["08/06/2003","15.0"],["08/06/2005","35.0"],["08/06/2007","12.0"],["08/06/2010","10.0"],["08/06/2012","10.0"]], [["07/06/2000","21.0"],["08/06/2000","19.0"],["08/06/2003","14.0"],["08/06/2005","34.0"],["08/06/2007","11.0"],["08/06/2010","9.0"],["08/06/2012","9.0"]]]

I can get the jQplot to plot the two distinct lines in the chart! So I tried doing the same and returning a String exactly like that one through the servlet:

NOT WORKING SERVLET

    private void generateCoordinates(HttpServletRequest request, HttpServletResponse response) throws IOException{
        JSONArray coordinates = new JSONArray();
        JSONArray coordinates2 = new JSONArray();
        try {
            coordinates = VitalsBB.findChartCoordinatesByPatientPK();
            coordinates2 = VitalsBB.findChartCoordinatesByPatientPK2();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        response.getOutputStream().print( coordinates.toString() + ", " + coordinates2.toString());

    }

But that doesn't work, it gives me a parse Error. So do I need to modify the AJAX call? or is there a way to give back two JSON arrays.toString() to my form and store them in a variable? Or maybe I need two variables?

3
  • 1
    Did you try wrapping the output with '[' and ']'? Notice that you need this for it to be a JSON array. Commented Jul 11, 2012 at 20:57
  • that was IT, hourse spent on something soooooooo basic. THANK YOU Commented Jul 11, 2012 at 21:09
  • If it works before, and you change something, always check where you just made the change in logic. It is likely the culprit code :) Commented Jul 11, 2012 at 21:13

1 Answer 1

7

You're not enclosing the two sub-arrays in outer array brackets when you call response.getOutputStream().print().

Try this instead:

response.getOutputStream().print("[" + coordinates.toString() + ", " + coordinates2.toString() + "]");

If your code worked when you hardcoded the array, then this should work.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks pb2q, that was it, I am so grateful!

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.