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

来源:互联网 发布:淘宝茶叶店铺装修 编辑:程序博客网 时间:2024/05/19 18:17

依据预设,当程序建立一个 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.publicpartialclass index : System.Web.UI.Page
12.{
13.protectedvoidPage_Load(objectsender, EventArgs e)
14.{
15.}
16. 
17.privatevoidCreateChart()
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 =newRandom(Guid.NewGuid().GetHashCode());
34. 
35.for(inti = 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.protectedvoidButton1_Click(objectsender, EventArgs e)
53.{
54.CreateChart();
55.}
56.}
57.}

原创粉丝点击