tags:

views:

550

answers:

4

Hi guys,

I am doing Windows applications in VB.NET 2005. I want to use chart in my project. I already asked this question in this site. But viewers said the solution to use "MSChartControl" (but this is for Visual Studio 2008.). Is there any other way to create chart in our own code (without using other's third party dll). Kind help needed. Thanks in advance.

Sivakumar.P

+2  A: 

You can draw one yourself by using methods of the System.Drawing.Graphics object. That can be done directly in the form's OnPaint override/event or encapsulated in a separate component/control.

Public Class Form1
Protected Overrides Sub OnCreateControl()
  MyBase.OnCreateControl()
  SetStyle(ControlStyles.ResizeRedraw, True)
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
  MyBase.OnPaint(e)

  With e.Graphics
     .DrawLines(SystemPens.WindowText, New Point() { _
                New Point(60, 0), _
                New Point(60, ClientRectangle.Bottom - 60), _
                New Point(ClientRectangle.Right, ClientRectangle.Bottom - 60)})
     For y = 0 To 100 Step 15
        Dim wid = e.Graphics.MeasureString(y.ToString, Font).Width
        .DrawString(y.ToString, Font, SystemBrushes.WindowText, 60 - wid, CSng(ClientRectangle.Bottom - 60 - (y * (ClientRectangle.Size.Height - 60) / 100)))
     Next

     Dim sf As New System.Drawing.StringFormat(System.Drawing.StringFormatFlags.DirectionVertical)
     For x = 0 To 5
        Dim dateStr = DateTime.Today.AddDays(x).ToShortDateString()
        Dim xCoord As Integer = CInt(60 + (ClientSize.Width - 60) * (x + 0.5) / 6)
        Dim yBottom As Integer = ClientRectangle.Bottom - 60
        .DrawString(dateStr, Font, SystemBrushes.WindowText, xCoord, yBottom, sf)
        Dim yTop As Integer = CInt(yBottom - (CInt(Date.Today.AddDays(x).DayOfWeek) + 2) * (ClientSize.Height - 60) / 10)
        Dim bar As Rectangle = New Rectangle(xCoord, yTop, 18, yBottom - yTop)
        .FillRectangle(Brushes.LightBlue, bar)
        .DrawRectangle(SystemPens.WindowText, System.Drawing.Rectangle.Round(bar))
     Next
  End With
End Sub
End Class
BlueMonkMN
Can you give some example please
sivakumar
OK, I added some very simple sample code that draws a chart directly on a form. Of course you would want to have more variables and procedures to make it more flexible, but this demonstrates how to draw graphics that look like a chart.
BlueMonkMN
A: 

Your probably best off with ZedGraph rather than growing your own.

Martin
A: 

How about a javascript chart?

http://code.google.com/p/flot/

Jim Schubert
+1  A: 

There is an old MSDN magazine article on this topic. http://msdn.microsoft.com/en-us/magazine/cc301375.aspx

You might also see if there are any open source .NET chart components at CodePlex. http://www.codeplex.com

Jeremy