I. Description
JfreeChart is a very good chart drawing library on Java platform. It can not only generate a variety of charts, but also output in PNG and JPEG formats. It can also be associated with PDF and Excel. Therefore, we hope that the chart of the third party JfreeChart can be directly introduced into our report tool to use the report cell data, as shown in the following figure:
II. Solutions
Finereport creates a chart data source through custom functions, converts the generated JfreeChart chart into a picture, and then displays it in the report.
III. Examples
1. Create a dataset
private CategoryDataset[] createDatasets(){}
2. Generate JfreeChart object from chartfactory
private JFreeChart createChart(CategoryDataset acategorydataset[]){}
3. Output JfreeChart chart into picture
private BufferedImage createImage(int width, int height) {
CategoryDataset acategorydataset[] = createDatasets();
JFreeChart jfreechart = createChart(acategorydataset);
return jfreechart.createBufferedImage(width, height);
}
4. for complete code
please refer to:
Note: import the third-party packages jcommon-1.0.16.jar and jfreechart-1.0.13.jar in idea. For importing the third-party packages, please refer to the document to refer to the third-party jar package.
5. Compile and run
1) Compiling custom functions
compile JFreeToChart.java Class, generate JFreeToChart.class file, copy to report application directory
%FR_HOME%\webapps\webroot\WEB-INF\classes\com\fr\function
2) Register custom functions
Start the designer, click server > function manager, and the new function is named JFreeToChart, select JFreeToChart.class , as shown in the figure below:
At this point, the custom function is defined, and you can use it in the designer.
3) Using custom functions
Note: the data used in the chart can use the built-in dataset, and the specific data is the data on the left side of the template preview
As shown in the figure above, the jfreetochart chart data is expanded from the cell. Enter the formula = JFreeToChart (A2, B2) in the cell to display the JFreeToChart.
Note: before previewing the template through the designer, you need to put jcommon-1.0.16.jar and jfreechart-1.0.13.jar in %FR_HOME%\webapps\webroot\WEB-INF\lib, Restart the designer, and then preview it.
6. Template preview
Pagination preview, you can see the following effect: