UNPKG

9.51 kBMarkdownView Raw
1[<img src="https://cdn.anychart.com/images/logo-transparent-segoe.png?2" width="234px" alt="AnyChart - Robust JavaScript/HTML5 Chart library for any project">](https://www.anychart.com)
2
3[![NPM Version][npm-image]][npm-url]
4[![NPM Downloads][downloads-image]][downloads-url]
5[![Package Quality][quality-image]][quality-url]
6[![Build status][travis-image]][travis-url]
7[![David-dm][dependencies-image]][dependencies-url]
8<!--[![License][licence-image]][licence-url]-->
9
10
11# AnyChart NodeJS module.
12
13AnyChart NodeJS module provides an easy way to generate SVG, JPG and PNG images of the charts on the server side.
14Consider it for reporting systems that send charts by email or social networks sharing applications.
15
16## Table of Contents
17
18* [Download and install](#download-and-install)
19* [Quick start](#quick-start)
20* [Export Module API](#export-module-api)
21* [Examples](#examples)
22* [Contacts](#contacts)
23* [Links](#links)
24* [License](#license)
25
26## Download and install
27IMPORTANT: Requires Node.js 6 or newer.
28You can install AnyChart NodeJS export module using **npm**, **bower** or **yarn**:
29
30* `npm install anychart-nodejs`
31* `bower install anychart-nodejs`
32* `yarn add anychart-nodejs`
33
34AnyChart NodeJS module requires [ImageMagick](https://www.imagemagick.org/script/index.php) and [librsvg](https://github.com/GNOME/librsvg) to create images.
35
36Install ImageMagick and librsvg on Linux:
37
38```
39apt-get install imagemagick librsvg2-dev
40```
41
42Install ImageMagick and librsvg on Mac OS X
43
44```
45brew install imagemagick librsvg
46```
47
48Install ImageMagick and librsvg on Windows:
49
50- [imagemagick](https://www.imagemagick.org/script/download.php)<br>
51- [GTK+ bundle](http://win32builder.gnome.org/gtk+-bundle_3.6.4-20131201_win64.zip)<br>
52- [RSVG lib](https://downloads.sourceforge.net/project/tumagcc/converters/rsvg-convert.exe?r=https%3A%2F%2Fsourceforge.net%2Fprojects%2Ftumagcc%2Ffiles%2Frsvg-convert.exe%2Fdownload&ts=1500995628&use_mirror=netix)
53
54## Quick start
55To generate JPG image a chart from chart instance, create index.js file with the following content:
56```javascript
57// require file system and jsdom
58var fs = require('fs');
59
60// For jsdom version 10 or higher.
61// Require JSDOM Class.
62var JSDOM = require('jsdom').JSDOM;
63// Create instance of JSDOM.
64var jsdom = new JSDOM('<body><div id="container"></div></body>', {runScripts: 'dangerously'});
65// Get window
66var window = jsdom.window;
67
68// For jsdom version 9 or lower
69// var jsdom = require('jsdom').jsdom;
70// var document = jsdom('<body><div id="container"></div></body>');
71// var window = document.defaultView;
72
73// require anychart and anychart export modules
74var anychart = require('anychart')(window);
75var anychartExport = require('anychart-nodejs')(anychart);
76
77// create and a chart to the jsdom window.
78// chart creating should be called only right after anychart-nodejs module requiring
79var chart = anychart.pie([10, 20, 7, 18, 30]);
80chart.bounds(0, 0, 800, 600);
81chart.container('container');
82chart.draw();
83
84// generate JPG image and save it to a file
85anychartExport.exportTo(chart, 'jpg').then(function(image) {
86 fs.writeFile('anychart.jpg', image, function(fsWriteError) {
87 if (fsWriteError) {
88 console.log(fsWriteError);
89 } else {
90 console.log('Complete');
91 }
92 });
93}, function(generationError) {
94 console.log(generationError);
95});
96```
97
98Run the following command in the command line:
99```
100$ node index.js
101>> Complete
102```
103
104To generate PDF image a chart from Java Script string, create index.js file with the following content:
105```javascript
106// require file system and jsdom
107var fs = require('fs');
108
109// require only anychart export module
110var anychartExport = require('anychart-nodejs');
111
112// define javascript string that represent code of chart creating
113var chart = "var chart = anychart.pie([10, 20, 7, 18, 30]); chart.bounds(0, 0, 800, 600); chart.container('container'); chart.draw()";
114
115// generate PDF image and save it to a file
116anychartExport.exportTo(chart, 'pdf').then(function(image) {
117 fs.writeFile('anychart.pdf', image, function(fsWriteError) {
118 if (fsWriteError) {
119 console.log(fsWriteError);
120 } else {
121 console.log('Complete');
122 }
123 });
124}, function(generationError) {
125 console.log(generationError);
126});
127```
128
129Run the following command in the command line:
130```
131$ node index.js
132>> Complete
133```
134
135
136## Export Module API
137### `exportTo(target, options, callback):Promise`
138Generate an image asynchronously.
139**Parameters:**
140
141Name | Type | Description
142--- | --- | ---
143`target` | SVG string, XML string, Java Script string, a chart or a stage instance | **required** Data to be exported.
144`outputType` | string | Output type, possible values are: svg, jpg, png, pdf. Default: 'jpg'
145`dataType` | string | Type of target.
146`document` | Document | Document object where was rendered chart or should be.
147`containerId` | string | Id of container.
148`resources` | Array.\<string\> | Links to external resources.
149`callback` | Function | The result callback.
150
151**Returns:**
152Promise if no callback passed.
153
154### `exportToSync(target, options):Object`
155Generate an image synchronously.
156
157**Parameters:**
158
159Name | Type | Description
160--- | --- | ---
161`target` | SVG string, XML string, Java Script string, a chart or a stage instance | **required** Data to be exported.
162`outputType` | string | Output type, possible values are: svg, jpg, png, pdf.
163`dataType` | string | Type of target.
164`document` | Document | Document object where was rendered chart or should be.
165`containerId` | string | Id of container.
166`resources` | Array.\<string\> | Links to external resources.
167
168**Returns:**
169ArrayBuffer
170
171### `loadFont(path, callback):Promise`
172Loads the specified font asynchronously.
173
174**Parameters:**
175
176Name | Type | Description
177--- | --- | ---
178`path` | string | Path to the font.
179`callback` | Function | The result callback.
180
181**Returns:**
182Promise if no callback passed.
183
184
185### `loadFontSync(path):Object`
186Loads the specified font synchronously.
187
188**Parameters:**
189
190Name | Type | Description
191--- | --- | ---
192`path` | string | Path to font.
193
194**Returns:**
195Object
196
197## Examples
198Generating PDF image a chart with that requires external resources:
199```javascript
200// require file system and jsdom
201var fs = require('fs');
202
203// require only anychart export module
204var anychartExport = require('anychart-nodejs');
205
206// define javascript string that represent code of chart creating
207var chart = "var chart = anychart.map(); chart.bounds(0, 0, 800, 600); chart.geoData('anychart.maps.united_states_of_america'); chart.container('container'); chart.draw()";
208
209// exporting parameters
210var params = {
211 outputType: 'pdf',
212 resources: [
213 'https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.3.15/proj4.js',
214 'https://cdn.anychart.com/releases/v8/geodata/countries/united_states_of_america/united_states_of_america.js'
215 ]
216};
217
218// generate PDF image and save it to a file
219anychartExport.exportTo(chart, params).then(function(image) {
220 fs.writeFile('anychart.pdf', image, function(fsWriteError) {
221 if (fsWriteError) {
222 console.log(fsWriteError);
223 } else {
224 console.log('Complete');
225 }
226 });
227}, function(generationError) {
228 console.log(generationError);
229});
230```
231
232Run the following command in the command line:
233```
234$ node index.js
235>> Complete
236```
237
238Please, take a look at examples:
239* [Report Generation Utility](https://github.com/anychart-integrations/nodejs-report-generation-utility)
240* [Image Generation Utility](https://github.com/anychart-integrations/nodejs-image-generation-utility)
241* [Export Server Application](https://github.com/anychart-integrations/nodejs-export-server-application)
242
243## Contacts
244
245* Web: [www.anychart.com](www.anychart.com)
246* Email: [contact@anychart.com](mailto:contact@anychart.com)
247* Twitter: [anychart](https://twitter.com/anychart)
248* Facebook: [AnyCharts](https://www.facebook.com/AnyCharts)
249* LinkedIn: [anychart](https://www.linkedin.com/company/anychart)
250
251## Links
252
253* [AnyChart Website](https://www.anychart.com)
254* [Download AnyChart](https://www.anychart.com/download/)
255* [AnyChart Licensing](https://www.anychart.com/buy/)
256* [AnyChart Support](https://www.anychart.com/support/)
257* [Report Issues](https://github.com/AnyChart/AnyChart-NodeJS/issues)
258* [AnyChart Playground](https://playground.anychart.com)
259* [AnyChart Documentation](https://docs.anychart.com)
260* [AnyChart API Reference](https://api.anychart.com)
261* [AnyChart Sample Solutions](https://www.anychart.com/solutions/)
262* [AnyChart Integrations](https://www.anychart.com/integrations/)
263
264## License
265
266[© AnyChart.com - JavaScript charts](http://www.anychart.com). [![Analytics](https://ga-beacon.appspot.com/UA-228820-4/Plugins/NodeJS?pixel&useReferer)](https://github.com/igrigorik/ga-beacon)
267
268[npm-image]: https://img.shields.io/npm/v/anychart-nodejs.svg
269[npm-url]: https://www.npmjs.com/package/anychart-nodejs
270[downloads-image]: https://img.shields.io/npm/dm/anychart-nodejs.svg
271[downloads-url]: https://www.npmjs.com/package/anychart-nodejs
272[quality-image]: http://npm.packagequality.com/shield/anychart-nodejs.svg
273[quality-url]: http://packagequality.com/#?package=anychart-nodejs
274[licence-image]: https://img.shields.io/npm/l/anychart-nodejs.svg
275[licence-url]: https://github.com/AnyChart/AnyChart-NodeJS/blob/master/LICENSE
276[travis-image]: https://img.shields.io/travis/AnyChart/AnyChart-NodeJS.svg?branch=master&label=unit
277[travis-url]: https://travis-ci.org/AnyChart/AnyChart-NodeJS
278[dependencies-image]: https://david-dm.org/AnyChart/AnyChart-NodeJS.svg
279[dependencies-url]: https://david-dm.org/AnyChart/AnyChart-NodeJS