windowsforms-2455-controls-and-libraries-chart-control-getting-started-lesson-4-use-a-series-template-for-auto-created-series.md
This tutorial demonstrates how to create a chart bound to data, so that all series are auto-created based on a common template, which specifies universal options for all series. This is possible when the data for all series (their names, along with points’ arguments and values) is stored in the same data table.
Note that in this scenario, the view type and certain other settings will be the same for all series.
In this example, we’ll bind a chart to the “GSP” table in the GSP database (the gsp.mdb file is shipped with the installation of the XtraCharts Suite). This table contains the Gross State Product (GSP) statistics for certain US regions.
To bind a chart to data using series templates, do the following.
Start MS Visual Studio and create a new Windows Forms Application or open an existing one.
For the chart to occupy all the available space on the form, click the chart’s smart tag, and in its actions list, click Dock in Parent Container.
To create a data source for a chart, select it and click its smart tag. In the invoked actions list, expand the Choose Data Source drop-down selector and click the Add Project Data Source… link.
In the first step, leave the data source type as Database and click Next.
In the Choose a Database Model page, click Next.
On the following page, click New Connection….
The following message appears.
On the following page, you are asked whether or not the created connection string should be saved to the configuration file of your project.
The final page allows you to choose tables, which should be obtained from the database.
After performing the above steps, Visual Studio generates a set of classes that support ADO.NET architecture, some of which are then auto-assigned to the appropriate properties of your chart control. In particular:
gspDataSet (a DataSet object that is a collection of possibly inter-related tables) containing the gspDataTable ;
gSPBindingSource (a BindingSource component, which provides data in the chart’s dataset) is auto-assigned to the ChartControl.DataSource property;
gSPTableAdapter - a TableAdapter object, which contains methods to get and post data from the specified table in the database.
This means that the chart has been successfully bound to the data source. The next steps explain how to assign its data fields to the chart’s series template.
Now, let’s specify a data field for as many series to be auto-created as there are records in this field. To do this, set the ChartControl.SeriesDataMember property to Year.
Now, adjust the series’ template, which is accessed via the chart’s ChartControl.SeriesTemplate property.
Now, the chart possesses complete information about the data source and its data members. However, it still doesn’t show the real data at design time. To populate your chart with genuine data, click the chart’s smart tag and click Populate.
Finally, you can specify optional properties, which will be applied to all the auto-created series.
Adjust the series name template.
Adjust the crosshair cursor options.
Run the project and view the result.
See Also