获取Google趋势响应图表

时间:2015-04-06 13:16:44

标签: charts google-trends responsiveness

我想知道是否可以使用当前给定的代码(通过添加'未知'参数)来获取Google趋势图表

实施例

<script type="text/javascript" src="//www.google.com/trends/embed.js?hl=en-US&q=hoest,+Bronchitis,+Bronchiolitis,+RSV&geo=BE&date=1/2011+49m&cmpt=q&tz&tz&content=1&cid=TIMESERIES_GRAPH_0&export=5&w=900&h=330"></script>

当把100%&#39;放在&#39; 100%&#39;作为宽度值。 但是任何人都知道有一个额外的参数来改变你想要使用的单位(默认情况下是px)?

2 个答案:

答案 0 :(得分:1)

尝试在页面加载时使用javascript获取屏幕宽度,然后动态创建嵌入代码,用实际屏幕宽度替换定义的宽度参数。

<script>

//function which gets screen width
function getWidth() {
  if (self.innerHeight) {
    return self.innerWidth;
  }

  if (document.documentElement && document.documentElement.clientHeight) {
    return document.documentElement.clientWidth;
  }

  if (document.body) {
    return document.body.clientWidth;
  }
}

    //define screen width variable. Subtract 15 pixels from width just to be on the safe side and reduce chance of getting a horizontal scroll bar
    var screenWidth = getWidth()-Number(15);

    //replace the following URL with your own. Be sure to keep the modified part of the string in tact where it replaces the width with screenWidth variable
    var embedCode = "//www.google.com.au/trends/embed.js?hl=en-US&q=mick+fanning,+wwe&geo=ZA&date=now+7-d&cmpt=q&tz=Etc/GMT-10&tz=Etc/GMT-10&content=1&cid=TIMESERIES_GRAPH_0&export=5&w="+screenWidth+"&h=330";


//write this new code to browser. Split '<script'> tags to prevent browser errors when writing.
document.write('<scr'+'ipt type=\"text/javascript\" src=\"'+embedCode+'\"></scr'+'ipt>');

</script>

请注意,如果浏览器窗口发生变化,图表的宽度将无法调整。 (即,用户将手机侧翻)。

答案 1 :(得分:0)

如果您点击趋势图右上角的菜单,则可以选择在“桌面”或“移动”视图中嵌入图表。选择移动版,它会为您做出响应,并为您提供代码段。

example of responsive embed code