ceTe Software Help Library for Java August - 2020
DynamicPDF Generator for Java / Programming with Generator for Java / Charts
In This Topic
    Charts
    In This Topic

    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. 

    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.

    [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");
    

    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:

    [Java]
         chart.setAutoLayout(false);
         chart.layout();
         chart.getLegends().getLegend(1).setY(0);
    

    In This Section

    Chart Types and Examples
    Lists each supported chart type and a corresponding example.
    Chart Elements
    Lists and explains the different chart elements.
    See Also