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

Lesson 1 - Create a Simple Chart

  • 5 minutes to read

This lesson demonstrates how to create a simple chart with two Side-by-Side Bar series, populate it with data, and adjust its common settings.

Note

You can also create a chart using the Instant Layout Assistant. For more details, see the How to: Create a Chart Using the Instant Layout Assistant (for VS 2015 and earlier) example.

To create a simple charting application, do the following.

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

  • Run Microsoft Visual Studio 2012, 2013, 2015 or 2017.
  • Create a new WPF Application project.
  • Add the ChartControl object to your project. You can do this by dragging the ChartControl item from the DX.18.2: Data & Analytics Toolbox tab.

    Lesson1_Toolbox

  • Right-click the ChartControl object and choose Layout | Reset All in the context menu. This will stretch the chart to fill the entire window.

    DXChartsGettingStarted1_015

After this action, your XAML appears as follows. If it does not, overwrite your code with the code below.

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/charts" 
        x:Class="WPFCharts_Lesson1.MainWindow"
        Title="MainWindow" Height="503.731" Width="687.687">
    <Grid>
        <dxc:ChartControl>
            <dxc:ChartControl.Legends>
                <dxc:Legend/>
            </dxc:ChartControl.Legends>
            <dxc:XYDiagram2D>
                <dxc:BarSideBySideSeries2D DisplayName="Series 1"/>
            </dxc:XYDiagram2D>
        </dxc:ChartControl>
    </Grid>
</Window>

Note

You can add the ChartControl with the XYDiagram2D diagram type and the Side-by-Side Bar series, and add a Legends to the chart by overwriting your MainWindow.xaml file with this code, without dragging the ChartControl to the window.

If the auto-created XAML does not meet your requirements, you can leave the ChartControl object in XAML, as shown below, and create chart elements manually (for example, add another diagram type to the chart using the ChartControl.Diagram property).

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/charts" 
        x:Class="WPFCharts_Lesson1.MainWindow"
        Title="MainWindow" Height="503.731" Width="687.687">
    <Grid>
        <dxc:ChartControl>            
        </dxc:ChartControl>
    </Grid>
</Window>

However, in this case, you need to manually add references to the following libraries:

DevExpress.Data.v18.2, DevExpress.Xpf.Core.v18.2, DevExpress.Charts.v18.2.Core, DevExpress.Xpf.Charts.v18.2, DevExpress.Mvvm.v18.2, DevExpress.Xpf.Printing.v18.2 and DevExpress.Printing.v18.2.Core.

DXChartsGettingStarted1_02

Note

Normally, references added to these assemblies are chosen from the Global Assembly Cache (GAC). However, if you prefer to copy them locally, or need to include them later into your product’s installation, you can find copies in the following directory: C:\Program Files (x86)\DevExpress 18.2\Components\Bin\Framework\.

In this step, leave the auto-created XAML as is.

In the next step, you will add the second Side-by-Side Bar series to the chart and populate the series points with data.

Step 2. Provide Chart Data

You will need to fill the chart with data. Start by adding the second Side-by-Side Bar series to the auto-created Diagram2D diagram type.

  • Locate the Diagram.Series property and click the ellipsis button to invoke the Collection Editor: Series. In this editor, add the second BarSideBySideSeries2D by clicking the Add button.

    DXChartsGettingStarted1_04

  • For each series, locate the Series.Points property and add 4 SeriesPoint objects to them using the Add button.

    DXChartsGettingStarted1_05

  • For each series point, set the argument and value as follows.

    SeriesPoint Argument Value
    [0] A 1
    [1] B 2
    [2] C 3
    [3] D 4

    DXChartsGettingStarted1_06

  • Do the same for the second series with the following values.

    SeriesPoint Argument Value
    [0] A 4
    [1] B 3
    [2] C 2
    [3] D 1

After these actions are complete, you will get the following XAML.

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/charts" 
        x:Class="WPFCharts_Lesson1.MainWindow"
        Title="MainWindow" Height="503.731" Width="687.687">
    <Grid>
        <dxc:ChartControl>
            <dxc:ChartControl.Legends>
                <dxc:Legend/>
            </dxc:ChartControl.Legends>
            <dxc:XYDiagram2D>
                <dxc:BarSideBySideSeries2D DisplayName="Series 1">
                    <dxc:SeriesPoint Argument="A" Value="1"/>
                    <dxc:SeriesPoint Argument="B" Value="2"/>
                    <dxc:SeriesPoint Argument="C" Value="3"/>
                    <dxc:SeriesPoint Argument="D" Value="4"/>
                </dxc:BarSideBySideSeries2D>
                <dxc:BarSideBySideSeries2D>
                    <dxc:SeriesPoint Argument="A" Value="4"/>
                    <dxc:SeriesPoint Argument="B" Value="3"/>
                    <dxc:SeriesPoint Argument="C" Value="2"/>
                    <dxc:SeriesPoint Argument="D" Value="1"/>
                </dxc:BarSideBySideSeries2D>
            </dxc:XYDiagram2D>
        </dxc:ChartControl>
    </Grid>
</Window>

Step 3. Customize the Chart

In the final step, you will learn how to customize the chart (adjust series-view-specific options, customize crosshair cursor options, etc.).

Adjust Series-view-specific Options

Next, you will make the chart appear 3D.

Add a Chart Title

  • To create a title for your chart, select it, and in the Properties grid, click the ellipsis button for the ChartControlBase.Titles property.
  • In the invoked collection editor, click Add and set the TitleBase.Content to “My Title”.

    CreateChartTitle

Customize Crosshair Cursor Options

Crosshair cursor labels and a crosshair argument line are displayed on a chart by default. You can disable (enable) a crosshair cursor at the chart level (using the ChartControlBase.CrosshairEnabled property) and at the series level (using the XYSeries2D.CrosshairEnabled property).

Result

After performing all of the steps above, 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:dxc="http://schemas.devexpress.com/winfx/2008/xaml/charts" 
        x:Class="WPFCharts_Lesson1.MainWindow"
        Title="MainWindow" Height="503.731" Width="687.687">
    <Grid>
        <dxc:ChartControl>
            <dxc:ChartControl.Titles>
                <dxc:Title Content="My Title"/>
            </dxc:ChartControl.Titles>
            <dxc:ChartControl.CrosshairOptions>
                <dxc:CrosshairOptions ShowArgumentLabels="True" ShowValueLabels="True" ShowValueLine="True"/>
            </dxc:ChartControl.CrosshairOptions>
            <dxc:ChartControl.Legends>
                <dxc:Legend/>
            </dxc:ChartControl.Legends>
            <dxc:XYDiagram2D>
                <dxc:BarSideBySideSeries2D DisplayName="Series 1" LabelsVisibility="True">
                    <dxc:BarSideBySideSeries2D.Model>
                        <dxc:Quasi3DBar2DModel/>
                    </dxc:BarSideBySideSeries2D.Model>
                    <dxc:SeriesPoint Argument="A" Value="1"/>
                    <dxc:SeriesPoint Argument="B" Value="2"/>
                    <dxc:SeriesPoint Argument="C" Value="3"/>
                    <dxc:SeriesPoint Argument="D" Value="4"/>
                </dxc:BarSideBySideSeries2D>
                <dxc:BarSideBySideSeries2D LabelsVisibility="True" DisplayName="Series 2">
                    <dxc:BarSideBySideSeries2D.Model>
                        <dxc:Quasi3DBar2DModel/>
                    </dxc:BarSideBySideSeries2D.Model>
                    <dxc:SeriesPoint Argument="A" Value="4"/>
                    <dxc:SeriesPoint Argument="B" Value="3"/>
                    <dxc:SeriesPoint Argument="C" Value="2"/>
                    <dxc:SeriesPoint Argument="D" Value="1"/>
                </dxc:BarSideBySideSeries2D>
            </dxc:XYDiagram2D>
        </dxc:ChartControl>
    </Grid>
</Window>
  • Run the project. The following image illustrates the resulting chart at runtime.

    DXChartsGettingStarted1_11

Tip

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

See Also