Handle CSP and CSS compatibility
main<style> injection, and you need to support browsers that do not support the CSSStyleSheet() constructor, you must manually import vue-echarts/style.css.repository·main·Indexed 27 days ago
https://github.com/ecomfe/vue-echartsA Vue.js component wrapper for Apache ECharts™ (version 8.1.0-beta.2) that provides a seamless way to integrate visualizations into Vue applications. It features a VChart component with support for on-demand importing, reactive event bindings, declarative graphics via the #graphic slot, and callback slots for tooltips and DataView. It supports configuration via Provide/Inject for themes and options, and provides a manual-update mode for optimized rendering.
<style> injection, and you need to support browsers that do not support the CSSStyleSheet() constructor, you must manually import vue-echarts/style.css.You can use vue-echarts via CDN by including the following <script> tags in your HTML. Access the component interface via window.VueECharts.
<script src="https://cdn.jsdelivr.net/npm/echarts@6.0.0"></script>
<script src="https://cdn.jsdelivr.net/npm/vue@3.5.27"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@8.1.0-beta.2"></script>
<script>
const app = Vue.createApp({ ... })
// Register component globally
app.component('VChart', VueECharts)
</script>Use v-on to bind ECharts events. Note that only the .once modifier is supported because other modifiers are tightly coupled to DOM event mechanisms.
To bind native DOM events, prefix the event name with native:.
Example:
<VChart @highlight="handleHighlight" @native:click="handleClick" />You can use Vue's provide API to configure theme, init-options, update-options, and loading-options for a component tree.
Example (Composition API):
import { THEME_KEY } from "vue-echarts";
import { provide, ref } from "vue";
const theme = ref("dark");
provide(THEME_KEY, theme);To reduce bundle size, manually import only the required ECharts components and charts. You can use the ECharts Code Generator to generate the necessary import statements by pasting your option object.
<template>
<VChart class="chart" :option="option" />
</template>
<script setup>
import { use } from "echarts/core";
import { CanvasRenderer } from "echarts/renderers";
import { PieChart } from "echarts/charts";
import { TitleComponent, TooltipComponent, LegendComponent } from "echarts/components";
import VChart, { THEME_KEY } from "vue-echarts";
import { ref, provide } from "vue";
// Register required components
use([CanvasRenderer, PieChart, TitleComponent, TooltipComponent, LegendComponent]);
// Optionally provide a theme
provide(THEME_KEY, "dark");
const option = ref({
// ... your ECharts option
});
</script>You can bind ECharts events using Vue's v-on directive. Supported events include highlight, downplay, legendselectchanged, datazoom, and various mouse events like click and mouseover.
Note on Native DOM events: To bind native DOM events, prefix the event name with native:, for example @native:click.
<template>
<VChart :option="option" @highlight="handleHighlight" @native:click="handleClick" />
</template>You can build option.graphic elements declaratively using the #graphic slot and components from vue-echarts/graphic.
Available Components:
GGroup, GRect, GCircle, GText, GLine, GPolyline, GPolygon, GImage, GSector, GRing, GArc, GBezierCurve, GCompoundPath.
Note: In manual-update mode, you must call chartRef.setOption(...) to apply changes made via the #graphic slot.
<script setup lang="ts">
import { ref } from "vue";
import { GGroup, GRect, GText } from "vue-echarts/graphic";
const option = { /* ... */ };
const overlay = ref({ x: 84, y: 22 });
function onDrag(event: any) {
overlay.value.x = event.offsetX - 44;
overlay.value.y = event.offsetY - 14;
}
</script>
<template>
<VChart :option="option">
<template #graphic>
<GGroup :x="overlay.x" :y="overlay.y">
<GRect :width="88" :height="28" draggable @drag="onDrag" />
<GText :x="10" :y="8" :text="`x: ${overlay.x}`" />
</GGroup>
</template>
</VChart>
</template>In vue-echarts v8+, event listeners provided via attributes are reactive by default. When the value of an attribute changes, the listener is automatically updated. This applies to the following patterns:
on<Event>: Standard ECharts events.onZr:<event>: ZRender-specific events.onNative:<event>: Native DOM events.Template syntax and runtime method signatures remain unchanged, so existing code using these patterns will continue to work but will now benefit from reactivity.
To minimize bundle size, manually import only the ECharts components and charts you need. You can use the import code generator by pasting your option code to get the exact import statements.
<template>
<VChart class="chart" :option="option" />
</template>
<script setup>
import { use } from "echarts/core";
import { CanvasRenderer } from "echarts/renderers";
import { PieChart } from "echarts/charts";
import { TitleComponent, TooltipComponent, LegendComponent } from "echarts/components";
import VChart, { THEME_KEY } from "vue-echarts";
import { ref, provide } from "vue";
use([CanvasRenderer, PieChart, TitleComponent, TooltipComponent, LegendComponent]);
provide(THEME_KEY, "dark");
const option = ref({
// ... your echarts option
});
</script>To use vue-echarts in your project, install both echarts and vue-echarts using npm.
npm install echarts vue-echartsThe #graphic slot allows you to build option.graphic elements declaratively using specialized components. You must import these from vue-echarts/graphic.
Available Components:
GGroup, GRect, GCircle, GText, GLine, GPolyline, GPolygon, GImage, GSector, GRing, GArc, GBezierCurve, GCompoundPath.
Note: In manual-update mode, changes to graphic elements must be committed via chartRef.setOption(...).
<script setup lang="ts">
import { ref } from "vue";
import { GGroup, GRect, GText } from "vue-echarts/graphic";
const option = { /* ... */ };
const overlay = ref({ x: 84, y: 22 });
function onDrag(event: any) {
overlay.value.x = event.offsetX - 44;
overlay.value.y = event.offsetY - 14;
}
</script>
<template>
<VChart :option="option">
<template #graphic>
<GGroup :x="overlay.x" :y="overlay.y">
<GRect :width="88" :height="28" :r="6" fill="#5470c6" draggable @drag="onDrag" />
<GText :x="10" :y="8" :text="`x: ${Math.round(overlay.x)}`" text-fill="#fff" />
</GGroup>
</template>
</VChart>
</template>When upgrading to vue-echarts@8, be aware of the following breaking changes:
vue-echarts@7 if you require Vue 2 compatibility.class support is now required. For legacy browsers, you must transpile the code to ES5 manually.vue-echarts/csp entry point has been removed. Use the standard vue-echarts entry point. Manual inclusion of vue-echarts/style.css is only necessary if you have a strict CSP preventing inline styles AND target browsers lacking CSSStyleSheet() support.Note: You should also consult the Apache ECharts 6 upgrade guide for ECharts-specific breaking changes.