Table of Contents

Charts in reports

A Chart in a report draws as a ReportPageChart: its plot, markers, data labels, axes, title and legend become shapes and text, so a chart reaches PDF, images, the browser viewer, Word and Excel. Every rule was measured against the rasters Microsoft's report engine draws.

This guide uses chart-points.rdl: a line of monthly revenue with circle markers and value labels, and a scatter of units sold against price.

Give the charts their data

using DocWright.Reporting.Data;

var sales = new ReportDataTable()
    .AddColumn("Month", typeof(string))
    .AddColumn("Revenue", typeof(double))
    .AddColumn("Price", typeof(double))
    .AddColumn("Units", typeof(double))
    .AddRow("Jan", 12400.0, 4.5, 820.0)
    .AddRow("Feb", 9800.0, 6.0, 610.0)
    .AddRow("Mar", 15300.0, 3.5, 990.0)
    .AddRow("Apr", 13100.0, 5.0, 700.0);
var provider = new ReportInMemoryDataProvider().Add("Sales", sales);

Read and draw the charts

using DocWright.Fonts;
using DocWright.Reporting.Pagination;
using DocWright.Reporting.Processing;

var fonts = new FontEngine();
using ReportInstance instance = ReportProcessor.Process(definition, provider, new ReportRunOptions());
ReportPageModel pages = ReportPaginator.Paginate(instance, fonts);

foreach (ReportPageChart chart in pages.Pages[0].Items.OfType<ReportPageChart>())
{
    // Markers are closed, filled paths; data labels and axis numbers are labels.
    int markers = chart.Shapes.OfType<ReportShapePath>().Count(p => p.Closed && p.Fill.HasValue);
    List<string> texts = chart.Shapes.OfType<ReportShapeLabel>().Select(l => l.Text).ToList();
    Console.WriteLine($"{chart.Name}: {markers} markers, labels {string.Join(" ", texts)}");
}

Output:

Revenue: 5 markers, labels Revenue by month Revenue 0 5000 10000 15000 20000 Jan Feb Mar Apr 12,400 9,800 15,300 13,100
PriceUnits: 4 markers, labels Units sold against price 0 200 400 600 800 1000 0 2 4 6 8
using DocWright.Core.Diagnostics;
using DocWright.Core.Rendering;
using DocWright.Renderers.Pdf;

using (FileStream pdf = File.Create("chart-points.pdf"))
{
    new PdfLayoutRenderer(fonts).Render(pages.ToLayoutDocument(), pdf, new RenderOptions(), new CollectingConversionDiagnostics());
}
chart-points.pdf · page 1
A line chart of revenue by month with circle markers and the value of each month written above or below its point, and a scatter chart of units sold against price drawn with diamonds
Both charts on page 1. Download: chart-points.pdf.

What is drawn

  • Plots: column, bar, line (plain or Smooth), area, pie, doughnut and scatter, with any number of series. Stacked and PercentStacked stack on a column, a bar and an area.
  • A scatter puts each point at its own X and Y from ChartDataPointValues, on a numeric X axis built the same way as the value axis. A point with no X takes its category's place, counting from 1. Without a marker each point is a small square in the series' colour.
  • Markers (ChartMarker): Square, Circle, Diamond, Triangle, Cross and Star4, Star5, Star6, Star10, at the stated Size, in the marker's own colour or the series'. Auto gives each series the next kind in that order. A line's legend swatch shows its marker.
  • Data labels (ChartDataLabel): the point's value, formatted by the label's Format, or the Label text. On a line, Auto puts a label above its point unless the point is lower than its neighbours, and keeps it inside the plot's right and bottom edges, as Reporting Services does.
Important

Three Reporting Services rules that are easy to trip over, and that DocWright follows:

  • Markers and labels go on the data point, inside ChartDataPoint, which is where Report Builder writes them. A ChartMarker on the series makes Reporting Services refuse the whole report; DocWright reports it (DXP9405) and draws the chart without it.
  • UseValueAsLabel defaults to false. A data label stating only <Visible>true</Visible> shows nothing. Set UseValueAsLabel to show the value, or give a Label.
  • Outside is not a position Reporting Services accepts: it draws an error message instead of the chart. DocWright reports it and places the labels as Auto.

Bubble, range, stepped, polar, funnel, pyramid and three-dimensional charts are not drawn. They keep their frame, axes, title and legend, and DXP9405 names what is missing.

Next steps