Consider the following context as that's the one I found myself a couple of months ago. I was working on a large Symfony project that runs on PHP 7, as we all know, the libraries to create charts in PHP are really awful. So the option was to simply create the charts using an alternative solution in JavaScript as the charts should only appear on the browser when the user uses the application. Unfortunately, a couple of months after, they required the creation of PDF reports that needed to have the same charts that were shown on the browser (yeah great, I need to embed them in PDFs as well). For the knowledge I had till that point, the situation was very problematic.
After some research, I discovered that fortunately, I was using Highcharts to create the charts in the browser, so the solution existed already to implement it on the server-side, so I wouldn't need either to rewrite the charts on the server-side as the code of the charts in the frontend should be similar.
In this article, I will explain to you how to easily render highcharts on your server using Node.js and the highcharts-export-server
module.
Requirements
We assume that you have Node.js already installed on your server.
Install Highcharts Export Server
The first way to use the highcharts-export-server
is to use it as a module. This means, writing your own script, creating the image version of the charts in a custom way. In this case, we will simply create a script that will use custom code to generate the charts. The second way is to run the module as an HTTP service (as a server), so third party applications like the one you are working it can simply do a request to this server to obtain the image as a response.
As the first step, install the module using NPM with the following command:
The installation will take a while as this will install the Highcharts library and PhantomJS. When installing highcharts you will be prompted for information about licenses and other modules, in our case, we will use the free version so the answer to most of the prompts it will make, will be answered with a no.
For more information about the highcharts-export-server module, please visit the official repository at Github here.
A. Using Highcharts Export Server as a module
There are multiple ways to export your chart using the highcharts export server. I will describe the most useful ones in this article:
Exporting as a PNG image
In this example, we will create the index.js file that will be executed with Node.js. This file contains the basic implementation of the highcharts export module, where it will be imported along with the filesystem module. Then, initialize the exporter (PhantomJS) and define the options of the chart. The object chartOptions
contains the information that you use as usual to plot the chart in the frontend, the labels, data, and so on. Then use the export method of the chartExporter to run to draw the chart on the headless browser (PhantomJS), when it's rendered, the browser will trigger the callback that will allow you to obtain the base64 information of the image to store it into a file:
If you run the script with Node.js using:
The script will be executed, generating a success output, and the image output-chart.png
, that in this case with the information of this example will look like this:
The image could be now embedded wherever you want, in the browser, in a PDF, etc.
Using as a module (exporting as an SVG file)
If you want to export the chart in SVG format, you may do it as well using the module. You need to change the type property using SVG as value and provide the outfile
parameter as well:
B. Using Highcharts Export as a service
Now, the first solution, using it as a module is great for small applications, where the feature wouldn't be used way too much theoretically. However, if you are trying to create a service where multiple applications can interact with the creation of the chart image, the best way to do it is to run the module as a server. Therefore, you need to install highcharts-export-server globally:
The installation will prompt the same things as when installing as a module, licenses, installation of PhantomJS, etc. When the installation finishes, you should be able to run the command from the terminal enabling it as a server with the enableServer parameter:
This command will basically start the server with the given configuration:
in this case, the address would be http://localhost:7801, as I'm working locally. If you want to run this on a live server you will need as well configure the reverse proxy, Nginx if you want to use it with a specific domain.
Now, with this, you will be able to render the charts from a web service. Execute a simple application/json
request with the chart data to the defined address and the response will be an image of the chart. The JSON that we are sending in the request will be the following one:
You can test it with curl, for example, the following command will request the chart with the previous information and will store it in the output-file.png
file:
The response, in this case, will be:
Happy coding ❤️!
1 Comment