C# Flot Ajax实时图表无法从CS页面获取数据

C# Flot Ajax实时图表无法从CS页面获取数据,c#,jquery,asp.net,ajax,flot,C#,Jquery,Asp.net,Ajax,Flot,我正在尝试一个Flot Ajax实时图表的示例,其中的数据来自cs页面,但没有传递数据。图表已创建,但数据点未显示。我错过了什么 Default.aspx <%@ Page Title="Flot Examples: Real-time updates" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

我正在尝试一个Flot Ajax实时图表的示例,其中的数据来自cs页面,但没有传递数据。图表已创建,但数据点未显示。我错过了什么

Default.aspx

<%@ Page Title="Flot Examples: Real-time updates" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript" src="js/flot/jquery.min.js"></script>
<script type="text/javascript" src="js/flot/jquery.flot.js"></script>
<script type="text/javascript" src="js/flot/jquery.flot.min.js"></script>
<script type="text/javascript" src="js/flot/jquery.flot.time.js"></script>    
<script type="text/javascript" src="js/flot/jquery.flot.axislabels.js"></script>
<script type="text/javascript" src="js/flot/jshashtable-2.1.js"></script>
<script type="text/javascript" src="js/flot/jquery.numberformatter-1.2.3.min.js"></script>
<script type="text/javascript" src="js/flot/jquery.flot.symbol.js"></script>

<!-- Javascript -->
<script type="text/javascript">
    var cpu = [], cpuCore = [], disk = [];
    var dataset;
    var totalPoints = 100;
    var updateInterval = 5000;
    var now = new Date().getTime();

    var options = {
        series: {
            lines: {
                lineWidth: 1.2
            },
            bars: {
                align: "center",
                fillColor: { colors: [{ opacity: 1 }, { opacity: 1}] },
                barWidth: 500,
                lineWidth: 1
            }
        },
        xaxis: {
            mode: "time",
            tickSize: [60, "second"],
            tickFormatter: function (v, axis) {
                var date = new Date(v);

                if (date.getSeconds() % 20 == 0) {
                    var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
                    var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
                    var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();

                    return hours + ":" + minutes + ":" + seconds;
                } else {
                    return "";
                }
            },
            axisLabel: "Time",
            axisLabelUseCanvas: true,
            axisLabelFontSizePixels: 12,
            axisLabelFontFamily: 'Verdana, Arial',
            axisLabelPadding: 10
        },
        yaxes: [
            {
                min: 0,
                max: 100,
                tickSize: 5,
                tickFormatter: function (v, axis) {
                    if (v % 10 == 0) {
                        return v + "%";
                    } else {
                        return "";
                    }
                },
                axisLabel: "CPU loading",
                axisLabelUseCanvas: true,
                axisLabelFontSizePixels: 12,
                axisLabelFontFamily: 'Verdana, Arial',
                axisLabelPadding: 6
            }, {
                max: 5120,
                position: "right",
                axisLabel: "Disk",
                axisLabelUseCanvas: true,
                axisLabelFontSizePixels: 12,
                axisLabelFontFamily: 'Verdana, Arial',
                axisLabelPadding: 6
            }
        ],
        legend: {
            noColumns: 0,
            position:"nw"
        },
        grid: {      
            backgroundColor: { colors: ["#ffffff", "#EDF5FF"] }
        }
    };

    function initData() {
        for (var i = 0; i < totalPoints; i++) {
            var temp = [now += updateInterval, 0];

            cpu.push(temp);
            cpuCore.push(temp);
            disk.push(temp);
        }
    }

    function GetData() {
        //set no cache
        $.ajaxSetup({ cache: false });

        $.ajax({
            url: 'Default.aspx',
            dataType: 'json',
            success: update,
            error: function () {
                setTimeout(GetData, updateInterval);
            }
        });
    }

    var temp;

    function update(_data) {
        //remove first item of array
        cpu.shift();
        cpuCore.shift();
        disk.shift();

        now += updateInterval


        //add the data retrieve from backend to array
        temp = [now, _data.cpu];
        cpu.push(temp);

        temp = [now, _data.core];
        cpuCore.push(temp);

        temp = [now, _data.disk];
        disk.push(temp);


        //update legend label so users can see the latest value in the legend
        dataset = [
            { label: "CPU:" + _data.cpu + "%", data: cpu, lines: { fill: true, lineWidth: 1.2 }, color: "#00FF00" },
            { label: "Disk:" + _data.disk + "KB", data: disk, color: "#0044FF", bars: { show: true }, yaxis: 2 },
            { label: "CPU Core:" + _data.core + "%", data: cpuCore, lines: { lineWidth: 1.2}, color: "#FF0000" }        
        ];

        //redraw the chart
        $.plot($("#flot-placeholder1"), dataset, options);

        //prepare for the update
        setTimeout(GetData, updateInterval);
    }


    $(document).ready(function () {
        initData();

        dataset = [        
            { label: "CPU", data: cpu, lines:{fill:true, lineWidth:1.2}, color: "#00FF00" },
            { label: "Disk:", data: disk, color: "#0044FF", bars: { show: true }, yaxis: 2 },
            { label: "CPU Core", data: cpuCore, lines: { lineWidth: 1.2}, color: "#FF0000" }
        ];

        $.plot($("#flot-placeholder1"), dataset, options);
        setTimeout(GetData, updateInterval);
    });


    </script>

    <!-- HTML -->
    <div style="width:900px;height:600px;text-align:center;margin:10px">        
        <div id="flot-placeholder1" style="width:100%;height:100%;margin:0 auto"></div>     
    </div>
</asp:Content>
Default.asp.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.DataVisualization.Charting;

public partial class _Default : Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Controls.Clear();

        Random rnd = new Random();

        int CPUloading = rnd.Next(0, 100);

        int CPUcore = CPUloading - 30 < 0 ? 0 : rnd.Next(0, CPUloading - 30);

        int Disk = rnd.Next(56, 1024);

        Response.Write("{\"cpu\":" + CPUloading + ", \"core\":" + CPUcore + ", \"disk\":" + Disk + "}");

    }
}

什么是响应。写{\cpu\:+CPUloading+,\core\:+CPUcore+,\disk\:+disk+};回来console.log\u更新函数中的数据并更新您的问题。使用flot时,如果绘图显示但没有数据点,通常意味着数据格式错误…感谢您的帮助。我刚刚意识到响应没有被调用,但它应该被调用,因为它在页面加载中,所以我将Default.aspx更改为使用asp标记而不是html。现在调用了Response.Write,但我在页面上看到的唯一输出是{cpu:16,core:0,disk:516},图形不再显示;如何使用ASP.NET webforms进行适当的AJAX调用?我不能回答那个问题。我唯一使用的.NET webstack是MVC。看看这个。实际上是马克提出的,而且效果很好。我还建议使用JavaScriptSerializer来转换数据。