0

我正在尝试使用谷歌服务在仪表和图表上显示一些数据。

好吧,我面临着一些麻烦

  1. 我想在仪表上显示实时值(​​一旦进入数据库)
  2. 并且仪表在指定的时间间隔后不会刷新。

我的 asp.net 文件代码是

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <script type='text/javascript' src='https://www.google.com/jsapi'></script>
    <h2>
        Welcome to ASP.NET!
    </h2>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <div>
                <asp:Literal ID="lt" runat="server"></asp:Literal>
            </div>
            <div id='chart_div'>
            </div>
            <asp:Timer ID="Timer" Interval="10000" runat="server" ontick="Timer_Tick" >
            </asp:Timer>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="Timer" EventName="Tick" />
        </Triggers>
    </asp:UpdatePanel>
</asp:Content>

C# 内容是

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        // here i am taking a object of StringBuilder class named str  
        StringBuilder str = new StringBuilder();
        SqlConnection con = new SqlConnection("Data Source=PC-Name\\SQLEXPRESS;Initial Catalog=MCAS;Integrated Security=SSPI");

        protected void Page_Load(object sender, EventArgs e)
        {
            con.Open();
            //if (Page.IsPostBack == false)
            {
                // here i am calling function  chart_bind(); in the page load event of the page
                bind_chart();
            }
            con.Close();
        }

        private void bind_chart()
        {
            // here i am using SqlDataAdapter for the sql server select query
            SqlDataAdapter adp = new SqlDataAdapter("select top(10) * from Test", con);
            // here am taking datatable
            DataTable dt = new DataTable();
            try
            {
                // here datatale dt is fill wit the adp
                adp.Fill(dt);
                // this string m catching in the stringbuilder class
                // in the str m writing same javascript code that is given by the google.
                // my data that will come from the sql server
                // below code is same like as google's javascript code
                str.Append(@" <script type='text/javascript'>
                        google.load('visualization', '1', {packages:['gauge']});
                        google.setOnLoadCallback(drawChart);
                        function drawChart() {
                        var data = new google.visualization.DataTable();
                        data.addColumn('string', 'Label');
                        data.addColumn('number', 'Value');");
                // inside the below line dt.Rows.Count explain that
                // how many rows comes in dt or total rows
                str.Append("data.addRows(" + dt.Rows.Count + ");");

                Int32 i;
                for (i = 0; i <= dt.Rows.Count - 1; i++)
                {
                    // i need this type of output "  data.setValue(0, 0, 'Memory'); so on  in the first line so for this
                    //m using i for the 0,1& 2 and so on . and after this i am writting zero and after this student_name using datatable
                    str.Append("data.setValue( " + i + "," + 0 + "," + "'" + dt.Rows[i]["x"].ToString() + "');");
                    // i need this type of output "   data.setValue(0, 1, 80);
                    //so on  in the first line so for this
                    //m using i for the 0,1& 2 and so on . and after this i am writting zero and after this percentage using datatable
                    str.Append("data.setValue(" + i + "," + 1 + "," + dt.Rows[i]["y"].ToString() + ") ;");
                    //   str.Append("['" + (dt.Rows[i]["student_name"].ToString()) + "'," + dt.Rows[i]["average_marks"].ToString() + "],");
                }

                str.Append("var chart = new google.visualization.Gauge(document.getElementById('chart_div'));");
                // in the below line i am setting the height and width of the Gauge using your own requrirement
                str.Append(" var options = {width: 800, height: 300, redFrom: 90, redTo: 100,");
                //  str.Append(" var chart = new google.visualization.BarChart(document.getElementById('chart_div'));");
                str.Append("yellowFrom: 75, yellowTo: 90, minorTicks: 5};");
                str.Append("chart.draw(data, options);}");
                str.Append("</script>");
                lt.Text = str.ToString().TrimEnd(',');
            }
            catch
            {

            }
            finally
            {

            }
        }

        protected void Timer_Tick(object sender, EventArgs e)
        {
            bind_chart();
        }
    }
}

Web 配置文件是

   ?xml version="1.0"?>
    <!--
      For more information on how to configure your ASP.NET application, please visit
      http://go.microsoft.com/fwlink/?LinkId=169433
      -->
    <configuration>
      <connectionStrings>
        <add name="ApplicationServices" connectionString="data source=PC-Name\\SQLEXPRESS;Initial Catalog=MCAS;Integrated Security=SSPI;User Instance=true" providerName="System.Data.SqlClient"/>
      </connectionStrings>
      <system.web>
        <compilation debug="true" targetFramework="4.0">
          <assemblies>
            <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
            <add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
          </assemblies>
        </compilation>
        <authentication mode="Forms">
          <forms loginUrl="~/Account/Login.aspx" timeout="2880"/>
        </authentication>
        <membership>
          <providers>
            <clear/>
            <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/>
          </providers>
        </membership>
        <profile>
          <providers>
            <clear/>
            <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
          </providers>
        </profile>
        <roleManager enabled="false">
          <providers>
            <clear/>
            <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/"/>
            <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/"/>
          </providers>
        </roleManager>
      </system.web>
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
      </system.webServer>
    </configuration>

我不知道为什么不采取时间滴答触发事件进行回发。

和要显示的实时值。

4

0 回答 0