AnyChart NodeJS module.
AnyChart NodeJS module provides an easy way to generate SVG, JPG and PNG images of the charts on the server side.
Consider it for reporting systems that send charts by email or social networks sharing applications.
Table of Contents
Download and install
IMPORTANT: Requires Node.js 6 or newer.
You can install AnyChart NodeJS export module using npm, bower or yarn:
npm install anychart-nodejs
bower install anychart-nodejs
yarn add anychart-nodejs
AnyChart NodeJS module requires ImageMagick and librsvg to create images.
Install ImageMagick and librsvg on Linux:
apt-get install imagemagick librsvg2-dev
Install ImageMagick and librsvg on Mac OS X
brew install imagemagick librsvg
Install ImageMagick and librsvg on Windows:
Quick start
To generate JPG image a chart from chart instance, create index.js file with the following content:
var fs = require('fs');
var JSDOM = require('jsdom').JSDOM;
var jsdom = new JSDOM('<body><div id="container"></div></body>', {runScripts: 'dangerously'});
var window = jsdom.window;
var anychart = require('anychart')(window);
var anychartExport = require('anychart-nodejs')(anychart);
var chart = anychart.pie([10, 20, 7, 18, 30]);
chart.bounds(0, 0, 800, 600);
chart.container('container');
chart.draw();
anychartExport.exportTo(chart, 'jpg').then(function(image) {
fs.writeFile('anychart.jpg', image, function(fsWriteError) {
if (fsWriteError) {
console.log(fsWriteError);
} else {
console.log('Complete');
}
});
}, function(generationError) {
console.log(generationError);
});
Run the following command in the command line:
$ node index.js
>> Complete
To generate PDF image a chart from Java Script string, create index.js file with the following content:
var fs = require('fs');
var anychartExport = require('anychart-nodejs');
var chart = "var chart = anychart.pie([10, 20, 7, 18, 30]); chart.bounds(0, 0, 800, 600); chart.container('container'); chart.draw()";
anychartExport.exportTo(chart, 'pdf').then(function(image) {
fs.writeFile('anychart.pdf', image, function(fsWriteError) {
if (fsWriteError) {
console.log(fsWriteError);
} else {
console.log('Complete');
}
});
}, function(generationError) {
console.log(generationError);
});
Run the following command in the command line:
$ node index.js
>> Complete
Export Module API
exportTo(target, options, callback):Promise
Generate an image asynchronously.
Parameters:
Name | Type | Description |
---|
target | SVG string, XML string, Java Script string, a chart or a stage instance | required Data to be exported. |
outputType | string | Output type, possible values are: svg, jpg, png, pdf. Default: 'jpg' |
dataType | string | Type of target. |
document | Document | Document object where was rendered chart or should be. |
containerId | string | Id of container. |
resources | Array.<string> | Links to external resources. |
callback | Function | The result callback. |
Returns:
Promise if no callback passed.
exportToSync(target, options):Object
Generate an image synchronously.
Parameters:
Name | Type | Description |
---|
target | SVG string, XML string, Java Script string, a chart or a stage instance | required Data to be exported. |
outputType | string | Output type, possible values are: svg, jpg, png, pdf. |
dataType | string | Type of target. |
document | Document | Document object where was rendered chart or should be. |
containerId | string | Id of container. |
resources | Array.<string> | Links to external resources. |
Returns:
ArrayBuffer
loadFont(path, callback):Promise
Loads the specified font asynchronously.
Parameters:
Name | Type | Description |
---|
path | string | Path to the font. |
callback | Function | The result callback. |
Returns:
Promise if no callback passed.
loadFontSync(path):Object
Loads the specified font synchronously.
Parameters:
Name | Type | Description |
---|
path | string | Path to font. |
Returns:
Object
Examples
Generating PDF image a chart with that requires external resources:
var fs = require('fs');
var anychartExport = require('anychart-nodejs');
var chart = "var chart = anychart.map(); chart.bounds(0, 0, 800, 600); chart.geoData('anychart.maps.united_states_of_america'); chart.container('container'); chart.draw()";
var params = {
outputType: 'pdf',
resources: [
'https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.3.15/proj4.js',
'https://cdn.anychart.com/releases/v8/geodata/countries/united_states_of_america/united_states_of_america.js'
]
};
anychartExport.exportTo(chart, params).then(function(image) {
fs.writeFile('anychart.pdf', image, function(fsWriteError) {
if (fsWriteError) {
console.log(fsWriteError);
} else {
console.log('Complete');
}
});
}, function(generationError) {
console.log(generationError);
});
Run the following command in the command line:
$ node index.js
>> Complete
Please, take a look at examples:
Contacts
Links
License
© AnyChart.com - JavaScript charts.