Custom Chart : Chart « GUI Applications « VB.Net Tutorial

VB.Net Tutorial
1. Language Basics
2. Data Type
3. Operator
4. Statements
5. Date Time
6. Class Module
7. Development
8. Collections
9. Generics
10. Attributes
11. Event
12. Stream File
13. GUI
14. GUI Applications
15. 2D Graphics
16. I18N Internationlization
17. Reflection
18. Regular Expressions
19. Security
20. Socket Network
21. Thread
22. Windows
23. XML
24. Database ADO.net
25. Design Patterns
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial » GUI Applications » Chart 
14. 4. 1. Custom Chart
Custom Chart
 

' Quote from
'Visual Basic 2005 Cookbook Solutions for VB 2005 Programmers
'by Tim Patrick (Author), John Craig (Author)
'# Publisher: O'Reilly Media, Inc. (September 212006)
'# Language: English
'# ISBN-100596101775
'# ISBN-13978-0596101770



Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

public class CustomChart
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class


Public Class Form1
    Private Sub Form1_Paint(ByVal sender As Object, _
          ByVal e As System.Windows.Forms.PaintEventArgs_
          Handles Me.Paint
        ' ----- Draw a nice chart.
        Dim canvas As Graphics = e.Graphics

        ' ----- Create an array of data points to plot.
        Dim chartData() As Single = _
           {20334425172463755433}

        ' ----- Create some pens.
        Dim penRed As New Pen(Color.Red, -1)
        Dim penBlack As New Pen(Color.Black, -1)
        Dim penShadow As New Pen(Color.Gray, -1)

        ' ----- Prepare to add labels.
        Dim labelFont As New Font("Arial"3, FontStyle.Regular)
        Dim labelBrush As New SolidBrush(Color.Blue)

        ' ----- Used to plot the various elements.
        Dim x1, y1 As Single 'Lower left corner
        Dim x2, y2 As Single 'Upper right corner
        Dim scaleX, scaleY As Single
        Dim xScan, yScan As Single
        Dim oneBar As RectangleF

        ' ----- Set the scaling.
        x1 = -10
        y1 = -10
        x2 = 110
        y2 = 110
        scaleX = Me.ClientSize.Width / (x2 - x1)
        scaleY = Me.ClientSize.Height / (y2 - y1)
        canvas.ScaleTransform(scaleX, -scaleY'(inverted)
        canvas.TranslateTransform(-x1, -y2)    '(inverted)

        ' ----- Color the background.
        canvas.Clear(Color.Cornsilk)

        ' ----- Draw chart outline rectangle.
        canvas.DrawRectangle(penBlack, New Rectangle(00100100))

        ' ----- Draw the chart grid.
        For xScan = 10 To 90 Step 10
            canvas.DrawLine(penBlack, xScan, 0, xScan, 100)
        Next xScan
        For yScan = 10 To 90 Step 10
            canvas.DrawLine(penBlack, 0, yScan, 100, yScan)
        Next yScan

        ' ----- Draw some shadowed bars.
        For xScan = To 90 Step 10
            ' ----- Draw the shadow first.
            oneBar.X = xScan + 0.6
            oneBar.Y = 0
            oneBar.Width = 6
            oneBar.Height = chartData(xScan \ 102
            canvas.FillRectangle(New SolidBrush(Color.FromArgb(127, _
               Color.Gray)), oneBar)

            ' ----- Now draw the bar in front.
            oneBar.X = xScan + 2
            oneBar.Y = 0
            oneBar.Height = chartData(xScan \ 10)
            canvas.FillRectangle(New SolidBrush(Color.Red), oneBar)
        Next xScan

        ' ----- Need to un-invert the scaling so text labels are
        '       right-side-up.
        canvas.ResetTransform()
        canvas.ScaleTransform(scaleX, scaleY)
        canvas.TranslateTransform(-x1, -y1)

        ' ----- Label the Y axis.
        For yScan = To 100 Step 10
            canvas.DrawString(yScan.ToString, labelFont, labelBrush, _
               -* yScan.ToString.Length - 397 - yScan)
        Next yScan

        ' ----- Label the X axis
        For xScan = To 100 Step 10
            canvas.DrawString(xScan.ToString, labelFont, labelBrush, _
               xScan + 1.7 * xScan.ToString.Length, 103)
        Next xScan

        ' ----- Cleanup.
        labelFont.Dispose()
        labelBrush.Dispose()
        penRed.Dispose()
        penBlack.Dispose()
        penShadow.Dispose()
        canvas = Nothing
    End Sub

    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgsHandles Me.Resize
        ' ----- Refresh on resize.
        Me.Refresh()
    End Sub
End Class


<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.SuspendLayout()
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(549394)
        Me.DoubleBuffered = True
        Me.Name = "Form1"
        Me.Text = "Drawing a Simple Chart"
        Me.ResumeLayout(False)

    End Sub

End Class

        
14. 4. Chart
14. 4. 1. Custom ChartCustom Chart
14. 4. 2. Char CPU and MemoryChar CPU and Memory
14. 4. 3. Performance Monitor
14. 4. 4. GDI+ Line ChartGDI+ Line Chart
14. 4. 5. Thread GraphThread Graph
ww___w__.__j_ava__2s__.___co___m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.