Introduce JFreeChart

  • Last update:January 18, 2021
  • 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:

    image.png

    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:

    https://github.com/finereport-overseas/example/blob/release/10.0/src/main/java/com/fr/function/JFreeToChart.java

    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:

    企业微信截图_16082019108552.png

    At this point, the custom function is defined, and you can use it in the designer.

    3) Using custom functions

    企业微信截图_16082019521566.png

    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:

    image.png

    Attachment List


    Theme: Secondary Development
    • Helpful
    • Not helpful
    • Only read

    滑鼠選中內容,快速回饋問題

    滑鼠選中存在疑惑的內容,即可快速回饋問題,我們將會跟進處理。

    不再提示

    7s后關閉

    Get
    Help
    Online Support
    Professional technical support is provided to quickly help you solve problems.
    Online support is available from 9:00-12:00 and 13:30-17:30 on weekdays.
    Page Feedback
    You can provide suggestions and feedback for the current web page.
    Pre-Sales Consultation
    Business Consultation
    Business: international@fanruan.com
    Support: support@fanruan.com
    Page Feedback
    *Problem Type
    Cannot be empty
    Problem Description
    0/1000
    Cannot be empty

    Submitted successfully

    Network busy