Charts

DynamicPDF Core Suite for .NET has built-in support for six different charting types. The charts rendered with DynamicPDF are vector based and therefore can be scaled to any size without losing resolution. Like any other page elements, charts can be placed at any location on a page.

Creating Charts

To create a chart follow the simple steps below:

  1. Instantiate a Chart.
  2. Instantiate a Series.
  3. Add the series to the Chart's PrimaryPlotArea.
  4. Fill the series with values (or fill it with SeriesElements).
  5. Add any chart titles, axis data, etc. to the series or chart
  6. Add the Chart to the Page it is to be displayed on.

Simple Example

The following code demonstrates how to create a very simple PDF, with a bar chart added to the page.

Document document = new Document();
Page page = new Page();
document.Pages.Add(page);

Chart chart = new Chart(0, 0, 400, 200);

IndexedBarSeries barSeries1 = new IndexedBarSeries("Website A");
chart.PrimaryPlotArea.Series.Add(barSeries1);
barSeries1.Values.Add(new float[] { 5, 7, 9, 6 }); 
IndexedBarSeries barSeries2 = new IndexedBarSeries("Website B");
chart.PrimaryPlotArea.Series.Add(barSeries2);
barSeries2.Values.Add(new float[] { 4, 2, 5, 8 });
IndexedBarSeries barSeries3 = new IndexedBarSeries("Website C");
chart.PrimaryPlotArea.Series.Add(barSeries3);
barSeries3.Values.Add(new float[] { 2, 4, 6, 9 });

page.Elements.Add(chart);
document.Draw(pdfFilePath);        
Dim MyDocument As New Document()
Dim MyPage As New Page()
MyDocument.Pages.Add(MyPage)

Dim MyChart As New Chart(0, 0, 400, 200)

Dim MyBarSeries1 As New IndexedBarSeries("Website A")
MyChart.PrimaryPlotArea.Series.Add(MyBarSeries1)
MyBarSeries1.Values.Add(New Single() {5, 7, 9, 6})
Dim MyBarSeries2 As New IndexedBarSeries("Website B")
MyChart.PrimaryPlotArea.Series.Add(MyBarSeries2)
MyBarSeries2.Values.Add(New Single() {4, 2, 5, 8})
Dim MyBarSeries3 As New IndexedBarSeries("Website C")
MyChart.PrimaryPlotArea.Series.Add(MyBarSeries3)
MyBarSeries3.Values.Add(New Single() {2, 4, 6, 9})

MyPage.Elements.Add(MyChart)
MyDocument.Draw(pdfFilePath)        

A Default Chart

The chart page element in its simplest form is a collection of PlotAreas. In a lot of cases a chart will only need to contain one PlotArea (although any number of PlotAreas can be added). Since by default, every chart has a primary PlotArea already created (known as the PrimaryPlotArea) most charts are ready to have series of data added to them right away. A series is what is used to add the actual data to a chart (series are actually added to a PlotArea).

Adding Series

The first time a series is added to a PlotArea, the PlotArea will use that initial series to logically determine certain property values for the PlotArea and the Chart to use. The Axis, Axis Labels, Tick Marks and Legend are all created by default when the series is added to a PlotArea.

As in our above example, a chart was created, three IndexedBarSeries were defined and those series were added to the PrimaryPlotArea. When the first series was added to the PlotArea, the specific type of X and Y Axis for that PlotArea were determined along with the min and max axis values (based on the values in that series passed in). The Legend was automatically created and an entry for that series was added. As additional series were added to the PrimaryPlotArea, additional determinations were made. The PlotArea determines whether the new series can share the same axis (if it cannot share, it will create and add the appropriate new axis) and whether the min and max values on the axis need to be adjusted. Allowing the PlotArea to determine all this information will greatly simplify the overall chart creation process for developers.

Using a Non-Default Axis

If the developer wishes to use different Axis than those assumed from the PlotArea then the new Axis should be instantiated and passed into the constructor for the series. In this case, if a series already contains a defined Axis when added to a PlotArea, those Axes already defined will be used.

Modifying Layout Locations

By default, the AutoLayout property of the Chart is set to true. Being set to true indicates that the X, Y, Width and Height properties for all the chart elements (the Chart and the Legend) are calculated automatically. In order to modify the position of any chart elements use the following steps:

  1. Make sure the element that you want to change the location of is already added to the Chart (either directly or indirectly).
  2. Set the Chart's AutoLayout property to false.
  3. Call the Chart's Layout() method.
  4. Manually set the desired element properties.

For example, to change the Y location of the Chart's Legend, add the following three lines of code to the above example directly before the call to the Draw method:

chart.AutoLayout = false;
chart.Layout();
chart.Legends[0].Y = 0;        
MyChart.AutoLayout = False
MyChart.Layout()
MyChart.Legends(0).Y = 0 

In this topic