ChartIQ HTML5 Charts For Java

ChartIQ is the ideal solution for futureproofing stock charting with Java applications. Java developers gain immediate access to ChartIQ's advanced charting functionality within their Java application. That charting application will also operate within native HTML5 applications providing an easy upgrade path for those firms anticipating an eventual move to an HTML5 based solution.

Compatibility

ChartIQ is compatible with JDK version 7 and version 8 or JXBrowser. We recommend JXBrowser if possible as it has the best performance. If not possible, then we recommend JDK8 for optimized performance. ChartIQ has provided several polyfills in order to support JDK7 that are not required when using JDK8.

The Package

The API package includes ChartIQ's advanced HTML5 files plus additional files specific to Java applications:

  • js/stx-quickstart-java.html is a version of stx-quickstart.html for use with Java.
  • js/stx-advanced-java.html is a version of stx-advanced.html that is pre-wired for use in a Java application.
  • js/stx-java.js contains helper functions and sample code for communicating between Java and ChartIQ
  • js/stx-tfc-java.js contains an implementation of Trading From the Chart (TFC) that sends trades to the Java container for handling

Two sample Java applications are included:

ChartIQ:

+ ChartIQ.java is a class for interacting with the HTML5 charts
+ ChartIQDemo.java is a simple JFX application for instantiating a ChartIQ instance

ChartIQJXBrowser:

+ ChartIQDemo.java is a very simple piece of code to demonstrate how to use ChartIQ with JXBrowser (JXBrowser is a commercial product that embeds Chromium into a Java application. It is very easy to use and has superior performance). Note: This project does not include examples for communication between Java and JS. Refer to ChartIQ project for examples.

Configuration

ChartIQ runs within a "webview". Set the static variable ChartIQ.mainURL to the location of stx-advanced-java.html. This could be a local file on your filesystem:

ChartIQ.mainURL="file:///home/user/dev/stx-advanced-java.html";

or it may be at a network address:

ChartIQ.mainURL="http://127.0.0.1/stx-advanced-java.html";

Note: In the sample application (ChartIQDemo) we pass the location as the argument to the application. This can be changed in Eclipse under "Run Configurations".

<i>Please note</i> For production, please request a version of the ChartIQ library that is not domain locked if you choose to use file:///. Evaluation copies of ChartIQ will work without the lock.

JavaScript <--> Java

The class "ChartIQ" handles bidirectional communication between JavaScript and Java. The ChartIQ API methods can be called directly from Java. Likewise, information can be passed back to Java from the ChartIQ API.

We recommend passing JSON data between JavaScript and Java. This creates a clean, debuggable interface and eliminates any ambiguity.

One tricky aspect of a JavaScript <--> Java interface is dealing with asynchronous actions. JavaScript is a purely asynchronous language. When JavaScript passes control to Java, if Java does not return right away then the JavaScript interface will freeze. Consider:

Java Pseudo-Code:

public placeTrade(order){
    // send order across network.
    // wait for reply - JavaScript interface is hung waiting for the network
    // return response
}

JavaScript Code:

window.java.placeOrder(order);
// JavaScript doesn't get here until the network returns

If this were a pure Java app, the UI would continue to function in another thread but this is not the case with JavaScript so we must properly handle asynchronous actions. We do this through the use of callback functions, and with Java we specifically use the STX.asynchronizer method that is included in stx-java.js. Here is the same example using this methodology:

Java Pseudo-Code:

    public placeOrder(callback, order) {
        // Create thread to do work
        Runnable r=new Runnable() { public void run() {
            // Place order, do network stuff

            // JS callback in original thread
            chartiq.doJFX(new Runnable() {
                public void run() {
                    win.call(callback, response);
                }
            });
        }}; 

        new Thread(r).start();
    }

JavaScript Code:

function cb(response){ // do something with response};
window.java.placeOrder(STX.asynchronizer(cb), order);

With this scenario the execution of JavaScript and Java are decoupled. The asynchronizer creates an ephemeral global JavaScript function that encapsulates the callback. By convention we send the name of this function as the first argument to any Java call.