0

我是asp.net用 Js 图表创建的网络表单。MS SQL 数据库给出的那个 Js 图表数据。我想在 05 秒内更新图表。在没有更新面板的页面加载中,图表正确显示。但添加到更新面板后,图表不显示。我试图用各种方式解决这个问题。但我仍然无法找到解决方案。我希望你的支持。

我的 aspx 代码为:

<script src="Content/js/ChartJS.js"></script>

<asp:UpdatePanel ID="UpdatePanel3" runat="server">
    <ContentTemplate>
        <div class="row" style="text-align: center; padding-left: 5%;">
            <div class="col">
                <div style="width: 500px; height: 300px;">
                    <asp:Literal ID="chartTemp" runat="server"></asp:Literal><br /><br />
                    <p>Temperature</p>
                </div>
            </div>
        <asp:Timer ID="Timer3" runat="server" Interval="5000" OnTick="Timer1_Tick"> </asp:Timer>
    </ContentTemplate>
</asp:UpdatePanel>

我的 aspx.cs 代码为:

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        objDeviceClass.DeviceNme = qryStrng;

        string chartType = "Temp";
        DataTable dtTempChart = objDeviceClass.GetChartData(chartType);

        if (dtTempChart != null)
        {
            String chart = "";
            chart = "<canvas id=\"line-chart\"  width=\"60\" height=\"20\"></canvas>";
            chart += "<script>";
            chart += "new Chart(document.getElementById(\"line-chart\"), { type: 'line', data: {labels: [";

            string valueX = "";
            for (int i = 0; i < dtTempChart.Rows.Count; i++)
                valueX += dtTempChart.Rows[i]["time"].ToString() + ",";
            chart += valueX;

            chart += "],datasets: [{ data: [";

            // get data from database and add to chart
            String value = "";
            for (int i = 0; i < dtTempChart.Rows.Count; i++)
                value += dtTempChart.Rows[i]["temperature"].ToString() + ",";
            value = value.Substring(0, value.Length - 1);
            chart += value.ToString();



            chart += "],label: \"Temperature (°C)\",borderColor: \"#3e95cd\",fill: true}"; // Chart color
            chart += "]},options: { title: { display: true,text: 'Temperature (°C)'} }"; // Chart title
            chart += "});";
            chart += "</script>";

            chartTemp.Text = chart;

        }
    }
4

0 回答 0