Tutorial: Creating a Basic Chart
This tutorial demonstrates how to add the Chart control to your Windows Forms or Web application, and then add data, title, legends, and annotations to your chart.
Adding a Chart Control
Follow the steps below to add a chart control to your Windows Forms for Web application.
To add a chart control to your application
In design view, open the Toolbox.
From the Data category, drag a Chart control to the design area.
If you cannot see the Chart control in the Toolbox, right click in the Toolbox, select Choose Items, and then select the following namespaces in the .NET Framekwork Components tab:
Note
To find the Chart control more easily in the Toolbox, hide the inactive controls. To do this, right-click the Toolbox, and clear Show All item.
Right-click the Chart control in the design area and select Properties.
In the Properties window, click the Categorized button.
In the Chart category of the Properties window, click the ChartAreas collection property, and then click the ellipsis button (…). This opens the ChartAreas Collection Editor.
The ChartAreas collection (a ChartAreaCollection object) contains all chart areas in the Chart control. Notice that the collection already contains a chart area named "ChartArea1".
Click Add, and then click OK. A new ChartArea object is added to the collection with the default name "ChartArea2".
Notice that the chart in the design area shrinks to half the original size. This is because the newly created chart area is placed at the bottom, but does not contain a data series yet.
In the Properties window, click the Series collection, and then click the ellipsis button. This opens the Series Collection Editor.
The Series collection (a SeriesCollection object) contains all data series in the Chart control. Notice that the collection already contains a series named "Series1".
In the Series Collection Editor, click Add, and then set the following properties with the newly created Series object:
Property
Value
ChartArea
ChartArea2
ChartType
Bar
Name
BarChart
Click OK, and then click OK again.
Now there are chart areas, each chart area contains one data series. However, you will not see anything when you run your application because the series do not have any data yet.
Adding Data to the Chart
Follow the steps below to add data to the Chart control you just created.
To add data to the chart
Open the Series Collection Editor again.
In the Members area, select Series1.
In the Data category of the Series1 properties area, click the Points collection property, and then click the ellipsis button. This opens the DataPoint Collection Editor.
The Points collection (a DataPointCollection object) contains all data points in the current series.
In the DataPoint Collection Editor, click Add, and then assign a value to the YValues property in the newly created DataPoint object.
Repeat this step until you have five data points in the collection.
Click OK, and then repeat the previous steps to add data points to the BarChart series.
In the Series Collection Editor, click OK.
Run your application.
Now you can see two chart areas in the Chart control displaying the data points you just created in the two series. Note the different chart types of the two series.
Adding Legends to the Chart
Follow the steps below to add a legend for each series you just created.
To add a legend for each series
In the Chart category of the Properties window, click the Legends collection property, and then click the … button at the right. This opens the Legend Collection Editor.
The Legends collection (a LegendCollection object) contains all legends in the Chart control.
If there is not already a legend in the Legend Collection Editor, click Add.
Set the following properties with the Legend object:
Property
Value
DockedToChartArea
ChartArea1
Docking
Right
IsDockedInsideChartArea
False
Click Add again, and then set the following properties with the newly created Legend object:
Property
Value
DockedToChartArea
ChartArea2
Docking
Right
Click OK.
Note that in the design area, the second legend is shown to be empty. By default, both series are assigned to the first legend. You can assign each series to a different legend.
Open the Series Collection Editor again.
In the Members area, select BarChart.
In the Legend category of the BarChart properties area, in the Legend property's dropdown list, select Legend2.
Click OK.
Run your application.
Adding a Title to the Chart
Follow the steps below to add a title to your chart.
To add a title to the chart
In the Chart category of the Properties window, click the Titles collection property, and then click the ellipsis button. This opens the Title Collection Editor.
The Titles collection (a TitleCollection object) contains all titles in the Chart control.
In the Legend Collection Editor, click Add.
In the Appearance category of the Title1 properties area, type a chart title in the Text property.
Click OK.
Run your application.
Marking a Data Point with an Annotation
Follow the steps below to use an annotation to mark a data point on the chart.
To mark a data point with an annotation
In the Chart category of the Properties window, click the Annotations collection property, and then click the ellipsis button. This opens the Annotation Collection Editor.
The Annotations collection (an AnnotationCollection object) contains all annotations in the Chart control.
In the Annotation Collection Editor, click the Add arrow and select ArrowAnnotation.
In the Anchor category of the ArrowAnnotation1 properties area, click the AnchorDataPoint arrow and select the first data point in Series1.
Set the following properties with the newly created Legend object:
Property
Value
Height
-5
Width
0
AnchorOffSetY
-2.5
In the Misc category, expand the SmartLabelStyle property and set its IsOverlappedHidden property to False.
Click OK.
Run your application.
Next Step
You have now learned how to add the Chart control to your Windows Forms or Web application, and then add data, title, legends, and annotations to your chart. To learn how to use 3D charts, see Tutorial: Changing to a 3D Chart.
See Also
Reference
System.Windows.Forms.DataVisualization.Charting
System.Web.UI.DataVisualization.Charting