Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
655 views
in Technique[技术] by (71.8m points)

javafx 2 - How i can use jfreeChart with Javafx2

For a JavaFX2 Application I have to show different types of Charts.I am using jfreeChart for Charts creation and code given below:

public static JFreeChart generatePieChart() {
        DefaultPieDataset dataSet = new DefaultPieDataset();
        dataSet.setValue("China", 25);
        dataSet.setValue("India", 25);
        dataSet.setValue("United States", 50);
        JFreeChart chart = ChartFactory.createPieChart(
                "World Population by countries", dataSet, true, true, false);

        return chart;
    }

This returns me a chart object. How can I integrate this with my JavaFx Node like HBox etc.?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This answer discusses a few different approaches for chart integration in JavaFX.

Using the FXGraphics2D bridge to paint JFreeChart

David Gilbert (JFreeChart creator) created a bridge which allows a JFreeChart to be painted using JavaFX primitives. The project is FXGraphics2D:

FXGraphics2D is a free implementation of the Graphics2D API that targets the JavaFX Canvas. The code has been developed for the use of Orson Charts and JFreeChart, but will be generally useful for any code that uses the Graphics2D API.

FXGraphics2D requires JDK 1.8.0 or later and is licensed under the terms of a (three clause) BSD-style license.

See the FXGraphics2D site and the answer to Interoperability between Graphics2D and GraphicsContext for more information.

David notes in a comment on another answer:

FXGraphics2D is now integrated directly in JFreeChart (at version 1.0.18) so JavaFX support is now included (requires JDK/JRE 1.8).

Solution Embedding JFreeChart in JavaFX

Use a SwingNode from Java 8 to embed Swing based content such as JFreeChart in JavaFX applications.

jfreechart

import javafx.application.Application;
import javafx.embed.swing.SwingNode;
import javafx.scene.*;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import org.jfree.chart.*;
import org.jfree.data.general.DefaultPieDataset;
 
public class JFreeChartSwingNodePopulationPieChart extends Application {
  @Override public void start(Stage stage) {
    final SwingNode chartSwingNode = new SwingNode();

    chartSwingNode.setContent(
      new ChartPanel(
        generatePieChart()
      )      
    );

    stage.setScene(
      new Scene(
        new StackPane(    
          chartSwingNode
        )    
      )
    );
    
    stage.show();
  }

  private JFreeChart generatePieChart() {
    DefaultPieDataset dataSet = new DefaultPieDataset();
    dataSet.setValue("China",        1344.0);
    dataSet.setValue("India",        1241.0);
    dataSet.setValue("United States", 310.5);

    return ChartFactory.createPieChart(
      "Population 2011", dataSet, true, true, false
    );
  }  
  
  public static void main(String[] args) { launch(args); }
}

Alternative Solution Using JavaFX Charts

Use JavaFX's own charting framework.

JavaFX charts do not cover exactly the same functionality as JFreeChart, but are excellent for many applications.

The default pie chart code posted in the question can trivially be replicated in JavaFX using JavaFX charts, eliminating the need to use an external library for such charts.

populationpiejavafx

import javafx.application.Application;
import javafx.collections.*;
import javafx.scene.*;
import javafx.scene.chart.*;
import javafx.stage.Stage;
 
public class JavaFXPopulationPieChart extends Application {
  @Override public void start(Stage stage) {
    final PieChart chart = new PieChart(
      FXCollections.observableArrayList(
        new PieChart.Data("China",         1344.0),
        new PieChart.Data("India",         1241.0),
        new PieChart.Data("United States",  310.5)
      )      
    );
    chart.setTitle("Population 2011");

    stage.setScene(new Scene(chart));
    stage.show();
  }

  public static void main(String[] args) { launch(args); }
}

On JavaFX and JFreeChart Functional Comparisons

If only there were support for error bars. Without them, the charting framework has much less value for science apps.

The JavaFX Advanced Candlestick Chart from Oracle's Ensemble Sample Application demonstrates how to use JavaFX to build a candlestick chart. Even though error bars are different from candlesticks, I think the implementation of a custom error bar chart would be quite possible using the existing JavaFX charting library and the code would end up similar in many ways to the Ensemble candlestick chart sample.

But sure, the point is still valid, some things such as error bars are currently going to be easier to achieve leveraging a mature library such as JFreeChart.

As Alexander points out, if there is required functionality missing from the standard JavaFX charting framework which looks like it would be applicable to a wide range of users, then it is best to file a feature request to get the functionality embedded into the standard library. Though some kinds of advanced chart features are probably best handled by a 3rd party extension library for JavaFX charting (of which there are none currently available that I know of).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...