简体   繁体   English

Google图表无法显示

[英]Google Charts Not Displaying

I am loading my data from a MS database with an AJAX call. 我正在通过AJAX调用从MS数据库加载我的数据。 When it returns my tables are returning and displaying on the page. 当它返回时,我的表将返回并显示在页面上。

When i look in the dev editor i can see the JS for the Google Charts is returning, but its not displaying on screen. 当我查看开发编辑器时,我可以看到Google Charts的JS正在返回,但它没有显示在屏幕上。

Do I need to reprint this data somehow? 我需要以某种方式重新打印这些数据吗? I've already tried moving the Google code into my AJAX call, but then I lose the array I've captured. 我已经尝试将Google代码移动到我的AJAX调用中,但后来我丢失了我捕获的数组。

Any help is appreciated. 任何帮助表示赞赏。

//AJAX call // AJAX调用

function reportBuild(date1, date2, dataType){
  date2 = $("#dateRangeA").val();
  date1 = $("#dateRangeB").val();
  dataType = $("#dataType").val();

  var xhttp;
  xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
     document.getElementById("chartBuild").innerHTML = this.responseText;
    }
  }
  xhttp.open("GET", "test.asp?d1="+date1+"&d2="+date2+"&dataType="+dataType, true);
  xhttp.send();

}

//test.asp page //test.asp页面

var testArray = [];

        testArray = [
          <%
          do until DataChartFetch.EOF
            %>
            ['<% Response.Write(DataChartFetch.Fields(dataType&"Name1")) %>',
            <% Response.Write(DataChartFetch.Fields(dataType&"Num1")) %>,
            <% Response.Write(DataChartFetch.Fields(dataType&"Num1")) %>],

            ['<% Response.Write(DataChartFetch.Fields(dataType&"Name2")) %>',
            <% Response.Write(DataChartFetch.Fields(dataType&"Num2")) %>,
            <% Response.Write(DataChartFetch.Fields(dataType&"Num2")) %>],

            ['<% Response.Write(DataChartFetch.Fields(dataType&"Name3")) %>',
            <% Response.Write(DataChartFetch.Fields(dataType&"Num3")) %>,
            <% Response.Write(DataChartFetch.Fields(dataType&"Num3")) %>],

            ['<% Response.Write(DataChartFetch.Fields(dataType&"Name4")) %>',
            <% Response.Write(DataChartFetch.Fields(dataType&"Num4")) %>,
            <% Response.Write(DataChartFetch.Fields(dataType&"Num4")) %>],

            ['<% Response.Write(DataChartFetch.Fields(dataType&"Name5")) %>',
            <% Response.Write(DataChartFetch.Fields(dataType&"Num5")) %>,
            <% Response.Write(DataChartFetch.Fields(dataType&"Num5")) %>],
          <%
          DataChartFetch.MoveNext
            loop
          %>
        ]


google.charts.load('current', {'packages':['bar']});
      google.charts.setOnLoadCallback(barchart_material);
      function barchart_material() {

        var data = new google.visualization.DataTable();
        data.addColumn('string', '');
        data.addColumn('number', 'Title');
        data.addColumn({type: 'number', role: 'annotation'});

        //Response to get dates and ranges
        data.addRows(testArray);

        var options = {
          'chartArea': {'width': '90%', 'height': '85%'},
          'legend': {'position': 'top', alignment: 'start'},
        };
        var chart = new google.charts.Bar(document.getElementById('barchart_material'));
        chart.draw(data, options);
      } //end google display function

I solved the issue. 我解决了这个问题。

Inside of the AJAX call i placed the Google Charts function. 在AJAX调用中,我放置了Google Charts功能。

i used 我用了

localStorage.setItem("jsonArray", JSON.stringify(testArray));

inside my test.asp page 在我的test.asp页面内

and inside the ajax call, used 并在ajax调用内部使用

 jsonArray = JSON.parse(localStorage.getItem("jsonArray"));

to return the array and use it in the data. 返回数组并在数据中使用它。

final code 最终代码

function reportBuild(date1, date2, dataType){
  date2 = $("#dateRangeA").val();
  date1 = $("#dateRangeB").val();
  dataType = $("#dataType").val();

  var xhttp;
  xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
     document.getElementById("chartBuild").innerHTML = this.responseText;
    }
  }
  xhttp.open("GET", "test.asp?d1="+date1+"&d2="+date2+"&dataType="+dataType, true);
  xhttp.send();

jsonArray = JSON.parse(localStorage.getItem("jsonArray"));

google.charts.load('current', {'packages':['bar']});
      google.charts.setOnLoadCallback(barchart_material);
      function barchart_material() {

        var data = new google.visualization.DataTable();
        data.addColumn('string', '');
        data.addColumn('number', 'OSAP');
        data.addColumn({type: 'number', role: 'annotation'});

        //Response to get dates and ranges
        data.addRows(jsonArray);

        var options = {
          'chartArea': {'width': '90%', 'height': '85%'},
          'legend': {'position': 'top', alignment: 'start'},
        };
        var chart = new google.charts.Bar(document.getElementById('barchart_material'));
        chart.draw(data, options);
      } //end google display function


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM