…and dont forget to add the javafx-swing dependency to your pom.xml (Maven):

XChart is Awesome

Looking for a full-featured open source charting library? Knowm XChart has been gaining in popularity as more and more chart types have been added. While JavaFX comes with some simple default chart types (Area, Bar, Bubble, Line, Pie, Scatter), perhaps you are looking for more flexible charting API or perhaps you have already built an application with XChart and are just looking to port your code over to JavaFX. Here are some reasons to use XChart:

  1. No required additional dependencies, ~182KB Jar, Java 8 and up
  2. Multiple Y-Axis charts
  3. Line charts
  4. Step charts
  5. Scatter charts
  6. Area charts
  7. Step Area charts
  8. Bar charts
  9. Histogram charts
  10. Pie charts
  11. Donut charts
  12. Bubble charts
  13. Stick charts
  14. Dial charts
  15. Radar charts
  16. OHLC charts
  17. Box Plot
  18. Error bars
  19. Logarithmic axes
  20. Number, Date, Bubble and Category X-Axis
  21. Multiple series
  22. Tool tips
  23. Extensive customization
  24. Themes – XChart, GGPlot2, Matlab
  25. Right-click, Save-As…
  26. User-defined axes range
  27. Definable legend placement
  28. CSV import and export
  29. High resolution chart export
  30. Export as PNG, JPG, BMP, GIF with custom DPI setting
  31. Export SVG, EPS using optional de.erichseifert.vectorgraphics2d library
  32. Export PDF using optional pdfbox-graphics2d library
  33. Real-time charts

The JavaFX Code

We will create a simple JavaFX application that will display an example XChart in a resizable window when the user selects this option from the menu. Lets first define the GUI with a .fxml file:

And our application code:

The most relevant code is contained in the showChart() method. We simply need to embed the swing content (JPanel) inside of a SwingNode:

To insure the xChart resizes to its parent window, we add it to the AnchorPane and set it’s anchors:

Finally, we add the swing node as a child of the AnchorPane:

And that’s all there is to it.

Java XChart in JavaFX

Java XChart in JavaFX

Java XChart in JavaFX

Java XChart in JavaFX

Related Posts

Subscribe To Our Newsletter

Join our low volume mailing list to receive the latest news and updates from our team.

Leave a Comment

Knowm 32X32 Crossbar

Knowm Newsletter

Are you ready for memristor AI processors? With our newsletter, you will be.