@@ -9,6 +9,12 @@ class Exports {
9
9
this . w = ctx . w
10
10
}
11
11
12
+ svgStringToNode ( svgString ) {
13
+ const parser = new DOMParser ( )
14
+ const svgDoc = parser . parseFromString ( svgString , 'image/svg+xml' )
15
+ return svgDoc . documentElement
16
+ }
17
+
12
18
scaleSvgNode ( svg , scale ) {
13
19
// get current both width and height of the svg
14
20
let svgWidth = parseFloat ( svg . getAttributeNS ( null , 'width' ) )
@@ -30,18 +36,43 @@ class Exports {
30
36
if ( ! scale ) {
31
37
scale = 1 // if no scale is specified, don't scale...
32
38
}
33
- let svgString = this . w . globals . dom . Paper . svg ( )
34
39
35
- // clone the svg node so it remains intact in the UI
36
- const svgNode = this . w . globals . dom . Paper . node . cloneNode ( true )
37
-
38
- // in case the scale is different than 1, the svg needs to be rescaled
40
+ const width = w . globals . svgWidth * scale
41
+ const height = w . globals . svgHeight * scale
42
+
43
+ const clonedNode = w . globals . dom . elWrap . cloneNode ( true )
44
+ clonedNode . style . width = width + 'px'
45
+ clonedNode . style . height = height + 'px'
46
+ const serializedNode = new XMLSerializer ( ) . serializeToString ( clonedNode )
47
+
48
+ let svgString = `
49
+ <svg xmlns="http://www.w3.org/2000/svg"
50
+ version="1.1"
51
+ xmlns:xlink="http://www.w3.org/1999/xlink"
52
+ class="apexcharts-svg"
53
+ xmlns:data="ApexChartsNS"
54
+ transform="translate(0, 0)"
55
+ width="${ w . globals . svgWidth } px" height="${ w . globals . svgHeight } px">
56
+ <foreignObject width="100%" height="100%">
57
+ <div xmlns="http://www.w3.org/1999/xhtml" style="width:${ width } px; height:${ height } px;">
58
+ <style type="text/css">
59
+ .apexcharts-tooltip, .apexcharts-toolbar, .apexcharts-xaxistooltip, .apexcharts-yaxistooltip, .apexcharts-xcrosshairs, .apexcharts-ycrosshairs, .apexcharts-zoom-rect, .apexcharts-selection-rect {
60
+ display: none;
61
+ }
62
+ </style>
63
+ ${ serializedNode }
64
+ </div>
65
+ </foreignObject>
66
+ </svg>
67
+ `
68
+
69
+ const svgNode = this . svgStringToNode ( svgString )
39
70
40
71
if ( scale !== 1 ) {
41
72
// scale the image
42
73
this . scaleSvgNode ( svgNode , scale )
43
74
}
44
- // Convert image URLs to base64
75
+
45
76
this . convertImagesToBase64 ( svgNode ) . then ( ( ) => {
46
77
svgString = new XMLSerializer ( ) . serializeToString ( svgNode )
47
78
resolve ( svgString . replace ( / & n b s p ; / g, ' ' ) )
@@ -84,37 +115,8 @@ class Exports {
84
115
} )
85
116
}
86
117
87
- cleanup ( ) {
88
- const w = this . w
89
-
90
- // hide some elements to avoid printing them on exported svg
91
- const xcrosshairs = w . globals . dom . baseEl . getElementsByClassName (
92
- 'apexcharts-xcrosshairs'
93
- )
94
- const ycrosshairs = w . globals . dom . baseEl . getElementsByClassName (
95
- 'apexcharts-ycrosshairs'
96
- )
97
- const zoomSelectionRects = w . globals . dom . baseEl . querySelectorAll (
98
- '.apexcharts-zoom-rect, .apexcharts-selection-rect'
99
- )
100
- Array . prototype . forEach . call ( zoomSelectionRects , ( z ) => {
101
- z . setAttribute ( 'width' , 0 )
102
- } )
103
- if ( xcrosshairs && xcrosshairs [ 0 ] ) {
104
- xcrosshairs [ 0 ] . setAttribute ( 'x' , - 500 )
105
- xcrosshairs [ 0 ] . setAttribute ( 'x1' , - 500 )
106
- xcrosshairs [ 0 ] . setAttribute ( 'x2' , - 500 )
107
- }
108
- if ( ycrosshairs && ycrosshairs [ 0 ] ) {
109
- ycrosshairs [ 0 ] . setAttribute ( 'y' , - 100 )
110
- ycrosshairs [ 0 ] . setAttribute ( 'y1' , - 100 )
111
- ycrosshairs [ 0 ] . setAttribute ( 'y2' , - 100 )
112
- }
113
- }
114
-
115
118
svgUrl ( ) {
116
119
return new Promise ( ( resolve ) => {
117
- this . cleanup ( )
118
120
this . getSvgString ( ) . then ( ( svgData ) => {
119
121
const svgBlob = new Blob ( [ svgData ] , {
120
122
type : 'image/svg+xml;charset=utf-8' ,
@@ -132,7 +134,6 @@ class Exports {
132
134
? options . scale || options . width / w . globals . svgWidth
133
135
: 1
134
136
135
- this . cleanup ( )
136
137
const canvas = document . createElement ( 'canvas' )
137
138
canvas . width = w . globals . svgWidth * scale
138
139
canvas . height = parseInt ( w . globals . dom . elWrap . style . height , 10 ) * scale // because of resizeNonAxisCharts
0 commit comments