Skip to content

Commit ee12ad9

Browse files
committed
feat: vue-echarts-next first version
1 parent e4ff8e6 commit ee12ad9

22 files changed

+3218
-1044
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_style = space
6+
indent_size = 2
7+
end_of_line = lf
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist

package-lock.json

Lines changed: 2557 additions & 870 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,47 @@
11
{
22
"name": "vue-echarts-next",
33
"version": "0.1.0",
4-
"private": true,
54
"scripts": {
65
"serve": "vue-cli-service serve",
7-
"build": "vue-cli-service build",
6+
"build:demo": "vue-cli-service build",
7+
"build": "rollup -c rollup.config.js",
88
"lint": "vue-cli-service lint"
99
},
10+
"main": "dist/index.cjs.min.js",
11+
"module": "dist/index.esm.min.js",
12+
"unpkg": "dist/index.umd.min.js",
13+
"jsdelivr": "dist/index.umd.min.js",
1014
"dependencies": {
1115
"core-js": "^3.6.5",
12-
"vue": "^3.0.0"
16+
"resize-detector": "^0.2.2"
1317
},
1418
"devDependencies": {
19+
"@rollup/plugin-node-resolve": "^11.1.1",
1520
"@typescript-eslint/eslint-plugin": "^2.33.0",
1621
"@typescript-eslint/parser": "^2.33.0",
1722
"@vue/cli-plugin-babel": "~4.5.0",
1823
"@vue/cli-plugin-eslint": "~4.5.0",
1924
"@vue/cli-plugin-typescript": "~4.5.0",
2025
"@vue/cli-service": "~4.5.0",
2126
"@vue/compiler-sfc": "^3.0.0",
27+
"@vue/composition-api": "^1.0.0-rc.1",
2228
"@vue/eslint-config-prettier": "^6.0.0",
2329
"@vue/eslint-config-typescript": "^5.0.2",
30+
"echarts": "^5.0.2",
2431
"eslint": "^6.7.2",
2532
"eslint-plugin-prettier": "^3.1.3",
2633
"eslint-plugin-vue": "^7.0.0-0",
34+
"postcss": "^8.2.5",
2735
"prettier": "^1.19.1",
28-
"typescript": "~3.9.3"
36+
"rollup": "^2.38.5",
37+
"rollup-plugin-postcss": "^4.0.0",
38+
"rollup-plugin-terser": "^7.0.2",
39+
"rollup-plugin-typescript2": "^0.29.0",
40+
"typescript": "^4.1.3",
41+
"vue": "npm:vue@^3.0.5"
42+
},
43+
"peerDependencies": {
44+
"echarts": "^5.0.2",
45+
"vue": "^2.6.11 || ^3.0.0"
2946
}
3047
}

rollup.config.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import typescript from "rollup-plugin-typescript2";
2+
import { terser } from "rollup-plugin-terser";
3+
import postcss from "rollup-plugin-postcss";
4+
import resolve from "@rollup/plugin-node-resolve";
5+
6+
/** @type {import('rollup').RollupOptions} */
7+
const options = [
8+
{
9+
plugins: [
10+
typescript({
11+
useTsconfigDeclarationDir: true
12+
}),
13+
postcss()
14+
],
15+
external: ["vue", "echarts/core", "resize-detector"],
16+
input: "src/index.ts",
17+
output: [
18+
{
19+
file: "dist/index.esm.js",
20+
format: "es",
21+
sourcemap: true
22+
},
23+
{
24+
file: "dist/index.esm.min.js",
25+
format: "es",
26+
sourcemap: true,
27+
plugins: [
28+
terser({
29+
format: {
30+
comments: false
31+
}
32+
})
33+
]
34+
},
35+
{
36+
file: "dist/index.cjs.js",
37+
format: "cjs",
38+
exports: "default",
39+
sourcemap: true
40+
},
41+
{
42+
file: "dist/index.cjs.min.js",
43+
format: "cjs",
44+
exports: "default",
45+
sourcemap: true,
46+
plugins: [
47+
terser({
48+
format: {
49+
comments: false
50+
}
51+
})
52+
]
53+
}
54+
]
55+
},
56+
{
57+
plugins: [
58+
resolve(),
59+
typescript({
60+
useTsconfigDeclarationDir: true
61+
}),
62+
postcss()
63+
],
64+
external: ["vue", "echarts/core"],
65+
input: "src/all.ts",
66+
output: [
67+
{
68+
file: "dist/index.umd.js",
69+
format: "umd",
70+
name: "VueECharts",
71+
sourcemap: true,
72+
globals: {
73+
vue: "Vue",
74+
"echarts/core": "echarts"
75+
}
76+
},
77+
{
78+
file: "dist/index.umd.min.js",
79+
format: "umd",
80+
name: "VueECharts",
81+
sourcemap: true,
82+
globals: {
83+
vue: "Vue",
84+
"echarts/core": "echarts"
85+
},
86+
plugins: [
87+
terser({
88+
format: {
89+
comments: false
90+
}
91+
})
92+
]
93+
}
94+
]
95+
}
96+
];
97+
98+
export default options;
File renamed without changes.

src/App.vue

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/ECharts.ts

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
import {
3+
defineComponent,
4+
ref,
5+
shallowRef,
6+
toRefs,
7+
watch,
8+
computed,
9+
inject,
10+
onMounted,
11+
onUnmounted,
12+
h,
13+
PropType
14+
} from "vue";
15+
import { init as initChart } from "echarts/core";
16+
import { EChartsType, OptionType } from "@/types";
17+
import {
18+
usePublicAPI,
19+
useAutoresize,
20+
autoresizeProps,
21+
useLoading,
22+
loadingProps
23+
} from "./composables";
24+
import "./style.css";
25+
26+
type InitParameters = Parameters<typeof initChart>;
27+
type ThemeParameter = InitParameters[1];
28+
type InitOptsParameter = InitParameters[2];
29+
30+
export default defineComponent({
31+
name: "echarts",
32+
props: {
33+
options: Object as PropType<OptionType>,
34+
theme: {
35+
type: [Object, String] as PropType<ThemeParameter>
36+
},
37+
initOptions: Object as PropType<InitOptsParameter>,
38+
group: String,
39+
manualUpdate: Boolean,
40+
...autoresizeProps,
41+
...loadingProps
42+
},
43+
setup(props, { attrs }) {
44+
const defaultInitOptions = inject(
45+
"echartsInitOptions",
46+
{}
47+
) as InitOptsParameter;
48+
const root = ref<HTMLElement>();
49+
const chart = shallowRef<EChartsType>();
50+
const manualOptions = shallowRef<OptionType>();
51+
const realOptions = computed(
52+
() => manualOptions.value || props.options || Object.create(null)
53+
);
54+
55+
function init(options?: OptionType) {
56+
if (chart.value || !root.value) {
57+
return;
58+
}
59+
60+
const instance = (chart.value = initChart(
61+
root.value,
62+
props.theme,
63+
props.initOptions || defaultInitOptions
64+
));
65+
66+
if (props.group) {
67+
instance.group = props.group;
68+
}
69+
70+
Object.keys(attrs)
71+
.filter(key => key.indexOf(`on`) === 0)
72+
.forEach(key => {
73+
const handler = attrs[key] as any;
74+
75+
if (!handler) {
76+
return;
77+
}
78+
79+
if (key.indexOf("onZr:") === 0) {
80+
instance.getZr().on(key.slice(5).toLowerCase(), handler);
81+
} else {
82+
instance.on(key.slice(2).toLowerCase(), handler);
83+
}
84+
});
85+
86+
instance.setOption(options || realOptions.value, true);
87+
}
88+
89+
function mergeOptions(options: OptionType, ...rest: any[]) {
90+
if (props.manualUpdate) {
91+
manualOptions.value = options;
92+
}
93+
94+
if (!chart.value) {
95+
init(options);
96+
} else {
97+
chart.value.setOption(options, ...rest);
98+
}
99+
}
100+
101+
function cleanup() {
102+
if (chart.value) {
103+
chart.value.dispose();
104+
chart.value = undefined;
105+
}
106+
}
107+
108+
const {
109+
theme,
110+
initOptions,
111+
group,
112+
autoresize,
113+
manualUpdate,
114+
loading,
115+
loadingOptions
116+
} = toRefs(props);
117+
let unwatchOptions: (() => void) | null = null;
118+
watch(
119+
manualUpdate,
120+
manualUpdate => {
121+
if (typeof unwatchOptions === "function") {
122+
unwatchOptions();
123+
unwatchOptions = null;
124+
}
125+
126+
if (!manualUpdate) {
127+
unwatchOptions = watch(
128+
() => props.options,
129+
(val, oldVal) => {
130+
if (!val) {
131+
return;
132+
}
133+
if (!chart.value) {
134+
init();
135+
} else {
136+
// mutating `options` will lead to merging
137+
// replacing it with new reference will lead to not merging
138+
// eg.
139+
// `this.options = Object.assign({}, this.options, { ... })`
140+
// will trigger `this.chart.setOption(val, true)
141+
// `this.options.title.text = 'Trends'`
142+
// will trigger `this.chart.setOption(val, false)`
143+
chart.value.setOption(val, val !== oldVal);
144+
}
145+
},
146+
{ deep: true }
147+
);
148+
}
149+
},
150+
{
151+
immediate: true
152+
}
153+
);
154+
155+
watch([theme, initOptions], () => {
156+
cleanup();
157+
init();
158+
});
159+
160+
watch(
161+
() => group,
162+
group => {
163+
if (group && group.value && chart.value) {
164+
chart.value.group = group.value;
165+
}
166+
}
167+
);
168+
169+
const publicApi = usePublicAPI(chart, init);
170+
171+
useLoading(chart, loading, loadingOptions);
172+
173+
useAutoresize(chart, autoresize, root, realOptions);
174+
175+
onMounted(() => {
176+
if (props.options) {
177+
init();
178+
}
179+
});
180+
181+
onUnmounted(cleanup);
182+
183+
return {
184+
root,
185+
mergeOptions,
186+
...publicApi
187+
};
188+
},
189+
render() {
190+
return h("div", {
191+
ref: "root",
192+
class: "echarts"
193+
});
194+
}
195+
});

src/all.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import "echarts";
2+
3+
export { default } from "./ECharts";

src/assets/logo.png

-6.69 KB
Binary file not shown.

0 commit comments

Comments
 (0)