Skip to content

Commit beb688b

Browse files
committed
fixes #2963, fixes #3452; safari bugs fixed by removing legend from svg foreignObject
1 parent c5109b3 commit beb688b

File tree

3 files changed

+38
-41
lines changed

3 files changed

+38
-41
lines changed

src/modules/Core.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,7 @@ export default class Core {
107107
gl.dom.elLegendWrap = document.createElement('div')
108108
gl.dom.elLegendWrap.classList.add('apexcharts-legend')
109109

110-
gl.dom.elLegendWrap.setAttribute('xmlns', 'http://www.w3.org/1999/xhtml')
111-
gl.dom.elLegendForeign.appendChild(gl.dom.elLegendWrap)
112-
110+
gl.dom.elWrap.appendChild(gl.dom.elLegendWrap)
113111
gl.dom.Paper.node.appendChild(gl.dom.elLegendForeign)
114112

115113
gl.dom.elGraphical = gl.dom.Paper.group().attr({

src/modules/Exports.js

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ class Exports {
99
this.w = ctx.w
1010
}
1111

12+
svgStringToNode(svgString) {
13+
const parser = new DOMParser()
14+
const svgDoc = parser.parseFromString(svgString, 'image/svg+xml')
15+
return svgDoc.documentElement
16+
}
17+
1218
scaleSvgNode(svg, scale) {
1319
// get current both width and height of the svg
1420
let svgWidth = parseFloat(svg.getAttributeNS(null, 'width'))
@@ -30,18 +36,43 @@ class Exports {
3036
if (!scale) {
3137
scale = 1 // if no scale is specified, don't scale...
3238
}
33-
let svgString = this.w.globals.dom.Paper.svg()
3439

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)
3970

4071
if (scale !== 1) {
4172
// scale the image
4273
this.scaleSvgNode(svgNode, scale)
4374
}
44-
// Convert image URLs to base64
75+
4576
this.convertImagesToBase64(svgNode).then(() => {
4677
svgString = new XMLSerializer().serializeToString(svgNode)
4778
resolve(svgString.replace(/&nbsp;/g, '&#160;'))
@@ -84,37 +115,8 @@ class Exports {
84115
})
85116
}
86117

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-
115118
svgUrl() {
116119
return new Promise((resolve) => {
117-
this.cleanup()
118120
this.getSvgString().then((svgData) => {
119121
const svgBlob = new Blob([svgData], {
120122
type: 'image/svg+xml;charset=utf-8',
@@ -132,7 +134,6 @@ class Exports {
132134
? options.scale || options.width / w.globals.svgWidth
133135
: 1
134136

135-
this.cleanup()
136137
const canvas = document.createElement('canvas')
137138
canvas.width = w.globals.svgWidth * scale
138139
canvas.height = parseInt(w.globals.dom.elWrap.style.height, 10) * scale // because of resizeNonAxisCharts

src/modules/legend/Legend.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { SVG } from '@svgdotjs/svg.js'
2-
31
import CoreUtils from '../CoreUtils'
42
import Dimensions from '../dimensions/Dimensions'
53
import Graphics from '../Graphics'

0 commit comments

Comments
 (0)