學習了Chart控件的初步使用方法,生成柱形圖和餅圖。sql
<asp:Chart ID="Chart1" runat="server" Width="553px" Height="403px"> <Series> <asp:Series LegendText="得票數" Name="Series1" IsValueShownAsLabel="True" XValueMember="選擇項" YValueMembers="數量" Legend="Legend1" Color="Red" Font="Microsoft Sans Serif, 12pt" LabelForeColor="Green"> </asp:Series> <asp:Series ChartArea="ChartArea2" ChartType="Pie" Legend="Legend2" Name="Series2" IsValueShownAsLabel="True" XValueMember="選擇項" YValueMembers="數量" XValueType="String"> </asp:Series> </Series> <ChartAreas> <asp:ChartArea Name="ChartArea1"> <AxisY Title="得票數"> <LabelStyle ForeColor="Blue" /> </AxisY> <AxisX Title="課程"> <MajorGrid LineColor="Yellow" /> <%--線條顏色--%> </AxisX> </asp:ChartArea> <asp:ChartArea Name="ChartArea2"> <AxisY Title="得票數"> <LabelStyle ForeColor="Blue" /> </AxisY> <AxisX Title="課程"> <MajorGrid LineColor="Yellow" /> <%--線條顏色--%> </AxisX> </asp:ChartArea> </ChartAreas> <Legends> <asp:Legend Name="Legend1"> </asp:Legend> <asp:Legend Name="Legend2"> <Position X="80" Y="50" Height="20" Width="30" /> </asp:Legend> </Legends> <Titles> <asp:Title Name="Title1" Text="投票結果"> </asp:Title> </Titles> </asp:Chart>
後臺代碼:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Chart1.DataSource = GetTable();
//#XVAL表示數據點的X值,#YVAL表示數據點的Y值 Chart1.Series[1].Label = "課程:#VALX,得票數:#VALY";
//顯示在餅圖外面 Chart1.Series[1].CustomProperties = "PieLabelStyle=Outside"; Chart1.Series[1].LabelToolTip = "#VALX: #VALY";
//自定義圖例 Chart1.Series[1].LegendText = "#VALX"; } } public DataTable GetTable() { string collectionstring = @"Data Source=.\sqlexpress;Initial Catalog=OnlineVoteDB;User ID=sa;Pwd=xx;Persist Security Info=True;"; SqlConnection conn = new SqlConnection(collectionstring); SqlDataAdapter sad = new SqlDataAdapter("select name as 選擇項,votecount as 數量 from [Items] where [SubjectID]=13 ", conn); DataSet ds = new DataSet(); sad.Fill(ds, "table"); conn.Close(); return ds.Tables[0]; } }
效果: