关于ASP.net Chart- Column图改变Points上标Label文字样式

来源:互联网 发布:网络主播签约协议 编辑:程序博客网 时间:2024/05/16 04:39

首先一样先拉入一张Chart 并且设定为Column图

 

\
 

设定一些Series 其中我建立的范例LegendText 为 能力、智力、速度三种

 

\
 
其中的Label 为  #VAL
 
\
 
看一下ASPX Code :

view sourceprint?
01.<asp:Chart ID="Chart1" runat="server">
02.<Legends>
03.<asp:Legend Alignment="Center" Docking="Bottom" Name="Legend1">
04.</asp:Legend>
05.</Legends>
06.<Series>
07.<asp:Series Name="Series1" LegendText="能力" Label="#VAL"></asp:Series>
08.<asp:Series ChartArea="ChartArea1" LegendText="智力" Name="Series2"  Label="#VAL">
09.</asp:Series>
10.<asp:Series ChartArea="ChartArea1" LegendText="速度" Name="Series3"  Label="#VAL">
11.</asp:Series>
12.</Series>
13.<ChartAreas>
14.<asp:ChartArea Name="ChartArea1"></asp:ChartArea>
15.</ChartAreas>
16.</asp:Chart>

再来就是C# Code 这边导入数据

C#:

 

view sourceprint?
01.DataTable dt = new DataTable();
02. 
03.dt.Columns.Add("英雄名称");
04. 
05.dt.Columns.Add("能力");
06.dt.Columns.Add("智力");
07.dt.Columns.Add("速度");
08.Chart1.Series[0].XValueMember = "英雄名称";
09.Chart1.Series[0].YValueMembers = "能力";
10.Chart1.Series[1].YValueMembers = "智力";
11.Chart1.Series[2].YValueMembers = "速度";
12. 
13.dt.Rows.Add(new object[] { "浩克", 5, 5, 0 });
14.dt.Rows.Add(new object[] { "钢铁人", 5, 5, 2 });
15.dt.Rows.Add(new object[] { "美国队长", 5, 1, 4 });
16.Chart1.DataSource = dt.DefaultView;
17. 
18. 
19.Chart1.DataBind();

这时候来看一下结果..

 

\
 

这时候老板需求是 因为高分的5 希望是红色显示其上标数字 , 并且 0 分的就是不要出现其上标数字

查了一下做法,因为这是必须独立去设定每一个分数上面的上标文字

所以在 Databind 之后加上这段Code.

C#:

 

view sourceprint?
01.foreach (var series in Chart1.Series)
02.{
03.foreach (var point in series.Points)
04.{
05.if (point.YValues[0] == 0)
06.{
07.point.Label = string.Empty;
08.}
09.else if (point.YValues[0] >= 5)
10.{
11.point.LabelForeColor = Color.Red;
12.}
13. 
14.}
15.}

结果:

 

\
 

一些很简单的小技巧,给需要用到的人自己也笔记一下

0 0
原创粉丝点击