依据预设,当程序建立一个 Chart ,X Y轴均会有网格线 。如下图:
但如果只要显示 X=5 跟 Y = 0 的这两条呢? 如下图 :
很简单,只要将X Y 轴的MajorGrid 的 LineWidth属性设定为0 就可以了
程序如下:
01.
using
System;
02.
using
System.Collections.Generic;
03.
using
System.Linq;
04.
using
System.Web;
05.
using
System.Web.UI;
06.
using
System.Web.UI.WebControls;
07.
using
System.Web.UI.DataVisualization.Charting;
08.
using
System.Drawing;
09.
namespace
TestChart
10.
{
11.
public
partial
class
index : System.Web.UI.Page
12.
{
13.
protected
void
Page_Load(
object
sender, EventArgs e)
14.
{
15.
}
16.
17.
private
void
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.
string
exportFileName = 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.
protected
void
Button1_Click(
object
sender, EventArgs e)
53.
{
54.
CreateChart();
55.
}
56.
}
57.
}