C#利用Chart类别建立不含格线的直条图

依据预设,当程序建立一个 Chart ,X Y轴均会有网格线 。如下图:

 

\
 

但如果只要显示 X=5 跟 Y = 0 的这两条呢? 如下图 :

 

\
 

很简单,只要将X Y 轴的MajorGrid 的 LineWidth属性设定为0 就可以了
程序如下:

 

01.usingSystem;
02.usingSystem.Collections.Generic;
03.usingSystem.Linq;
04.usingSystem.Web;
05.usingSystem.Web.UI;
06.usingSystem.Web.UI.WebControls;
07.usingSystem.Web.UI.DataVisualization.Charting;
08.usingSystem.Drawing;
09.namespaceTestChart
10.{
11.publicpartial class index : System.Web.UI.Page
12.{
13.protectedvoid Page_Load(objectsender, EventArgs e)
14.{
15.}
16. 
17.privatevoid CreateChart()
18.{
19.using(Chart chart = new Chart())
20.{
21.ChartArea area = chart.ChartAreas.Add("chartArea");
22.area.Visible =true;
23. 
24.area.AxisX.MajorGrid.LineWidth = 0;
25.area.AxisX2.Enabled = AxisEnabled.False;
26. 
27.area.AxisY.MajorGrid.LineWidth = 0;
28.area.AxisY2.Enabled = AxisEnabled.False;
29. 
30.Series series = chart.Series.Add("firstSeries");
31.series.ChartType = SeriesChartType.Column;
32. 
33.Random r =new Random(Guid.NewGuid().GetHashCode());
34. 
35.for(int i = 5; i < 50; i += 5)
36.{
37.series.Points.AddXY(i, r.Next(5, 30));
38.}
39. 
40.series.XAxisType = AxisType.Primary;
41.series.YAxisType = AxisType.Primary;
42.series.Color = Color.Cyan;
43.series.ChartArea = area.Name;
44.series.Enabled =true;
45. 
46.stringexportFileName = Server.MapPath(".") +"/testImage.jpg";
47.chart.SaveImage(exportFileName, ChartImageFormat.Jpeg);
48.this.ClientScript.RegisterStartupScript(this.GetType(),"exportChart", string.Format("alert('已成功汇出至:{0}');", exportFileName.Replace('\\','/')),true);
49.}
50.}
51. 
52.protectedvoid Button1_Click(objectsender, EventArgs e)
53.{
54.CreateChart();
55.}
56.}
57.}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值