Skip to content

Plugins: JFreeChart

danfickle edited this page Jul 17, 2019 · 1 revision

The JFreeChart plugins provide an easy way to embed nice looking pie or bar diagrams into your document.

Example

<html>
<head>
<style>
object {
  margin-top: 8px;
  border: 2px solid blue;
  display: block;
}
</style>
</head>
<body>

<object type="jfreechart/pie" style="width:400px;height:400px;" title="Fruit Pie Chart 1">
    <data name="Apple" value="23.2" url="https://www.google.de?q=apple-pie"/>
    <data name="Pear" value="43.2" url="https://www.google.de?q=pear-pie"/>
    <data name="Orange" value="53.2" url="https://www.google.de?q=orange-pie"/>
</object>

<object type="jfreechart/bar" style="width:400px;height:400px;" title="Fruit Bar Chart"
        categories-title="Category" series-title="Series">
    <data series="Value" category="Apple" value="23.2" url="https://www.google.de?q=apple"/>
    <data series="Value" category="Pear" value="43.2" url="https://www.google.de?q=pear"/>
    <data series="Value" category="Orange" value="33.2" url="https://www.google.de?q=orange"/>
    <data series="Price/kg" category="Apple" value="2.2" url="https://www.google.de?q=apple-price"/>
    <data series="Price/kg" category="Pear" value="4.2" url="https://www.google.de?q=pear-price"/>
    <data series="Price/kg" category="Orange" value="5.2" url="https://www.google.de?q=orange-price"/>
</object>

</body>
</html>
  	<dependency>
  		<!-- Objects support plugin. -->
  		<groupId>com.openhtmltopdf</groupId>
  		<artifactId>openhtmltopdf-objects</artifactId>
  		<version>${openhtml.version}</version>
  	</dependency>
	<dependency>
		<groupId>org.jfree</groupId>
		<artifactId>jfreechart</artifactId>
		<version>1.5.0</version>
	</dependency>
public static void main(String... args) throws Exception { 
        try (OutputStream os = new FileOutputStream("/Users/me/Documents/pdf-issues/output/charts.pdf")) {
            PdfRendererBuilder builder = new PdfRendererBuilder();
            builder.withUri("file:///Users/me/Documents/pdf-issues/charts.html");
            builder.useFastMode();
            builder.toStream(os);
            
            DefaultObjectDrawerFactory factory = new DefaultObjectDrawerFactory();
            factory.registerDrawer("jfreechart/pie", new JFreeChartPieDiagramObjectDrawer());
            factory.registerDrawer("jfreechart/bar", new JFreeChartBarDiagramObjectDrawer());
            builder.useObjectDrawerFactory(factory);

            builder.run();
        }
}

Result

JFreeChart support in OpenHTMLtoPDF project.

Notes

  • The size of the chart is determined by CSS width and height properties. JFreeChart will attempt to layout the chart in the given size. Min/max CSS width/height properties are not supported.
  • Links on charts are implemented through using the quad points link annotation feature. Unfortunately, most PDF readers other than Acrobat do NOT support this feature and thus the links will not render nicely on those readers.
  • The font used seems to be Times builtin font. This means that charts may not support characters outside the Latin set. Also not suitable for PDF/A or PDF/UA documents which expect embedded fonts.

Developers

  • If working on code that impacts the JFreeChart plugins, please remove the @Ignore annotation on the VisualRegressionTest::testReplacedSizingJFreeChartPie test while working. This will enable you to see the differences during development (assuming you are running the tests). The test is not enabled by default due to likely differences in output between JDK versions.