【问题标题】:How do I make dygraph expand to fill height on div如何使 dygraph 扩展以填充 div 上的高度
【发布时间】:2016-11-04 22:02:37
【问题描述】:

我无法让 DyGraph 渲染到正确的高度。我的理想是图形是主浏览器区域高度的 100%。

即我希望它填充导航栏和页脚之间的所有空间。

通过下面的设置,我得到了一个图表,但它只是一条水平线。我不想指定 px 高度,因为我想填充任何可用的空间。

_PnlDyGraph.cshtml:

<script type="text/javascript" src="dygraph-combined-dev.js"></script>
<h1>Title</h1>
<div id="mygrapharea" style="width: 100%; height:100%;"></div>
<script type="text/javascript">
    @{
        string txtER = (string)Session["PnlDataForChart"];
            <text>
               var dataT = "@txtER"
            </text>
     }
    g = new Dygraph(document.getElementById("mygrapharea"),
                dataT 
        );
</script>

_Traders.cshtml:

<div class="row">
    <div class="col-sm-6">
        @Html.Partial("~/Views/Employee/_PnlDyGraph.cshtml")
    </div>
</div>

_Layoutcshtml:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    @*<title>@ViewBag.Title - Platform</title>*@
    <title>Platform</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")

</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <img src="~/Images/Banner_Front.png" alt="Banner" style="width:300px;height:50px;"/>
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
                    <li>@Html.ActionLink("Traders", "Traders", "Traders")</li>
                    <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                </ul>
                @Html.Partial("_LoginPartial")
            </div>
        </div>
    </div>
    <div class="container body-content">
        @RenderBody()

        <hr />
        <footer>
            @*<p>&copy; @DateTime.Now.Year</p>*@
        </footer>
    </div>

    @*@Scripts.Render("~/bundles/jquery")*@
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

【问题讨论】:

  • 根据 div 高度获取大小,如果您调整页面大小,则 DyGraph 不会自动调整大小,因此您应该使用 DyGraph 的 resize() 方法。像 gygraph 设置变量是 g1 然后 g1.resize();它适用于我的应用程序
  • 我包含在
    g = new Dygraph(document.getElementById("mygrapharea"), dataT ); g.resize();
    之后
  • 问题依旧

标签: c# asp.net-mvc dygraphs


【解决方案1】:

您应该将此 div 写入另一个 div 并以像素为单位指定 div 高度,在内部 div 中您将以百分比指定。

没有内容,高度没有价值来计算百分比。但是,如果未指定父级,则宽度将从 DOM 中获取百分比。

例如。

<div   id="parent"  style="width: 100px; height: 1000px; background-color: orange">
  <div id="mygrapharea" style="width: 100px; height: 100%;   background-color: blue"></div>
</div>

希望对你有帮助

【讨论】:

  • 您可能希望 div 和其中的段落具有 100% 的高度,但该 div 实际上有两个父元素: 和 。为了将 div 的高度定义为相对高度,您还必须设置 body 和 html 元素的高度。因此,您不仅需要使用 CSS 设置 div 的高度,还需要设置 body 和 html 元素的高度。这可能是一个挑战,因为您很快就会被所有设置为 100% 高度的东西弄得不知所措,只是为了达到这种预期的效果。
猜你喜欢
  • 1970-01-01
  • 2013-07-19
  • 2015-09-21
  • 1970-01-01
  • 1970-01-01
  • 2011-04-02
  • 2011-07-31
  • 1970-01-01
  • 2017-08-14
相关资源
最近更新 更多