Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 获取Google图表图像以填充DIV的宽度_Html_Google Visualization - Fatal编程技术网

Html 获取Google图表图像以填充DIV的宽度

Html 获取Google图表图像以填充DIV的宽度,html,google-visualization,Html,Google Visualization,我正在使用Google的图表API获取一些折线图,并将其放置在一个DIV中,如下所示: <div class="chart" style="display:block"> <img src="http://chart.apis.google.com/chart?chs=620x40&cht=lfi&chco=0077CC&&chm=B,E6F2FA,0,0,0&chls=1,0,0&chd=t:27,25,25,25,25,2

我正在使用Google的图表API获取一些折线图,并将其放置在一个DIV中,如下所示:

<div class="chart" style="display:block">
  <img src="http://chart.apis.google.com/chart?chs=620x40&cht=lfi&chco=0077CC&&chm=B,E6F2FA,0,0,0&chls=1,0,0&chd=t:27,25,25,25,25,27,100,31,25,36,25,25,39,25,31,25,25,25,26,26,25,25,28,25,25,100,28,27,31,25,27,27,29,25,27,26,26,25,26,26,35,33,34,25,26,25,36,25,26,37,33,33,37,37,39,25,25,25,25">
</div>

我必须传递所需图表图像的高度和宽度,谷歌图表API会呈现它,例如
chs=620x40
。我希望图表图像是我父母的
div
的。我需要计算宽度并动态构造此图表URL,以便获得正确大小的图表图像。我该怎么做

(我对jQuery不是很在行,我正试图避免使用一些臃肿的库)


谢谢

您可以使用以下JavaScript(与jQuery一起使用):

函数sizeCharts(){
$(“.chart”)。每个(函数(){
var w=$(this.width();
var h=$(this).height();//或者将其更改为var h=40
$("
将[etc]替换为您希望使用的url的其余部分。上述代码中发生的情况是,它将使用页面中的
chart
类迭代所有内容,并将图表以适当的大小放入其中

如果您使用流动布局(即,您的站点调整大小以填充一定百分比的屏幕),那么您还需要包含
$(function(){…})
位,该位在调整页面大小时运行相同的代码。请注意此处使用计时器,否则将为调整窗口大小的每个像素重新加载相同的图表