16,721
社区成员




Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
createBG() '画饼图
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
createZG() '画柱状图
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
createZXG() '画折线图
End Sub
Sub createBG() '绘制饼图
Dim height As Integer = 200
Dim width As Integer = 200
Dim bmp As New Bitmap(width, height)
Dim g As Graphics = Graphics.FromImage(bmp)
Dim pen As Pen = New Pen(Color.Black)
Dim outline As Rectangle = New Rectangle(0, 0, height - 5, width - 5)
g.SmoothingMode = SmoothingMode.AntiAlias
g.Clear(Color.White)
'绘制饼图
'绘制一个由边框(该边框由一对坐标、高度和宽度指定)定义的椭圆
g.DrawEllipse(pen, outline)
'填充由一对坐标、一个宽度、一个高度以及两条射线指定的椭圆所定义的扇形区的内部
'关键在第二,三个参数:从x轴到扇形边1的度数;从扇形边1到边2的读书,下面绘制了各占90度的图
'g.FillPie(New SolidBrush(Color.Red), outline, -20.0F, 90.0F)
'g.FillPie(New SolidBrush(Color.Yellow), outline, 70.0F, 150.0F)
'g.FillPie(New SolidBrush(Color.Blue), outline, 220.0F, 100.0F)
'g.FillPie(New SolidBrush(Color.Green), outline, 320.0F, 20.0F)
g.FillPie(New SolidBrush(Color.Red), outline, 0.0F, 90.0F)
g.FillPie(New SolidBrush(Color.Yellow), outline, 90.0F, 90.0F)
g.FillPie(New SolidBrush(Color.Blue), outline, 180.0F, 90.0F)
g.FillPie(New SolidBrush(Color.Green), outline, 270.0F, 90.0F)
bmp.Save(Page.Response.OutputStream, ImageFormat.Jpeg)
g.Dispose()
bmp.Dispose()
End Sub
Sub createZG()
Dim height As Integer = 200
Dim width As Integer = 200
Dim space As Integer = 20 '原点到左边和下边的距离
Dim interval As Integer = 20 '单位长度
Dim max_x As Integer = 8 'x轴最大刻度
Dim max_y As Integer = 8
Dim bmp As New Bitmap(width, height)
Dim g As Graphics = Graphics.FromImage(bmp)
Dim pen As Pen = New Pen(Color.Black, 1)
Dim pen_chart As Pen = New Pen(Color.Blue, 12)
'定义一组数据
Dim arrData() As Double = {5.21, 3.34, 2.5, 7.65, 6.54, 1.2, 2.32, 6.48}
Dim i As Integer
g.SmoothingMode = SmoothingMode.AntiAlias
g.Clear(Color.White)
'g.DrawLine(pen,point1,point2)
'Pen 对象,它确定线条的颜色、宽度和样式。
'Point 结构,它表示要连接的第一个点。
'Point 结构,它表示要连接的第二个点。
g.DrawLine(pen, New Point(space, height - space), New Point(width, height - space)) 'x轴
g.DrawLine(pen, New Point(space, 0), New Point(space, height - space)) 'y轴
'x轴上的刻度
For i = 0 To width - interval Step interval
If i <= max_x * interval Then
'g.DrawString(string,Font,Brush,PointF)
'String对象,要绘制的字符串。
'Font 对象,它定义字符串的文本格式。
'Brush 对象,它确定所绘制文本的颜色和纹理。
'PointF 结构,它指定所绘制文本的左上角。
'可以单独先定义变量drawString, drawFont, drawBrush, drawPoint
g.DrawString(i / interval, New Font("Arail", 9, FontStyle.Regular), SystemBrushes.WindowText, New PointF(space + i - 5, height - space))
End If
Next
'y轴上的刻度
For i = 0 To height - interval Step interval
If i <= max_y * interval Then
g.DrawLine(pen, New Point(space, height - i - space), New Point(space + 5, height - i - space))
If i <> 0 Then
g.DrawString(i / interval, New Font("Arial", 9, FontStyle.Regular), SystemBrushes.WindowText, New PointF(space - 12, height - space - i - 6))
End If
End If
Next
'柱形图
For i = 0 To UBound(arrData)
g.DrawLine(pen_chart, New Point(space + (i + 1) * interval, height - space - arrData(i) * interval), New Point(space + (i + 1) * interval, height - space))
Next
'输出结果
bmp.Save(Response.OutputStream, ImageFormat.Jpeg)
g.Dispose()
bmp.Dispose()
End Sub
Sub createZXG()
Dim height As Integer = 200
Dim width As Integer = 200
Dim space As Integer = 20 '原点到左边和下边的距离
Dim interval As Integer = 20 '单位长度
Dim max_x As Integer = 8 'x轴最大刻度
Dim max_y As Integer = 8
Dim bmp As New Bitmap(width, height)
Dim g As Graphics = Graphics.FromImage(bmp)
Dim pen As Pen = New Pen(Color.Black, 1)
Dim pen_chart As Pen = New Pen(Color.Blue, 2) '在此处设置折线宽度和颜色
'定义一组数据
Dim arrData() As Double = {5.21, 3.34, 2.5, 7.65, 6.54, 1.2, 2.32, 6.48}
Dim i As Integer
g.SmoothingMode = SmoothingMode.AntiAlias
g.Clear(Color.White)
'g.DrawLine(pen,point1,point2)
'Pen 对象,它确定线条的颜色、宽度和样式。
'Point 结构,它表示要连接的第一个点。
'Point 结构,它表示要连接的第二个点。
g.DrawLine(pen, New Point(space, height - space), New Point(width, height - space)) 'x轴
g.DrawLine(pen, New Point(space, 0), New Point(space, height - space)) 'y轴
'x轴上的刻度
For i = 0 To width - interval Step interval
If i <= max_x * interval Then
'g.DrawString(string,Font,Brush,PointF)
'String对象,要绘制的字符串。
'Font 对象,它定义字符串的文本格式。
'Brush 对象,它确定所绘制文本的颜色和纹理。
'PointF 结构,它指定所绘制文本的左上角。
'可以单独先定义变量drawString, drawFont, drawBrush, drawPoint
g.DrawString(i / interval, New Font("Arail", 9, FontStyle.Regular), SystemBrushes.WindowText, New PointF(space + i - 5, height - space))
End If
Next
'y轴上的刻度
For i = 0 To height - interval Step interval
If i <= max_y * interval Then
g.DrawLine(pen, New Point(space, height - i - space), New Point(space + 5, height - i - space))
If i <> 0 Then
g.DrawString(i / interval, New Font("Arial", 9, FontStyle.Regular), SystemBrushes.WindowText, New PointF(space - 12, height - space - i - 6))
End If
End If
Next
'-----------------------------------以上同画柱状图---------------------------------------------
'画折线图
For i = 0 To UBound(arrData) - 1
g.DrawLine(pen_chart, New Point(space + i * interval, height - space - arrData(i) * interval), New Point(space + (i + 1) * interval, height - space - arrData(i + 1) * interval))
'g.DrawLine(pen_chart, New Point(space + (i + 1) * interval, height - space - arrData(i) * interval), New Point(space + (i + 1) * interval, height - space))
Next
bmp.Save(Response.OutputStream, ImageFormat.Jpeg)
g.Dispose()
bmp.Dispose()
End Sub
Dim G As Graphics ' = PictureBox1.CreateGraphics()
Dim bmp As New Bitmap(PictureBox1.Width, PictureBox1.Height)
G = Graphics.FromImage(bmp)
Dim P As New Pen(Color.Green, 2)
G.DrawLine(P, 0, 300, 733, 300)
G.DrawLine(P, 600, 0, 600, 349)
G.DrawLine(P, 0, 300, 5, 297)
G.DrawLine(P, 0, 300, 5, 303)
G.DrawLine(P, 600, 0, 597, 5)
G.DrawLine(P, 600, 0, 603, 5)
P.Width = 1
Dim i As Long
Dim W As Long = 30
For i = 1 To 19
G.DrawLine(P, 600 - W * i, 0, 600 - W * i, 300)
Next
For i = 1 To 28
G.DrawLine(P, 0, 300 - 10 * i, 600, 300 - 10 * i)
If i Mod 5 = 0 Then
G.DrawString(i * 10, New Font("宋体", 9), Brushes.Red, 605, 300 - i * 10 - 4)
G.DrawString(i, New Font("宋体", 9), Brushes.Cyan, 640, 300 - i * 10 - 4)
G.DrawString(i * 4, New Font("宋体", 9), Brushes.Yellow, 675, 300 - i * 10 - 4)
End If
Next
G.DrawString("0", New Font("宋体", 9), Brushes.White, 605, 305)
G.DrawString("时间", New Font("宋体", 9), Brushes.White, 5, 305)
G.DrawString("电压(V)", New Font("宋体", 9), Brushes.Red, 605, 5)
G.DrawString("电流(A)", New Font("宋体", 9), Brushes.Cyan, 623, 19)
G.DrawString("功率(KW)", New Font("宋体", 9), Brushes.Yellow, 655, 32)
For i = 0 To 19
P.Color = Color.Red
G.DrawLine(P, 600 - i * W, 300 - CSng(DY(i)) / 100, 600 - (i + 1) * W, 300 - CSng(DY(i + 1) / 100))
Next
PictureBox1.Image = bmp