Imports System.Drawing.Drawing2D
Imports System.Drawing
'绘制图表
Public Class ClassDrawChart
#Region "字段"
Private mData As ArrayList '数据
Private mItemName As ArrayList '检测项目名称
Private mintCount As Integer '数据个数
Private mintAllTestCount As Integer '总检测次数
Private mType As ClassTest.TestType '类型
Private mBmp As Bitmap '图形
Private mImageStream As System.IO.MemoryStream '图形流
Private mintWidth As Integer = 1080 '宽
Private mintHeight As Integer = 740 '高
Private GDI As Graphics 'GDI
#End Region
#Region "属性"
Public ReadOnly Property Image() As Bitmap '图形
Get
Return mBmp
End Get
End Property
Public ReadOnly Property ImageStream() As System.IO.MemoryStream '图形流
Get
Return mImageStream
End Get
End Property
#End Region
#Region "方法"
Public Sub New(ByVal Data As ArrayList, ByVal ItemName As ArrayList, ByVal Type As ClassTest.TestType, ByVal AllTestCoun As Integer) '构造函数
mData = Data
mItemName = ItemName
mintCount = Data.Count
mType = Type
mintAllTestCount = AllTestCoun
End Sub '构造
Public Sub Drawing()
Try
'绘制
mBmp = New Bitmap(mintWidth, mintHeight)
mImageStream = New System.IO.MemoryStream
GDI = Graphics.FromImage(mBmp)
GDI.Clear(Color.White)
'绘制标题
DrawingTitle(mType, GDI)
'绘制坐标轴
DrawingXYAxis(mType, GDI)
'绘制柱状图
DrawingChart(mType, GDI)
'保存图表至内存中
mBmp.Save(mImageStream, Imaging.ImageFormat.Bmp)
Catch ex As Exception
Debug.WriteLine(ex.ToString)
ClassComm.WriteErrMessage(ex.ToString, "ClassDrawChart_Drawing")
End Try
End Sub '绘制
Private Sub DrawingTitle(ByVal Type As ClassTest.TestType, ByVal GDI As Graphics)
Try
'绘制标题
Dim Title As String
Dim TitleFont As New Font("宋体", 12, FontStyle.Bold)
Dim X, Y As Integer
Select Case Type
Case ClassTest.TestType.CarComputer
Title = "车身电脑不合格统计图表"
Case ClassTest.TestType.CompositeMeter
Title = "组合仪表不合格统计图表"
End Select
X = (mintWidth - TitleFont.Size * Title.Length * 1.4) / 2
Y = 22
GDI.DrawString(Title, TitleFont, New SolidBrush(Color.Black), X, Y)
Catch ex As Exception
Debug.WriteLine(ex.ToString)
ClassComm.WriteErrMessage(ex.ToString, "ClassDrawChart_DrawingTitle")
End Try
End Sub '绘制标题
Private Sub DrawingXYAxis(ByVal Type As ClassTest.TestType, ByVal GDI As Graphics)
Try
'绘制坐标轴
Dim XAxis_X1, XAxis_Y1, XAxis_X2, XAxis_Y2 As Integer 'X轴坐标
Dim YAxis_X1, YAxis_Y1, YAxis_X2, YAxis_Y2 As Integer 'Y轴坐标
Dim Point_1, Point_2, Point_3 As Point '三角坐标
Dim TrigonPoint As Point() '三角坐标系
Dim TrigonBrush As New SolidBrush(Color.Black) '笔刷
Dim LinePen As New Pen(Color.Black, 2)
XAxis_X1 = 20
XAxis_X2 = mintWidth - 20
XAxis_Y1 = mintHeight - 50
XAxis_Y2 = XAxis_Y1
GDI.DrawLine(LinePen, XAxis_X1, XAxis_Y1, XAxis_X2, XAxis_Y2) 'X轴
Point_1 = New Point(XAxis_X2, XAxis_Y1)
Point_2 = New Point(XAxis_X2 - 15, XAxis_Y1 - 5)
Point_3 = New Point(XAxis_X2 - 15, XAxis_Y1 + 5)
TrigonPoint = New Point() {Point_1, Point_2, Point_3}
GDI.FillPolygon(TrigonBrush, TrigonPoint) 'X轴三角
YAxis_X1 = 40
YAxis_X2 = YAxis_X1
YAxis_Y1 = 50
YAxis_Y2 = mintHeight - 30
GDI.DrawLine(LinePen, YAxis_X1, YAxis_Y1, YAxis_X2, YAxis_Y2) 'Y轴
Point_1 = New Point(YAxis_X1, YAxis_Y1)
Point_2 = New Point(YAxis_X1 - 5, YAxis_Y1 + 15)
Point_3 = New Point(YAxis_X1 + 5, YAxis_Y1 + 15)
TrigonPoint = New Point() {Point_1, Point_2, Point_3}
GDI.FillPolygon(TrigonBrush, TrigonPoint) 'X轴三角
Catch ex As Exception
Debug.WriteLine(ex.ToString)
ClassComm.WriteErrMessage(ex.ToString, "ClassDrawChart_DrawingXYAxis")
End Try
End Sub '绘制坐标轴
Private Sub DrawingChart(ByVal type As ClassTest.TestType, ByVal GDI As Graphics)
'绘制柱状图
Try
Dim PerWidth, PerHeight, AllWidth, AllHeight As Integer
Dim iLoop As Integer
Dim X1, Y1, X2, Y2, X, Y, Width, Height As Integer
Dim RectangleBrush As New HatchBrush(HatchStyle.BackwardDiagonal, Color.Red, Color.White)
Dim RectanglePen As New Pen(Color.Red)
Dim DataFont As New Font("宋体", 9)
Dim TitleFont As New Font("宋体", 9)
Dim TitleDrawFormat As New StringFormat
AllWidth = mintWidth - 40 - 20 - 15 - 20 '绘制柱状图的Y轴长度
AllHeight = mintHeight - 50 - 15 - 20 - 30 - 20
X1 = 40
Y1 = mintHeight - 50
X2 = mintWidth - 20 - 15 - 20
Y2 = Y1
PerWidth = (X2 - X1) / mintCount
PerHeight = AllHeight / mintAllTestCount
Width = PerWidth / 3
'绘制
For iLoop = 0 To mintCount - 1
'绘制柱状图
If mData(iLoop) = 0 Then
X = X1 + (PerWidth * 2 / 3) + (PerWidth * iLoop)
Y = Y2
Height = 10
GDI.FillRectangle(RectangleBrush, X, Y, Width, Height)
GDI.DrawRectangle(RectanglePen, X, Y, Width, Height)
Else
X = X1 + (PerWidth * 2 / 3) + (PerWidth * iLoop)
Y = Y2 - (PerHeight * mData(iLoop))
Height = PerHeight * mData(iLoop)
GDI.FillRectangle(RectangleBrush, X, Y, Width, Height)
GDI.DrawRectangle(RectanglePen, X, Y, Width, Height)
End If
'绘制数字
Select Case mData(iLoop).ToString.Length
Case 1 '1位数字
GDI.DrawString(mData(iLoop), DataFont, New SolidBrush(Color.Red), X, Y - DataFont.Height)
Case 2 '2位数字
GDI.DrawString(mData(iLoop), DataFont, New SolidBrush(Color.Red), X - DataFont.Size / 2, Y - DataFont.Height)
Case 3 '3位数字
GDI.DrawString(mData(iLoop), DataFont, New SolidBrush(Color.Red), X - DataFont.Size * 2 / 3, Y - DataFont.Height)
End Select
'绘制名称
TitleDrawFormat.FormatFlags = StringFormatFlags.DirectionVertical
GDI.DrawString(mItemName(iLoop), TitleFont, New SolidBrush(Color.Blue), (X1 + (PerWidth * iLoop)), (Y1 - mItemName(iLoop).ToString.Length * TitleFont.Height), TitleDrawFormat)
Next
Catch ex As Exception
Debug.WriteLine(ex.ToString)
ClassComm.WriteErrMessage(ex.ToString, "ClassDrawChart_DrawingChart")
End Try
End Sub '绘制柱状图
Public Sub Print(ByVal GDI As Graphics)
'打印
Try
GDI.DrawImage(mBmp, 0, 0)
Catch ex As Exception
Debug.WriteLine(ex.ToString)
ClassComm.WriteErrMessage(ex.ToString, "ClassDrawChart_Print")
End Try
End Sub '打印图表
#End Region
End Class