![cover](https://img-blog.csdnimg.cn/2ffd3d286b4c4abb87c3b71e779e8421.png)
vue3使用echarts
vue3使用echarts
·
本地安装Echarts
npm install echarts --save
有淘宝镜像的可以选择 (安装速度快)
cnpm install echarts --save
新建一个echarts.js文件
// 引入 echarts 核心模块,核心模块提供了 echarts 使用必须要的接口。
import * as echarts from "echarts/core";
/** 引入柱状图and折线图图表,图表后缀都为 Chart */
import { BarChart, LineChart } from "echarts/charts";
// 引入提示框,标题,直角坐标系,数据集,内置数据转换器组件,组件后缀都为 Component
import {
TitleComponent,
TooltipComponent,
GridComponent,
DatasetComponent,
TransformComponent,
} from "echarts/components";
// 标签自动布局,全局过渡动画等特性
import { LabelLayout, UniversalTransition } from "echarts/features";
// 引入 Canvas 渲染器,注意引入 CanvasRenderer 或者 SVGRenderer 是必须的一步
import { CanvasRenderer } from "echarts/renderers";
// 注册必须的组件
echarts.use([
TitleComponent,
TooltipComponent,
GridComponent,
DatasetComponent,
TransformComponent,
BarChart,
LabelLayout,
UniversalTransition,
CanvasRenderer,
LineChart,
]);
// 导出
export default echarts;
创建好的js文件全局引入到main.js内
import { createApp } from 'vue'
import App from './App.vue'
import router from "./router/index"
import echarts from './utils/echarts';
const app = createApp(App)
app.config.globalProperties.$echarts = echarts
app.use(router)
app.use(pinia)
app.mount('#app')
这里需要注意:(组件名大写首字母)
按需引入时:import { LineChart } from “echarts/charts”
<script setup>逻辑内使用echart
因为setup中没有this,而且这时候还没有渲染.所以,在setup中,也可以使用provide/inject把echarts引入进来. 在根组件里引入echarts
// App.vue文件内插入生产者provide
<script setup>
import * as echarts from "echarts";
import { provide } from "vue";
provide("echarts", echarts);
</script>
<template>
<router-view></router-view>
</template>
<style>
body {
margin: 0px;
}
</style>
在需要的页面中inject
<template>
<div>
<div id="main"></div>
<div id="maychar"></div>
</div>
</template>
<script lang="js">
import { defineComponent, onMounted, inject } from "vue"; // 主要
export default defineComponent({
setup() {
onMounted(() => {
change();
changetype();
});
let echarts = inject("echarts"); // 主要
// 基本柱形图
const change = () => {
const chartBox = echarts.init(document.getElementById("main")); // 主要
const option = {
xAxis: {
data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
},
yAxis: {},
series: [
{
type: "bar",
data: [23, 24, 18, 25, 27, 28, 25],
},
],
};
chartBox.setOption(option);
// 根据页面大小自动响应图表大小
window.addEventListener("resize", function () {
chartBox.resize();
});
};
// 折线图
const changetype = () => {
// 获取组件实例
const machart = echarts.init(document.getElementById("maychar"));
// 设置配置项
const option = {
xAxis: {
data: ["A", "B", "C", "D", "E"],
},
yAxis: {},
series: [
{
data: [10, 22, 28, 43, 49],
type: "line",
stack: "x",
},
{
data: [5, 4, 3, 5, 10],
type: "line",
stack: "x",
},
],
};
// 复制
machart.setOption(option);
// 根据页面大小自动响应图表大小
window.addEventListener("resize", function () {
machart.resize();
});
};
return {
changetype,
};
},
});
</script>
<style lang="scss" scoped>
#main {
min-width: 31.25rem;
min-height: 31.25rem;
// max-height: 500px;
}
#maychar {
max-height: 500px;
// max-height: 400px;
height: 500px;
}
</style>
达到统一管理引入的echarts效果
更多推荐
所有评论(0)