1
2
3 Code
4 using System;
5 using System.Data;
6 using System.Configuration;
7 using System.Web;
8 using System.Web.Security;
9 using System.Web.UI;
10 using System.Web.UI.WebControls;
11 using System.Web.UI.WebControls.WebParts;
12 using System.Web.UI.HtmlControls;
13 using System.Drawing.Imaging;
14 using System.Drawing;
15 /// <summary>
16 /// DrawingCurve 的摘要说明
17 /// </summary>
18 public class DrawingCurve
19 {
20
21 public int intXLong = 800; //图片大小 长
22 public int intYLong = 600; //图片大小 高
23 public int intXMultiple = 1; //零刻度的值 X
24 public int intYMultiple = 0; //零刻度的值 Y
25 public int intXMax = 12; //最大刻度(点数) X
26 public int intYMax = 30; //最大刻度(点数) Y
27
28 public int intLeft = 50; //左边距
29 public int intRight = 120; //右边距
30 public int intTop = 30; //上边距
31 public int intEnd = 50; //下边距
32
33 public string strXText = "时间(单位:月)"; //单位 X
34 public string strYText = "数量(单位:个)"; //单位 Y
35 public string strTitle = "趋势线图"; //标题
36 public DataTable tbData; //要统计的数据
37
38
39 private int intXScale = 30; //一刻度长度 X
40 private int intYScale = 30; //一刻度高度 Y
41 //private int intX = 0; //0点 X坐标
42 //private int intY = 0; //0点 Y坐标
43 public int intData = 0; //记录数
44
45 public DrawingCurve()
46 {
47
48 intXScale = (intXLong - intLeft - intRight) / (intXMax + 1);//一刻度长度 X
49 intYScale = (intYLong - intTop - intEnd) / (intYMax + 1);//一刻度高度 Y
50
51 //intX = intXLong - intLeft; //0点 X坐标
52 //intY = intYLong - intEnd; //0点 Y坐标
53 }
54
55 public Bitmap DrawingImg()
56 {
57
58 Bitmap img = new Bitmap(intXLong, intYLong); //图片大小
59 Graphics g = Graphics.FromImage(img);
60 g.Clear(Color.Snow);
61 g.DrawString(strTitle, new Font("宋体", 14), Brushes.Black, new Point(5, 5));
62 g.DrawLine(new Pen(Color.Black, 2), intLeft, intYLong - intEnd, intXLong - intRight, intYLong - intEnd); //绘制横向
63 g.DrawLine(new Pen(Color.Black, 2), intLeft, intTop, intLeft, intYLong - intEnd); //绘制纵向
64
65 //绘制纵坐标
66 g.DrawString(strYText, new Font("宋体", 12), Brushes.Black, new Point(intLeft, intTop));//Y 单位
67 Point p1 = new Point(intLeft - 10, intYLong - intEnd);
68 for (int j = 0; j <= intYMax; j++)
69 {
70 p1.Y = intYLong - intEnd - j * intYScale;
71 Point pt = new Point(p1.X + 10, p1.Y);
72 //绘制纵坐标的刻度和直线
73 g.DrawLine(Pens.Black, pt, new Point(p1.X + 15, p1.Y));
74 //绘制纵坐标的文字说明
75 g.DrawString(Convert.ToString(j + intYMultiple), new Font("宋体", 12), Brushes.Black, new Point(p1.X - 25, p1.Y - 8));
76 }
77
78 //绘制横坐标
79 g.DrawString(strXText, new Font("宋体", 12), Brushes.Black, new Point(intXLong - intRight, intYLong - intEnd));//X 单位
80 Point p = new Point(intLeft, intYLong - intEnd);
81 for (int i = 0; i < intXMax; i++)
82 {
83 p.X = intLeft + i * intXScale;
84 //绘制横坐标刻度和直线
85 g.DrawLine(Pens.Black, p, new Point(p.X, p.Y - 5));
86 //绘制横坐标的文字说明
87 g.DrawString(Convert.ToString(i + intXMultiple), new Font("宋体", 12), Brushes.Black, p);
88 }
89
90
91 intData = tbData.Rows.Count;
92 if (intData > 0)
93 {
94 //趋势线图
95 for (int i = 0; i < intData - 1; i++)
96 {
97 DataRow Row1 = tbData.Rows[i];
98 DataRow Row2 = tbData.Rows[i + 1];
99 //定义起点
100 Point rec = new Point(Convert.ToInt32(intLeft + ((TurnNumber(Row1[0].ToString()) - intXMultiple) * intXScale)), Convert.ToInt32(intYLong - intEnd - (TurnNumber(Row1[1].ToString()) - intYMultiple) * intYScale));
101 //定义终点
102 Point dec = new Point(Convert.ToInt32(intLeft + ((TurnNumber(Row2[0].ToString()) - intXMultiple) * intXScale)), Convert.ToInt32(intYLong - intEnd - (TurnNumber(Row2[1].ToString()) - intYMultiple) * intYScale));
103 //绘制趋势折线
104 g.DrawLine(new Pen(Color.Red), rec, dec);
105 }
106 }
107
108 return img;
109 }
110
111 //转换数字
112 private double TurnNumber(string str)
113 {
114 double dubReturn;
115 try
116 {
117 dubReturn = Convert.ToDouble(str);
118 }
119 catch
120 {
121 dubReturn = 0;
122 }
123 return dubReturn;
124
125 }
126
127 }
128
129 ----------------------------------调用------------------------------------------------
130
131 protected void Page_Load(object sender, EventArgs e)
132 {
133 DataTable MyTable = new DataTable();
134
135
136 string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("App_Data/db.mdb");
137 string cmdstr = "SELECT 月份,销量 FROM Result";
138 OleDbConnection con = new OleDbConnection(constr);
139 con.Open();
140 //OleDbCommand cmd = new OleDbCommand(cmdstr, con);
141 OleDbDataAdapter da = new OleDbDataAdapter(cmdstr, con);
142 da.Fill(MyTable);
143
144
145 DrawingCurve MyDc = new DrawingCurve();
146 MyDc.tbData = MyTable;
147
148 Bitmap img = new Bitmap(100, 100);
149 img = MyDc.DrawingImg();
150 Graphics g = Graphics.FromImage(MyDc.DrawingImg());
151
152 //显示图形
153 img.Save(Response.OutputStream, ImageFormat.Jpeg);;
154 g.Dispose();
155 Response.Write("<br>" + MyDc.intData.ToString());
156 }
157
158
159
160
161
2
3 Code
4 using System;
5 using System.Data;
6 using System.Configuration;
7 using System.Web;
8 using System.Web.Security;
9 using System.Web.UI;
10 using System.Web.UI.WebControls;
11 using System.Web.UI.WebControls.WebParts;
12 using System.Web.UI.HtmlControls;
13 using System.Drawing.Imaging;
14 using System.Drawing;
15 /// <summary>
16 /// DrawingCurve 的摘要说明
17 /// </summary>
18 public class DrawingCurve
19 {
20
21 public int intXLong = 800; //图片大小 长
22 public int intYLong = 600; //图片大小 高
23 public int intXMultiple = 1; //零刻度的值 X
24 public int intYMultiple = 0; //零刻度的值 Y
25 public int intXMax = 12; //最大刻度(点数) X
26 public int intYMax = 30; //最大刻度(点数) Y
27
28 public int intLeft = 50; //左边距
29 public int intRight = 120; //右边距
30 public int intTop = 30; //上边距
31 public int intEnd = 50; //下边距
32
33 public string strXText = "时间(单位:月)"; //单位 X
34 public string strYText = "数量(单位:个)"; //单位 Y
35 public string strTitle = "趋势线图"; //标题
36 public DataTable tbData; //要统计的数据
37
38
39 private int intXScale = 30; //一刻度长度 X
40 private int intYScale = 30; //一刻度高度 Y
41 //private int intX = 0; //0点 X坐标
42 //private int intY = 0; //0点 Y坐标
43 public int intData = 0; //记录数
44
45 public DrawingCurve()
46 {
47
48 intXScale = (intXLong - intLeft - intRight) / (intXMax + 1);//一刻度长度 X
49 intYScale = (intYLong - intTop - intEnd) / (intYMax + 1);//一刻度高度 Y
50
51 //intX = intXLong - intLeft; //0点 X坐标
52 //intY = intYLong - intEnd; //0点 Y坐标
53 }
54
55 public Bitmap DrawingImg()
56 {
57
58 Bitmap img = new Bitmap(intXLong, intYLong); //图片大小
59 Graphics g = Graphics.FromImage(img);
60 g.Clear(Color.Snow);
61 g.DrawString(strTitle, new Font("宋体", 14), Brushes.Black, new Point(5, 5));
62 g.DrawLine(new Pen(Color.Black, 2), intLeft, intYLong - intEnd, intXLong - intRight, intYLong - intEnd); //绘制横向
63 g.DrawLine(new Pen(Color.Black, 2), intLeft, intTop, intLeft, intYLong - intEnd); //绘制纵向
64
65 //绘制纵坐标
66 g.DrawString(strYText, new Font("宋体", 12), Brushes.Black, new Point(intLeft, intTop));//Y 单位
67 Point p1 = new Point(intLeft - 10, intYLong - intEnd);
68 for (int j = 0; j <= intYMax; j++)
69 {
70 p1.Y = intYLong - intEnd - j * intYScale;
71 Point pt = new Point(p1.X + 10, p1.Y);
72 //绘制纵坐标的刻度和直线
73 g.DrawLine(Pens.Black, pt, new Point(p1.X + 15, p1.Y));
74 //绘制纵坐标的文字说明
75 g.DrawString(Convert.ToString(j + intYMultiple), new Font("宋体", 12), Brushes.Black, new Point(p1.X - 25, p1.Y - 8));
76 }
77
78 //绘制横坐标
79 g.DrawString(strXText, new Font("宋体", 12), Brushes.Black, new Point(intXLong - intRight, intYLong - intEnd));//X 单位
80 Point p = new Point(intLeft, intYLong - intEnd);
81 for (int i = 0; i < intXMax; i++)
82 {
83 p.X = intLeft + i * intXScale;
84 //绘制横坐标刻度和直线
85 g.DrawLine(Pens.Black, p, new Point(p.X, p.Y - 5));
86 //绘制横坐标的文字说明
87 g.DrawString(Convert.ToString(i + intXMultiple), new Font("宋体", 12), Brushes.Black, p);
88 }
89
90
91 intData = tbData.Rows.Count;
92 if (intData > 0)
93 {
94 //趋势线图
95 for (int i = 0; i < intData - 1; i++)
96 {
97 DataRow Row1 = tbData.Rows[i];
98 DataRow Row2 = tbData.Rows[i + 1];
99 //定义起点
100 Point rec = new Point(Convert.ToInt32(intLeft + ((TurnNumber(Row1[0].ToString()) - intXMultiple) * intXScale)), Convert.ToInt32(intYLong - intEnd - (TurnNumber(Row1[1].ToString()) - intYMultiple) * intYScale));
101 //定义终点
102 Point dec = new Point(Convert.ToInt32(intLeft + ((TurnNumber(Row2[0].ToString()) - intXMultiple) * intXScale)), Convert.ToInt32(intYLong - intEnd - (TurnNumber(Row2[1].ToString()) - intYMultiple) * intYScale));
103 //绘制趋势折线
104 g.DrawLine(new Pen(Color.Red), rec, dec);
105 }
106 }
107
108 return img;
109 }
110
111 //转换数字
112 private double TurnNumber(string str)
113 {
114 double dubReturn;
115 try
116 {
117 dubReturn = Convert.ToDouble(str);
118 }
119 catch
120 {
121 dubReturn = 0;
122 }
123 return dubReturn;
124
125 }
126
127 }
128
129 ----------------------------------调用------------------------------------------------
130
131 protected void Page_Load(object sender, EventArgs e)
132 {
133 DataTable MyTable = new DataTable();
134
135
136 string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("App_Data/db.mdb");
137 string cmdstr = "SELECT 月份,销量 FROM Result";
138 OleDbConnection con = new OleDbConnection(constr);
139 con.Open();
140 //OleDbCommand cmd = new OleDbCommand(cmdstr, con);
141 OleDbDataAdapter da = new OleDbDataAdapter(cmdstr, con);
142 da.Fill(MyTable);
143
144
145 DrawingCurve MyDc = new DrawingCurve();
146 MyDc.tbData = MyTable;
147
148 Bitmap img = new Bitmap(100, 100);
149 img = MyDc.DrawingImg();
150 Graphics g = Graphics.FromImage(MyDc.DrawingImg());
151
152 //显示图形
153 img.Save(Response.OutputStream, ImageFormat.Jpeg);;
154 g.Dispose();
155 Response.Write("<br>" + MyDc.intData.ToString());
156 }
157
158
159
160
161
浙公网安备 33010602011771号