HyJavaImages Full Documentation

the java api for highcharts

OVERVIEW

The HyJavaImages library is a pure Java library that allows you to create PNG, JPEG, WEBP and PDF images of your Javascript charts.
There is built-in support for Highcharts, Highcharts Stock, ApexCharts and Vaadin charts Javascript charting libraries.

There are Java methods to convert:

  • Highcharts, Highcharts Stock, Apexcharts and Vaadin charts chart options to a base64 encoded image in PNG, JPEG, WEBP or PDF format
  • HTML to a base64 encoded image in PNG, JPEG, WEBP or PDF format
  • A URL to a base64 encoded image in PNG, JPEG, WEBP or PDF format

The chart options method above allows for direct integration with HyJavaCharts, HyJavaStock, HyJavaApex and Vaadin charts.
You are now able to generate Javascript charts and create an image, all from within a Java application on the server, with no Javascript coding!

The library also allows you to create an image of any web page or supplied HTML text.
This allows you to create images of charts generated using any Javascript charting library.

The library will start a Chrome headless server/or connect to an existing running Chrome headless server.
It then uses the DevTools Protocol to control Chrome.

JAVADOC API

The HyJavaImages API can be viewed online or refer to the API jar provided in the product download.

HOW TO USE THE LIBRARY

Ensure Chrome is installed on your system.
Include the HyJavaImages jar in your project.

The code below will generate a base64 encoded string in png format for Highcharts, Highcharts stock and Apexcharts chart options.

// Startup Chrome headless - (1)
Chrome chrome = Chrome.getInstance();
chrome.start();

// Generate as many chart images as needed - (2)
String base64;
ImageGenerator img = new ImageGenerator();

// Highchart example - (3)
img.setJavaScript(LibraryType.HIGHCHARTS, null, "highcharts.js");
base64 = img.toBase64Image("{your chart options...}");

// Highstock example
img.setJavaScript(LibraryType.HIGHCHARTS_STOCK, null, "stock/highstock.js");
base64 = img.toBase64Image("{your chart options...}");

// ApexCharts example
img.setJavaScript(LibraryType.APEXCHARTS, null);
base64 = img.toBase64Image("{your chart options...}");

// Close down Chrome headless - (4)
chrome.stop();

Code description:

(1) Get the instance of Chrome and start the Chrome headless server using all defaults.
In a web servlet application chrome.start() should be called once in Servlet.init().

(2) The ImageGenerator class provides all methods to generate chart images.

(3) The setJavaScript method tells the library which Javascript libraries to use to generate the chart. The 2nd parameter is the library version or null for the latest.

(4) Stop the Chrome headless server.
In a web servlet application chrome.stop() should be called once in Servlet.destroy().

MAIN LIBRARY CLASSES

Chrome Class

The Chrome class is a singleton that provides methods to control the Chrome headless server.

Main functionality:

  • Starts and Stops a new instance of a Chrome headless server. See the start() and stop() methods.
  • Chrome is started with a set of default Chrome arguments. Chrome arguments can be specified using the setChromeArguments() method.
  • When starting Chrome, the library will search in the standard install locations for the Chrome binary. If the library cannot find the binary or you wish to use a non-standard install you can also specify the Chrome binary with the setChromeBinaryPath() method.
    Search locations:
    Linux: “/opt/google/chrome/chrome”;
    Windows: “/Program Files/Google/Chrome/Application/chrome.exe” and “/Program Files (x86)/Google/Chrome/Application/chrome.exe”;
    MacOS: “/Applications/Google Chrome.app/Contents/MacOS/Google Chrome”.
  • The library can connect/disconnect from an existing running Chrome headless server. See the connect() and disconnect() methods. Chrome headless would need to be started by another process. I.E. not by the HyJavaImages library.
  • When starting/connecting with the start() or connect() methods the library will create a pool of Chrome pages ready for re-use.
    The default pool size value of 2 will suit most applications. Large volume applications can set this to a higher value to handle a higher concurrency.
    See the setPoolSize() method.
    When generating images the library will acquire a page from the pool, generate the image, release the page back to the pool. If there are no available pages in the pool the library will wait until one becomes free.
  • By default the Chrome headless server will listen on a random available port. You can specify the port using the setPort() method.
  • By default the image size created will be 800 x 600 pixels. You can specify the image size using the setWindowSize() method.
  • HyJavaImages can also be run as a HTTP server. To route HyJavaImages requests to a shared HTTP server use the setViaHTTPServerURL() method. Refer to the HTTP Server section below.
ImageGenerator Class

The ImageGenerator class provides methods to generate the required PNG/JPEG/WEBP/PDF images.

Main functionality:

  • To generate a chart image Base64 encoded string from Highchart, Highchart stock or Apexcharts chart options see the toBase64Image() methods.
  • To generate a chart image Base64 encoded string from html text see the htmlToBase64Image() methods.
  • To generate a chart image Base64 encoded string from a URL see the toBase64Image() methods.
  • One or more sets of global chart options can be specified with the setGlobalOptions() method.
  • By default the image created will be 800 x 600 pixel in PNG format. If required use the ImageOptions class to define the image format (PNG/JPEG/WEBP); the JPEG quality; the image scale (resolution); a full page image; the x, y, width and height to determine the rectangular area that the image will be taken from.
  • By default the PDF created will be portrait, 8.5 x 11 inch with 0.4 inch margins. If required use the PdfOptions class to define the paper size; the paper width and height, the margins; portrait or landscape; the header/footer; continuous page; page ranges; scale. There are static helper methods cm() and px() to convert cm and pixels to inches.
  • The setJavaScript() and setJavaScriptAlternate() methods enable the library to load the required Javascript to render the chart. Also see the setJavaScriptAdditional() and setJavaScriptStatements() methods.
  • If required use the setCSSExternal() and setCSSInternal() methods to enable the library to load any required CSS.
  • Each chart is contained within its own div tag. If required use the setChartDivAttributes() method to define the chart div tag attributes. The method can be used to specify values for the style or class attributes of the div.
  • To emulate a device, media type, locale, timezone or geolocation see the setEmulate…() methods.
  • To include multiple charts in the one image see the setAdditionalChartOptions() method. Also the setAdditionalChartDivAttributes() and setChartGroupDivAttributes() methods may be of interest.

HyJavaImages HTTP SERVER

The HyJavaImages HTTP server is a high performance HTTP server that allows all HyJavaImages requests to be routed to a shared HTTP server.
The HTTP server will handle all image generation requests and return the base64 image format.

To use the HTTP server the only code change required is to replace the Chrome.start() method with the Chrome.setViaHTTPServerURL() method.
Your applications would not be required to start a Chrome headless server.

For more information see the HyJavaImages HTTP Server Product page.

REQUIREMENTS

You must have Chrome 71 or later installed on your system. The library requires a Chrome version that supports the DevTools protocol v1.3.

JAVA VERSION

The HyJavaImages library requires Java 8 or greater.

SUPPORT

If you have any questions or issues on the HyJavaImages library, please contact our support team via the Contact Us area on the home page.

The Java API’s fully support all chart types and chart options in Highcharts v5.x, v6.x, v7.x, v8.x, v9.x;  Highcharts Stock v8.x, v9.x and ApexCharts v3.x.


Current versions supported are:

HyJavaCharts

HyJavaStock

HyJavaApex

Start the HyJavaCharts demo Java web application


Start the HyJavaStock demo Java web application


Start the HyJavaApex demo Java web application


Start the HyJavaImages demo Java web application