天高任鸟飞

博客园 首页 联系 订阅 管理

Imports Microsoft.VisualBasic
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.data

Partial Public Class WebChart
 
    Shared Sub drawInit(ByRef G As Graphics, ByRef BMP As System.Drawing.Bitmap, ByVal width As Integer, ByVal height As Integer)

        Dim myBrush As System.Drawing.SolidBrush

        BMP = New System.Drawing.Bitmap(width, height, Imaging.PixelFormat.Format24bppRgb)
        G = System.Drawing.Graphics.FromImage(BMP)
        myBrush = New System.Drawing.SolidBrush(Color.FromArgb(0, 224, 224, 224))

        '清空全部区域
        myBrush.Color = Color.FromArgb(255, 224, 224, 224)
        G.FillRectangle(myBrush, 0, 0, width, height)
    End Sub

    Shared Sub ShowImage(ByVal BMP As System.Drawing.Bitmap, ByVal image As WebControls.Image)

        Microsoft.VisualBasic.Randomize(My.Computer.Clock.TickCount)
        Dim ImageKey As String = "IMG_" & My.Computer.Clock.TickCount & Microsoft.VisualBasic.Rnd & My.Computer.Clock.TickCount
        image.Page.Application(ImageKey) = BMP
        image.ImageUrl = "~/ImageServices.ashx?ImageKey=" & ImageKey
    End Sub

End Class




Imports Microsoft.VisualBasic
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.data

Partial Public Class WebChart

    Public Class LineChart

#Region "内部参数"

        Dim _LineWidth As Integer
        Public Property LineWidth() As Integer
            Get
                If _LineWidth = 0 Then _LineWidth = 1
                Return _LineWidth
            End Get
            Set(ByVal Value As Integer)
                _LineWidth = Value
            End Set
        End Property


        Dim _DashStyle As DashStyle = DashStyle.Solid
        Public Property DashStyle() As DashStyle
            Get
                Return _DashStyle
            End Get
            Set(ByVal Value As DashStyle)
                _DashStyle = Value
            End Set
        End Property


        Public TopMargin As Integer = 30
        Public LeftMargin As Integer = 30
        Public RightMargin As Integer = 30
        Public BottomMargin As Integer = 30

        Public dataSource As DataView

        Public ReadOnly Property DrawingColors() As Object
            Get
                Dim c(7) As Drawing.Color
                c(0) = Color.FromArgb(255, &H33, &H33, &HFF)
                c(1) = Color.FromArgb(255, &HFF, &HFF, &H0)
                c(2) = Color.FromArgb(255, &HCC, &H66, &H0)
                c(3) = Color.FromArgb(255, &HCC, &HCC, &HFF)
                c(4) = Color.FromArgb(255, &H99, &H66, &HFF)
                c(5) = Color.FromArgb(255, &H0, &H99, &HFF)
                c(6) = Color.FromArgb(255, &HFF, &HFF, &H99)
                c(7) = Color.Orange
                Return c
            End Get

        End Property
#End Region


        Dim G As Graphics

        Dim myBrush As New System.Drawing.SolidBrush(Color.Blue)

        Dim penColor As New Pen(Color.Blue)

        Dim BMP As System.Drawing.Bitmap '' ???????????????????????????? 全局变量?
        Dim x As Single
        Dim y As Single
        Dim w As Single
        Dim h As Single
        Dim MaxValue As Integer
        Dim MinValue As Integer
        Dim MaxBoundary, MinBoundary As Integer
        Dim kh As Single
        Dim hh As Single


#Region "Line Chart"
        '建立函数
        Sub New(ByVal width As Integer, ByVal height As Integer, ByVal dt As DataView)
            '建立图形  物件初始化
            drawInit(G, BMP, width, height)


            x = 0 + Me.TopMargin
            y = 0 + Me.LeftMargin
            w = BMP.Width - Me.LeftMargin - Me.RightMargin
            h = BMP.Height - Me.TopMargin - Me.BottomMargin


            Me.dataSource = dt
        End Sub


        Sub ShowImage(ByVal image As WebControls.Image)

            '绘制框线
            RenderOutLine()

            '确定资料
            If dataSource Is Nothing Then
                myBrush.Color = Color.Red
                G.DrawString("没资料", New Font("Arial", 7, FontStyle.Regular), myBrush, BMP.Width / 2, BMP.Height / 2)
                Exit Sub
            End If

            '绘制格线
            RenderNumberLines()

            '绘制折线
            DrawLineChart()

            '显示图表
            WebChart.ShowImage(BMP, image)
        
        End Sub

 

        Sub RenderOutLine()
          
            myBrush.Color = Color.FromArgb(0, 224, 224, 224)


            myBrush.Color = Color.FromArgb(255, 224, 224, 224)
            G.FillRectangle(myBrush, 0, 0, CInt(BMP.Width), CInt(BMP.Height)) '  边框

            penColor.Color = Color.YellowGreen
         
            G.DrawLine(penColor, x, y, x, y + h) 'Y轴

            G.DrawLine(penColor, x, y + h, x + w, y + h) 'x轴 倒转

        
            myBrush.Color = Color.Red
            G.DrawString("BELHKG", New Font("Arial", 7, FontStyle.Regular), myBrush, BMP.Width - 220, BMP.Height - 14)
        End Sub
        '//////////////// 点类

        Private Class pointAndValue
            Public point As Point
            Public value As String
            Sub New(ByVal point As Point, ByVal value As String)
                Me.point = point
                Me.value = value

            End Sub
        End Class
        '////////////////////////////////


        Sub DrawLineChart()
            Dim dt As DataView = Me.dataSource
            'Dim MaxValue As Integer  '最大值
            'Dim MinValue As Integer  '最小值
            'Dim MaxBoundary, MinBoundary As Integer
            Dim BarNumber As Integer = dt.Table.Columns.Count
            Dim UnitxPosition As Integer = Me.w / (BarNumber + 1) '每列x增加值
            Dim xPosition As Integer
            Dim offsetX As Integer = 0
            Dim yh As Integer = y + h
            Dim pointsFor As New ArrayList
            '记录要绘出的点
            Dim pointAndValueFor As New ArrayList


            xPosition = UnitxPosition
            Dim n As Integer = 0
            '跑每个数列
            Dim j As Integer = 0
            Dim yhh As Integer = 20
            Dim row As Integer = dt.Table.Rows.Count
            For Each ro As DataRow In dt.Table.Rows

                ' xPosition = UnitxPosition + (n * offsetX)
                xPosition = UnitxPosition
                '决定列的颜色
                Dim C As Color
                C = DrawingColors(n Mod DrawingColors.Length)

                '开始画 Line
                Dim LastPoint As Point
                LastPoint = Nothing
                Dim fpoint As Point = Nothing
                'Dim yadd As Double = 0.0
                'fpoint.Y = yadd

                For i As Integer = 0 To dt.Table.Columns.Count - 1
                    'hh
                  
                    Dim 值 As String
                    Dim Hight As Double
                    Dim BarY As Double

                    If i <> 0 Then

                        值 = Val(ro.Item(i))
                        Hight = (h / kh) * 值
                        BarY = y + h - Hight
                    Else
                        BarY = y + h - yhh
                    End If
                    值 = ro.Item(i).ToString
                    ' Line
                    If LastPoint.X = 0 And LastPoint.Y = 0 Then
                        LastPoint.X = x + xPosition - 30
                        LastPoint.Y = BarY
                        'If i = 1 Then

                        '    fpoint.X = xPosition - 60
                        '    fpoint.Y = BarY
                        '    pointsFor.Add(fpoint)
                        '    pointAndValueFor.Add(New pointAndValue(New Point(xPosition - 60, fpoint.Y), Val(ro.Item(0))))
                        '    G.DrawLine(penColor, fpoint, LastPoint)

                        'End If
                    Else

 

                        pointsFor.Add(LastPoint)
                        pointsFor.Add(New Point(x + xPosition - 30, BarY))

                        penColor.Color = C
                        penColor.Width = Me.LineWidth
                        penColor.DashStyle = Me.DashStyle

                        G.DrawLine(penColor, LastPoint, New Point(x + xPosition - 30, BarY))

                        LastPoint = New Point(x + xPosition - 30, BarY)
                    End If

                    pointAndValueFor.Add(New pointAndValue(New Point(x + xPosition - 30, BarY), 值)) '  类pointandvalue值

                    If n = 0 Then
                        If i <> 0 Then
                            G.DrawString(dt.Table.Columns.Item(i).Caption, New Font("Arial  ", 10, FontStyle.Regular), myBrush, x + xPosition - 35, y + h)
                        End If

                    End If

 

                    xPosition += UnitxPosition
                Next

                '描点
                myBrush.Color = Color.Black
                For Each point As Point In pointsFor
                    G.FillEllipse(myBrush, point.X - 1, point.Y - 1, 3, 3)
                Next
                '描值
                For Each item As pointAndValue In pointAndValueFor
                    myBrush.Color = Color.Black
                    G.DrawString(item.value, New Font("Arial", 7, FontStyle.Regular), myBrush, item.point.X - 10, item.point.Y - 10)

                Next
                yhh += 40

                n += 1
               ' G.DrawString(ro.Item(0), New Font("Arial  ", 10, FontStyle.Regular), myBrush, fpoint.X, fpoint.Y)
            Next
        End Sub

 
        Sub RenderNumberLines()
            Dim dt As DataView = Me.dataSource

            Dim BarNumber As String = dt.Table.Columns.Count
 
            For Each row As DataRow In dt.Table.Rows
                For i As Integer = 0 To dt.Table.Columns.Count - 1
                    Dim n As Single = Val(row(i))

                    If MaxValue < n Then MaxValue = n
                    If MinValue > n Then MinValue = n '找出最大值
                Next
            Next


            MaxBoundary = (Val(Val(Left(Trim(MaxValue), 1)) + 1) & Right("0000000000", Len(Trim(MaxValue)) - 1)) '最大值后面加0000 再等分
            MinBoundary = 0
            Dim stept As Double = MaxBoundary / 10
            kh = MaxBoundary - MinBoundary
            'If MaxBoundary < 10 Then
            '    stept = 1
            'End If
            Dim s As Integer = y + h
            For i As Double = MinBoundary To MaxBoundary Step (stept) ' 必须满足10等份
                myBrush.Color = Color.Blue
                If i <> 0 Then
                    G.DrawString(i, New Font("Arial", 7, FontStyle.Regular), myBrush, x - 20, s)
                End If
                penColor.DashStyle = DashStyle.Dash
                penColor.Color = Color.Gray

                G.DrawLine(penColor, x, s, x + Me.w, s)

                s = s - h / 10

            Next

        End Sub
#End Region

    End Class

End Class