簡體   English   中英

帶有Google圖表的JSP JSON-折線圖

[英]JSP JSON with Google Charts - Line Charts

我的圖沒有加載。 我嘗試使用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:

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

找到了我的出路。 我從:

obj = JSON.stringify(glucoseData);

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM