0

I am trying to use google chart in Java Servlet code following this link tutorial - https://developers.google.com/chart/interactive/docs/gallery/barchart

I am able to display the exact chart which is shown in the above link in my java servlet but the problem is when I am trying to iterate my hashmap and display that data in the graph, it is not displaying on the java servlet.

    out.println("<script type='text/javascript' src='https://www.gstatic.com/charts/loader.js'></script>");
    out.println("<div id='chart_div'></div>");
    out.println("<script type = 'text/javascript'>");
    out.println("google.charts.load('current', {packages: ['corechart', 'bar']});");
    out.println("google.charts.setOnLoadCallback(drawBasic);");

    out.println("function drawBasic() {");

    out.println("var data = google.visualization.arrayToDataTable([");
    out.println("['Product Name', 'Product Quantity'],");
    for (Map.Entry<String, Integer> entry : prod.entrySet()) 
    {

        out.println("['"+entry.getKey()+"','"+entry.getValue()+"'],");
    }
    out.println(" ]);");


    out.println("var options = {");
    out.println("title: 'Product Quantity',");
    out.println("chartArea: {width: '50%'},");

    out.println(" hAxis: {");
    out.println("title: 'Quantity',");
    out.println("minValue: 0");
    out.println(" },");
    out.println(" vAxis: {");
    out.println(" title: 'Product Name'");
    out.println(" }");
    out.println(" };");

    out.println("var chart = new google.visualization.BarChart(document.getElementById('chart_div'));");

    out.println("chart.draw(data, options);");
    out.println("}");
    out.println("</script>");

I am sure that my HashMap has data and I have printed it to ensure it. Could you please guide where I am getting wrong in while getting the data on the chart using HashMap.

4
  • 1
    Have you looked at the HTML that you're generating? Does it look correct (matched quotes, JSON looks correct, etc.) Are there any errors in the console for the browser tools?
    – stdunbar
    Commented Oct 26, 2017 at 2:14
  • I didn't get you @stdunbar
    – Rohan Jain
    Commented Oct 26, 2017 at 2:33
  • 1
    You're ultimately generating something that runs in the browser, correct? Since you're generating the HTML and JavaScript via code you can look at what you generated by viewing the source in the browser. And if you try to run it all modern browsers have debuggers built in to them - there may be some messages in there too. I was just looking for other possible errors that are not related to the iteration.
    – stdunbar
    Commented Oct 26, 2017 at 2:37
  • Yes, I am trying to print the chart on HTML. I will try to run it on different browser and dubug it. @stdunbar
    – Rohan Jain
    Commented Oct 26, 2017 at 2:55

1 Answer 1

0

the BarChart data format calls for numbers or column roles
starting in the second column of the data

but you are passing string values

remove the single quotes from the second value in the array, as follows...

out.println("['"+entry.getKey()+"',"+entry.getValue()+"],");
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.