DynamicPDF Generator for Java 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.
To create a chart follow the simple steps below:
The following code demonstrates how to create a very simple PDF, with a bar chart added to the page.
[Java]
Document document = new Document();
Page page = new Page();
document.getPages().add(page);
Chart chart = new Chart(0, 0, 400, 200);
IndexedBarSeries barSeries1 = new IndexedBarSeries("Website A");
chart.getPrimaryPlotArea().getSeries().add(barSeries1);
barSeries1.getValues().add(new float[] { 5, 7, 9, 6 });
IndexedBarSeries barSeries2 = new IndexedBarSeries("Website B");
chart.getPrimaryPlotArea().getSeries().add(barSeries2);
barSeries2.getValues().add(new float[] { 4, 2, 5, 8 });
IndexedBarSeries barSeries3 = new IndexedBarSeries("Website C");
chart.getPrimaryPlotArea().getSeries().add(barSeries3);
barSeries3.getValues().add(new float[] { 2, 4, 6, 9 });
page.getElements().add(chart);
document.draw("[PhysicalPath]/Simplechart.pdf");
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).
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.
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.
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:
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:
[Java]
chart.setAutoLayout(false);
chart.layout();
chart.getLegends().getLegend(1).setY(0);