首页 > 代码库 > Chart控件使用初步

Chart控件使用初步

学习了Chart控件的初步使用方法,生成柱形图和饼图。

   <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];        }    }
效果:

技术分享

Chart控件使用初步