asp.net使用chart控件简单制作柱状体、饼图总结

本文介绍了如何使用ASP.NET图表控件创建柱形图、拆线图和饼图,包括详细HTML代码和后台实现方式。通过调整样式和配置参数,轻松实现数据可视化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.jq-school.com/Show.aspx?id=311

本文章由jquery学堂2群的网友【北京-web-小虾米】整理分享,非常感谢!

效果图:

ASP.Net chart控件简单制作柱状体、饼图总结

ASP.Net chart控件简单制作柱状体、饼图总结ASP.Net chart控件简单制作柱状体、饼图总结


1、柱形图:

html代码如下 :


01 <asp:Chart ID="Chart1" runat="server" ImageType="Jpeg" ChartDashStyle="solid"
02 Height="320px" style="margin-bottom: 0px" Width="614px">
03 <Legends>
04  <asp:Legend Name="Legend1" >
05   </asp:Legend>
06   </Legends>
07   <Series>   
08   </Series>
09   <ChartAreas>
10 <asp:ChartArea Name="ChartArea1">
11 <AxisX>
12     <MajorTickMark Enabled="False" LineColor="White" LineDashStyle="NotSet" />
13     <MajorGrid Enabled="false" />
14     <MinorTickMark LineColor="White" />
15 </AxisX>
16 <AxisY>
17     <MajorTickMark LineColor="White" />
18     <MajorGrid Enabled="False" />
19     <MinorTickMark LineColor="White" />
20 </AxisY>
21 </asp:ChartArea>
22 </ChartAreas>
23 <Titles>
24     <asp:Title Name="Title1">
25     </asp:Title>
26 </Titles>
27 </asp:Chart>
后台代码如下:



01 Chart1.Height = 400;
02 Chart1.Width =1200;
03 //title属性说明
04 //边框样式设置
05 Chart1.ChartAreas["ChartArea1"].BorderColor = Color.Black;
06 Chart1.ChartAreas["ChartArea1"].BorderWidth = 2;
07 Chart1.ChartAreas[0].AxisX.Interval = 1;
08 Chart1.ChartAreas[0].AxisX.IntervalOffset = 1;
09 Chart1.ChartAreas[0].AxisX.LabelStyle.IsStaggered = true;
10  
11 Chart1.Series.Add("实际值");
12 Chart1.Series["实际值"]["PixelPointWidth"] = "20";
13 Chart1.Series["实际值"].Points.DataBindXY(list1, list2); //添加数据源,X、Y轴(结合这里是动态数组)
14 for (int i = 0; i < list1.Count; i++)
15 {
16     Chart1.Series["实际值"].Points[i].Label = list2[i].ToString();//柱状图顶部添加说明数据
17 }
18  
19 //设置图例说明
20 Chart1.ChartAreas["ChartArea1"].AxisY.TitleFont = new Font("微软雅黑", float.Parse("8"), FontStyle.Regular);
21 Chart1.ChartAreas["ChartArea1"].AxisY.TitleForeColor = Color.FromName("Black");
22 Chart1.ChartAreas["ChartArea1"].AxisY.Title = "单位;" "万千瓦时";
23 Chart1.ChartAreas["ChartArea1"].AxisX.TitleFont = new Font("微软雅黑", float.Parse("8"), FontStyle.Regular);
24 Chart1.ChartAreas["ChartArea1"].AxisX.TitleForeColor = Color.FromName("Black");
25 Chart1.ChartAreas["ChartArea1"].AxisX.Title = date + "发电量";


2、拆线图:

只需要增加Series以及Series的图标类型(Series.ChartType = SeriesChartType.Line)和数据源即可。


3、饼图:

html代码如下:


01 <asp:Chart ID="Chart2" runat="server" ImageType="Jpeg" ChartDashStyle="solid"
02 Height="322px" Width="440px" onclick="Chart2_Click">
03     <Legends>
04         <asp:Legend Name="Legend1" >
05         </asp:Legend>
06     </Legends>
07     <Series>
08     </Series>
09     <ChartAreas>
10         <asp:ChartArea Name="ChartArea1">
11         </asp:ChartArea>
12     </ChartAreas>
13     <Titles>
14         <asp:Title Name="Title1">
15         </asp:Title>
16     </Titles>
17 </asp:Chart>
后台代码如下:



01 /// <summary>
02 /// 2013年5月22日 修改 并添加说明
03 /// ToolTip:鼠标放在图标上显示数据(#VALX:指标名称,#VALY指标值)
04 /// LegendToolTip:鼠标放在图例上显示数据(#PERCENT:百分比)
05 /// PostBackValue:返回值(#INDEX:索引值)
06 /// LegendPostBackValue:图例返回值
07 /// LegendText:图例值
08 /// Label:饼图值
09 /// </summary>
10 string createdate = StringHelper.GetCurrentDate("yyyy-MM-dd");
11 string date = StringHelper.AddDate("d", -1, createdate, "yyyy-MM-dd");      
12 //string date = "2013-01-08";
13 Series series = Chart2.Series.Add("My Series");
14 series.ToolTip = "#VALX:#VALY 万千瓦时";
15 series.LegendToolTip = "#PERCENT";
16 series.PostBackValue = "#INDEX";
17 series.LegendPostBackValue = "#INDEX";
18 series.LegendText= "#VALX";
19 series.Label = "#VALX[#PERCENT]";
20 series.Points.DataBindXY(list1, list2);
21 Chart2.Series[0]["PieLabelStyle"] = "Outside";//饼图说明显示方式(外面)
22 series.ChartType = SeriesChartType.Pie; //图标的显示风格(饼图)
23 series.ShadowOffset = 2;
24 series.BorderColor = Color.DarkGray;
25 Chart2.Width = 880;
26 Chart2.Height = 400;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值