当父容器隐藏时,Flot 图不会呈现

发布于 2024-10-26 00:14:33 字数 789 浏览 2 评论 0原文

我遇到了一个问题,即浮点图无法在选项卡式界面中呈现,因为占位符 div 是带有“display: none”的 div 的子级。将显示轴,但不显示图形内容。

为了解决这个问题,我编写了下面的 javascript 函数作为绘图函数的包装器。对于其他人做类似的事情可能会有用。

function safePlot(placeholderDiv, data, options){

    // Move the graph place holder to the hidden loader
    // div to render
    var parentContainer = placeholderDiv.parent();
    $('#graphLoaderDiv').append(placeholderDiv);

    // Render the graph
    $.plot(placeholderDiv, data, options);

    // Move the graph back to it's original parent
    // container
    parentContainer.append(placeholderDiv);
}

这是图形加载器 div 的 CSS,可以放置 页面上的任何位置。

#graphLoaderDiv{
    visibility: hidden;
    position: absolute;
    top: 0px;
    left: 0px;
    width: 500px;
    height: 150px;
}

I was having an issue where a flot graph would not render in a tabbed interface because the placeholder divs were children of divs with 'display: none'. The axes would be displayed, but no graph content.

I wrote the javascript function below as a wrapper for the plot function in order to solve this issue. It might be useful for others doing something similar.

function safePlot(placeholderDiv, data, options){

    // Move the graph place holder to the hidden loader
    // div to render
    var parentContainer = placeholderDiv.parent();
    $('#graphLoaderDiv').append(placeholderDiv);

    // Render the graph
    $.plot(placeholderDiv, data, options);

    // Move the graph back to it's original parent
    // container
    parentContainer.append(placeholderDiv);
}

Here is the CSS for the graph loader div which can be placed
anywhere on the page.

#graphLoaderDiv{
    visibility: hidden;
    position: absolute;
    top: 0px;
    left: 0px;
    width: 500px;
    height: 150px;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

也许这是更好的解决方案。它可以用作 $.plot() 的替代品:

var fplot = function(e,data,options){
  var jqParent, jqHidden;
  if (e.offsetWidth <=0 || e.offetHeight <=0){
    // lets attempt to compensate for an ancestor with display:none
    jqParent = $(e).parent();
    jqHidden = $("<div style='visibility:hidden'></div>");
    $('body').append(jqHidden);
    jqHidden.append(e);
  }

  var plot=$.plot(e,data,options);

  // if we moved it above, lets put it back
  if (jqParent){
     jqParent.append(e);
     jqHidden.remove();
  }

  return plot;
};

然后只需调用 $.plot() 并将其更改为 fplot( )

Perhaps this is better solution. It can be used as a drop in replacement for $.plot():

var fplot = function(e,data,options){
  var jqParent, jqHidden;
  if (e.offsetWidth <=0 || e.offetHeight <=0){
    // lets attempt to compensate for an ancestor with display:none
    jqParent = $(e).parent();
    jqHidden = $("<div style='visibility:hidden'></div>");
    $('body').append(jqHidden);
    jqHidden.append(e);
  }

  var plot=$.plot(e,data,options);

  // if we moved it above, lets put it back
  if (jqParent){
     jqParent.append(e);
     jqHidden.remove();
  }

  return plot;
};

Then just take your call to $.plot() and change it to fplot()

︶ ̄淡然 2024-11-02 00:14:33

唯一无需任何 CSS 技巧即可工作的方法是在 1 秒后加载绘图,如下所示:

$('#myTab a[href="#tabname"]').on("click", function() {
    setTimeout(function() {
       $.plot($(divChartArea), data, options);       
    }, 1000);
});

或对于较旧的 jquery

$('#myTab a[href="#tabname"]').click (function() {
      setTimeout(function() {
         $.plot($(divChartArea), data, options);       
      }, 1000);
    });

上面的示例应用于 Bootstrap 标签的 Click 功能。但应该适用于任何隐藏的 div 或对象。

工作示例: http://topg.org/server-desteria-factions-级别-类-令牌-id388539
只需单击“玩家”选项卡,您就会看到上面的示例正在运行。

The only thing that works without any CSS trick is to load the plot 1 second after like this:

$('#myTab a[href="#tabname"]').on("click", function() {
    setTimeout(function() {
       $.plot($(divChartArea), data, options);       
    }, 1000);
});

or for older jquery

$('#myTab a[href="#tabname"]').click (function() {
      setTimeout(function() {
         $.plot($(divChartArea), data, options);       
      }, 1000);
    });

The above example is applied to Bootstrap tags for Click funtion. But should work for any hidden div or object.

Working example: http://topg.org/server-desteria-factions-levels-classes-tokens-id388539
Just click the "Players" tab and you'll see the above example in action.

挽清梦 2024-11-02 00:14:33

这是一个常见问题解答:

您的#graphLoaderDiv 必须有宽度和高度,不幸的是,不可见的 div 没有它们。相反,使其可见,但将其左侧设置为 -10000px。然后,一旦您准备好显示它,只需将其左侧设置为 0px(或其他值)即可。

This one is a FAQ:

Your #graphLoaderDiv must have a width and height, and unfortunately, invisible divs do not have them. Instead, make it visible, but set its left to -10000px. Then once you are ready to show it, just set it's left to 0px (or whatever).

堇色安年 2024-11-02 00:14:33

好吧,我现在更好地理解你实际上在说什么......但我仍然认为你的答案太复杂了。我只是使用选项卡式界面进行了尝试,其中加载时图形位于隐藏选项卡中。这似乎对我来说效果很好。

http://jsfiddle.net/ryleyb/dB8UZ/

我没有可见性:hidden 位在那里,但似乎没有必要...

您也可以设置 visibility:hidden ,然后将选项卡代码更改为如下所示:

$('#tabs').tabs({
  show: function(e,ui){
    if (ui.index != 2) { return; }
    $('#graphLoaderDiv').css('visibility','visible');
  }
});

但给出信息但这些似乎都不是特别必要的。

OK, I understand better now what you're actually saying... I still think your answer is too complicated though. I just tried this out using a tabbed interface where the graph is in a hidden tab when it's loaded. It seems to work fine for me.

http://jsfiddle.net/ryleyb/dB8UZ/

I didn't have the visibility:hidden bit in there, but it didn't seem necessary...

You could also have visibility:hidden set and then change the tabs code to something like this:

$('#tabs').tabs({
  show: function(e,ui){
    if (ui.index != 2) { return; }
    $('#graphLoaderDiv').css('visibility','visible');
  }
});

But given the information provided, none of that seems particularly necessary.

勿忘心安 2024-11-02 00:14:33

我知道这有点旧,但您也可以尝试使用 Flot 的调整大小插件。

http://benalman.com/projects/jquery-resize-plugin/

它是并不完美,因为有时您会看到可能缩小的非尺寸图形的闪现。此外,根据您使用的图表类型,某些格式和定位可能会关闭。

I know this is a bit old but you can also try using the Resize plugin for Flot.

http://benalman.com/projects/jquery-resize-plugin/

It is not perfect because you'll sometimes get a flash of the non-sized graph which may be shrunk. Also some formatting and positioning may be off depending on the type of graph that you are using.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文