Skip to main content
A newer version of this page is available. .

Lesson 3 - Bind Chart Series to Data

  • 5 minutes to read

This lesson explains how to manually add a series to a chart’s diagram, and then bind this series to a datasource. If you wish to learn how to automatically generate a series from a datasource, proceed to Lesson 4 - Use a Series Template for Auto-Created Series.

Note that although the Microsoft Access database (.mdb) is used in this tutorial, the ChartControl can be bound to a variety of other data providers. For more information on data binding, refer to the Providing Data section.

To create a data-bound charting application, do the following.

Step 1. Create a New Project and Add a Chart Control

  • Run MS Visual Studio 2012, 2013, 2015 or 2017.
  • Create a new WPF Application project.
  • Add the ChartControl component to your project, as you did in Lesson 1 (see step 1).

Step 2. Connect to a Datasource

In this step, you will bind the ChartControl to the CarsDB database. The chart will represent this table as bars.

To perform design-time data binding, the Items Source Configuration Wizard is used. To add a datasource to a WPF application, proceed with the following steps.

  • Locate the ChartControl in the form designer and click its smart-tag. Then, in the ChartControl actions list, click the Data Source Wizard to invoke the wizard.

    ClickDataSourceWizard

  • Select the ADO.NET Typed DataSet data technology and click the New Data Source… button.

    ADOTypedDataSet

    A message box that notifies you of the necessity to rebuild the solution and reopen the Items Source Configuration Wizard appears.

  • Click Ok to run the Data Source Configuration Wizard.

    InfoMessageBox

  • In the Data Source Configuration Wizard, leave the Database item as is and click Next.

    LeaveDataBaseItem

  • In the next page, click Next.

    DXChartsGettingStarted3_02

  • On the Choose Your Data Connection page, click New Connection… to create a connection to a database.

    ChooseNewConnection

    This invokes the Add Connection dialog.

  • Leave the Microsoft Access Database File as your data source. Then, click the Browse… button and specify the CarsDB.mdb database as your data connection. By default, it is stored in the following path.

    C:\Users\Public\Documents\DevExpress Demos 18.2\Components\Data

    Lesson3_AddConnection

  • Click OK to close the dialog and then click Next in the wizard.
  • This invokes a message box that asks whether or not to copy the database file to the project and modify the connection. Click Yes, as shown below.

    ModifyConnectionMessageBox

  • In the next wizard page, you can choose whether to save the connection settings to the application configuration file or not.

    Lesson3_SaveConnectionString

    Make sure that the Yes, save the connection as: check box is checked and click Next.

  • Expand the Tables list and select the Cars item. Then click Finish to close the Data Source Configuration Wizard.

    Lesson3_SelectCarsItem

Step 3. Bind the Chart to a Datasource

  • Rebuild the solution and open the Items Source Configuration Wizard, as you did in the previous step. The newly created data source will be displayed within the Data Sources section. Click Next.

    Lesson3_AssignDataSource

  • Leave Simple Binding to bind the control to a plain collection of data objects and click Next.

    Lesson3_LeaveSimpleBinding

  • Leave the Cars table and click Finish.

    Lesson3_LeaveCarsTable

  • Specify the name of the data field that contains the series points’ argument. To do this, locate the Series.ArgumentDataMember property of the BarSideBySideSeries2D object in the Properties window and set it to Model.

    DXChartsGettingStarted2_06

  • To define the bound datasource’s data field from which the series obtains the values of its points, set the BarSideBySideSeries2D‘s Series.ValueDataMember property to Price.

    DXCharts_GettingStarted_6_1

    After performing this step, your XAML should look like this.

    <Window
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" 
            xmlns:CarsDBDataSetTableAdapters="clr-namespace:Lesson3.CarsDBDataSetTableAdapters" 
            xmlns:local="clr-namespace:Lesson3" xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/charts" 
            x:Class="Lesson3.MainWindow"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <dx:TypedSimpleSource x:Key="TypedSimpleSource" AdapterType="{x:Type CarsDBDataSetTableAdapters:CarsTableAdapter}" 
                                  ContextType="{x:Type local:CarsDBDataSet}" Path="Cars">
                <dx:DesignDataManager.DesignData>
                    <dx:DesignDataSettings RowCount="5"/>
                </dx:DesignDataManager.DesignData>
            </dx:TypedSimpleSource>
        </Window.Resources>
    
        <Grid>
            <dxc:ChartControl DataSource="{Binding Data, Source={StaticResource TypedSimpleSource}}">
                <dxc:ChartControl.Legend>
                    <dxc:Legend/>
                </dxc:ChartControl.Legend>
                <dxc:XYDiagram2D>
                    <dxc:BarSideBySideSeries2D DisplayName="Series 1"
                                               ArgumentDataMember="Model" ValueDataMember="Price"/>
                </dxc:XYDiagram2D>
            </dxc:ChartControl>
        </Grid>
    </Window>
    

Step 4. Customize the Chart

Result

After performing all of the above steps, your XAML should appear as follows.

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" 
        xmlns:CarsDBDataSetTableAdapters="clr-namespace:Lesson3.CarsDBDataSetTableAdapters" 
        xmlns:local="clr-namespace:Lesson3" xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/charts" 
        x:Class="Lesson3.MainWindow"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <dx:TypedSimpleSource x:Key="TypedSimpleSource" AdapterType="{x:Type CarsDBDataSetTableAdapters:CarsTableAdapter}" 
                              ContextType="{x:Type local:CarsDBDataSet}" Path="Cars">
            <dx:DesignDataManager.DesignData>
                <dx:DesignDataSettings RowCount="5"/>
            </dx:DesignDataManager.DesignData>
        </dx:TypedSimpleSource>
    </Window.Resources>

    <Grid>
        <dxc:ChartControl DataSource="{Binding Data, Source={StaticResource TypedSimpleSource}}">
            <dxc:ChartControl.CrosshairOptions>
                <dxc:CrosshairOptions ShowArgumentLabels="True" ShowValueLine="True" ShowValueLabels="True"/>
            </dxc:ChartControl.CrosshairOptions>
            <dxc:ChartControl.Legend>
                <dxc:Legend/>
            </dxc:ChartControl.Legend>
            <dxc:XYDiagram2D>
                <dxc:XYDiagram2D.AxisX>
                    <dxc:AxisX2D>
                        <dxc:AxisX2D.Label>
                            <dxc:AxisLabel Staggered="True"/>
                        </dxc:AxisX2D.Label>
                    </dxc:AxisX2D>
                </dxc:XYDiagram2D.AxisX>
                <dxc:BarSideBySideSeries2D DisplayName="Cars"
                                           ArgumentDataMember="Model" ValueDataMember="Price"/>
            </dxc:XYDiagram2D>
        </dxc:ChartControl>
    </Grid>
</Window>

Run the project. The following image illustrates the resulting chart at runtime.

DXChartsGettingStarted2_08

Tip

A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E3155.

See Also