Skip to content

Commit 7b3c569

Browse files
committed
bug fixes and improvements
1 parent a28a557 commit 7b3c569

File tree

6 files changed

+602
-84
lines changed

6 files changed

+602
-84
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ node_modules
99
*.png
1010
images/
1111
package-lock.json
12+
test/img/

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,4 @@ Please, take a look at examples:
255255
[downloads-image]: https://img.shields.io/npm/dm/anychart-nodejs.svg?style=flat-square
256256
[downloads-url]: https://www.npmjs.com/package/anychart-nodejs
257257
[quality-image]: http://npm.packagequality.com/shield/anychart-nodejs.svg?style=flat-square
258-
[quality-url]: http://packagequality.com/#?package=anycahrt-nodejs
258+
[quality-url]: http://packagequality.com/#?package=anychart-nodejs

bower.json

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,23 @@
2525
"library"
2626
],
2727
"dependencies": {
28-
"opentype.js": "^0.6.6",
28+
"anychart": "^8.0.0",
2929
"async": "^0.9.2",
30-
"es6-promise": "^4.0.5",
31-
"anychart": "8.0.0-dev-preview",
32-
"jsdom": "^9.9.1"
30+
"deasync": "^0.1.12",
31+
"es6-promise": "^4.2.2",
32+
"fast-xml-parser": "^2.8.3",
33+
"fontfaceobserver": "^2.0.13",
34+
"gm": "^1.23.0",
35+
"js-gc": "^0.0.3",
36+
"jsdom": "^9.9.1",
37+
"mime-types": "^2.1.17",
38+
"opentype.js": "^0.6.6",
39+
"request": "^2.83.0",
40+
"vm2": "^3.5.2",
41+
"xmldom": "^0.1.27"
42+
},
43+
"devDependencies": {
44+
"chai": "^3.5.0",
45+
"mocha": "^3.5.3"
3346
}
3447
}

lib/anychart-node.js

Lines changed: 101 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -202,40 +202,47 @@ function prepareDocumentForRender(doc) {
202202
window.defaultBounds = defaultBounds;
203203
window.setTimeout = function(code, delay, arguments) {};
204204
window.setInterval = function(code, delay, arguments) {};
205-
anychart.global(window);
206205
}
207206

208207
function getParams(args) {
209208
var arrLength = args.length;
210209
var lastArg = args[arrLength - 1];
211210
var callback = isFunction(lastArg) ? lastArg : null;
212211

213-
var options = arrLength === 1 ? undefined : callback ? args[arrLength - 2] : lastArg;
212+
var options = arrLength === 1 ? void 0 : callback ? arrLength > 2 ? args[arrLength - 2] : void 0: lastArg;
214213
var params = {};
215214

215+
params.callback = callback;
216+
216217
if (typeof options === 'string') {
217218
params.outputType = options;
218219
} else if (typeof options === 'object') {
219220
extend(params, options)
220221
}
221222

222223
var target = args[0];
223-
if (target && typeof target === 'string' && !isDef(params.dataType)) {
224-
target = target.replace(/^[\s\xa0]+|[\s\xa0]+$/g, '');
224+
if (target && !isDef(params.dataType)) {
225+
if (typeof target === 'string') {
226+
target = target.replace(/^[\s\xa0]+|[\s\xa0]+$/g, '');
225227

226-
try {
227-
JSON.parse(target);
228-
params.dataType = 'json';
229-
} catch (e) {
230-
if (fastXmlParser.validate(target)) {
231-
if (target.lastIndexOf('<svg') !== -1) {
232-
params.dataType = 'svg';
228+
try {
229+
JSON.parse(target);
230+
params.dataType = 'json';
231+
} catch (e) {
232+
if (fastXmlParser.validate(target)) {
233+
if (target.lastIndexOf('<svg') !== -1) {
234+
params.dataType = 'svg';
235+
} else {
236+
params.dataType = 'xml';
237+
}
233238
} else {
234-
params.dataType = 'xml';
239+
params.dataType = 'javascript';
235240
}
236-
} else {
237-
params.dataType = 'javascript';
238241
}
242+
} else {
243+
var isChart = typeof target.draw === 'function';
244+
var isStage = typeof target.resume === 'function';
245+
params.dataType = isChart ? 'chart' : isStage ? 'stage' : void 0;
239246
}
240247
}
241248

@@ -292,7 +299,7 @@ function applyResourcesToDoc(params, resources, callback) {
292299
//
293300
// });
294301
} else if (type == mime.contentType('js')) {
295-
scripts += ' ' + resource.body;
302+
scripts += ' ' + resource.body + ';';
296303
}
297304
}
298305

@@ -339,23 +346,47 @@ function loadExternalResources(resources, params, callback) {
339346
}
340347
}
341348

349+
function getSvgString(svgElement, width, height) {
350+
var svg = '';
351+
if (svgElement) {
352+
if (!width || isPercent(width))
353+
width = defaultBounds.width;
354+
if (!height || isPercent(height))
355+
height = defaultBounds.height;
356+
357+
svgElement.setAttribute('width', width);
358+
svgElement.setAttribute('height', height);
359+
svg = xmlNs + svgElement.outerHTML;
360+
}
361+
return svg;
362+
}
363+
342364
function getSvg(target, params, callback) {
343365
var dataType = params.dataType;
366+
344367
if (dataType === 'svg') {
345368
return callback(null, fixSvg(target), params);
346369
} else {
347-
var svg, ns, container, svgElement;
370+
var svg, container, svgElement;
348371
var res = params.resources;
349372

350-
if (!params.document) {
373+
var isChart = dataType === 'chart';
374+
var isStage = dataType === 'stage';
375+
376+
if (!params.document && !(isChart || isStage)) {
351377
params.iframeId = createSandbox(params.containerId);
352378
params.document = iframeDoc;
353379
}
354-
prepareDocumentForRender(params.document);
380+
if (params.document)
381+
prepareDocumentForRender(params.document);
355382

356383
return loadExternalResources(res, params, function(err, params) {
357384
var document = params.document;
358-
var window = document.defaultView;
385+
var window = document && document.defaultView;
386+
var bounds, width, height;
387+
388+
if (window)
389+
anychart.global(window);
359390

360391
if (dataType === 'javascript') {
361392
var script = new vm.VM({
@@ -367,61 +398,83 @@ function getSvg(target, params, callback) {
367398
script.run(target);
368399

369400
var svgElements = document.getElementsByTagName('svg');
370-
svgElement = svgElements[0];
371-
svg = xmlNs + (svgElement ? svgElement.outerHTML : '');
372-
401+
var chartToDispose = [];
373402
for (var i = 0, len = svgElements.length; i < len; i++) {
374403
svgElement = svgElements[i];
375404
var id = svgElement.getAttribute('ac-id');
376405
var stage = anychart.graphics.getStage(id);
377406
if (stage) {
378407
var charts = stage.getCharts();
379-
for (var chart in charts) {
380-
charts[chart].dispose();
408+
for (var chartId in charts) {
409+
var chart = charts[chartId];
410+
bounds = chart.bounds();
411+
svg = getSvgString(svgElement, bounds.width(), bounds.height());
412+
chartToDispose.push(chart);
381413
}
382414
stage.dispose();
383415
}
384416
}
417+
418+
for (i = 0, len = chartToDispose.length; i < len; i++) {
419+
chartToDispose[i].dispose();
420+
}
385421
} else {
386422
if (dataType === 'json') {
387423
target = anychart.fromJson(target);
424+
isChart = true;
388425
} else if (dataType === 'xml') {
389426
target = anychart.fromXml(XMLparser.parseFromString(target));
427+
isChart = true;
390428
}
391-
392429
target.container(params.containerId);
393430

394-
var isChart = typeof target.draw === 'function';
395-
var isStage = typeof target.resume === 'function';
396-
397-
if (isChart) {
431+
if (isChart || isStage) {
398432
if (target.animation)
399433
target.animation(false);
400434
if (target.a11y)
401435
target.a11y(false);
402436

403437
container = target.container();
404438
if (!container) {
405-
//todo (blackart) needs add link to chart instance for parent anychart object. while it's not true will be used this approach.
406-
target = anychart.fromJson(target.toJson());
439+
if (params.document) {
440+
var div = params.document.createElement('div');
441+
div.setAttribute('id', params.containerId);
442+
params.document.body.appendChild(div);
443+
} else if (isChart) {
444+
//todo (blackart) for resolve this case need to add link to chart instance for parent anychart object. while it's not true will be used this approach.
445+
params.iframeId = createSandbox(params.containerId);
446+
params.document = iframeDoc;
447+
prepareDocumentForRender(params.document);
448+
anychart.global(params.document.defaultView);
449+
target = anychart.fromJson(target.toJson());
450+
} else {
451+
console.warn('Warning! Cannot find context of executing. Please define \'document\' in exporting params.');
452+
return callback(null, '', params);
453+
}
407454
target.container(params.containerId);
408455
container = target.container();
409456
}
410-
target.draw();
411457

412-
svgElement = container.container().getElementsByTagName('svg')[0];
413-
svg = xmlNs + svgElement.outerHTML;
458+
if (isChart) {
459+
target.draw();
414460

415-
target.dispose();
416-
} else if (isStage) {
417-
container = target.container();
418-
if (!container) {
419-
console.warn('Warning! Target chart has not container. Use container() method to set it.');
420-
return '';
461+
bounds = target.bounds();
462+
width = bounds.width();
463+
height = bounds.height();
464+
} else {
465+
target.resume();
466+
467+
bounds = target.getBounds();
468+
width = bounds.width;
469+
height = bounds.height;
421470
}
422471

423-
svgElement = container.getElementsByTagName('svg')[0];
424-
svg = ns + svgElement.outerHTML;
472+
svgElement = isChart ? container.getStage().domElement() : target.domElement();
473+
svg = getSvgString(svgElement, width, height);
474+
475+
if (dataType === 'json' && dataType === 'xml') {
476+
target.dispose();
477+
}
425478
} else {
426479
console.warn('Warning! Wrong format of incoming data.');
427480
svg = '';
@@ -618,6 +671,8 @@ AnychartExport.prototype.exportTo = function(target, options, callback) {
618671
}
619672

620673
var params = getParams(arguments);
674+
callback = params.callback;
675+
621676
if (typeof callback === 'function') {
622677
try {
623678
getSvg(target, params, function(err, svg, params) {
@@ -691,6 +746,8 @@ AnychartExport.prototype.loadFontSync = function(path) {
691746
return fonts[font.names.fullName.en.toLowerCase()] = opentype.loadSync(path);
692747
};
693748

749+
AnychartExport.prototype.anychartify = anychartify;
750+
694751
var AnychartExportWrapper = function(anychartInst) {
695752
if (anychartInst) {
696753
anychart = anychartInst;
@@ -707,6 +764,8 @@ AnychartExportWrapper.loadFont = AnychartExport.prototype.loadFont;
707764

708765
AnychartExportWrapper.loadFontSync = AnychartExport.prototype.loadFontSync;
709766

767+
AnychartExportWrapper.anychartify = AnychartExport.prototype.anychartify;
768+
710769
//endregion
711770

712771
loadDefaultFontsSync();

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "anychart-nodejs",
33
"main": "./lib/anychart-node",
4-
"version": "1.3.3",
4+
"version": "1.3.5",
55
"description": "AnyChart NodeJS module provides an easy way to generate SVG, JPG and PNG images of the charts on the server side.",
66
"title": "AnyChart NodeJS",
77
"homepage": "https://github.com/AnyChart/AnyChart-NodeJS",

0 commit comments

Comments
 (0)