To use the code in your own project, you may copy the code inside the "createChart" method to your project, and change the "viewer" variable to the name of your control.
In this section, we will provide step by step instructions as an example.
- Add a reference to ChartDirector to your Windows Forms project. See Using ChartDirector in Your Own Projects.
- Drag a ChartDirector WinChartViewer control from the Visual Studio toolbox onto the Windows Form. Set the ID of the control to "winChartViewer1".
- Double click on any empty space on the Form to edit the code of the Form. Add the following line of code at the top to import the ChartDirector namespace.
[VB]
Imports ChartDirector
[C#]
using ChartDirector;
- Copy and paste the following code into the Form Load event handler.
[Windows Forms - VB Version]'The data for the bar chart Dim data() As Double = {85, 156, 179.5, 211, 123} 'The labels for the bar chart Dim labels() As String = {"Mon", "Tue", "Wed", "Thu", "Fri"} 'Create a XYChart object of size 250 x 250 pixels Dim c As XYChart = New XYChart(250, 250) 'Set the plotarea at (30, 20) and of size 200 x 200 pixels c.setPlotArea(30, 20, 200, 200) 'Add a bar chart layer using the given data c.addBarLayer(data) 'Set the x axis labels using the given labels c.xAxis().setLabels(labels) 'output the chart winChartViewer1.Chart = c 'include tool tip for the chart winChartViewer1.ImageMap = c.getHTMLImageMap("clickable", "", _ "title='{xLabel}: US${value}K'")
[Windows Forms - C# Version]//The data for the bar chart double[] data = {85, 156, 179.5, 211, 123}; //The labels for the bar chart string[] labels = {"Mon", "Tue", "Wed", "Thu", "Fri"}; //Create a XYChart object of size 250 x 250 pixels XYChart c = new XYChart(250, 250); //Set the plotarea at (30, 20) and of size 200 x 200 pixels c.setPlotArea(30, 20, 200, 200); //Add a bar chart layer using the given data c.addBarLayer(data); //Set the x axis labels using the given labels c.xAxis().setLabels(labels); //output the chart winChartViewer1.Chart = c; //include tool tip for the chart winChartViewer1.ImageMap = c.getHTMLImageMap("clickable", "", "title='{xLabel}: US${value}K'");