Skip to main content
You are viewing help content for pre-release software. This document and the features it describes are subject to change.
All docs
V26.2
  • Manage Charts

    • 14 minutes to read

    The Word Processing Document API allows you to add charts to a document and modify charts in existing documents. A chart is a shape (a ShapeBase descendant) in a paragraph. The API includes two chart classes:

    Class Chart Types
    ChartShape Standard charts (for example, bar, line, pie, and area charts). The ChartType enumeration lists these chart types.
    ChartExShape Charts of types introduced in Microsoft Word 2016 (for example, treemap, sunburst, funnel, waterfall, and histogram charts). The ChartExType enumeration lists these chart types.

    The series type specifies the chart type. For example, a BarSeries object creates a bar chart, and a TreemapSeries object creates a treemap chart.

    If you do not specify the chart size, the API inserts a 6 x 3.5 inch chart. Use the ShapeBase.Size property to specify the chart size in document units (1/300 of an inch).

    Add a Standard Chart to a Document

    Follow the steps below to add a standard chart to a document:

    1. Create a ChartShape object.
    2. Create a series of the required type and specify series values and arguments. Add the series to the ChartShape.Series collection.
    3. Optional. Use ChartShape.ChartTitle, ChartShape.Legend, and ChartShape.Style properties to customize the chart.
    4. Add the chart to the Paragraph.InlineElements collection.

    The following code snippet adds a bar chart with two series to a document:

    DevExpress Word Processing Document API - Bar Chart with Two Series in a Document

    using System.IO;
    using DevExpress.Docs.Office;
    using DevExpress.Docs.Word;
    
    // Create a document and add a paragraph.
    using var document = new WordDocument();
    var paragraph = document.Body.AddParagraph();
    
    // Create a bar chart with two series that share arguments.
    var quarters = new ChartStringData(new[] { "Q1", "Q2", "Q3", "Q4" });
    var chart = new ChartShape();
    chart.Series.Add(new BarSeries {
        Arguments = quarters,
        Values = new ChartNumericData(new double[] { 12, 15.5, 14, 18 })
    });
    chart.Series.Add(new BarSeries {
        Arguments = quarters,
        Values = new ChartNumericData(new double[] { 9.8, 11.2, 13, 12.5 })
    });
    
    // Specify the chart title, legend position, and chart style.
    chart.ChartTitle = new ChartTitle(new TextArea("Quarterly Sales"));
    chart.Legend = new Legend { Position = LegendPositionType.Bottom };
    chart.Style = ChartStyle.ColorGradient;
    
    // Insert the chart into the paragraph.
    paragraph.InlineElements.Add(chart);
    
    // Save the document to the output folder.
    Directory.CreateDirectory("output");
    using var output = File.Create(Path.Combine("output", "Document.docx"));
    document.Save(output);
    

    To bind series to cells in the chart’s embedded workbook instead of value arrays, use the ChartShape.Data property to assign cell values, and assign ChartDataExpressionReference objects (for example, Sheet1!$B$2:$B$5) to series values and arguments.

    Add a Chart of a Microsoft Word 2016 Type

    To add a chart of a type introduced in Microsoft Word 2016, create a ChartExShape object and add a series of the required type (for example, TreemapSeries) to the ChartExShape.Series collection. The following code snippet adds a treemap chart to a document:

    DevExpress Word Processing Document API - Treemap Chart in a Document

    using System.IO;
    using DevExpress.Docs.Office;
    using DevExpress.Docs.Word;
    
    // Create a document.
    using var document = new WordDocument();
    
    // Create a treemap chart.
    // The first array contains parent categories, and the second array contains child categories.
    var chart = new ChartExShape();
    chart.Series.Add(new TreemapSeries {
        Arguments = new ChartHierarchicalStringData(
        new string[] { "A", "A", "A", "A", "B", "B", "B", "B", "C", "C", "C", "C" },
        new string[] { "A1", "A2", "A3", "A4", "B1", "B2", "B3", "B4", "C1", "C2", "C3", "C4" }),
        Values = new ChartNumericData(new[] { 10.0, 12.0, 5.0, 8.0, 15.0, 10.0, 12.0, 5.0, 8.0, 15.0, 9.0, 11.0 })
    });
    
    // Add a paragraph and insert the chart into the paragraph.
    document.Body.AddParagraph().InlineElements.Add(chart);
    
    // Save the document to the output folder.
    Directory.CreateDirectory("output");
    using var output = File.Create(Path.Combine("output", "Document.docx"));
    document.Save(output);
    

    Display Data Labels

    Data labels display values, category names, or series names next to data points. You can configure data labels at the following levels:

    Series-level settings take precedence over view-level settings.

    Use DataLabelBase.ShowValue, DataLabelBase.ShowCategoryName, and DataLabelBase.ShowSeriesText properties to specify label content. The LabelPosition property (for example, BarDataLabels.LabelPosition) specifies the label position relative to the data point. Available positions depend on the chart type.

    In a ChartExShape, configure data labels for an individual series (for example, use the TreemapSeries.DataLabels property).

    The following code snippet displays values above bars in the chart from the Add a Standard Chart to a Document section and applies a number format to the values:

    using DevExpress.Docs.Office;
    
    // Display values above bars in all bar series.
    var barView = (BarChartView)chart.Views[0];
    barView.DataLabels.ShowValue = true;
    barView.DataLabels.LabelPosition = BarDataLabelPosition.OutsideEnd;
    
    // Display values with one decimal place.
    barView.DataLabels.NumberFormat = new NumberFormatOptions {
        FormatCode = "0.0",
        IsSourceLinked = false
    };
    

    Configure Axes

    When you add the first series to a ChartShape, the chart creates axes that the series type requires. For example, a bar chart has an argument axis and a value axis. A pie chart has no axes. Use the following properties to access axes:

    Use the following properties to customize an axis:

    To configure axes in a chart of a Microsoft Word 2016 type, use ChartExShape.ArgumentAxis, ChartExShape.ValueAxis, and ChartExShape.SecondaryValueAxis properties.

    The following code snippet adds axis titles to the chart from the Add a Standard Chart to a Document section, specifies the value axis range, and customizes gridlines:

    using System.Drawing;
    using DevExpress.Docs.Office;
    
    // Add axis titles.
    chart.ArgumentAxis.Title = new ChartTitleOptions("Quarter");
    chart.ValueAxis.Title = new ChartTitleOptions("Millions of USD");
    
    // Specify the value axis range and the interval between major gridlines.
    chart.ValueAxis.Scaling.Min = 0;
    chart.ValueAxis.Scaling.Max = 20;
    chart.ValueAxis.MajorUnit = 5;
    
    // Display light gray major gridlines.
    chart.ValueAxis.MajorGridlines = new ChartLineOptions {
        OutlineStyle = new OutlineStyle { Fill = new SolidFill(Color.LightGray) }
    };
    

    Create a Combo Chart

    A ChartShape can display series of different types (for example, bars and a line). Add series of these types to the ChartShape.Series collection. The chart creates a separate view for each series type and axis group.

    Series in the same axis group require the same axis types. If an axis group contains series that require other axis types (for example, a bar series and a scatter series), the Add method of the ChartShape.Series collection does not add the series and throws an InvalidOperationException.

    To plot a series against secondary axes (for example, if series values differ in scale), set the series AxisGroup property (for example, LineSeries.AxisGroup) to ChartAxisGroupType.Secondary. The chart creates ChartShape.SecondaryArgumentAxis and ChartShape.SecondaryValueAxis axes for the secondary axis group.

    Secondary axes have the same default positions as primary axes. To display the secondary value axis on the right side of the plot area, set the Axis.CrossType property of the secondary value axis to Max.

    The following code snippet creates a combo chart that displays revenue as bars and profit margin as a line plotted against the secondary value axis:

    using System.IO;
    using DevExpress.Docs.Office;
    using DevExpress.Docs.Word;
    
    // Create a document and add a paragraph.
    using var document = new WordDocument();
    var paragraph = document.Body.AddParagraph();
    
    // Add a bar series. The series uses the primary axes.
    var months = new ChartStringData(new[] { "Jan", "Feb", "Mar", "Apr" });
    var chart = new ChartShape();
    chart.Series.Add(new BarSeries {
        Text = "Revenue",
        Arguments = months,
        Values = new ChartNumericData(new double[] { 42000, 48500, 51200, 57800 })
    });
    
    // Add a line series and plot it against the secondary axes.
    chart.Series.Add(new LineSeries {
        Text = "Profit Margin",
        Arguments = months,
        Values = new ChartNumericData(new double[] { 0.18, 0.21, 0.19, 0.24 }),
        AxisGroup = ChartAxisGroupType.Secondary
    });
    
    // Hide the secondary argument axis.
    chart.SecondaryArgumentAxis.Visible = false;
    
    // Display the secondary value axis on the right side without gridlines.
    chart.SecondaryValueAxis.CrossType = AxisCrossType.Max;
    chart.SecondaryValueAxis.MajorGridlines = null;
    
    // Display secondary axis values as percentages.
    chart.SecondaryValueAxis.NumberFormat = new NumberFormatOptions {
        FormatCode = "0%",
        IsSourceLinked = false
    };
    
    // Specify the chart title and legend position.
    chart.ChartTitle = new ChartTitle(new TextArea("Revenue and Profit Margin"));
    chart.Legend = new Legend { Position = LegendPositionType.Bottom };
    
    // Insert the chart into the paragraph.
    paragraph.InlineElements.Add(chart);
    
    // Save the document to the output folder.
    Directory.CreateDirectory("output");
    using var output = File.Create(Path.Combine("output", "Document.docx"));
    document.Save(output);
    

    Customize Individual Data Points

    To modify the appearance of an individual data point, use the series CustomDataPoints property (for example, BarSeriesBase.CustomDataPoints). The CustomDataPoints dictionary maps zero-based point indexes to DataPoint objects. A DataPoint object specifies fill, outline, marker, and other settings that take precedence over series settings for the point.

    To customize the label of an individual data point, add a data label object of the chart type (for example, BarDataLabel) to the series DataLabels collection. Use the zero-based point index as the key.

    In a ChartExShape, use the SeriesExBase.CustomDataPoints property and DataPointEx objects.

    The following code snippet highlights the fourth bar of the first series in the chart from the Add a Standard Chart to a Document section and displays a data label for this bar:

    using System.Drawing;
    using DevExpress.Docs.Office;
    
    var series = (BarSeries)chart.Series[0];
    
    // Apply orange fill to the fourth bar (Q4).
    series.CustomDataPoints.Add(3, new DataPoint { Fill = new SolidFill(Color.Orange) });
    
    // Display a data label for the fourth bar.
    series.DataLabels.Add(3, new BarDataLabel { ShowValue = true });
    

    Modify Charts in a Document

    To find charts, iterate through paragraphs in the document body and check the type of each element in the Paragraph.InlineElements collection.

    The following code snippet loads a document, displays legends of standard charts at the top, and applies the GrayOutline chart style:

    using System.IO;
    using DevExpress.Docs.Office;
    using DevExpress.Docs.Word;
    
    using var document = new WordDocument(File.OpenRead("Document.docx"));
    
    foreach (var block in document.Body) {
        if (block is not Paragraph paragraph)
            continue;
        foreach (var element in paragraph.InlineElements) {
            if (element is ChartShape chart) {
                chart.Legend = new Legend { Position = LegendPositionType.Top };
                chart.Style = ChartStyle.GrayOutline;
            }
        }
    }
    

    To delete a chart, pass the ChartShape or ChartExShape object to the Remove method of the Paragraph.InlineElements collection.

    Supported Chart Types

    A Chart object maintains two collections: Series and Views. Series objects supply data and define the chart type. Views store rendering settings that apply to all series of the same type. When you add a series, the API associates the series with an existing view or creates a new view.

    The ChartType enumeration lists chart types available for the ChartShape class:

    Enumeration Value Series Object View Object
    Area
    DevExpress Office Charts API - Charts - Area
    Stacked Area
    DevExpress Office Charts API - Charts - Stacked Area
    100% Stacked Area
    DevExpress Office Charts API - Charts - 100% Stacked Area
    AreaSeries AreaChartView
    3D Area
    DevExpress Office Charts API - Charts - Area 3D
    3D Stacked Area
    DevExpress Office Charts API - Charts - 3D Stacked Area
    3D 100% Stacked Area
    DevExpress Office Charts API - Charts - 3D 100% Stacked Area
    Area3DSeries Area3DChartView
    Clustered Bar
    DevExpress Office Charts API - Charts - Clustered Bar
    Stacked Bar
    DevExpress Office Charts API - Charts - Stacked Bar
    100% Stacked Bar
    DevExpress Office Charts API - Charts - 100% Stacked Bar
    Clustered Column
    DevExpress Office Charts API - Charts - Column
    Stacked Column
    DevExpress Office Charts API - Charts - Stacked Column
    100% Stacked Column
    DevExpress Office Charts API - Charts - 100% Stacked Column
    BarSeries BarChartView
    3D Clustered Bar
    DevExpress Office Charts API - Charts - Clustered Bar 3D
    3D Stacked Bar
    DevExpress Office Charts API - Charts - 3D Stacked Bar
    3D 100% Stacked Bar
    DevExpress Office Charts API - Charts - 3D 100% Stacked Bar
    3D Clustered Column
    DevExpress Office Charts API - Charts - Clustered Column 3D
    3D Stacked Column
    DevExpress Office Charts API - Charts - 3D Stacked Column
    3D 100% Stacked Column
    DevExpress Office Charts API - Charts - 3D 100% Stacked Column
    3D Column
    DevExpress Office Charts API - Charts - Column 3D
    Bar3DSeries Bar3DChartView
    Bubble
    DevExpress Office Charts API - Charts - Bubble
    3D Bubble
    DevExpress Office Charts API - Charts - 3D Bubble
    BubbleSeries BubbleChartView
    Doughnut
    DevExpress Office Charts API - Charts - Doughnut
    DoughnutSeries DoughnutChartView
    Line
    DevExpress Office Charts API - Charts - Line
    Stacked Line
    DevExpress Office Charts API - Charts - Stacked Line
    100% Stacked Line
    DevExpress Office Charts API - Charts - 100% Stacked Line
    LineSeries LineChartView
    3D Line
    DevExpress Office Charts API - Charts - Line 3D
    Line3DSeries Line3DChartView
    Pie of Pie
    DevExpress Office Charts API - Charts - Pie of Pie
    Bar of Pie
    DevExpress Office Charts API - Charts - Bar of Pie
    ChartOfPieSeries ChartOfPieView
    Pie
    DevExpress Office Charts API - Charts - Pie
    PieSeries PieChartView
    Pie 3D
    DevExpress Office Charts API - Charts - Pie 3D
    Pie3DSeries Pie3DChartView
    Radar
    DevExpress Office Charts API - Charts - Radar
    Filled Radar
    DevExpress Office Charts API - Charts - Filled Radar
    RadarSeries RadarChartView
    Scatter
    DevExpress Office Charts API - Charts - Scatter
    ScatterSeries ScatterChartView
    High-Low-Close
    DevExpress Office Charts API - Charts - High-Low-Close
    Open-High-Low-Close
    DevExpress Office Charts API - Charts - Open-High-Low-Close
    Volume-High-Low-Close
    DevExpress Office Charts API - Charts - Volume-High-Low-Close
    Volume-Open-High-Low-Close
    DevExpress Office Charts API - Charts - Volume-Open-High-Low-Close
    StockSeries StockChartView
    Surface
    DevExpress Office Charts API - Charts - Surface
    SurfaceSeries SurfaceChartView
    Surface 3D
    DevExpress Office Charts API - Charts - Surface 3D
    Surface3DSeries Surface3DChartView

    The ChartExType enumeration lists Microsoft Word 2016 chart types available for the ChartExShape class:

    Chart type Series type
    Box and Whisker
    DevExpress Office Charts API - Charts - Box and Whisker
    BoxAndWhiskerSeries
    Funnel
    DevExpress Office Charts API - Charts - Funnel
    FunnelSeries
    Histogram
    DevExpress Office Charts API - Charts - Histogram
    HistogramSeries
    Pareto
    DevExpress Office Charts API - Charts - Pareto
    ParetoSeries
    Filled Map
    DevExpress Office Charts API - Charts - Map
    MapSeries
    Sunburst
    DevExpress Office Charts API - Charts - Sunburst
    SunburstSeries
    Treemap
    DevExpress Office Charts API - Charts - Treemap
    TreemapSeries
    Waterfall
    DevExpress Office Charts API - Charts - Waterfall
    WaterfallSeries

    Call the series ConvertTo method to convert a series to another chart type. The ConvertTo method returns a new series with data and settings converted from the original series. Remove the original series from the chart and add the converted series:

    SeriesBase convertedSeries = series.ConvertTo(ChartType.Line);
    chart.Series.Remove(series);
    chart.Series.Add(convertedSeries);