Charts

DynamicPDF Core Suite for .NET has built-in support for six different charting types. The charts rendered by DynamicPDF are vector based, allowing them to be scaled without losing resolution. Like page elements, charts can be placed anywhere on a page.

Define charts and chart types using one of the DynamicPDF series (SeriesBase).

Creating Charts

Use the following steps to create a chart.

  1. Instantiate a Chart.
  2. Instantiate a Series.
  3. Add the series to the Chart instance's PrimaryPlotArea.
  4. Fill the series with values (or fill it with series elements).
  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 simple bar chart added to a 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

A chart is a collection of PlotAreas. Usually, a chart only requires a single PlotArea (although numerous PlotAreas can be added). By default, a chart has a primary PlotArea already created (known as the PrimaryPlotArea allowing most charts to have a series of data added without requiring specifying a PlotArea.

Adding Series

The first time a series is added to a PlotArea, the PlotArea uses 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. The PlotArea determines whether the new series can share the same axis. If the series cannot share the axis, then it creates and adds an appropriate new axis. If it cannot share an axis, the min and max values on the axis will usually require adjusting.

Allowing a PlotArea to determine its relevant information simplifies an overall chart's creation.

Using a Non-Default Axis

To use a different axis than those assumed by the PlotArea, a new axis should be instantiated and passed into the constructor for the series. If a series already contains a defined axis (Axis) when added to a PlotArea, it uses the previously defined axis.

Modifying Layout Locations

By default, the AutoLayout property of a Chart instance is set to True, indicating that the, X, Y, Width and Height properties for all the chart elements (the Chart and the Legend) are calculated automatically.

Use the following steps to modify the position of any chart elements.

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

For example, to change the Y location of a Chart instance'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