简体   繁体   English

使用谷歌图表api并通过ajax在cluetip中显示内容

[英]Using google charts api and displaying content via ajax in cluetip

I have a jsp page that uses google charts api to display data in the form of bar charts. 我有一个jsp页面,使用谷歌图表api以条形图的形式显示数据。 Here is the code for that. 是代码。 I want to display this page in a tooltip ( cluetip ). 我想在工具提示中显示此页面( cluetip )。
My Google Chart code works well when I directly open that page in the browser. 当我在浏览器中直接打开该页面时,我的Google Chart代码效果很好。 But when I try to display it in a tooltip via ajax, there is no chart drawn in the tooltip. 但是当我尝试通过ajax在工具提示中显示它时,工具提示中没有绘制图表。 The tooltip is blank. 工具提示是空白的。 I suspect because of the external javascript that is imported inside the bar chart jsp page. 我怀疑是因为在条形图jsp页面内导入了外部javascript。

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

Is it violating the same origin policy?Am I right about it? 它违反了相同的原产地政策吗?我是对的吗? Is there any way to make it work? 有没有办法使它工作?



EDIT#1 编辑#1

The Google Chrome developer Console only shows a request sent to the Web Page(which uses the Google Chart) but no request is sent to the external javascript that is imported in that page (The external javascript shown above). 谷歌浏览器开发者控制台仅示出了发送到Web页面的请求(它使用谷歌图),但没有请求被发送到在该网页中导入的外部JavaScript(如上所示的外部JavaScript)。

EDIT#2 编辑#2

I think the reason for the request not being made to fetch the external javascript is that 我认为没有获取外部JavaScript的请求的原因是

when you load a page via ajax,any script tags in that page will not be executed. 当您通过ajax加载页面时,该页面中的任何脚本标记都不会被执行。 So the javascript is not getting executed. 所以javascript没有被执行。

How can we manually execute the javscript after getting the data in ajax? 在ajax中获取数据后,我们如何手动执行javscript?



EDIT#3 编辑#3

Actually I have a table with many rows in my JSP. 实际上我的JSP中有一个包含许多行的表。 And each row contains a LINK ; row contains a LINK ; on which if you hover a Google bar Chart will be displayed inside a tooltip(different one for each row). 如果您将鼠标悬停在Google条形图上,图表将显示在工具提示内(每行不同)。 So on hovering each row, the URL for the chart to be fetched will be different. 因此,在悬停每一行时,要获取的图表的URL将不同。 I want to pass this URL as a parameter. And this URL will be used in ajax to fetch the data into tooltip. 此URL将在ajax中用于将数据提取到工具提示中。

This is kind of a pseudo-code since I'm assuming you already have the chart.jsp page ready to go. 这是一种伪代码,因为我假设你已经准备好了chart.jsp页面。 I did my tests in PHP and it worked fine. 我在PHP中进行了测试,它运行良好。 I'm also using QTip2 . 我也在使用QTip2

This is your chart.jsp page: 这是你的chart.jsp页面:

<script type="text/javascript" src="http://www.google.com/jsapi"></script>

int foo = Integer.parseInt(request.getParameter("foo"));
 switch(foo) {
  case 1:
  print
  <script>
  google.load('visualization', '1', {packages:['corechart']});
  function drawChart() {
    var data = google.visualization.arrayToDataTable([
      ['Year', 'Sales', 'Expenses'],
      ['2004',  1000,      400],
      ['2005',  1170,      460],
      ['2006',  660,       1120],
      ['2007',  1030,      540]
    ]);

    var options = {
      title: 'Company Performance',
      hAxis: {title: 'Year', titleTextStyle: {color: 'red'}}
    };

    var chart = new google.visualization.ColumnChart(document.getElementById('visualization'));
    chart.draw(data, options);
  }
  google.setOnLoadCallback(drawChart);   
  </script>     
  break;
    ...
}

<div id="visualization"></div>

On the other side, where you're calling the chart.jsp inside the tooltip via iframe: 另一方面,您通过iframe在工具提示中调用chart.jsp:

<script>
$(function() {
  $('.tip').qtip({
    content: function(){
      var rel = $(this).attr('rel');
      return $('<iframe id="tip" frameBorder="0" src="chart.jsp?foo='+ rel +'" />')
    },
    hide: 'unfocus, click'
  }); 
});
</script>

<a class="tip" href="javascript:void(0)" rel="1">Hover</a>

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

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