jQuery load Google Visualization API with AJAX

后端 未结 10 2089
栀梦
栀梦 2020-12-24 02:17

There is an issue that I cannot solve, I\'ve been looking a lot in the internet but found nothing.

I have this JavaScript that is used to do an Ajax request by PHP.

相关标签:
10条回答
  • 2020-12-24 03:06

    This is a bit of a shot in the dark:

    google.setOnLoadCallback(function() { drawData(html) });

    It may be that the reference to html is lost, and a closure is required.

    0 讨论(0)
  • 2020-12-24 03:06

    Looks like you're missing the Google library that provides the visualization. Are you sure you've included all the needed google scripts?

    0 讨论(0)
  • 2020-12-24 03:09

    I am using an AJAX-based tab system and Google's Interactive Line Chart Visualizations in one of my projects and ran into a similar brick wall.

    Because of AJAX's inherent blocking of cross-domain scripting, you can't load the Google javascript API (http://www.google.com/jsapi) or any other external resources.

    And since Google's terms of service prohibit offline (aka "not hosted on Google") use of their visualization API, you can't legally get a copy of the scripts and host them yourself as is required.

    I tried a hacky workaround of including a file called "get_vis.php" instead of the "visualization_page.php" in my tabs; where the contents of "get_vis.php" is:

    <?php 
    echo file_get_contents('http://domain.com/path/to/visualization_page.php');
    ?>
    

    But, no luck, it seems the only way to get the API to load properly is to adjust security settings so as to allow interaction with Google's servers. I don't know if it helps, but good luck.

    0 讨论(0)
  • 2020-12-24 03:13

    This works for me

    google.load("visualization", "1", { packages: ["corechart"] });
    
                 var chart ;
                 var data ;
                 var options;
            function Change(s)
            {
                  // s in json format    
                  google.setOnLoadCallback(reDraw(s));
                  function reDraw(s) {
                      console.log(new Function("return " + s)().length); // to test if json is right
                      data = google.visualization.arrayToDataTable(new Function("return "+s)());
    
                        options = {
                        title: 'Product Scanes',
                        vAxis: { title: '', titleTextStyle: { color: 'red'} }         
                    };
    
                  }         
                    chart = new google.visualization.BarChart(document.getElementById('chart_div'));
                    chart.draw(data, options);
            }  
            function requestDate() // cal the method when you want to draw the chart 
            {
    
                $.ajax({
                    type: "POST", // CHANGED
                   // contentType: "application/json; charset=utf-8",
                    url: "something.php",
                    data: {  parameters you wanna pass },
                    success: function (d) { 
                    Change(d);
    
    
                    }
                });    
            }
    }
    
    0 讨论(0)
提交回复
热议问题