简体   繁体   English

将 dygraph 内容注入 jquery UI 手风琴不显示

[英]Inject dygraph content into jquery UI accordion does not show

I am creating grids of graphs using the dygraphs library and injecting them in an accordion element using the below code.我正在使用dygraphs库创建图形网格,并使用以下代码将它们注入到accordion元素中。 The issue comes from the accordion : if you comment the accordion part out in the jsFiddle (adjusting the name of the div to inject code into), the graphs do show up.问题来自手风琴:如果您在jsFiddle注释手风琴部分(调整div的名称以将代码注入其中),图表会显示出来。

The jsFiddle here .这里jsFiddle

the html : html

<div id="theAccordion">
</div>

the javascript : javascript

function createGraph(destination) {
  var g = new Dygraph(
    document.getElementById(destination),
    [[0,1],[1,2],[2,3],[3,4]],
    {
    labels: ["x","y"],
  strokeWidth: 0.0,
  drawPoints:true
 });
}

$(function() { 

var accordionHtml = "";
accordionHtml += "<h3 id='accordion-h3'>Global</h4>";
accordionHtml += "<div id='accordion-1'></div>";

$("#theAccordion").html(accordionHtml);
$("#theAccordion").accordion({
collapsible: true,
    autoHeight: true,
    active: false,
    heightStyle: "content"
})

var tableHtml = "";
tableHtml+= "<table><tr><td><div id='graph0'></div></td><td><div id='graph1'></div></td></tr></table>"
$("#accordion-1").html(tableHtml);
for (var i=0;i<2;i++) {
  createGraph("graph"+i);
}
});

All help welcome !欢迎所有帮助!

Dygraph works by drawing on an HTML5 canvas. Dygraph 通过在 HTML5 画布上绘图来工作。 If you inspect the inner divs of the collapsed element, that canvas has a width and height of 0 when the page first loads.如果您检查折叠元素的内部 div,当页面首次加载时,该画布的宽度和高度为 0。 That probably happens because the accordion is collapsed initially.这可能是因为手风琴最初是倒塌的。 The element resizes itself to fill its parent container, but since the container is empty it doesn't know how big to make itself.元素调整自己的大小以填充其父容器,但由于容器是空的,它不知道自己有多大。 This is further supported by the fact that if the window is resized at all when the accordion is opened, the graphs load.如果在打开手风琴时完全调整窗口大小,则会加载图形,这一事实进一步支持了这一点。

That being said, Dygraph doesn't seem to have a method to trigger a DOM refresh.话虽如此,Dygraph 似乎没有触发 DOM 刷新的方法。 One of its functions ( updateOptions ) might be able to do that but that would be a bit of a hack.它的一个功能( updateOptions )可能能够做到这一点,但这会有点updateOptions

Aside from that function, there are two possible simple fixes I've identified:除了该功能之外,我还确定了两个可能的简单修复:

  1. If it's possible to have the accordion opened when the page loads, that seems to fix the issue.如果可以在页面加载时打开手风琴,这似乎可以解决问题。 You can do that by setting active = 0 in the call to accordion .您可以通过在对accordion的调用中设置active = 0来做到这一点。

  2. If it's possible to set the width and height manually (in pixels), that works as well:如果可以手动设置宽度和高度(以像素为单位),那也可以:

(extra space since the code renderer doesn't like being right after a numbered list) (额外的空间,因为代码渲染器不喜欢紧跟在编号列表之后)

{
  labels: ["x","y"],
  strokeWidth: 0.0,
  drawPoints:true,
  height: 320,
  width: 480
}

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

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