@@ -202,40 +202,47 @@ function prepareDocumentForRender(doc) {
202
202
window . defaultBounds = defaultBounds ;
203
203
window . setTimeout = function ( code , delay , arguments ) { } ;
204
204
window . setInterval = function ( code , delay , arguments ) { } ;
205
- anychart . global ( window ) ;
206
205
}
207
206
208
207
function getParams ( args ) {
209
208
var arrLength = args . length ;
210
209
var lastArg = args [ arrLength - 1 ] ;
211
210
var callback = isFunction ( lastArg ) ? lastArg : null ;
212
211
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 ;
214
213
var params = { } ;
215
214
215
+ params . callback = callback ;
216
+
216
217
if ( typeof options === 'string' ) {
217
218
params . outputType = options ;
218
219
} else if ( typeof options === 'object' ) {
219
220
extend ( params , options )
220
221
}
221
222
222
223
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, '' ) ;
225
227
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
+ }
233
238
} else {
234
- params . dataType = 'xml ' ;
239
+ params . dataType = 'javascript ' ;
235
240
}
236
- } else {
237
- params . dataType = 'javascript' ;
238
241
}
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 ;
239
246
}
240
247
}
241
248
@@ -292,7 +299,7 @@ function applyResourcesToDoc(params, resources, callback) {
292
299
//
293
300
// });
294
301
} else if ( type == mime . contentType ( 'js' ) ) {
295
- scripts += ' ' + resource . body ;
302
+ scripts += ' ' + resource . body + ';' ;
296
303
}
297
304
}
298
305
@@ -339,23 +346,47 @@ function loadExternalResources(resources, params, callback) {
339
346
}
340
347
}
341
348
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
+
342
364
function getSvg ( target , params , callback ) {
343
365
var dataType = params . dataType ;
366
+
344
367
if ( dataType === 'svg' ) {
345
368
return callback ( null , fixSvg ( target ) , params ) ;
346
369
} else {
347
- var svg , ns , container , svgElement ;
370
+ var svg , container , svgElement ;
348
371
var res = params . resources ;
349
372
350
- if ( ! params . document ) {
373
+ var isChart = dataType === 'chart' ;
374
+ var isStage = dataType === 'stage' ;
375
+
376
+ if ( ! params . document && ! ( isChart || isStage ) ) {
351
377
params . iframeId = createSandbox ( params . containerId ) ;
352
378
params . document = iframeDoc ;
353
379
}
354
- prepareDocumentForRender ( params . document ) ;
380
+ if ( params . document )
381
+ prepareDocumentForRender ( params . document ) ;
355
382
356
383
return loadExternalResources ( res , params , function ( err , params ) {
357
384
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 ) ;
359
390
360
391
if ( dataType === 'javascript' ) {
361
392
var script = new vm . VM ( {
@@ -367,61 +398,83 @@ function getSvg(target, params, callback) {
367
398
script . run ( target ) ;
368
399
369
400
var svgElements = document . getElementsByTagName ( 'svg' ) ;
370
- svgElement = svgElements [ 0 ] ;
371
- svg = xmlNs + ( svgElement ? svgElement . outerHTML : '' ) ;
372
-
401
+ var chartToDispose = [ ] ;
373
402
for ( var i = 0 , len = svgElements . length ; i < len ; i ++ ) {
374
403
svgElement = svgElements [ i ] ;
375
404
var id = svgElement . getAttribute ( 'ac-id' ) ;
376
405
var stage = anychart . graphics . getStage ( id ) ;
377
406
if ( stage ) {
378
407
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 ) ;
381
413
}
382
414
stage . dispose ( ) ;
383
415
}
384
416
}
417
+
418
+ for ( i = 0 , len = chartToDispose . length ; i < len ; i ++ ) {
419
+ chartToDispose [ i ] . dispose ( ) ;
420
+ }
385
421
} else {
386
422
if ( dataType === 'json' ) {
387
423
target = anychart . fromJson ( target ) ;
424
+ isChart = true ;
388
425
} else if ( dataType === 'xml' ) {
389
426
target = anychart . fromXml ( XMLparser . parseFromString ( target ) ) ;
427
+ isChart = true ;
390
428
}
391
-
392
429
target . container ( params . containerId ) ;
393
430
394
- var isChart = typeof target . draw === 'function' ;
395
- var isStage = typeof target . resume === 'function' ;
396
-
397
- if ( isChart ) {
431
+ if ( isChart || isStage ) {
398
432
if ( target . animation )
399
433
target . animation ( false ) ;
400
434
if ( target . a11y )
401
435
target . a11y ( false ) ;
402
436
403
437
container = target . container ( ) ;
404
438
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
+ }
407
454
target . container ( params . containerId ) ;
408
455
container = target . container ( ) ;
409
456
}
410
- target . draw ( ) ;
411
457
412
- svgElement = container . container ( ) . getElementsByTagName ( 'svg' ) [ 0 ] ;
413
- svg = xmlNs + svgElement . outerHTML ;
458
+ if ( isChart ) {
459
+ target . draw ( ) ;
414
460
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 ;
421
470
}
422
471
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
+ }
425
478
} else {
426
479
console . warn ( 'Warning! Wrong format of incoming data.' ) ;
427
480
svg = '' ;
@@ -618,6 +671,8 @@ AnychartExport.prototype.exportTo = function(target, options, callback) {
618
671
}
619
672
620
673
var params = getParams ( arguments ) ;
674
+ callback = params . callback ;
675
+
621
676
if ( typeof callback === 'function' ) {
622
677
try {
623
678
getSvg ( target , params , function ( err , svg , params ) {
@@ -691,6 +746,8 @@ AnychartExport.prototype.loadFontSync = function(path) {
691
746
return fonts [ font . names . fullName . en . toLowerCase ( ) ] = opentype . loadSync ( path ) ;
692
747
} ;
693
748
749
+ AnychartExport . prototype . anychartify = anychartify ;
750
+
694
751
var AnychartExportWrapper = function ( anychartInst ) {
695
752
if ( anychartInst ) {
696
753
anychart = anychartInst ;
@@ -707,6 +764,8 @@ AnychartExportWrapper.loadFont = AnychartExport.prototype.loadFont;
707
764
708
765
AnychartExportWrapper . loadFontSync = AnychartExport . prototype . loadFontSync ;
709
766
767
+ AnychartExportWrapper . anychartify = AnychartExport . prototype . anychartify ;
768
+
710
769
//endregion
711
770
712
771
loadDefaultFontsSync ( ) ;
0 commit comments