In this section, we will provide step by steps instructions on how to use ChartDirector in a Visual Basic project from scratch. After following these steps, you should create a VB project the same as the sample VB project in "[ChartDirector]\vbHelloWorld".
- Install the ChartViewer control in the VB Toolbox.
Right click in any empty location in the VB Toolbox, and select Component. Select "ChartDirector ChartViewer" from the component lists, and press OK.
If you could not find "ChartDirector ChartViewer" from the component list, you may press the "Browse" button, and browse to "[system32]\chartviewer.ocx" and select it.
After the above step, you should see the ChartViewer control appear in the VB Toolbox.
- Add a reference to ChartDirector in the VB project. This is by selecting Project/Reference in the VB IDE, then checked the box "ChartDirector".
- Put an instance of the ChartViewer control on a Form.
- Cut and paste the following code into the Form_Load method of the Form.
Dim cd As New ChartDirector.API 'The data for the bar chart Dim data() data = Array(85, 156, 179.5, 211, 123) 'The labels for the bar chart Dim labels() labels = Array("Mon", "Tue", "Wed", "Thu", "Fri") 'Create a XYChart object of size 250 x 250 pixels Dim c As Object Set c = cd.XYChart(250, 250) 'Set the plotarea at (30, 20) and of size 200 x 200 pixels Call c.setPlotArea(30, 20, 200, 200) 'Add a bar chart layer using the given data Call c.addBarLayer(data) 'Set the x axis labels using the given labels Call c.xAxis().setLabels(labels) 'output the chart Set ChartViewer1.Picture = c.makePicture() 'include tool tip for the chart ChartViewer1.ImageMap = c.getHTMLImageMap("clickable", "", _ "title='{xLabel}: US${value}K'")
Note that the above code is the same as that in the previous section The First Windows Project, except the variable "viewer" is replaced with the actual name of the control "ChartViewer1". Please refer to the previous version for detail explanation of the code.