简体   繁体   English

使动态生成的Google图表具有响应能力

[英]Make dynamically generated Google Charts Responsive

I have followed the examples to make Google Charts responsive using the code below: 我按照以下示例使用以下代码使Google图表具有响应能力:

$(document).ready( function () {
    //Make Chart(s) Responsive
    $(window).resize(function(){
        drawVisualization();
    });

This works great in my html prototypes however I am now generating my charts using PHP. 这在我的html原型中效果很好,但是现在我正在使用PHP生成图表。 As an example I may have to generate n charts so I iterate through a set of data in PHP generating the DIV the chart will reside in, the JSON data and so on. 作为示例,我可能必须生成n个图表,以便遍历PHP中的一组数据以生成图表将驻留的DIV,JSON数据等等。

My DIVs are generated to be unique such as: 我的DIV生成为唯一,例如:

<div id="visualization_**identifier**" style="height:200px;">

I then create a function per chart to take care of the rendering.drawing, I can if needed name it uniquely: 然后,我为每个图表创建一个函数来处理render.drawing,如果需要,我可以对其进行唯一的命名:

function drawVisualization_**identifier**()

This is all working however the jQuery code requires I call each drawVisualization_**identifier**() in turn to refresh the charts, is there a way I can dynamically create a list and refresh them? 一切正常,但是jQuery代码要求我drawVisualization_**identifier**()调用每个drawVisualization_**identifier**()来刷新图表,有没有办法动态创建列表并刷新它们?

I assume I can add a class to my vizualization divs and query for them with typical jQuery code (although I'm not entirely sure how) - if that can be done can I manipulate it in to calls to each drawVisualization_**identifier**() function to perform the refresh? 我假设我可以在我的vizualization div中添加一个类,并使用典型的jQuery代码查询它们(尽管我不确定如何做到)-如果可以做到,我可以操纵它来调用每个drawVisualization_**identifier**()函数执行刷新?

Something (very roughly!) along the lines of: 类似于(大致)的东西:

$(document).ready( function () {
    //Make Chart(s) Responsive
    $(window).resize(function(){
        foreach(get visualization div names){
            refreshVizualization('vizualization'+vizDivName);
        }
    });

Any help would be much appreciated, while I feel I'm getting there with jQuery and charts this one is making my head hurt. 任何帮助将不胜感激,尽管我觉得我正在使用jQuery和图表,但这使我头疼。 Seems odd that for as fantastic as Google Charts are there doesn't seem to be a "redraw on resize" capability. 似乎奇怪的是,与Google图表一样出色的功能似乎没有“调整大小时重新绘制”的功能。

Seem I have a tendency to have a bright idea just after giving up and posting my question. 似乎在放弃并提出我的问题后,我就有一个好主意的趋势。 I realized that during my PHP chart generation I could also create (in PHP) a javaScript function to do the refresh then call that function from the jQuery resize. 我意识到在生成PHP图表的过程中,我还可以创建(在PHP中)一个javaScript函数来进行刷新,然后从jQuery调整大小中调用该函数。 The PHP below follows the loop that creates my charts / divs / callback functions and seems to work beautifully. 下面的PHP遵循创建我的图表/ divs /回调函数的循环,并且看起来工作得很漂亮。

#Generate Chart Refresh Function to call on Window Resize
print('<script>');
print('function drawVisualization() {');
foreach($entity as $chartid){
    print('   drawVisualization_'.$chartid["uniqueIDperChart"].'();');
}
print('};');
print('</script>');

So, if I have four charts the function will have four drawVizualization_uniqueID(); 因此,如果我有四个图表,该函数将具有四个drawVizualization_uniqueID(); calls then the function drawVisualization() is called from the jQuery event tied to the window resize. 调用,然后从绑定到窗口调整大小的jQuery事件中调用函数drawVisualization()。

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

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