🍓🍓开发后台管理系统时,经常使用ECharts图表渲染首页信息,在首页使用多个图表时,选择封装ECharts组件复用的方式可以减少代码量,增加开发效率🍓🍓
🍅效果需求:

- 使用组件传递ECharts中的
option
属性
- 手动/自动设置chart尺寸
- chart自适应宽高
- 动态展示获取到的后端数据
🍑代码实现( 静态图表🍪•ᴥ•🍪 ):
- 首先我们需要导入echarts:
npm i echarts
- 选择在
main.js
中配置ECharts
全局变量声明或仅组件内使用
import * as echarts from 'echarts';
Vue.prototype.$echarts = echarts
import * as echarts from 'echarts';
- 创建
static_options.js
作为静态options配置项,配置项内对象即echarts
实例中的源码,🔴链接在此🔴
export const barOption = {
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow'
}
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
xAxis: [
{
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
axisTick: {
alignWithLabel: true
}
}
],
yAxis: [
{
type: 'value'
}
],
series: [
{
name: 'Direct',
type: 'bar',
barWidth: '60%',
data: [10, 52, 200, 334, 390, 330, 220]
}
]
}
<template>
<div