Back to Devexpress

Lesson 1 - Bind Chart Series to Data

wpf-9757-controls-and-libraries-charts-suite-chart-control-getting-started-lesson-1-bind-chart-series-to-data.md

latest15.5 KB
Original Source

Lesson 1 - Bind Chart Series to Data

  • Oct 08, 2024
  • 6 minutes to read

This lesson explains how to add a series to a chart’s diagram, bind this series to a data source, and configure basic chart options.

Step 1. Create a New Project and Add a Chart

  1. Run Microsoft Visual Studio.

  2. Create a new WPF Application project. Name it Lesson1BindChartToData.

  3. Drag the ChartControl component from the DX.25.2: Data & Analytics toolbox section to the Main Window.

  4. Right-click the ChartControl and select Layout | Reset All in the context menu to make the chart fill the entire window.

The MainWindow markup should appear as follows:

xaml
<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Lesson1BindChartToData"
        xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/charts" 
        x:Class="Lesson1BindChartToData.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="315" Width="560">
    <Grid>
        <dxc:ChartControl/>
    </Grid>
</Window>

References to the following libraries are automatically added to the project:

  • DevExpress.Data.v25.2
  • DevExpress.Xpf.Core.v25.2
  • DevExpress.Charts.v25.2.Core
  • DevExpress.Xpf.Charts.v25.2
  • DevExpress.Mvvm.v25.2
  • DevExpress.Xpf.Printing.v25.2
  • DevExpress.Printing.v25.2.Core

Note

The references come from the Global Assembly Cache (GAC). To copy them locally or include them later in your product’s installation, use the following directory:

_C:\Program Files\DevExpress 25.2\Components\Bin\Framework_

Step 2. Prepare a Data Model

You can bind the chart to a database, to an XML file, or to data created at runtime. The data source should implement the IEnumerable or IListSource interface, or their descendants. Refer to the Providing Data section for more information about how to populate a chart with data.

In this topic, you bind a chart to an ObservableCollection<T>. Use the DataPoint class implementation to develop a Data Model:

csharp
using System.Collections.ObjectModel;
using System.Windows;

namespace Lesson1BindChartToData {
    public class DataPoint {
        public string Argument { get; set; }
        public double Value { get; set; }
        public static ObservableCollection<DataPoint> GetDataPoints() {
            return new ObservableCollection<DataPoint> {
                    new DataPoint { Argument = "Asia", Value = 5.289D},
                    new DataPoint { Argument = "Australia", Value = 2.2727D},
                    new DataPoint { Argument = "Europe", Value = 3.7257D},
                    new DataPoint { Argument = "North America", Value = 4.1825D},
                    new DataPoint { Argument = "South America", Value = 2.1172D}
                   };
        }
    }
}
vb
Imports System.Collections.ObjectModel
Imports System.Windows

Namespace Lesson1BindChartToData
    Public Class DataPoint
        Public Property Argument() As String
        Public Property Value() As Double
        Public Shared Function GetDataPoints() As ObservableCollection(Of DataPoint)
            Return New ObservableCollection(Of DataPoint) From {
                New DataPoint With {
                    .Argument = "Asia",
                    .Value = 5.289R
                },
                New DataPoint With {
                    .Argument = "Australia",
                    .Value = 2.2727R
                },
                New DataPoint With {
                    .Argument = "Europe",
                    .Value = 3.7257R
                },
                New DataPoint With {
                    .Argument = "North America",
                    .Value = 4.1825R
                },
                New DataPoint With {
                    .Argument = "South America",
                    .Value = 2.1172R
                }
            }
        End Function
    End Class
End Namespace

Step 3. Add a ViewModel

Implement the MainWindowViewModel class with the following code snippet:

csharp
using System.Collections.ObjectModel;
using System.Windows;

namespace Lesson1BindChartToData {
    public class MainWindowViewModel {
        public ObservableCollection<DataPoint> Data { get; private set; }
        public MainWindowViewModel() {
            this.Data = DataPoint.GetDataPoints();
        }
    }
}
vb
Imports System.Collections.ObjectModel
Imports System.Windows

Namespace Lesson1BindChartToData

    Public Class MainWindowViewModel
        Private privateData As ObservableCollection(Of DataPoint)
        Public Property Data() As ObservableCollection(Of DataPoint)
            Get
                Return privateData
            End Get
            Private Set(ByVal value As ObservableCollection(Of DataPoint))
                privateData = value
            End Set
        End Property
        Public Sub New()
            Me.Data = DataPoint.GetDataPoints()
        End Sub
    End Class
End Namespace

Step 4. Specify the Data Context

Build the solution. Click the MainWindow’s smart tag and specify the DataContext property as shown below:

xaml
<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Lesson1BindChartToData"
        xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/charts" 
        x:Class="Lesson1BindChartToData.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="315" Width="560">
    <Window.DataContext>
        <local:MainWindowViewModel/>
    </Window.DataContext>
    <Grid>
        <dxc:ChartControl/>
    </Grid>
</Window>

Step 5. Bind the Chart to Data

Click the Chart Control’s smart tag. Specify the ChartControl.DataSource property as shown in the following image:

The DataSource property appears in the markup:

xaml
<dxc:ChartControl DataSource="{Binding Data}"/>

Step 6. Specify the Chart Type

In the Chart Control’s smart tag menu, set the ChartControl.Diagram property to the XYDiagram2D type and add a legend and a title.

Specify the BarSideBySideSeries2D series view type in the XYDiagram2D‘s smart tag:

The following properties appear in the markup:

xaml
<dxc:ChartControl DataSource="{Binding Data}" >
    <dxc:ChartControl.Titles>
        <dxc:Title Content="Title"/>
    </dxc:ChartControl.Titles>
    <dxc:ChartControl.Legends>
        <dxc:Legend/>
    </dxc:ChartControl.Legends>
    <dxc:XYDiagram2D>
        <dxc:BarSideBySideSeries2D/>
    </dxc:XYDiagram2D>
</dxc:ChartControl>

Step 7. Populate the Series with Data

Specify the data source fields that supply values for series point arguments and values.

  1. Set the Series.ArgumentDataMember property to Argument.

  2. Set the Series.ValueDataMember property to Value.

The following properties appear in the markup:

xaml
<dxc:XYDiagram2D>
    <dxc:BarSideBySideSeries2D ArgumentDataMember="Argument" ValueDataMember="Value"/>
</dxc:XYDiagram2D>

Step 7. Customize the Chart

Specify the Series Name

Set the Series.DisplayName property to Annual Statistics. The display name identifies the series in the legend.

Customize the Chart Title

In the Title’s property grid, set the TitleBase.HorizontalAlignment property to Center. Define TitleBase.Content as Sales by Regions and click OK.

Configure the Crosshair Cursor’s Options

To customize the crosshair options, click the ChartControl.CrosshairOptions property’s New button to create a CrosshairOptions instance. Enable the following properties:

Set XYSeries2D.CrosshairLabelPattern to ${V:f2}M.

Results

Run the project to see the results.

The resulting code appears as follows:

Markup

xaml
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:Lesson1BindChartToData" 
    xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/charts"
    xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" 
    x:Class="Lesson1BindChartToData.MainWindow"
    mc:Ignorable="d" Title="MainWindow" Height="400" Width="650">
    <Window.DataContext>
        <local:MainWindowViewModel/>
    </Window.DataContext>
    <Grid>
        <dxc:ChartControl DataSource="{Binding Data}">
            <dxc:ChartControl.CrosshairOptions>
                <dxc:CrosshairOptions ShowArgumentLabels="True" 
                                      ShowValueLabels="True" 
                                      ShowValueLine="True"/>
            </dxc:ChartControl.CrosshairOptions>
            <dxc:ChartControl.Titles>
                <dxc:Title Content="Sales by Regions" 
                           HorizontalAlignment="Center"/>
            </dxc:ChartControl.Titles>
            <dxc:ChartControl.Legends>
                <dxc:Legend/>
            </dxc:ChartControl.Legends>
            <dxc:XYDiagram2D>
                <dxc:BarSideBySideSeries2D DisplayName="Annual Statistics" 
                                           ArgumentDataMember="Argument" 
                                           ValueDataMember="Value" 
                                           CrosshairLabelPattern="${V:f2}M"/>
            </dxc:XYDiagram2D>
        </dxc:ChartControl>
    </Grid>
</Window>

Code-Behind

csharp
using System.Collections.ObjectModel;
using System.Windows;
namespace Lesson1BindChartToData {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
        }
    }
    public class MainWindowViewModel {
        public ObservableCollection<DataPoint> Data { get; private set; }
        public MainWindowViewModel() {
            this.Data = DataPoint.GetDataPoints();
        }
    }
    public class DataPoint {       
        public string Argument { get; set; }
        public double Value { get; set; }
        public static ObservableCollection<DataPoint> GetDataPoints() {
            return new ObservableCollection<DataPoint> {
                    new DataPoint { Argument = "Asia", Value = 5.289D},
                    new DataPoint { Argument = "Australia", Value = 2.2727D},
                    new DataPoint { Argument = "Europe", Value = 3.7257D},
                    new DataPoint { Argument = "North America", Value = 4.1825D},
                    new DataPoint { Argument = "South America", Value = 2.1172D}
                   };
        }
    }
}
vb
Imports System.Collections.ObjectModel
Imports System.Windows
Namespace Lesson1BindChartToData
    ''' <summary>
    ''' Interaction logic for MainWindow.xaml
    ''' </summary>
    Partial Public Class MainWindow
        Inherits Window

        Public Sub New()
            InitializeComponent()
        End Sub
    End Class
    Public Class MainWindowViewModel
        Private privateData As ObservableCollection(Of DataPoint)
        Public Property Data() As ObservableCollection(Of DataPoint)
            Get
                Return privateData
            End Get
            Private Set(ByVal value As ObservableCollection(Of DataPoint))
                privateData = value
            End Set
        End Property
        Public Sub New()
            Me.Data = DataPoint.GetDataPoints()
        End Sub
    End Class
    Public Class DataPoint
        Public Property Argument() As String
        Public Property Value() As Double
        Public Shared Function GetDataPoints() As ObservableCollection(Of DataPoint)
            Return New ObservableCollection(Of DataPoint) From {
                New DataPoint With {
                    .Argument = "Asia",
                    .Value = 5.289R
                },
                New DataPoint With {
                    .Argument = "Australia",
                    .Value = 2.2727R
                },
                New DataPoint With {
                    .Argument = "Europe",
                    .Value = 3.7257R
                },
                New DataPoint With {
                    .Argument = "North America",
                    .Value = 4.1825R
                },
                New DataPoint With {
                    .Argument = "South America",
                    .Value = 2.1172R
                }
            }
        End Function
    End Class
End Namespace

See Also

Lesson 3 - Use a Series Template for Auto-Created Series

Provide Data