如何在VB.NET 2008专业版中创建图表 [英] How to create charts in VB.NET 2008 professional edition

查看:58
本文介绍了如何在VB.NET 2008专业版中创建图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用vb.net 2008专业版. vb.net 2008是否支持创建图表. Bcos我无法从水晶报表创建图表

[edit]已删除呼喊"-OriginalGriff [/edit]

I am using vb.net 2008 profesional edition. Does vb.net 2008 supports creating charts. Bcos I am unable create charts from crystal reports

[edit]SHOUTING removed - OriginalGriff[/edit]

推荐答案

您可以尝试使用预定义的Pen-Object的StartCap和EndCap功能:

''假设您必须绘制从点Pnt1到点Pnt2的箭头:

You can try the StartCap and EndCap feature of the predefined Pen-Object:

''Suppose you have to draw the arrow from Point Pnt1 to Point Pnt2:

Dim Graph As Graphics = Me.CreateGraphics()
Dim Pen1 As New Pen(Color.Blue, 5)

Pen1.StartCap = Drawing2D.LineCap.ArrowAnchor
Pen1.EndCap = Drawing2D.LineCap.DiamondAnchor

Graph.DrawLine(Pen1, Pnt1.X, Pnt1.Y, Pnt2.X, Pnt2.Y)


这段代码在您的表单上绘制了一个漂亮的箭头:

在Visual Basic中绘制一个简单的箭头


This code draws a nice arrow onto your form:

Drawing a simple arrow in Visual Basic

objEnum As IDictionaryEnumerator - the data from which the graph has to be drawn.
    intItemCount As Integer - number of items to show in a graph (you can get from the DictionaryEnumerator itself).
    strGraphTitle As String - title has to be drawn for the graph.
    Xaxis As Integer - starting X axis.
    Yaxis As Integer - starting Y axis.
    MaxWidth As Int16 - maximum width of the graph (for calculation purpose).
    MaxHt As Int16 - maximum height of the graph (for calculation purpose).
    clearForm As Boolean - whether the Form has to clear or not.
    Optional ByVal SpaceRequired As Boolean - whether space is required in between 2 graphs. 

The graph has to be called in the Paint event of the Form and the parameters should be resized in the Resize event of the Form.

LoadColorArray procedure is used to have required colors to draw the graph. Because, if we use RGB, overall the graph will not be good visibly.

The parameter clearForm can be used when you need to show 2 graphs in the same Form; it will be useful then...
Collapse | Copy Code

Public Sub drawBarChart(ByVal objEnum As IDictionaryEnumerator, _
          ByVal intItemCount As Integer, ByVal strGraphTitle As String, _
          ByVal Xaxis As Integer, ByVal Yaxis As Integer, _
          ByVal MaxWidth As Int16, ByVal MaxHt As Int16, _
          ByVal clearForm As Boolean, _
          Optional ByVal SpaceRequired As Boolean = False)

    Dim intGraphXaxis As Integer = Xaxis
    Dim intGraphYaxis As Integer = Yaxis
    Dim intWidthMax As Integer = MaxWidth
    Dim intHeightMax As Integer = MaxHt
    Dim intSpaceHeight As Integer
    Dim intMaxValue As Integer = 0
    Dim intCounter As Integer
    Dim intBarWidthMax
    Dim intBarHeight
    Dim strText As String
    Try
        Dim grfx As Graphics = CreateGraphics()
        If clearForm = True Then
            grfx.Clear(BackColor)
        End If

        grfx.DrawString(strGraphTitle, New Font("VERDANA", 12.0, _
          FontStyle.Bold, GraphicsUnit.Point), _
          Brushes.DeepPink, intGraphXaxis + (intWidthMax / 4), _
          (intGraphYaxis - intHeightMax) - 40)

        'Get the Height of the Bar        

        intBarHeight = CInt(intHeightMax / intItemCount)

        'Get the space Height of the Bar 

        intSpaceHeight = _
          CInt((intHeightMax / (intItemCount - 1)) - intBarHeight)

        'Find Maximum of the input value

        If Not objEnum Is Nothing Then
            While objEnum.MoveNext = True
                If objEnum.Value > intMaxValue Then
                    intMaxValue = objEnum.Value
                End If
            End While
        End If

        'Get the Maximum Width of the Bar

        intBarWidthMax = CInt(intWidthMax / intMaxValue)

        ' Obtain the Graphics object exposed by the Form.


        If Not objEnum Is Nothing Then
            intCounter = 1
            objEnum.Reset()
            'Draw X axis and Y axis lines

            grfx.DrawLine(Pens.Black, intGraphXaxis, _
              intGraphYaxis, intGraphXaxis + intWidthMax, _
              intGraphYaxis)
            grfx.DrawLine(Pens.Black, intGraphXaxis, _
              intGraphYaxis, intGraphXaxis, _
              (intGraphYaxis - intHeightMax) - 25)

            While objEnum.MoveNext = True
                'Get new Y axis

                intGraphYaxis = intGraphYaxis - intBarHeight
                'Draw Rectangle

                grfx.DrawRectangle(Pens.Black, _
                  New Rectangle(intGraphXaxis, intGraphYaxis, _
                  intBarWidthMax * objEnum.Value, intBarHeight))
                'Fill Rectangle

                grfx.FillRectangle(objColorArray(intCounter), _
                  New Rectangle(intGraphXaxis, intGraphYaxis, _
                  intBarWidthMax * objEnum.Value, intBarHeight))
                'Display Text and value

                strText = "(" & objEnum.Key & "," & objEnum.Value & ")"
                grfx.DrawString(strText, New Font("VERDANA", 8.0, _
                  FontStyle.Regular, GraphicsUnit.Point), _
                  Brushes.Black, intGraphXaxis + _
                  (intBarWidthMax * objEnum.Value), intGraphYaxis)
                intCounter += 1
                If SpaceRequired = True Then
                    intGraphYaxis = intGraphYaxis - intSpaceHeight
                End If
                If intCounter > objColorArray.GetUpperBound(0) Then
                    intCounter = 1
                End If
            End While
            If clearForm = True Then
                grfx.Dispose()
            End If
        End If
    Catch ex As Exception
        Throw ex
    End Try
End Sub


objEnum As IDictionaryEnumerator - the data from which the graph has to be drawn.
    intItemCount As Integer - number of items to show in a graph (you can get from the DictionaryEnumerator itself).
    strGraphTitle As String - title has to be drawn for the graph.
    Xaxis As Integer - starting X axis.
    Yaxis As Integer - starting Y axis.
    MaxWidth As Int16 - maximum width of the graph (for calculation purpose).
    MaxHt As Int16 - maximum height of the graph (for calculation purpose).
    clearForm As Boolean - whether the Form has to clear or not.
    Optional ByVal SpaceRequired As Boolean - whether space is required in between 2 graphs. 

The graph has to be called in the Paint event of the Form and the parameters should be resized in the Resize event of the Form.

LoadColorArray procedure is used to have required colors to draw the graph. Because, if we use RGB, overall the graph will not be good visibly.

The parameter clearForm can be used when you need to show 2 graphs in the same Form; it will be useful then...
Collapse | Copy Code

Public Sub drawBarChart(ByVal objEnum As IDictionaryEnumerator, _
          ByVal intItemCount As Integer, ByVal strGraphTitle As String, _
          ByVal Xaxis As Integer, ByVal Yaxis As Integer, _
          ByVal MaxWidth As Int16, ByVal MaxHt As Int16, _
          ByVal clearForm As Boolean, _
          Optional ByVal SpaceRequired As Boolean = False)

    Dim intGraphXaxis As Integer = Xaxis
    Dim intGraphYaxis As Integer = Yaxis
    Dim intWidthMax As Integer = MaxWidth
    Dim intHeightMax As Integer = MaxHt
    Dim intSpaceHeight As Integer
    Dim intMaxValue As Integer = 0
    Dim intCounter As Integer
    Dim intBarWidthMax
    Dim intBarHeight
    Dim strText As String
    Try
        Dim grfx As Graphics = CreateGraphics()
        If clearForm = True Then
            grfx.Clear(BackColor)
        End If

        grfx.DrawString(strGraphTitle, New Font("VERDANA", 12.0, _
          FontStyle.Bold, GraphicsUnit.Point), _
          Brushes.DeepPink, intGraphXaxis + (intWidthMax / 4), _
          (intGraphYaxis - intHeightMax) - 40)

        'Get the Height of the Bar        

        intBarHeight = CInt(intHeightMax / intItemCount)

        'Get the space Height of the Bar 

        intSpaceHeight = _
          CInt((intHeightMax / (intItemCount - 1)) - intBarHeight)

        'Find Maximum of the input value

        If Not objEnum Is Nothing Then
            While objEnum.MoveNext = True
                If objEnum.Value > intMaxValue Then
                    intMaxValue = objEnum.Value
                End If
            End While
        End If

        'Get the Maximum Width of the Bar

        intBarWidthMax = CInt(intWidthMax / intMaxValue)

        ' Obtain the Graphics object exposed by the Form.


        If Not objEnum Is Nothing Then
            intCounter = 1
            objEnum.Reset()
            'Draw X axis and Y axis lines

            grfx.DrawLine(Pens.Black, intGraphXaxis, _
              intGraphYaxis, intGraphXaxis + intWidthMax, _
              intGraphYaxis)
            grfx.DrawLine(Pens.Black, intGraphXaxis, _
              intGraphYaxis, intGraphXaxis, _
              (intGraphYaxis - intHeightMax) - 25)

            While objEnum.MoveNext = True
                'Get new Y axis

                intGraphYaxis = intGraphYaxis - intBarHeight
                'Draw Rectangle

                grfx.DrawRectangle(Pens.Black, _
                  New Rectangle(intGraphXaxis, intGraphYaxis, _
                  intBarWidthMax * objEnum.Value, intBarHeight))
                'Fill Rectangle

                grfx.FillRectangle(objColorArray(intCounter), _
                  New Rectangle(intGraphXaxis, intGraphYaxis, _
                  intBarWidthMax * objEnum.Value, intBarHeight))
                'Display Text and value

                strText = "(" & objEnum.Key & "," & objEnum.Value & ")"
                grfx.DrawString(strText, New Font("VERDANA", 8.0, _
                  FontStyle.Regular, GraphicsUnit.Point), _
                  Brushes.Black, intGraphXaxis + _
                  (intBarWidthMax * objEnum.Value), intGraphYaxis)
                intCounter += 1
                If SpaceRequired = True Then
                    intGraphYaxis = intGraphYaxis - intSpaceHeight
                End If
                If intCounter > objColorArray.GetUpperBound(0) Then
                    intCounter = 1
                End If
            End While
            If clearForm = True Then
                grfx.Dispose()
            End If
        End If
    Catch ex As Exception
        Throw ex
    End Try
End Sub


这篇关于如何在VB.NET 2008专业版中创建图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆