apache / apache/echarts

[SVG] Allow adding DOM to the chart in SVG renderer through `foreignObject`

Open
#15,062 3 comments 0 reactions 0 assignees View on GitHub
discussion-required en new-feature SVG
Dominant language
TypeScript
Stars
67.3k
Forks
19.8k
Avg merge
11d 14h
Merged PRs (30d)
8

Description

### What problem does this feature solve?

For SVG renderer, it would be flexible and creative if allow adding DOM to the chart through [`foreignObject`](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/foreignObject).

### What does the proposed API look like?

Currently no good idea about how to design the API in ECharts. Here is the proposed usage in zrender.

**DOM string**

```ts
// DOM string
const dom = new zrender.ForeignObject({
style: {
x: 600,
y: 250,
width: 200,
height: 400
},
rotation: -60 * Math.PI / 180,
originX: 600 + 200 / 2,
originY: 250 + 400 / 2,
// scaleX: 2,
// scaleY: 2,
html: `

Apache ECharts


一个基于 JavaScript 的开源可视化图表库



`
});
zr.add(dom);
```

**DOM Array**

```ts
// DOM array
const h1 = document.createElement('h1');
h1.className = 'hover-change-color';
h1.innerText = 'Created Element';
const p = document.createElement('p');
p.innerText = new Date().toLocaleString();
setInterval(() => p.innerText = new Date().toLocaleString(), 1e3);

const dom2 = new zrender.ForeignObject({
style: {
x: 600,
y: 150,
width: 200,
height: 200
},
originX: 600 + 200 / 2,
originY: 150 + 200 / 2,
scaleX: 0.8,
scaleY: 0.8,
html: [h1, p]
});
zr.add(dom2);
```

**Video**

```ts
// video
const video = document.createElement('video');
video.src = 'https://cdn.jsdelivr.net/gh/apache/echarts-website@asf-site/v4/zh/video/index.mp4';
video.poster = 'https://cdn.jsdelivr.net/gh/apache/echarts-website@asf-site/v4/zh/video/index-4.jpg';
video.muted = true;
video.autoplay = true;
video.controls = true;
video.loop = true;
video.style.objectFit = 'contain';
video.style.maxWidth = '100%';

const videoDom = new zrender.ForeignObject({
style: {
x: 50,
y: 250,
width: 400,
height: 400
},
html: video
});
zr.add(videoDom);
```

### Screenshot

![min](https://user-images.githubusercontent.com/26999792/120296418-ae7d6c80-c2fa-11eb-9cdb-f162899ce3ca.gif)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.