ASP.NET中的chart控件綁定SQL Server數據庫

網上不少的chart控件的實例都沒有綁定數據庫,通過一番摸索後,終於實現了chart控件綁定數據庫。html

首先,在Visual Studio中創建一個網站,新建一個WebForm項目,名稱爲ChartTest1.aspxsql

前臺:數據庫

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ChartTest1.aspx.cs" Inherits="ChartTest1" %>

<%@ Register Assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       
    <asp:Chart ID="Chart1" runat="server">
    <Titles>
                <asp:Title Font="微軟雅黑, 16pt" Name="Title1" Text="****統計表">
                </asp:Title>
            </Titles>
            <borderskin skinstyle="Emboss"></borderskin>

        <Series>
            <asp:Series Name="Series1"  ChartType="Bubble" MarkerSize="8" MarkerStyle="Circle">
                </asp:Series>

        </Series>
       <ChartAreas>
                <asp:ChartArea Name="ChartArea1"  BorderColor="64, 64, 64, 64" BackSecondaryColor="Transparent" BackColor="64, 165, 191, 228" ShadowColor="Transparent" BackGradientStyle="TopBottom">
                </asp:ChartArea>
            </ChartAreas>

    </asp:Chart>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    </div>
    
    </form>
</body>
</html>

後臺代碼:網站

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.OleDb;

public partial class ChartTest1 : System.Web.UI.Page
{
    public static string conn = "Data Source=(local);Integrated Security=True;Initial Catalog=HESDK1";
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    //建立一張二維數據表
        /// <summary>
        /// 建立一張二維數據表
        /// </summary>
        /// <returns>Datatable類型的數據表</returns>
        DataTable CreatData()
        {
            DataTable dt = new DataTable();
            //實例化SqlConnection對象
            SqlConnection sqlCon = new SqlConnection();
            //實例化SqlConnection對象鏈接數據庫的字符串
            sqlCon.ConnectionString = conn;
            //定義SQL語句
            //sql公用部分
            string sqlQ = "select TO_c,Plc_c from History ";
           // DataTable tb = ADO.GetDataTable(sqlQ);
            dt = GetDataTable(sqlQ);
            
            return dt;
        }
        //數據庫查詢返回datatable
        public static DataTable GetDataTable(string Sqlstr)
        {
            DataTable dt;
            SqlDataAdapter sda;
            SqlConnection sqlCon = new SqlConnection();
            //實例化SqlConnection對象鏈接數據庫的字符串
            sqlCon.ConnectionString = conn;
            sqlCon.Open();
            sda = new SqlDataAdapter(Sqlstr, conn);
            //sda.SelectCommand.CommandTimeout = 120;
            dt = new DataTable();
            sda.Fill(dt);
            sqlCon.Close();
            return dt;
        }
       
       protected void Button1_Click(object sender, EventArgs e)
       {
         
           SqlConnection connection = new SqlConnection(conn);
           // 折線圖
           DataTable dt = CreatData();

           // #region 折線圖
           Chart1.DataSource = dt;//綁定數據
           Chart1.Series["Series1"].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Line;//設置圖表類型
           Chart1.Series[0].XValueMember = "TO_c";//X軸數據成員列
           Chart1.Series[0].YValueMembers = "Plc_c";//Y軸數據成員列
           Chart1.ChartAreas["ChartArea1"].AxisX.Title = "溫度";//X軸標題
           Chart1.ChartAreas["ChartArea1"].AxisX.TitleAlignment = StringAlignment.Far;//設置Y軸標題的名稱所在位置位遠
           Chart1.ChartAreas["ChartArea1"].AxisY.Title = "壓力";//Y軸標題
           Chart1.ChartAreas["ChartArea1"].AxisY.TitleAlignment = StringAlignment.Far;//設置Y軸標題的名稱所在位置位遠
           Chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;//X軸數據的間距
           Chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.Enabled = false;//不顯示豎着的分割線
           Chart1.Series[0].IsValueShownAsLabel = true;//顯示座標值
           // #endregion
          
          /*
           Chart1.Series["Series1"].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Line;//設置圖表類型
           string mySelectQuery = "SELECT TO_c,Plc_c FROM History;";
           SqlConnection connection = new SqlConnection(conn);
           SqlCommand myCommand = new SqlCommand(mySelectQuery, connection);
           myCommand.Connection.Open();
           SqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
           Chart1.DataBindTable(myReader, "TO_c");
           myReader.Close();
           myCommand.Connection.Close();
           * */
       }
}
相關文章
相關標籤/搜索