来自 C# 代码隐藏的动态 Plotly 图

Dynamic Plotly graphs from codebehind in C#

我正在尝试创建一个网页,该网页可以根据动态更改的文件动态创建新的绘图。我在使用 ScriptManager.RegisterClientScriptBlock 从代码隐藏访问 plotly 函数时遇到问题。也许我是从错误的方向来处理这个问题的,但这是我正在做的事情的要点。

代码隐藏:

    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "dayPop", "<script>doGraph();</script>", false);

HTML:

<div id="myDiv" style="width: 480px; height: 400px;"><!-- Plotly chart will be drawn inside this DIV --></div>

脚本:

    <script>
    var trace1 = {
        mode: 'lines',
        x: [1, 2, 3, 4],
        y: [10, 15, 13, 17]
    };

    var data = [trace1];

    //doGraph('myDiv');

    function doGraph() {
        var GRAPHONE = document.getElementById('myDiv');
        Plotly.newPlot(GRAPHONE, data);
    }
    </script>

当我使用注释掉的行但不是来自代码隐藏时,一切都很好。也许这里有人有这方面的经验?

<script>
$( document ).ready(function() {
    doGraph();
});
</script>

问题是,我想您正在使用 jquery,因此您的脚本代码应该如下所示。可以在浏览器呈现 div 之前调用 doGraph(),因为您应该在页面加载后执行它。如果你不使用 jquery ,你可以将这个方法附加到 body onload 函数上。但我强烈建议您查看 jquery.