带有Google图表的JSP JSON - 折线图

时间:2014-12-14 20:15:21

标签: javascript json jsp google-visualization

我的图表没有加载。我尝试使用javascript将JSON从JSP页面解析为HTML页面。

我不知道是什么问题。这是详细信息:

我的HTML页面:

<!DOCTYPE html>
<html>
<head>
    <title>Google Chart with jsp Mysql Json</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

    <script type="text/javascript" src="https://www.google.com/jsapi"></script>

    <script type="text/javascript">

        var obj="";
        google.setOnLoadCallback(drawChart);

        function drawChart() {

        $.ajax({
            url : 'getDataGlucose.jsp',
            dataType:'json',
            success : function(glucoseData) {
                obj = JSON.stringify(glucoseData)
                alert('Pass? : '+obj);
            },
                error : function(xhr, type) {
                alert('server error occoured')
            }
        });

        var google_data = {
          "cols": [{
              "type": "number"
          }, {
              "type": "number"
          }],
          rows: []
      };

      for (var i = 0; i < obj.length; i++) {
          google_data.rows.push({
              c: [{
                  v: obj[i].xValue
              }, {
                  v: obj[i].glucose
              }]
          });
      }

        var data = new google.visualization.DataTable(google_data);

        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));

            var options = {
                title: 'Glucose Data Intake',
                hAxis: {
                    title: 'Intake'
                },
                curveType: 'function',
                vAxis: {
                    title: 'mmHg'
                }
            };
            var chart = new google.visualization.LineChart(document.getElementById('chart_div'));

           chart.draw(data,options);
    }


    </script>
    </head>
    <body>
          <div id="chart_div"></div>
     </body>

    </html>

我从JSP页面生成JSON:

  

[{“glucose”:5.5,“xValue”:0},{“glucose”:6.2,“xValue”:1},{“glucose”:5.8,“xValue”:2},{“glucose” :7,“xValue”:3},{“glucose”:6,“xValue”:4},{“glucose”:7.5,“xValue”:5},{“glucose”:6,“xValue”:6 },{“glucose”:5.5,“xValue”:7},{“glucose”:6,“xValue”:8},{“glucose”:5.8,“xValue”:9}]

1 个答案:

答案 0 :(得分:-1)

找到我的出路。我改变了:

obj = JSON.stringify(glucoseData);

obj = eval('(' + JSON.stringify(glucoseData) + ')');