學習了Chart控件的初步使用方法,生成柱形圖和餅圖。
<asp:ChartID="Chart1"runat="server"Width="553px"Height="403px"><Series><asp:SeriesLegendText="得票數"Name="Series1"IsValueShownAsLabel="True"XValueMember="選擇項"YValueMembers="數量"Legend="Legend1"Color="Red"Font="MicrosoftSansSerif,12pt"LabelForeColor="Green"></asp:Series><asp:SeriesChartArea="ChartArea2"ChartType="Pie"Legend="Legend2"Name="Series2"IsValueShownAsLabel="True"XValueMember="選擇項"YValueMembers="數量"XValueType="String"></asp:Series></Series><ChartAreas><asp:ChartAreaName="ChartArea1"><AxisYTitle="得票數"><LabelStyleForeColor="Blue"/></AxisY><AxisXTitle="課程"><MajorGridLineColor="Yellow"/><%--線條顏色--%></AxisX></asp:ChartArea><asp:ChartAreaName="ChartArea2"><AxisYTitle="得票數"><LabelStyleForeColor="Blue"/></AxisY><AxisXTitle="課程"><MajorGridLineColor="Yellow"/><%--線條顏色--%></AxisX></asp:ChartArea></ChartAreas><Legends><asp:LegendName="Legend1"></asp:Legend><asp:LegendName="Legend2"><PositionX="80"Y="50"Height="20"Width="30"/></asp:Legend></Legends><Titles><asp:TitleName="Title1"Text="投票結果"></asp:Title></Titles></asp:Chart>后臺代碼:
PRotectedvoidPage_Load(objectsender,EventArgse){if(!IsPostBack){ Chart1.DataSource=GetTable(); //#XVAL表示數據點的X值,#YVAL表示數據點的Y值Chart1.Series[1].Label="課程:#VALX,得票數:#VALY"; //顯示在餅圖外面Chart1.Series[1].CustomProperties="PieLabelStyle=Outside";Chart1.Series[1].LabelToolT//自定義圖例Chart1.Series[1].LegendText="#VALX";}}publicDataTableGetTable(){stringcollectionstring=@"DataSource=./sqlexpress;InitialCatalog=OnlineVoteDB;UserID=sa;Pwd=xx;PersistSecurityInfo=True;";SqlConnectionconn=newSqlConnection(collectionstring);SqlDataAdaptersad=newSqlDataAdapter("selectnameas選擇項,votecountas數量from[Items]where[SubjectID]=13",conn);DataSetds=newDataSet();sad.Fill(ds,"table");conn.Close();returnds.Tables[0];}}效果:
新聞熱點
疑難解答