Compare commits

...

14 Commits

14 changed files with 438 additions and 344 deletions

View File

@ -1,3 +1,27 @@
## 6.4.0
* Delay the disposal of the ECharts instance to the moment the element is disconnected from the DOM if possible (#433).
## 6.3.3
* Make autoresize work for grid layout by default (#675).
## 6.3.2
* Added basic types for events (only event names).
## 6.3.1
* Revert the style change to prevent tooltips from being clipped.
## 6.3.0
* Injected values can now be wrapped in an object so that they can be reactive in Vue 2.
## 6.2.4
* Fixed that attributes were not outputted onto the chart root element for Vue 2 (#670).
## 6.2.3
* Fixed the problem that `v-on` stops working after upgrading to `vue@2.7.x`.

221
README.md
View File

@ -20,13 +20,13 @@ Not ready yet? Read documentation for older versions [here →](https://github.c
$ npm install echarts vue-echarts
```
To make `vue-echarts` work for *Vue 2* (<2.7.0), you need to have `@vue/composition-api` installed:
To make `vue-echarts` work for _Vue 2_ (<2.7.0), you need to have `@vue/composition-api` installed:
```sh
npm i -D @vue/composition-api
```
If you are using *NuxtJS* on top of *Vue 2* (<2.7.0), you'll also need `@nuxtjs/composition-api`:
If you are using _NuxtJS_ on top of _Vue 2_ (<2.7.0), you'll also need `@nuxtjs/composition-api`:
```sh
npm i -D @nuxtjs/composition-api
@ -34,87 +34,7 @@ npm i -D @nuxtjs/composition-api
And then add `'@nuxtjs/composition-api/module'` in the `buildModules` option in your `nuxt.config.js`.
<details>
<summary>Vue 3</summary>
```js
import { createApp } from 'vue'
import ECharts from 'vue-echarts'
import { use } from "echarts/core"
// import ECharts modules manually to reduce bundle size
import {
CanvasRenderer
} from 'echarts/renderers'
import {
BarChart
} from 'echarts/charts'
import {
GridComponent,
TooltipComponent
} from 'echarts/components'
use([
CanvasRenderer,
BarChart,
GridComponent,
TooltipComponent
])
const app = createApp(...)
// register globally (or you can do it locally)
app.component('v-chart', ECharts)
app.mount(...)
```
</details>
<details>
<summary>Vue 2</summary>
```js
import Vue from 'vue'
import ECharts from 'vue-echarts'
import { use } from 'echarts/core'
// import ECharts modules manually to reduce bundle size
import {
CanvasRenderer
} from 'echarts/renderers'
import {
BarChart
} from 'echarts/charts'
import {
GridComponent,
TooltipComponent
} from 'echarts/components'
use([
CanvasRenderer,
BarChart,
GridComponent,
TooltipComponent
]);
// register globally (or you can do it locally)
Vue.component('v-chart', ECharts)
new Vue(...)
```
</details>
We encourage manually importing components and charts from ECharts for smaller bundle size. See all supported renderers/charts/components [here →](https://github.com/apache/echarts/blob/master/src/echarts.all.ts)
But if you really want to import the whole ECharts bundle without having to import modules manually, just add this in your code:
```js
import "echarts";
```
#### SFC example
#### Example
<details>
<summary>Vue 3 <a href="https://stackblitz.com/edit/vue-echarts-vue-3?file=src%2FApp.vue">Demo →</a></summary>
@ -124,7 +44,7 @@ import "echarts";
<v-chart class="chart" :option="option" />
</template>
<script>
<script setup>
import { use } from "echarts/core";
import { CanvasRenderer } from "echarts/renderers";
import { PieChart } from "echarts/charts";
@ -134,7 +54,7 @@ import {
LegendComponent
} from "echarts/components";
import VChart, { THEME_KEY } from "vue-echarts";
import { ref, defineComponent } from "vue";
import { ref, provide } from "vue";
use([
CanvasRenderer,
@ -144,55 +64,44 @@ use([
LegendComponent
]);
export default defineComponent({
name: "HelloWorld",
components: {
VChart
},
provide: {
[THEME_KEY]: "dark"
},
setup () {
const option = ref({
title: {
text: "Traffic Sources",
left: "center"
},
tooltip: {
trigger: "item",
formatter: "{a} <br/>{b} : {c} ({d}%)"
},
legend: {
orient: "vertical",
left: "left",
data: ["Direct", "Email", "Ad Networks", "Video Ads", "Search Engines"]
},
series: [
{
name: "Traffic Sources",
type: "pie",
radius: "55%",
center: ["50%", "60%"],
data: [
{ value: 335, name: "Direct" },
{ value: 310, name: "Email" },
{ value: 234, name: "Ad Networks" },
{ value: 135, name: "Video Ads" },
{ value: 1548, name: "Search Engines" }
],
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: "rgba(0, 0, 0, 0.5)"
}
}
}
]
});
provide(THEME_KEY, "dark");
return { option };
}
const option = ref({
title: {
text: "Traffic Sources",
left: "center"
},
tooltip: {
trigger: "item",
formatter: "{a} <br/>{b} : {c} ({d}%)"
},
legend: {
orient: "vertical",
left: "left",
data: ["Direct", "Email", "Ad Networks", "Video Ads", "Search Engines"]
},
series: [
{
name: "Traffic Sources",
type: "pie",
radius: "55%",
center: ["50%", "60%"],
data: [
{ value: 335, name: "Direct" },
{ value: 310, name: "Email" },
{ value: 234, name: "Ad Networks" },
{ value: 135, name: "Video Ads" },
{ value: 1548, name: "Search Engines" }
],
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: "rgba(0, 0, 0, 0.5)"
}
}
}
]
});
</script>
@ -299,6 +208,14 @@ export default {
</details>
We encourage manually importing components and charts from ECharts for smaller bundle size. See all supported renderers/charts/components [here →](https://github.com/apache/echarts/blob/master/src/echarts.all.ts)
But if you really want to import the whole ECharts bundle without having to import modules manually, just add this in your code:
```js
import "echarts";
```
### CDN & Global variable
Drop `<script>` inside your HTML file and access the component via `window.VueECharts`.
@ -310,7 +227,7 @@ Drop `<script>` inside your HTML file and access the component via `window.VueEC
```html
<script src="https://cdn.jsdelivr.net/npm/vue@3.2.37"></script>
<script src="https://cdn.jsdelivr.net/npm/echarts@5.3.3"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.2.3"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.4.0"></script>
```
<!-- vue3Scripts:end -->
@ -330,7 +247,7 @@ app.component('v-chart', VueECharts)
```html
<script src="https://cdn.jsdelivr.net/npm/vue@2.7.5"></script>
<script src="https://cdn.jsdelivr.net/npm/echarts@5.3.3"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.2.3"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.4.0"></script>
```
<!-- vue2Scripts:end -->
@ -399,16 +316,16 @@ Vue-ECharts provides provide/inject API for `theme`, `init-options`, `update-opt
<summary>Vue 3</summary>
```js
import { INIT_OPTIONS_KEY } from 'vue-echarts'
import { THEME_KEY } from 'vue-echarts'
import { provide } from 'vue'
// composition API
provide(INIT_OPTIONS_KEY, ...)
provide(THEME_KEY, 'dark')
// options API
{
provide: {
[INIT_OPTIONS_KEY]: { ... }
[THEME_KEY]: 'dark'
}
}
```
@ -419,16 +336,36 @@ provide(INIT_OPTIONS_KEY, ...)
<summary>Vue 2</summary>
```js
import { INIT_OPTIONS_KEY } from 'vue-echarts'
import { THEME_KEY } from 'vue-echarts'
// in component options
{
provide: {
[INIT_OPTIONS_KEY]: { ... }
[THEME_KEY]: 'dark'
}
}
```
> **Note**
>
> You need to provide an object for Vue 2 if you want to change it dynamically.
>
> ```js
> // in component options
> {
> data () {
> return {
> theme: { value: 'dark' }
> }
> },
> provide () {
> return {
> [THEME_KEY]: this.theme
> }
> }
> }
> ```
</details>
### Methods
@ -460,10 +397,12 @@ You can bind events with Vue's `v-on` directive.
```vue
<template>
<v-chart :option="option" @highlight="handleHighlight"/>
<v-chart :option="option" @highlight="handleHighlight" />
</template>
```
> **Note**
>
> Only the `.once` event modifier is supported as other modifiers are tightly coupled with the DOM event system.
Vue-ECharts support the following events:

View File

@ -18,13 +18,13 @@
$ npm install echarts vue-echarts
```
要在 *Vue 2*<2.7.0下使用 `vue-echarts`需要确保 `@vue/composition-api` 已经安装
要在 _Vue 2_<2.7.0下使用 `vue-echarts`需要确保 `@vue/composition-api` 已经安装
```sh
npm i -D @vue/composition-api
```
如果你在使用基于 *Vue 2*<2.7.0 *NuxtJS*那么还需要安装 `@nuxtjs/composition-api`
如果你在使用基于 _Vue 2_<2.7.0 _NuxtJS_那么还需要安装 `@nuxtjs/composition-api`
```sh
npm i -D @nuxtjs/composition-api
@ -32,87 +32,7 @@ npm i -D @nuxtjs/composition-api
然后在 `nuxt.config.js` `buildModules` 选项中添加 `'@nuxtjs/composition-api/module'`
<details>
<summary>Vue 3</summary>
```js
import { createApp } from 'vue'
import ECharts from 'vue-echarts'
import { use } from "echarts/core";
// 手动引入 ECharts 各模块来减小打包体积
import {
CanvasRenderer
} from 'echarts/renderers'
import {
BarChart
} from 'echarts/charts'
import {
GridComponent,
TooltipComponent
} from 'echarts/components'
use([
CanvasRenderer,
BarChart,
GridComponent,
TooltipComponent
]);
const app = createApp(...)
// 全局注册组件(也可以使用局部注册)
app.component('v-chart', ECharts)
app.mount(...)
```
</details>
<details>
<summary>Vue 2</summary>
```js
import Vue from 'vue'
import ECharts from 'vue-echarts'
import { use } from 'echarts/core'
// 手动引入 ECharts 各模块来减小打包体积
import {
CanvasRenderer
} from 'echarts/renderers'
import {
BarChart
} from 'echarts/charts'
import {
GridComponent,
TooltipComponent
} from 'echarts/components'
use([
CanvasRenderer,
BarChart,
GridComponent,
TooltipComponent
]);
// 全局注册组件(也可以使用局部注册)
Vue.component('v-chart', ECharts)
new Vue(...)
```
</details>
为了更小的打包体积,我们建议手动从 ECharts 引入单个图表和组件。请参考所有支持的渲染器/图表/组件。[前往 →](https://github.com/apache/echarts/blob/master/src/echarts.all.ts)
但如果你实在需要全量引入 ECharts 从而无需手动引入模块,只需要在代码中添加:
```js
import "echarts";
```
#### 单文件组件示例
#### 示例
<details>
<summary>Vue 3 <a href="https://stackblitz.com/edit/vue-echarts-vue-3?file=src%2FApp.vue">Demo →</a></summary>
@ -122,7 +42,7 @@ import "echarts";
<v-chart class="chart" :option="option" />
</template>
<script>
<script setup>
import { use } from "echarts/core";
import { CanvasRenderer } from "echarts/renderers";
import { PieChart } from "echarts/charts";
@ -132,7 +52,7 @@ import {
LegendComponent
} from "echarts/components";
import VChart, { THEME_KEY } from "vue-echarts";
import { ref, defineComponent } from "vue";
import { ref, provide } from "vue";
use([
CanvasRenderer,
@ -142,55 +62,44 @@ use([
LegendComponent
]);
export default defineComponent({
name: "HelloWorld",
components: {
VChart
},
provide: {
[THEME_KEY]: "dark"
},
setup: () => {
const option = ref({
title: {
text: "Traffic Sources",
left: "center"
},
tooltip: {
trigger: "item",
formatter: "{a} <br/>{b} : {c} ({d}%)"
},
legend: {
orient: "vertical",
left: "left",
data: ["Direct", "Email", "Ad Networks", "Video Ads", "Search Engines"]
},
series: [
{
name: "Traffic Sources",
type: "pie",
radius: "55%",
center: ["50%", "60%"],
data: [
{ value: 335, name: "Direct" },
{ value: 310, name: "Email" },
{ value: 234, name: "Ad Networks" },
{ value: 135, name: "Video Ads" },
{ value: 1548, name: "Search Engines" }
],
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: "rgba(0, 0, 0, 0.5)"
}
}
}
]
});
provide(THEME_KEY, "dark");
return { option };
}
const option = ref({
title: {
text: "Traffic Sources",
left: "center"
},
tooltip: {
trigger: "item",
formatter: "{a} <br/>{b} : {c} ({d}%)"
},
legend: {
orient: "vertical",
left: "left",
data: ["Direct", "Email", "Ad Networks", "Video Ads", "Search Engines"]
},
series: [
{
name: "Traffic Sources",
type: "pie",
radius: "55%",
center: ["50%", "60%"],
data: [
{ value: 335, name: "Direct" },
{ value: 310, name: "Email" },
{ value: 234, name: "Ad Networks" },
{ value: 135, name: "Video Ads" },
{ value: 1548, name: "Search Engines" }
],
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: "rgba(0, 0, 0, 0.5)"
}
}
}
]
});
</script>
@ -297,6 +206,14 @@ export default {
</details>
为了更小的打包体积,我们建议手动从 ECharts 引入单个图表和组件。请参考所有支持的渲染器/图表/组件。[前往 →](https://github.com/apache/echarts/blob/master/src/echarts.all.ts)
但如果你实在需要全量引入 ECharts 从而无需手动引入模块,只需要在代码中添加:
```js
import "echarts";
```
### CDN & 全局变量
用如下方式在 HTML 中插入 `<script>` 标签,并且通过 `window.VueECharts` 来访问组件接口:
@ -304,12 +221,11 @@ export default {
<details>
<summary>Vue 3 <a href="https://stackblitz.com/edit/vue-echarts-vue-3-global?file=index.html">Demo →</a></summary>
<!-- vue3Scripts:start -->
```html
<script src="https://cdn.jsdelivr.net/npm/vue@3.2.37"></script>
<script src="https://cdn.jsdelivr.net/npm/echarts@5.3.3"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.2.3"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.4.0"></script>
```
<!-- vue3Scripts:end -->
@ -329,7 +245,7 @@ app.component('v-chart', VueECharts)
```html
<script src="https://cdn.jsdelivr.net/npm/vue@2.7.5"></script>
<script src="https://cdn.jsdelivr.net/npm/echarts@5.3.3"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.2.3"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.4.0"></script>
```
<!-- vue2Scripts:end -->
@ -396,10 +312,12 @@ Vue.component("v-chart", VueECharts);
```vue
<template>
<v-chart :option="option" @highlight="handleHighlight"/>
<v-chart :option="option" @highlight="handleHighlight" />
</template>
```
> **Note**
>
> 仅支持 `.once` 修饰符,因为其它修饰符都与 DOM 事件机制强耦合。
Vue-ECharts 支持如下事件:
@ -458,16 +376,16 @@ Vue-ECharts 为 `theme`、`init-options`、`update-options` 和 `loading-options
<summary>Vue 3</summary>
```js
import { INIT_OPTIONS_KEY } from 'vue-echarts'
import { THEME_KEY } from 'vue-echarts'
import { provide } from 'vue'
// composition API
provide(INIT_OPTIONS_KEY, ...)
// 组合式 API
provide(THEME_KEY, 'dark')
// options API
// 选项式 API
{
provide: {
[INIT_OPTIONS_KEY]: { ... }
[THEME_KEY]: 'dark'
}
}
```
@ -478,16 +396,36 @@ provide(INIT_OPTIONS_KEY, ...)
<summary>Vue 2</summary>
```js
import { INIT_OPTIONS_KEY } from 'vue-echarts'
import { THEME_KEY } from 'vue-echarts'
// in component options
// 组件选项中
{
provide: {
[INIT_OPTIONS_KEY]: { ... }
[THEME_KEY]: 'dark'
}
}
```
> **Note**
>
> 在 Vue 2 中,如果你想动态地改变这些选项,那么你需要提供一个对象。
>
> ```js
> // 组件选项中
> {
> data () {
> return {
> theme: { value: 'dark' }
> }
> },
> provide () {
> return {
> [THEME_KEY]: this.theme
> }
> }
> }
> ```
</details>
### 方法

View File

@ -1,6 +1,6 @@
{
"name": "vue-echarts",
"version": "6.2.3",
"version": "6.4.0",
"description": "Vue.js component for Apache ECharts.",
"author": "GU Yiling <justice360@gmail.com>",
"scripts": {
@ -41,6 +41,7 @@
"comment-mark": "^1.0.0",
"core-js": "^3.23.0",
"echarts": "^5.3.2",
"echarts-liquidfill": "^3.1.0",
"eslint": "^7.20.0",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-vue": "^8.7.1",
@ -50,6 +51,7 @@
"postcss-nested": "^5.0.5",
"prettier": "^2.6.2",
"qs": "^6.10.5",
"raw-loader": "^4.0.2",
"resize-detector": "^0.3.0",
"rimraf": "^3.0.2",
"rollup": "^2.72.1",
@ -60,7 +62,7 @@
"tslib": "^2.4.0",
"typescript": "4.6.4",
"vue": "^3.2.33",
"vue2": "npm:vue@^2.7.4",
"vue2": "npm:vue@^2.7.14",
"webpack": "^5.72.1"
},
"peerDependencies": {

65
pnpm-lock.yaml generated
View File

@ -16,6 +16,7 @@ specifiers:
comment-mark: ^1.0.0
core-js: ^3.23.0
echarts: ^5.3.2
echarts-liquidfill: ^3.1.0
eslint: ^7.20.0
eslint-plugin-prettier: ^3.3.1
eslint-plugin-vue: ^8.7.1
@ -25,6 +26,7 @@ specifiers:
postcss-nested: ^5.0.5
prettier: ^2.6.2
qs: ^6.10.5
raw-loader: ^4.0.2
resize-detector: ^0.3.0
rimraf: ^3.0.2
rollup: ^2.72.1
@ -36,7 +38,7 @@ specifiers:
typescript: 4.6.4
vue: ^3.2.33
vue-demi: ^0.13.2
vue2: npm:vue@^2.7.4
vue2: npm:vue@^2.7.14
webpack: ^5.72.1
dependencies:
@ -48,10 +50,10 @@ devDependencies:
'@rollup/plugin-node-resolve': 11.2.1_rollup@2.75.7
'@typescript-eslint/eslint-plugin': 4.33.0_lzzr7k3tjtqil7aczuhmzzwame
'@typescript-eslint/parser': 4.33.0_e4zyhrvfnqudwdx5bevnvkluy4
'@vue/cli-plugin-babel': 5.0.7_szrn552deqanolg2xhvxv3f6si
'@vue/cli-plugin-babel': 5.0.7_bfbyvxbwdkbpzjdhlu2ghmbxte
'@vue/cli-plugin-eslint': 5.0.7_sogf6mc3t6tnpddmelmhv5sy5y
'@vue/cli-plugin-typescript': 5.0.7_fqel5ol4p36s622pmc3xeh5nmy
'@vue/cli-service': 5.0.7_khqsjqtt6bg4xxifpcjoaqsude
'@vue/cli-service': 5.0.7_wh7dux2groxeldr2stfgzduwne
'@vue/compiler-sfc': 3.2.37
'@vue/composition-api': 1.7.0_vue@3.2.37
'@vue/eslint-config-prettier': 6.0.0_rklrwr6twxyl7u6a6qb67cswlu
@ -59,6 +61,7 @@ devDependencies:
comment-mark: 1.1.1
core-js: 3.23.3
echarts: 5.3.3
echarts-liquidfill: 3.1.0_echarts@5.3.3
eslint: 7.32.0
eslint-plugin-prettier: 3.4.1_fqyzhpusvewbsl54pqqbxqaegm
eslint-plugin-vue: 8.7.1_eslint@7.32.0
@ -68,6 +71,7 @@ devDependencies:
postcss-nested: 5.0.6_postcss@8.4.14
prettier: 2.7.1
qs: 6.11.0
raw-loader: 4.0.2_webpack@5.73.0
rimraf: 3.0.2
rollup: 2.75.7
rollup-plugin-dts: 4.2.2_tawkl7lyp7gxtk5srebtmqqvue
@ -77,7 +81,7 @@ devDependencies:
tslib: 2.4.0
typescript: 4.6.4
vue: 3.2.37
vue2: /vue/2.7.4
vue2: /vue/2.7.14
webpack: 5.73.0
packages:
@ -1934,9 +1938,11 @@ packages:
svg-tags: 1.0.0
dev: true
/@vue/babel-preset-app/5.0.7_vue@3.2.37:
/@vue/babel-preset-app/5.0.7_6aw556sdtr7wdbt7wby5rddhty:
resolution: {integrity: sha512-oepDJNWbYxosfopP7SPDZTBQl0k7KN5xs/uGWT12K+ih6f54Qo/4gEEoIVI8p2yuCRLIIc46SIhZn3AugOgx1g==}
peerDependencies:
'@babel/core': '*'
core-js: ^3
vue: ^2 || ^3.2.13
peerDependenciesMeta:
core-js:
@ -2046,20 +2052,21 @@ packages:
resolution: {integrity: sha512-Fz0C0TnJI5ARYV7U9DtE9XnYb7OHGhtKv1Wh1fJ8Ugy97kFMnHnXHl+/MCISx/EKdCaKJf0zs7ylHoZGhRhhfg==}
dev: true
/@vue/cli-plugin-babel/5.0.7_szrn552deqanolg2xhvxv3f6si:
/@vue/cli-plugin-babel/5.0.7_bfbyvxbwdkbpzjdhlu2ghmbxte:
resolution: {integrity: sha512-qWrwhX7rZ8vQq6+niKG8OkBak0vDhIWzJsLXH5i+xzm04mcudPJwPZhQOBpN3XXkipsic1ZCkhS9d3+VjsWQYg==}
peerDependencies:
'@vue/cli-service': ^3.0.0 || ^4.0.0 || ^5.0.0-0
dependencies:
'@babel/core': 7.18.6
'@vue/babel-preset-app': 5.0.7_vue@3.2.37
'@vue/cli-service': 5.0.7_khqsjqtt6bg4xxifpcjoaqsude
'@vue/babel-preset-app': 5.0.7_6aw556sdtr7wdbt7wby5rddhty
'@vue/cli-service': 5.0.7_wh7dux2groxeldr2stfgzduwne
'@vue/cli-shared-utils': 5.0.7
babel-loader: 8.2.5_fswvdo7jykdwhfxrdcvghfn6pa
thread-loader: 3.0.4_webpack@5.73.0
webpack: 5.73.0
transitivePeerDependencies:
- '@swc/core'
- core-js
- encoding
- esbuild
- supports-color
@ -2074,7 +2081,7 @@ packages:
'@vue/cli-service': ^3.0.0 || ^4.0.0 || ^5.0.0-0
eslint: '>=7.5.0'
dependencies:
'@vue/cli-service': 5.0.7_khqsjqtt6bg4xxifpcjoaqsude
'@vue/cli-service': 5.0.7_wh7dux2groxeldr2stfgzduwne
'@vue/cli-shared-utils': 5.0.7
eslint: 7.32.0
eslint-webpack-plugin: 3.2.0_oxnz3ipaot6yjz2b7jqxkugcdm
@ -2094,7 +2101,7 @@ packages:
peerDependencies:
'@vue/cli-service': ^3.0.0 || ^4.0.0 || ^5.0.0-0
dependencies:
'@vue/cli-service': 5.0.7_khqsjqtt6bg4xxifpcjoaqsude
'@vue/cli-service': 5.0.7_wh7dux2groxeldr2stfgzduwne
'@vue/cli-shared-utils': 5.0.7
transitivePeerDependencies:
- encoding
@ -2116,7 +2123,7 @@ packages:
dependencies:
'@babel/core': 7.18.6
'@types/webpack-env': 1.17.0
'@vue/cli-service': 5.0.7_khqsjqtt6bg4xxifpcjoaqsude
'@vue/cli-service': 5.0.7_wh7dux2groxeldr2stfgzduwne
'@vue/cli-shared-utils': 5.0.7
babel-loader: 8.2.5_fswvdo7jykdwhfxrdcvghfn6pa
fork-ts-checker-webpack-plugin: 6.5.2_irit3cwhef6e6mopqxb7zqusqa
@ -2141,10 +2148,10 @@ packages:
peerDependencies:
'@vue/cli-service': ^3.0.0 || ^4.0.0 || ^5.0.0-0
dependencies:
'@vue/cli-service': 5.0.7_khqsjqtt6bg4xxifpcjoaqsude
'@vue/cli-service': 5.0.7_wh7dux2groxeldr2stfgzduwne
dev: true
/@vue/cli-service/5.0.7_khqsjqtt6bg4xxifpcjoaqsude:
/@vue/cli-service/5.0.7_wh7dux2groxeldr2stfgzduwne:
resolution: {integrity: sha512-ajYBY4CADbuUUwTxRFEx/Jrjalm2YM0ICP37yD4je/wyzcfsKj2iSJJPZpoigPqm/AxO4XN5lYWi5apJNSTpxA==}
engines: {node: ^12.0.0 || >= 14.0.0}
hasBin: true
@ -2218,6 +2225,7 @@ packages:
postcss: 8.4.14
postcss-loader: 6.2.1_mepnsno3xmng6eyses4tepu7bu
progress-webpack-plugin: 1.0.16_webpack@5.73.0
raw-loader: 4.0.2_webpack@5.73.0
ssri: 8.0.1
terser-webpack-plugin: 5.3.3_webpack@5.73.0
thread-loader: 3.0.4_webpack@5.73.0
@ -2332,8 +2340,8 @@ packages:
'@vue/compiler-core': 3.2.37
'@vue/shared': 3.2.37
/@vue/compiler-sfc/2.7.4:
resolution: {integrity: sha512-WCaF33mlKLSvHDKvOD6FzTa5CI2FlMTeJf3MxJsNP0KDgRoI6RdXhHo9dtvCqV4Sywf9Owm17wTLT1Ymu/WsOQ==}
/@vue/compiler-sfc/2.7.14:
resolution: {integrity: sha512-aNmNHyLPsw+sVvlQFQ2/8sjNuLtK54TC6cuKnVzAY93ks4ZBrvwQSnkkIh7bsbNhum5hJBS00wSDipQ937f5DA==}
dependencies:
'@babel/parser': 7.18.6
postcss: 8.4.14
@ -3328,7 +3336,7 @@ packages:
lodash: ^4.17.20
marko: ^3.14.4
mote: ^0.2.0
mustache: ^4.0.1
mustache: ^3.0.0
nunjucks: ^3.2.2
plates: ~0.4.11
pug: ^3.0.0
@ -3917,6 +3925,14 @@ packages:
engines: {node: '>=6.0.0'}
dev: true
/echarts-liquidfill/3.1.0_echarts@5.3.3:
resolution: {integrity: sha512-5Dlqs/jTsdTUAsd+K5LPLLTgrbbNORUSBQyk8PSy1Mg2zgHDWm83FmvA4s0ooNepCJojFYRITTQ4GU1UUSKYLw==}
peerDependencies:
echarts: ^5.0.1
dependencies:
echarts: 5.3.3
dev: true
/echarts/5.3.3:
resolution: {integrity: sha512-BRw2serInRwO5SIwRviZ6Xgm5Lb7irgz+sLiFMmy/HOaf4SQ+7oYqxKzRHAKp4xHQ05AuHw1xvoQWJjDQq/FGw==}
dependencies:
@ -6311,6 +6327,17 @@ packages:
unpipe: 1.0.0
dev: true
/raw-loader/4.0.2_webpack@5.73.0:
resolution: {integrity: sha512-ZnScIV3ag9A4wPX/ZayxL/jZH+euYb6FcUinPcgiQW0+UBtEv0O6Q3lGd3cqJ+GHH+rksEv3Pj99oxJ3u3VIKA==}
engines: {node: '>= 10.13.0'}
peerDependencies:
webpack: ^4.0.0 || ^5.0.0
dependencies:
loader-utils: 2.0.2
schema-utils: 3.1.1
webpack: 5.73.0
dev: true
/read-pkg-up/7.0.1:
resolution: {integrity: sha512-zK0TB7Xd6JpCLmlLmufqykGE+/TlOePD6qKClNW7hHDKFh/J7/7gCWGR7joEQEW1bKq3a3yUZSObOoWLFQ4ohg==}
engines: {node: '>=8'}
@ -7475,10 +7502,10 @@ packages:
resolution: {integrity: sha512-4gDntzrifFnCEvyoO8PqyJDmguXgVPxKiIxrBKjIowvL9l+N66196+72XVYR8BBf1Uv1Fgt3bGevJ+sEmxfZzw==}
dev: true
/vue/2.7.4:
resolution: {integrity: sha512-8KGyyzFSj/FrKj1y7jyEpv8J4osgZx6Lk1lVzh1aP4BqsXZhATH1r0gdJNz00MMyBhK0/m2cNoPuOZ1NzeiUEw==}
/vue/2.7.14:
resolution: {integrity: sha512-b2qkFyOM0kwqWFuQmgd4o+uHGU7T+2z3T+WQp8UBjADfEv2n4FEMffzBmCKNP0IGzOEEfYjvtcC62xaSKeQDrQ==}
dependencies:
'@vue/compiler-sfc': 2.7.4
'@vue/compiler-sfc': 2.7.14
csstype: 3.1.0
dev: true

View File

@ -2,14 +2,13 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import {
defineComponent,
unref,
shallowRef,
toRefs,
watch,
computed,
inject,
onMounted,
onUnmounted,
onBeforeUnmount,
h,
nextTick,
watchEffect,
@ -28,7 +27,8 @@ import type {
InitOptions,
InitOptionsInjection,
UpdateOptions,
UpdateOptionsInjection
UpdateOptionsInjection,
Emits
} from "./types";
import {
usePublicAPI,
@ -37,10 +37,11 @@ import {
useLoading,
loadingProps
} from "./composables";
import { omitOn } from "./utils";
import { omitOn, unwrapInjected } from "./utils";
import { register, TAG_NAME, type EChartsElement } from "./wc";
import "./style.css";
const TAG_NAME = "x-vue-echarts";
const wcRegistered = register();
if (Vue2) {
Vue2.config.ignoredElements.push(TAG_NAME);
@ -67,9 +68,10 @@ export default defineComponent({
...autoresizeProps,
...loadingProps
},
emits: [] as unknown as Emits,
inheritAttrs: false,
setup(props, { attrs }) {
const root = shallowRef<HTMLElement>();
const root = shallowRef<EChartsElement>();
const chart = shallowRef<EChartsType>();
const manualOption = shallowRef<Option>();
const defaultTheme = inject(THEME_KEY, null);
@ -81,12 +83,14 @@ export default defineComponent({
const realOption = computed(
() => manualOption.value || props.option || null
);
const realTheme = computed(() => props.theme || unref(defaultTheme) || {});
const realTheme = computed(
() => props.theme || unwrapInjected(defaultTheme, {})
);
const realInitOptions = computed(
() => props.initOptions || unref(defaultInitOptions) || {}
() => props.initOptions || unwrapInjected(defaultInitOptions, {})
);
const realUpdateOptions = computed(
() => props.updateOptions || unref(defaultUpdateOptions) || {}
() => props.updateOptions || unwrapInjected(defaultUpdateOptions, {})
);
const nonEventAttrs = computed(() => omitOn(attrs));
@ -270,7 +274,17 @@ export default defineComponent({
init();
});
onUnmounted(cleanup);
onBeforeUnmount(() => {
if (wcRegistered && root.value) {
// For registered web component, we can leverage the
// `disconnectedCallback` to dispose the chart instance
// so that we can delay the cleanup after exsiting leaving
// transition.
root.value.__dispose = cleanup;
} else {
cleanup();
}
});
return {
chart,
@ -281,7 +295,11 @@ export default defineComponent({
};
},
render() {
const attrs = { ...this.nonEventAttrs };
// Vue 3 and Vue 2 have different vnode props format:
// See https://v3-migration.vuejs.org/breaking-changes/render-function-api.html#vnode-props-format
const attrs = (
Vue2 ? { attrs: this.nonEventAttrs } : { ...this.nonEventAttrs }
) as any;
attrs.ref = "root";
attrs.class = attrs.class ? ["echarts"].concat(attrs.class) : "echarts";
return h(TAG_NAME, attrs);

View File

@ -1,10 +1,10 @@
import { unwrapInjected } from "../utils";
import {
inject,
unref,
computed,
Ref,
watchEffect,
InjectionKey
type Ref,
type InjectionKey
} from "vue-demi";
import { EChartsType } from "../types";
@ -22,7 +22,7 @@ export function useLoading(
): void {
const defaultLoadingOptions = inject(LOADING_OPTIONS_KEY, {});
const realLoadingOptions = computed(() => ({
...unref(defaultLoadingOptions),
...unwrapInjected(defaultLoadingOptions, {}),
...loadingOptions?.value
}));

View File

@ -1,7 +1,12 @@
<template>
<main>
<!-- <v-chart
class="echarts" id="logo" :option="logo" :init-options="initOptions" autoresize />-->
<v-chart
class="echarts"
id="logo"
:option="logo"
:init-options="initOptions"
autoresize
/>
<h1>
<a href="https://github.com/ecomfe/vue-echarts">Vue-ECharts</a>
</h1>
@ -328,8 +333,8 @@ import {
DataZoomComponent
} from "echarts/components";
import { CanvasRenderer, SVGRenderer } from "echarts/renderers";
// import "echarts-liquidfill";
// import logo from "./data/logo";
import "echarts-liquidfill";
import logo from "./data/logo";
import getBar from "./data/bar";
import pie from "./data/pie";
import polar from "./data/polar";
@ -383,6 +388,7 @@ export default {
const options = qs.parse(location.search, { ignoreQueryPrefix: true });
return {
options,
logo,
bar: getBar(),
pie,
polar,
@ -719,7 +725,6 @@ select {
font: inherit;
padding: 0 0.5em;
transition: opacity 0.3s;
-webkit-appearance: none;
transition: all 0.2s;
&:focus {
@ -890,10 +895,6 @@ figure {
font-size: 0.8em;
}
select {
-webkit-appearance: none;
}
input[type="checkbox"] {
display: none;

15
src/index.vue2.d.ts vendored
View File

@ -1,6 +1,12 @@
/* eslint-disable @typescript-eslint/ban-types */
import { Ref, DefineComponent } from "vue-demi";
import { Option, InitOptions, UpdateOptions, EChartsType } from "./types";
import type { Ref, DefineComponent } from "vue-demi";
import type {
Option,
InitOptions,
UpdateOptions,
EChartsType,
Emits
} from "./types";
declare const LOADING_OPTIONS_KEY = "ecLoadingOptions";
declare const THEME_KEY = "ecTheme";
@ -47,7 +53,10 @@ declare const Chart: DefineComponent<
},
{},
{},
ChartMethods
ChartMethods,
{},
{},
Emits
>;
export default Chart;

View File

@ -1 +1 @@
x-vue-echarts{display:block;width:100%;height:100%}
x-vue-echarts{display:block;width:100%;height:100%;min-width:0}

View File

@ -1,12 +1,18 @@
import { init, SetOptionOpts } from "echarts/core";
import { Ref } from "vue";
import { init, type SetOptionOpts } from "echarts/core";
import type { Ref } from "vue";
export type Injection<T> = T | null | Ref<T | null> | { value: T | null };
type InitType = typeof init;
export type InitParameters = Parameters<InitType>;
export type Theme = NonNullable<InitParameters[1]>;
export type ThemeInjection = Theme | null | Ref<Theme | null>;
export type ThemeInjection = Injection<Theme>;
export type InitOptions = NonNullable<InitParameters[2]>;
export type InitOptionsInjection = InitOptions | null | Ref<InitOptions | null>;
export type InitOptionsInjection = Injection<InitOptions>;
export type UpdateOptions = SetOptionOpts;
export type UpdateOptionsInjection = Injection<UpdateOptions>;
export type EChartsType = ReturnType<InitType>;
type ZRenderType = ReturnType<EChartsType["getZr"]>;
@ -14,8 +20,64 @@ export type EventTarget = EChartsType | ZRenderType;
type SetOptionType = EChartsType["setOption"];
export type Option = Parameters<SetOptionType>[0];
export type UpdateOptions = SetOptionOpts;
export type UpdateOptionsInjection =
| UpdateOptions
| null
| Ref<UpdateOptions | null>;
type EChartsEventName =
| "click"
| "dblclick"
| "mousedown"
| "mousemove"
| "mouseup"
| "mouseover"
| "mouseout"
| "globalout"
| "contextmenu"
| "highlight"
| "downplay"
| "selectchanged"
| "legendselectchanged"
| "legendselected"
| "legendunselected"
| "legendselectall"
| "legendinverseselect"
| "legendscroll"
| "datazoom"
| "datarangeselected"
| "graphroam"
| "georoam"
| "treeroam"
| "timelinechanged"
| "timelineplaychanged"
| "restore"
| "dataviewchanged"
| "magictypechanged"
| "geoselectchanged"
| "geoselected"
| "geounselected"
| "axisareaselected"
| "brush"
| "brushEnd"
| "brushselected"
| "globalcursortaken"
| "rendered"
| "finished";
type ZRenderEventName =
| "click"
| "dblclick"
| "mousewheel"
| "mouseout"
| "mouseover"
| "mouseup"
| "mousedown"
| "mousemove"
| "contextmenu"
| "drag"
| "dragstart"
| "dragend"
| "dragenter"
| "dragleave"
| "dragover"
| "drop"
| "globalout";
type EventName = EChartsEventName | `zr:${ZRenderEventName}`;
export type Emits = {
[key in EventName]: null;
};

View File

@ -1,3 +1,6 @@
import { unref } from "vue-demi";
import type { Injection } from "./types";
type Attrs = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
@ -18,3 +21,16 @@ export function omitOn(attrs: Attrs): Attrs {
return result;
}
export function unwrapInjected<T, V>(
injection: Injection<T>,
defaultValue: V
): T | V {
const value = unref(injection);
if (value && typeof value === "object" && "value" in value) {
return value.value || defaultValue;
}
return value || defaultValue;
}

51
src/wc.ts Normal file
View File

@ -0,0 +1,51 @@
let registered: boolean | null = null;
export const TAG_NAME = "x-vue-echarts";
export interface EChartsElement extends HTMLElement {
__dispose: (() => void) | null;
}
export function register(): boolean {
if (registered != null) {
return registered;
}
if (
typeof HTMLElement === "undefined" ||
typeof customElements === "undefined"
) {
return (registered = false);
}
try {
// Class definitions cannot be transpiled to ES5
// so we are doing a little trick here to ensure
// we are using native classes. As we use this as
// a progressive enhancement, it will be fine even
// if the browser doesn't support native classes.
const reg = new Function(
"tag",
`class EChartsElement extends HTMLElement {
__dispose = null;
disconnectedCallback() {
if (this.__dispose) {
this.__dispose();
this.__dispose = null;
}
}
}
if (customElements.get(tag) == null) {
customElements.define(tag, EChartsElement);
}
`
);
reg(TAG_NAME);
} catch (e) {
return (registered = false);
}
return (registered = true);
}

View File

@ -14,5 +14,12 @@ module.exports = {
},
chainWebpack: config => {
config.entry("app").clear().add("./src/demo/main.ts");
config.module
.rule("svg")
.clear()
.test(/\.svg$/)
.use("raw-loader")
.loader("raw-loader");
}
};