Skip to content

2.10 Charting Support

OgreTransporter edited this page Mar 2, 2020 · 1 revision

The PDF specification has no specific support for charting. The PDF File Writer library provides charting support by allowing the developer to create a Microsoft Charting object and draw this object as an image into the PDF file. For more information about Microsoft Chart control note MSDN library documentation Visual Studio 2012 Chart Controls. The documentation for the charting name space is available at Data Visualization Charting Namespace. The attached ChartExample.cs has four examples of charts. If you intend to use charting, you need to add System.Windows.Forms.Visualization reference to your project. In each source module using Chart you need to add using System.Windows.Forms.DataVisualization.Charting;.

Adding a chart to a PDF document is four steps process.

  1. Create Chart object.
  2. Create PdfChart object.
  3. Build the chart.
  4. Draw the PdfChart to PdfContents.

The recommended way to create a chart is to use a static method of PdfChart object.

// Document is your PdfDocument object.
// Width and height are in user units.
// Resolution is in pixels per inch.
// Resolution is optional. If not included the library will take the .net default.
// Library will create Chart object with width and height in pixels and set resolution in pixels per inch
Chart MyChart = PdfChart.CreateChart(PdfDocument Document, Double Width, Double Height, Double Resolution);

You can instantiate Chart class yourself.

Chart MyChart = new Chart();
MyChart.RenderingDpiY = 300;	// example of 300 pixels per inch
MyChart.Width = 1950;		// example of 6.5 inches in pixels	
Mychart.Height = 1350;		// example of 4.6 inches in pixels

Next you create a PdfChart from the Chart created above. Optionally, you can override the resolution.

// resolution is optional. It will override the resolution set above.
PdfChart MyPdfChart = new PdfChart(PdfDocument Document, Chart MyChart, Double Resolution);

Next you build your chart. ChartExample.cs has four examples. The documentation for building a chart is beyond the scope of this article. There plenty of examples on the Internet.

PdfChart has a CreateFont method to simplify the creation of fonts. It will calculate font size based on chart's resolution.

// FontSizeUnit is an enumeration
// Available units: pixel, point, UserUnit, Inch, cm, mm
Font CreateFont(String FontFamilyName, FontStyle Style, Double FontSize, FontSizeUnit Unit);

The last step is drawing the chart.

// Draw the chart at OrigX, OrigY in user units
// The width and height of the chart are taken from the Chart object.
// They are calculated from the size in pixels and resolution of the chart.
public void PdfContents.DrawChart(PdfChart MyPdfChart, Double OrigX, Double OrigY);

// Draw the chart at OrigX, OrigY with Width and Height as specified, all in user units.
// NOTE: Width and Height should be selected to agree with the aspect ratio of the chart object.
public void PdfContents.DrawChart(PdfChart MyPdfChart, Double OrigX, Double OrigY, Double Width, Double Height);

The PdfChart class provides some optional methods to control image positioning.

The ImageSize method returns the largest rectangle with correct aspect ratio that will fit in a given area.

SizeD ImageSize(Double Width, Double Height);

The ImageSizePosition method returns the largest rectangle with correct aspect ratio that will fit in a given area and position it based on ContentAlignment enumeration.

ImageSizePos ImageSizePosition(Double Width, Double Height, ContentAlignment Alignment);

For coding examples please review 3.10 Draw Pie Chart and ChartExample.cs source code.

Clone this wiki locally