Friday, December 9, 2011

Working with MSChart in VB.Net

The MSChart control is an ActiveX control that lets you to add charting capabilities to your applications. We can create 2D or 3D charts in different styles. It will support almost all type of chart like Line Chart, Bar Chart, Pie Chart, Step, Combination, XY (Scatter) etc. . We can Add/Delete/Modify any items in the chart, such as Axis title, Axis scale, footnotes, data point series, and so on. You can even rotate the graph, add backdrop color or images to virtually any element of the chart. At run time, users can select portions of the chart and move and resize them at will, if you want to provide them with this capability.


Since MSChart is not a standard component so it will not visible in the toolbox, we need to add it manually. For adding Chart to your Project do the following steps:

1. Right Click The toolbox , click Choose Items- Choose Toolbox Items, new window comes with name “Choose Toolbox Items”

2. from that Select COM Components 

3. From the controls Select by checking -> Microsoft     Chart Control(6.0) (SP4)(OLEDB)







 5. Now double click the Chart Icon In the components, chart will appeared in your form, you need to enlarge that one to get propped view






Setting the Property of the Chart

We can set the property of the chart by Property Page or by Property Window:

a. Property Page
Right click the MSChart and select Properties, then a new window will appear (Property Page)



There are 8 property tabs in Property Page
1. Chart
In this tab we can select the type of chart, like 2D line or 3d line graph like that
2. Axis
In this tab we can set the Axis Color, Width, Scaling of axis etc
3. Axis Grid
In this tab we can set the Axis Major Gridlines Style, Width, Color etc
4. Series
In this tab we can set how much series we required, there will be option for hide and exclude the series and some built-in statistical function like Mean, Standard Deviation, regression etc that will automatically calculate and assign to series. By default there will be only 4 series, if want more then we need to add the programmatically (will explain later)
5. Series Color
In this tab we can set the Series Color, style and Width
6. Fonts
In this tab we can set the font, font style, size, effects and color for Title, Footnote, Legend and Axis Labels
7. Text
In this tab we can set text for 3Axis (X-Axis, Y-Axis, Second Y-Axis) and Footnotes
8. Backdrop
In this tab we can set Color and pattern for Chart, Title, Footnote, Legend and Plot

b. Property Window
By using Property window also you can set properties

Making Chart with MSChart

The topmost object of this hierarchy is MSChart, which lets you set the general characteristics of the chart and also exposes several custom events. All the other objects in the hierarchy are dependents of MSChart.

The MSChart associated with a DataGrid Object, This datagrid will store whatever the value we pass to the chart. We can pass valve to chart data grid directly or from spread sheet or from database. The Plot object is a compound object (that is, an object with other child objects) that contains all the graphical information about the data series (color, markers, backdrop pattern, position and attributes of light sources, and so on). The Title, Legend, and Footnote objects are compound objects with similar structures and control over the features of the relevant elements of the chart (text, color, position, and so on).
This example shows how to make a simple MSChart from database (MS Access).
1st step to create a database with 4 fields- Sample, Values, Max, Min and make fields as Sample- Text, Values- Number, Max- Number, Min-Number, then add some values for each column.

Then Open Microsoft VS and click on file menu, then from New select New Project, new project window will appear and from that select Visual basic ->windows Application and press ok.

New form will appear and on that add reference to the project because we are using MS access as database, fro that click on project menu and then click on add reference
New window will appear and from that select COM tab and select Microsoft ActiveX Data Objects 2.0 Library, then press OK
Create a data base with four fields (Sample, Values, Max, and Min) , place the database in bin folder of your application, then add MSChart control and Button Control to form as explained in the starting section. Then Right click the Solution Explore and then click Add and click on the Module.


Then add this code to module 

Public cn As ADODB.Connection
Public rs As ADODB.Recordset
Public cmd As String
Public Sub Connection()
On Error GoTo xx
cn = New ADODB.Connection
cn.CursorLocation = ADODB.CursorLocationEnum.adUseClient
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & My.Application.Info.DirectoryPath & "\database1.mdb"
If cn.State = ADODB.ObjectStateEnum.adStateClosed Then
cn.Open()
End If
Exit Sub
xx: 
MsgBox(Err.Description, MsgBoxStyle.Critical, "Error ...")
Exit Sub
End Sub

Then in the main form add one Button and MSChart control to the form and in the in the property of MSChart set the property that is color, axis etc (change chart type to Line, add Title, X and Y axis name from Text tab etc) and write the below code in the button click

Call Connection()
rs = New ADODB.Recordset
cmd = "select Sample,Values, Max, Min from Table1"
rs.Open(cmd, cn, ADODB.CursorTypeEnum.adOpenKeyset)
rs.MoveFirst()
With MSChart1
.ShowLegend = True
.EditCopy()
.EditPaste()
.DataSource = rs
End With

Then run the project and click on the button

Some Useful Property 

1. Providing X and Y axis value
For that 1st value scale auto value false, after that you can specify maximum and minimum value
MSChar1.Plot.Axis(MSChart20Lib.VtChAxisId.VtChAxisIdX).ValueScale.Auto = False
MSChar1.Plot.Axis(MSChart20Lib.VtChAxisId.VtChAxisIdX).ValueScale.Maximum = 100
MSChar1.Plot.Axis(MSChart20Lib.VtChAxisId.VtChAxisIdX).ValueScale.Minimum = 10
MSChar1.Plot.Axis(MSChart20Lib.VtChAxisId.VtChAxisIdY).ValueScale.Maximum = 50
MSChar1.Plot.Axis(MSChart20Lib.VtChAxisId.VtChAxisIdY).ValueScale.Minimum = 5

2. For Labeling X and Y axis
Mschart1.Plot.Axis(MSChart20Lib.VtChAxisId.VtChAxisIdX).AxisTitle.Text = "XXXXXX"
Mschart1.Plot.Axis(MSChart20Lib.VtChAxisId.VtChAxisIdY).AxisTitle.Text = "yyyyyyy"

3. Setting X and Y axis division
Set auto scale value of both axis to false
Mschart1.Plot.Axis(MSChart20Lib.VtChAxisId.VtChAxisIdX).ValueScale.MajorDivision = 20
Mschart1.Plot.Axis(MSChart20Lib.VtChAxisId.VtChAxisIdX).ValueScale.MinorDivision = 5
Mschart1.Plot.Axis(MSChart20Lib.VtChAxisId.VtChAxisIdY).ValueScale.MajorDivision = 10
Mschart1.Plot.Axis(MSChart20Lib.VtChAxisId.VtChAxisIdY).ValueScale.MinorDivision = 2 

4. Selection Property
Returns or sets a value that indicates whether or not users can select chart objects. True is the default value-The user can interactively select chart objects. False means the user cannot select chart objects.
MSChart1.AllowSelections = True / False

5. Adding more series (Default value 4)
ColumnCount property sets the number of columns in the current data grid associated with a chart.
MSChart1.ColumnCount = 10

6. Setting color and width of Series
For color –setting RBG format
Mschart1.Plot.SeriesCollection(1).DataPoints(1).Brush.FillColor.Set(250,250,)

For width -For that set the Width Property, need to specify Width in Point
MSChart1.Plot.SeriesCollection(1).Pen.Width = 25

7. Making Series Invisible 
For that need to set the ShowLine property to false, default value is zero
MSChart1.Plot.SeriesCollection(1).ShowLine = False

8. Add legend 
MSChart1.Legend.Location.LocationType =_ MSChart20Lib.VtChLocationType.VtChLocationTypeBottom
MSChart1.Legend.Location.Visible = True