简体   繁体   English

为每个模式弹出窗口重新绘制一个Google图表

[英]Redrawing a google chart for every modal pop up

Some of the answers on similar queries to this say that you can only call .load once. 一些与此类似的查询的答案表明,您只能调用.load一次。

So this is my bit of code. 这就是我的代码。
It only draws the graph for the first modal. 它仅绘制第一个模态的图形。

I need it to redraw the graph with the new information when I open subsequent modals. 当我打开后续模态时,我需要它以新信息重绘图形。

google.charts.setOnLoadCallback(load_page_data, true);

function load_page_data(){
  $(function(){
    $('.modal').on('show.bs.modal', function (){
      rid = $(this).prop('id') ;
      console.log(rid);

      $.ajax({
        url: 'https://www.google.com/jsapi?callback',
        cache: true,
        dataType: 'script',
        success: function(){
          google.load('visualization', '1',{
            packages:['corechart'],
            'callback' : function(){

              $.ajax({
                type: "POST",
                url: 'getdata.php',
                data:{'id': rid},
                async: false,
                success: function(data){
                if(data){
                  chart_data = data;
                  drawChart(chart_data, "My Chart", "Data");
                }
                },
              });
            }
          });
        }
      });

      //something end
    });
  });
}


function drawChart(chart_data, chart1_main_title, chart1_vaxis_title){
  var chart1_data = new google.visualization.DataTable(chart_data);
  var chart1_options ={
    title: chart1_main_title,
    vAxis:{
      title: chart1_vaxis_title,
      titleTextStyle:{
        color: 'red'}
    }
  };

  var chart1_chart = new google.visualization.BarChart

  (document.getElementById('chart_div'));
  chart1_chart.draw(chart1_data, chart1_options);
}

the load statement only needs to be called once per page 每个页面只需调用一次load语句

once the callback fires the first time, callback第一次触发后,
you can draw as many charts as needed 您可以根据需要绘制任意数量的图表

recommend loading google first, before anything else 建议先加载google,然后再进行其他操作

if you have $(document).ready somewhere, don't need it 如果您有$(document).ready某个地方,则不需要它

you can rely on google's callback to know when the page is ready 您可以依靠Google的回调来知道页面何时准备就绪

recommend set up similar to following... 建议设置类似于以下内容...

google.charts.load('current', {
  callback: load_page_data,
  packages: ['corechart']
});

function load_page_data(){
  $('.modal').on('show.bs.modal', function () {
      rid = $(this).prop('id') ;
      console.log(rid);
      $.ajax({
          type: "POST",
          url: 'getdata.php',
          data: {'id': rid},
          async: false,
          success: function(data){
              if(data){
                  chart_data = data;
                  drawChart(chart_data, "My Chart", "Data");
              }
          },
      });
  });
}

function drawChart(chart_data, chart1_main_title, chart1_vaxis_title) {
  var chart1_data = new google.visualization.DataTable(chart_data);
  var chart1_options = {
    title: chart1_main_title,
    vAxis: {title: chart1_vaxis_title,  titleTextStyle: {color: 'red'}}
  };

  var chart1_chart = new google.visualization.BarChart(document.getElementById('chart_div'));
  chart1_chart.draw(chart1_data, chart1_options);
}

note: you want to use the gstatic loader... 注意:您要使用静态加载程序...

<script src="https://www.gstatic.com/charts/loader.js"></script>

not jsapi 不是 jsapi

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

according to the release notes ... 根据发行说明 ...

The version of Google Charts that remains available via the jsapi loader is no longer being updated consistently. 可以通过jsapi加载程序保留的Google图表版本不再保持一致更新。 Please use the new gstatic loader from now on. 从现在开始,请使用新的静态加载器。

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

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