BellaDati charts.js library allows you to render BellaDati JSON charts data directly on web page. This web page can be rendered in your Java application.
You can find this example on GitHub https://github.com/BellaDati/belladati-charts-java-demo
Prerequisites
During this tutorial you will need following:
- JDK 8u72+
- IDE to implement Java standalone application (e.g. Eclipse IDE)
- jquery 1.7.1+
- raphael.js 2.1.1+
- belladati-charts.js
Step-by-step
Create new Java project in your IDE and setup build configuration to use JDK 8.
Create HTML file (e.g. index.html) in src/main/resources. This HTML page will be rendered in Java application. Example:
<!DOCTYPE html> <html> <head> <title>BellaDati Charts SDK Example</title> </head> <body> No content yet. </body> </html>
Download 3 required JavaScript libraries to src/main/resources (to the same folder where index.html is located). Add relative links to these libraries in HTML head:
<head> ... <script src="jquery-1.7.1.js"></script> <script src="raphael-min.js"></script> <script src="belladati-charts-min.js"></script> ... <head>
Define the container in HTML body, where the chart should be rendered:
<body> ... Sample Chart <div id="chart" style="width: 500px; height: 400px; border: 1px solid silver"></div> ... </body>
Create class used as main UI window. You can use Swing for basic UI components and JavaFX WebEngine to render HTML code:
Create main class to run standalone Java application. Create MainWindow, load URL with index.html and render the chart by executing JavaScript Charts.createAndRender("container_id", "json_chart_data"):
package com.belladati.charts.example; public class Main { private static final String HTML_INDEX = "/com/belladati/charts/example/index.html"; public static void main(String[] args) throws Exception { MainWindow window = new MainWindow(); window.setVisible(true); window.loadUrl("file://" + Main.class.getResource(HTML_INDEX).getPath()); window.loadJavaScript("Charts.createAndRender(\"chart\", json_chart_data);"); } }
Run it
Example
You can download standalone application as JAR file and run it:
java -jar belladati-charts-java-demo.jar