Charts

This module uses the DevExpress Spreadsheet API to add a chart to an Excel document.

Specify basic chart positioning and content options: Create Chart Sheet and Show Trendline.

Select Load From Template to use a predefined chart template. If you use a template, changes in the Chart Settings section may be ignored.

Use the Generate Chart and Save as... dropdown button to select the output format, generate the document, and download the result.

Sample Document
ChartTemplate.crtx



using DevExpress.Spreadsheet;
using DevExpress.Spreadsheet.Charts;
using DevExpress.XtraSpreadsheet.Services;
using DevExpress.Office.Services;
using System.Drawing;

Stream GenerateDocumentWithChart(Stream chartTemplateInputStream, ChartOptions options, DocumentFormat outputFormat) {
    using var workbook = new Workbook();
    var worksheet = workbook.Worksheets.First();
    worksheet.Name = "Chart Data";

    // Specify chart data
    var months = new[] { "January", "February", "March", "April", "May", "June" };
    var units = new[] { 50, 100, 30, 104, 87, 150 };
    var transactions = new[] { 900, 3000, 1200, 7000, 5100, 7500 };

    worksheet["A1"].Value = "Month";
    worksheet["B1"].Value = "Units Sold";
    worksheet["C1"].Value = "Total Transactions";

    for(int i = 0; i < months.Length; i++) {
        int row = i + 2;
        worksheet[$"A{row}"].Value = months[i];
        worksheet[$"B{row}"].Value = units[i];
        worksheet[$"C{row}"].Value = transactions[i];
    }

    // Apply formatting to chart data cells
    var headerRange = worksheet["A1:C1"];
    headerRange.Font.Bold = true;
    headerRange.Fill.BackgroundColor = Color.LightGray;

    var dataRange = worksheet.GetDataRange();
    dataRange.Borders.SetOutsideBorders(Color.Black, BorderLineStyle.Thin);

    worksheet.Columns.AutoFit(0, 2);

    // Create chart
    ChartObject chart;

    if(options.CreateChartSheet) {
        var chartSheet = workbook.ChartSheets.Add(ChartType.ColumnClustered, dataRange);
        chartSheet.Name = "Chart Sheet";
        chart = chartSheet.Chart;
    } else {
        var chartInstance = worksheet.Charts.Add(ChartType.ColumnClustered, dataRange);
        // For an embedded chart, set the location and size
        chartInstance.TopLeftCell = worksheet.Cells["F1"];
        chartInstance.BottomRightCell = worksheet.Cells["R30"];
        chart = chartInstance;
    }

    // Configure secondary series
    chart.Series[1].ChangeType(DevExpress.Spreadsheet.Charts.ChartType.LineMarker);
    chart.Series[1].AxisGroup = AxisGroup.Secondary;

    // Apply chart formatting
    chart.Title.SetValue("Example Chart");
    chart.Title.Visible = true;
    chart.Title.Font.Color = Color.FromArgb(0x1D, 0x2B, 0x64);

    chart.Legend.Visible = options.ShowLegend;
    chart.Legend.Position = options.LegendPosition;

    chart.Views[0].DataLabels.ShowValue = options.ShowDataLabels;

    // Apply primary axis settings
    if(chart.PrimaryAxes.Count >= 2) {
        var xAxis = chart.PrimaryAxes[0];
        var yAxis = chart.PrimaryAxes[1];

        xAxis.Title.SetValue("Months");
        yAxis.Title.SetValue("Units");

        xAxis.Title.Visible = yAxis.Title.Visible = options.ShowAxisTitles;
        xAxis.MajorGridlines.Visible = yAxis.MajorGridlines.Visible = options.ShowGridLines;
    }

    // Add trendline for the first series
    if(options.ShowTrendline) {
        var series = chart.Series[0];
        var trendline = series.Trendlines.Add(ChartTrendlineType.Linear);

        trendline.CustomName = $"{series.SeriesName.PlainText} Trend";
        trendline.Outline.SetSolidFill(Color.Red);
    }

    // Load chart template
    if(options.LoadFromTemplate) {
        chart.LoadTemplate(chartTemplateInputStream);
    }

    // Prepare output stream
    var outputStream = new MemoryStream();

    // Save the document
    workbook.SaveDocument(outputStream, outputFormat);

    outputStream.Position = 0;
    return outputStream;
}

public class ChartOptions {
    public bool LoadFromTemplate { get; set; }
    public bool CreateChartSheet { get; set; }
    public bool ShowTrendline { get; set; }
    public bool ShowDataLabels { get; set; }
    public bool ShowAxisTitles { get; set; }
    public bool ShowGridLines { get; set; }
    public bool ShowLegend { get; set; }
    public LegendPosition LegendPosition { get; set; } = LegendPosition.Right;
}