Chart控件的用法

最近用到統計方面的功能,文檔統計不用說,都是導出Excel,若要視圖效果,這裏推薦一個Asp.NET中的控件Char。dom

簡單示例:ide

 1 <asp:Chart ID="Chart1" runat="server" Width="627px">
 2             <Legends>
 3                 <asp:Legend></asp:Legend>
 4             </Legends>
 5             <Titles>
 6                 <asp:Title Text="測試趨勢圖"></asp:Title>
 7             </Titles>
 8             <Series>
 9                 <asp:Series Name="Series1" LegendText="測試1" ChartType="Line" Label="#VAL" 
10                     MarkerStyle="Square" LabelToolTip="#VAL" YValuesPerPoint="6"></asp:Series>
11                 <asp:Series Name="" LegendText="測試2" ChartType="Line" Label="#VAL"
12                      MarkerStyle="Circle" LabelToolTip="#VAL" YValuesPerPoint="6"></asp:Series>
13             </Series> 
14             <ChartAreas>
15                 <asp:ChartArea Name="ChartArea1" ShadowOffset="4">
16                     <AxisY LineColor="64, 64, 64, 64" Title="評分分數" LineWidth="2">
17                         <LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" />
18                         <MajorGrid LineColor="64, 64, 64, 64" />
19                     </AxisY>
20                 </asp:ChartArea>
21             </ChartAreas>
22         </asp:Chart>
View Code

視圖顯示說明:學習

能夠設定Series的ChartType屬性值,來決定顯示什麼樣的視圖,下面是經常使用的視圖舉例:測試

後臺綁定代碼:spa

protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = default(DataTable);
            dt = CreateDataTable();
 
            //設置圖表的數據源
            Chart1.DataSource = dt;
 
            //設置圖表Y軸對應項
            Chart1.Series[0].YValueMembers = "Volume1";
            Chart1.Series[1].YValueMembers = "Volume2";
 
            //設置圖表X軸對應項
            Chart1.Series[0].XValueMember = "Date";
             
            //綁定數據
            Chart1.DataBind();
        }
        private DataTable CreateDataTable()
        {
            //Create a DataTable as the data source of the Chart control
            DataTable dt = new DataTable();
 
            //Add three columns to the DataTable
            dt.Columns.Add("Date");
            dt.Columns.Add("Volume1");
            dt.Columns.Add("Volume2");
 
            DataRow dr;
 
            //Add rows to the table which contains some random data for demonstration
            dr = dt.NewRow();
            dr["Date"] = "Jan";
            dr["Volume1"] = 3731;
            dr["Volume2"] = 4101;
            dt.Rows.Add(dr);
 
            dr = dt.NewRow();
            dr["Date"] = "Feb";
            dr["Volume1"] = 6024;
            dr["Volume2"] = 4324;
            dt.Rows.Add(dr);
 
            dr = dt.NewRow();
            dr["Date"] = "Mar";
            dr["Volume1"] = 4935;
            dr["Volume2"] = 2935;
            dt.Rows.Add(dr);
 
            dr = dt.NewRow();
            dr["Date"] = "Apr";
            dr["Volume1"] = 4466;
            dr["Volume2"] = 5644;
            dt.Rows.Add(dr);
 
            dr = dt.NewRow();
            dr["Date"] = "May";
            dr["Volume1"] = 5117;
            dr["Volume2"] = 5671;
            dt.Rows.Add(dr);
 
            dr = dt.NewRow();
            dr["Date"] = "Jun";
            dr["Volume1"] = 3546;
            dr["Volume2"] = 4646;
            dt.Rows.Add(dr);
 
            return dt;
        }

  顯示結果以柱狀圖爲例,以下:3d

這是本人初次使用Char控件的學習筆記,不喜勿噴,謝謝!!code

相關文章
相關標籤/搜索