Test plan
- Local unit testing
- Docker
- AWS remote
- Linux
- Windows
- MacOS
- Integration Test with Snapshot version
Introduction
This library is used to take snapshots of the charts generated by ECharts-Java. Now it supports images in PNG and JPEG formats with pixelRatio control. Base64 is also supported. We plan to support SVG in the near future. (It is still in the testing stage, please file an issue if you spot a bug.).
Prerequisite
To use this library, make sure you've installed phantomjs
.
For Mac users using brew,
brew install phantomjs
For AWS Linux users,
sudo wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
sudo mkdir /opt/phantomjs
bzip2 -d phantomjs-2.1.1-linux-x86_64.tar.bz2
sudo tar -xvf phantomjs-2.1.1-linux-x86_64.tar --directory /opt/phantomjs/ --strip-components 1
sudo ln -s /opt/phantomjs/bin/phantomjs /usr/bin/phantomjs
For other users, please refer to the official website of phantomjs for downloading details.
For Mac users, if you encounter the error "phantomjs cannot be opened because the developer cannot be verified.", please refer to the solution here.
Installation
For a maven project, includes the following in your pom.xml
TBC
For a gradle project, includes the following
TBC
Usage
Snapshot.java
is providing several APIs to be used, which includes takeSnapshot() and saveSnapshot().
SnapshotSettingsBuilder.java
It constructs an object with the following properties related to metadata of the snapshot.
Required:
String fileType
: the fileType of the image. Now we support "png" and "jpg".Option option
: must have this field if Chart chart is not specified. This is the option object of ECharts.Chart<?,?> chart
: must have this field if Option option is not specified. This is the chart object defined in ECharts-Java.
Optional:
int delay
: the waiting time (in second) of an image to be fully rendered. Since some ECharts have the animation effects, it is suggested to wait for a few seconds before making a snapshot. The default value is 2.double pixelRation
: the pixel ration of an image. It is defined here.
takeSnapshot(SnapshotSettingsBuilder settings)
This function takes an object related to the settings of snapshot, and returns a base64 string of the image.
SnapshotSettingsBuilder builder = new SnapshotSettingsBuilder(option, "png");
return Snapshot.takeSnapshot(builder);
saveSnapshot(String imageData, String path)
This function takes the base64 string of an image, and saves the image by the suffix specified by path
. E.g. if the path is ./test.png
, it will store the image as png file. Now only PNG and JPG are supported. Any other file types will lead to a file with plain base64 string of that image. Note that the file type should be consistent with the one used in takeSnapshot.
SnapshotSettingsBuilder builder = new SnapshotSettingsBuilder(option, "jpg", 1, 2);
Snapshot.saveSnapshot(Snapshot.takeSnapshot(builder), "./test.jpg");