学习了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].LabelToolTip="#VALX:#VALY"; //自定义图例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];}}效果:
新闻热点
疑难解答