This demo uses the DevExpress Word Processing Document API to generate a chart in a new document. The demo generates a combo chart: bars or lines visualize different series.
Select Load from Template to apply settings from a CRTX template file. This chart template format is compatible with Microsoft Office applications such as Word or Excel.
The Chart Settings section allows you to specify chart display options. Note that these options may not be applied if you chose to use a chart template.
Click Generate Chart and Save as… to select an output format, generate a document, and download the result.
using DevExpress.Office.Services;
using DevExpress.Spreadsheet;
using DevExpress.Spreadsheet.Charts;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using DevExpress.XtraSpreadsheet.Services;
using System.Drawing;
Stream GenerateDocumentWithChart(Stream chartTemplateInputStream, ChartOptions options, DevExpress.XtraRichEdit.DocumentFormat outputFormat) {
OfficeCharts.Instance.ActivateCrossPlatformCharts();
using var wordProcessor = new RichEditDocumentServer();
var document = wordProcessor.Document;
// Set measurement unit to inches
document.Unit = DevExpress.Office.DocumentUnit.Inch;
// Create chart shape
var chartShape = document.Shapes.InsertChart(document.Range.End, DevExpress.XtraRichEdit.API.Native.ChartType.ColumnClustered);
// Specify the chart size and position
chartShape.Size = new System.Drawing.SizeF(6, 5);
chartShape.RelativeHorizontalPosition = ShapeRelativeHorizontalPosition.Column;
chartShape.RelativeVerticalPosition = ShapeRelativeVerticalPosition.Paragraph;
chartShape.Offset = new PointF(0, 0);
// Access the spreadsheet chart object
var chart = (ChartObject)chartShape.ChartFormat.Chart;
// Access a worksheet that stores chart data
var worksheet = (Worksheet)chartShape.ChartFormat.Worksheet;
// 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, DevExpress.Spreadsheet.BorderLineStyle.Thin);
worksheet.Columns.AutoFit(0, 2);
// Select chart data
chart.SelectData(dataRange);
// 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
wordProcessor.SaveDocument(outputStream, outputFormat);
outputStream.Position = 0;
return outputStream;
}
public class ChartOptions {
public bool LoadFromTemplate { 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;
}