- 纯Vue项目如何引入Echarts
- 安装 (不解释)
- 全局引入,
main.js 文件中引入并挂接,不是在.vue文件里
import * as echarts from 'echarts'
Vue.prototype.$echarts = echarts
- 饼状图模板(抽取成了外部独立模块,含toolbox自定义下载组件)+ 调用方式
<template>
<div ref="chart"></div>
</template>
<script>
import {statistic} from '@/api/mobileActiveStatistic'
export default {
name: 'pieChart',
components: {},
props: {
sup_this: {
type: Object,
default: null
}
},
data() {
return {
datas: [],
globaleFuncNames: []
}
},
methods: {
init() {
this.sup_this.myChart = this.$echarts.init(this.$refs.chart);
this.sup_this.myChart.resize({
width: 1000,
height: 400
});
this.sup_this.myChart.setOption({
title: {
text: '主标题',
subtext: '副标题',
x: 'center',
},
toolbox: {
show: true,
feature: {
saveAsImage: {
icon: 'path://M 384 448 V 110 h 230 v 310 h 180 l -300 324 l -324 -324 h 180 Z m -170 450 h 600 v 60 H 140 v -60 Z',
iconStyle: {
color: '#000'
},
}
}
},
tooltip: {
trigger: 'item',
formatter: "{a} <br/>{b} : {c} ({d}%)",
},
legend: {
type: 'scroll',
orient: 'vertical',
right: 10,
top: 50,
bottom: 20,
data: this.globaleFuncNames
},
series: [
{
name: '数据来源',
type: 'pie',
radius: '55%',
center: ['50%', '60%'],
data: this.datas,
itemStyle: {
emphasis: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
}
}
]
});
},
async pieChartDataHandleAndInit() {
this.init();
},
}
}
</script>
<template>
<el-divider content-position="left">统计结果展示</el-divider>
<pie-chart ref="pieChart" :sup_this="sup_this"></pie-chart>
<div slot="footer" class="dialog-footer">
<el-button :loading="loading" type="primary" @click="onSubmit">查询</el-button>
</div>
</template>
<script>
import pieChart from './pieChart'
export default {
components: { PieChart},
data() {
return {
sup_this:this
}
},
methods: {
onSubmit() {
this.initPieChart();
},
initPieChart() {
const _this = this.$refs.pieChart;
_this.pieChartDataHandleAndInit();
}
}
}
</script>
结合业务逻辑,实现效果下图这样:
- 折线图模板(抽取成了外部独立模块,含toolbox自定义下载组件)+ 调用方式
<template>
<div ref="chart"></div>
</template>
<script>
import {statistic} from '@/api/mobileActiveStatistic'
export default {
name: 'lineChart',
components: {},
props: {
sup_this: {
type: Object,
default: null
}
},
data() {
return {
labels: [],
series: []
}
},
mounted() {
},
methods: {
init() {
this.sup_this.myChart = this.$echarts.init(this.$refs.chart);
this.sup_this.myChart.resize({
width: 1000,
height: 400
});
this.sup_this.myChart.setOption({
tooltip: {
trigger: "axis",
},
toolbox: {
show: true,
feature: {
saveAsImage: {
icon: 'path://M 384 448 V 110 h 230 v 310 h 180 l -300 324 l -324 -324 h 180 Z m -170 450 h 600 v 60 H 140 v -60 Z',
iconStyle: {
color: '#000'
},
}
}
},
legend: {
type: 'scroll',
width: 500,
data: this.labels
},
grid: {
left: "3%",
right: "4%",
bottom: "3%",
containLabel: true,
},
xAxis: {
type: "category",
boundaryGap: false,
data: this.sup_this.form.years,
},
yAxis: {},
series: this.series,
});
},
async lineChartDataHandleAndInit() {
this.init()
},
}
}
</script>
<template>
<el-divider content-position="left">统计结果展示</el-divider>
<line-chart ref="lineChart" :sup_this="sup_this"></line-chart>
<div slot="footer" class="dialog-footer">
<el-button :loading="loading" type="primary" @click="onSubmit">查询</el-button>
</div>
</template>
<script>
import lineChart from "./lineChart";
export default {
components: { lineChart },
data() {
return {
sup_this:this
}
},
methods: {
onSubmit() {
this.initLineChart();
},
initLineChart() {
const _this = this.$refs.lineChart;
_this.lineChartDataHandleAndInit();
}
}
}
</script>
结合业务逻辑,实现效果下图这样:
|