简体   繁体   English

向使用标记的Google地理图表添加事件监听器

[英]Add an event-listener to a google geochart which uses markers

I am struggling a bit at the moment with google geochart. 目前,我在与Google geochart进行斗争。 I am trying to add an event-listener to a geochart which uses markers to display world countries, and when the user clicks on a marker, it loads country-specific data. 我正在尝试向使用标记显示世界国家的地理图表添加事件侦听器,并且当用户单击标记时,它将加载特定于国家/地区的数据。 My code works well except for the event-listeners. 除了事件监听器,我的代码运行良好。 Now, I know that when I am using the chartwrapper object I have to add two event listeners, one for the dashboard, and one for the chart once the dashboard event listener has loaded. 现在,我知道当我使用chartwrapper对象时,一旦加载了仪表板事件侦听器,就必须添加两个事件侦听器,一个用于仪表板,一个用于图表。 I have managed to get my code to work when I use regions instead of markers, but for markers I can't get it to work. 当我使用区域而不是标记时,我设法使代码正常工作,但是对于标记,我无法使其正常工作。 It should be the same event normally, basically 'select' but I can't get it to work. 通常,它应该是同一事件,基本上是“选择”,但我无法使其正常工作。 I have posted my whole code for you to get the global picture but you should probably focus only on the event listeners and the associated tableSelectHandler() function. 我已经发布了完整的代码供您获取全局图片,但您可能应该只专注于事件侦听器和关联的tableSelectHandler()函数。 I have tried so far to remove all the code in the function and to display only a generic alert but even that won't work. 到目前为止,我已经尝试过删除函数中的所有代码,并仅显示一般警报,但即使这样也无法正常工作。 Help please ! 请帮助 ! Many thanks 非常感谢

var script_url = [
  '/SymphonyAdminPanel/php/SQLSRV/finalshareByCountry.php',  
  '/SymphonyAdminPanel/php/SQLSRV/countryData.php',
  '/SymphonyAdminPanel/php/salesdata.php'    
];

var pn_1_data = null; //pn_1 datatable 
var pn_1 = null; // pn_1 div 
var pn_1_filter = null; // pn_1 filter 
var pn_1_ch = null; // pn_1 chart

function pn_1_draw() {  
  // Create a dashboard. 
  pn_1 = new google.visualization.Dashboard(document.getElementById('pn_1'));

  // Create a filter for our datatable 
  pn_1_filter = new google.visualization.ControlWrapper({
    'controlType': 'NumberRangeFilter',
    'containerId': 'pn_1_filter',
    'options': {
    'filterColumnIndex': 1, //Use filterColumIndex here & not filtercolumnIndex    
    }
  });

  // Chart wrapper object
  pn_1_ch = new google.visualization.ChartWrapper({
      'chartType' : 'GeoMap',
      'containerId' : 'pn_1_ch',
      'options': {
        'dataMode' : 'markers',
        'region' : 'world',
        'fontName': 'Arimo',
        'width': 900,
        'height' : 500     
      }

  });

  //We bind the elements to our dashboard 
  pn_1.bind(pn_1_filter, pn_1_ch);

  // Load Json data from server, create datatable & draw charts 
  $.getJSON(script_url[0], function(jresults){

     //We use the datatable declared earlier and assign it our $.getJson object       
     pn_1_data  = new google.visualization.DataTable(jresults.data);

     //We draw the whole dashboard object and its bound elements     
     pn_1.draw(pn_1_data);

     //We add an event listener to our dashboard which adds an event listener to the bound chart 
     google.visualization.events.addListener(pn_1, 'ready', function(){

      google.visualization.events.addListener(pn_1_ch,'select', tableSelectHandler);

     });   


  });

};

function tableSelectHandler(){
/*var selectedItem = pn_1_ch.getChart().getSelection()[0]; 
  var country = pn_1_data.getValue(selectedItem.row, 2);  

  //Set the geochart region to the country selected 
  pn_1_ch.setOption('region',country); 

  //Load new JSON data
  var url_updated = '/SymphonyAdminPanel/php/SQLSRV/countryData.php?&country='+country; 

  $.getJSON(url_updated, function(jresults){     

     pn_1_data  = new google.visualization.DataTable(jresults.data);

     pn_1.draw(pn_1_data);     

  }); */
  alert('Its working');       

};

I think that this is where the problem comes from : 我认为这是问题出处:

     //We add an event listener to our dashboard which adds an event listener to the bound chart 
 google.visualization.events.addListener(pn_1, 'ready', function(){

  google.visualization.events.addListener(pn_1_ch,'select', tableSelectHandler);

 });  

You're adding an event listenner that waits until pn_1 is ready in order to add another event listener to pn_1_ch. 您正在添加一个事件侦听器,该侦听器要等到pn_1准备好才能向pn_1_ch添加另一个事件侦听器。 I think it might interfere with the whole code. 我认为这可能会干扰整个代码。 Maybe you should try adding the event seperately. 也许您应该尝试单独添加事件。 Also try not to define the function outside of the addListener at first just to see if the function is working. 还要先尝试不要在addListener之外定义函数,只是看该函数是否正常工作。 Try this : 尝试这个 :

google.visualization.events.addListener(pn_1, 'ready', function(){ /*...*/ });
google.visualization.events.addListener(pn_1_ch,'select', function(){
   alert("this is working");
}); 

It appears that writting my code that way always works better for me. 看来以这种方式编写代码对我来说总是更好。

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

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