What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleGraph XY plots using MSChart
KeywordsMSChart, plot, XY plot, graph, equation, chart
CategoriesGraphics, Controls
When the program loads, it builds a two-dimensional array of X and Y values that it wants to plot. It then sets the MSChart control's ChartData property to the array. To make this an XY plot, the program sets the MSChart control's ChartType property to VtChChartType2dXY.
Private Sub Form_Load()
Dim theta_min As Single
Dim theta_max As Single
Dim dtheta As Single
Dim theta As Single
Dim r As Single
Dim values() As Single
Dim i As Integer
Dim num_theta As Integer

    theta_min = 0
    theta_max = 3.14159265
    num_theta = 100
    dtheta = theta_max / (num_theta - 1)
    ReDim values(1 To num_theta, 1 To 2)

    ' Compute the data values.
    theta = theta_min
    For i = 1 To num_theta
        r = Cos(3 * theta)
        values(i, 1) = r * Sin(theta) * 100
        values(i, 2) = r * Cos(theta) * 100
        theta = theta + dtheta
    Next i

    ' Send the data to the chart.
    Chart1.chartType = VtChChartType2dXY
    Chart1.RowCount = 2
    Chart1.ColumnCount = num_theta
    Chart1.ChartData = values
End Sub
My book Custom Controls Library implements a control that does simple graphics without the MSChart control.

Tip: Setting properties in code like this rather than at design time can be very useful for controls such as MSChart that get upgraded frequently. If you install a newer version of the control and remove the old version, then when you open an application the old control is replaced by a PictureBox and all of its properties are lost (although you can still read them in the .frm file with a text editor). If you set the properties in the code, you only need to replace the control and the code can set the properties for the new control.

Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.