vue项目中echarts图表的应用(总结)

一 . 安装echarts包
复制
二 . 放置两个图表的div,并给定高宽
| <div class="chart"> |
| <!-- 图表 --> |
| <div ref="social" style=" width: 100%; height:100% " /> |
| </div> |
| |
| |
| <div class="chart"> |
| |
| <div ref="provident" style=" width: 100%; height:100% " /> |
| </div> |
复制
三 . 在mounted中初始化图表
watch直接写入图表
| <script> |
| import CountTo from 'vue-count-to' |
| import { mapGetters } from 'vuex' |
| import { getHomeData, getMessageList } from '@/api/home' |
| import * as echarts from 'echarts' |
| export default { |
| components: { |
| CountTo |
| }, |
| data() { |
| return { |
| homeData: { |
| 'socialInsurance': {}, |
| 'providentFund': {} |
| }, |
| list: [] |
| } |
| }, |
| |
| computed: { |
| ...mapGetters(['name', 'avatar', 'company', 'departmentName']) |
| }, |
| watch: { |
| homeData() { |
| console.log(this.homeData) |
| |
| this.social.setOption({ |
| xAxis: { |
| type: 'category', |
| boundaryGap: false, |
| data: this.homeData.socialInsurance?.xAxis |
| }, |
| yAxis: { |
| type: 'value' |
| }, |
| series: [ |
| { |
| data: this.homeData.socialInsurance?.yAxis, |
| type: 'line', |
| areaStyle: { |
| color: '#04c9be' |
| }, |
| lineStyle: { |
| color: '#04c9be' |
| } |
| } |
| ] |
| }) |
| this.provident.setOption({ |
| xAxis: { |
| type: 'category', |
| boundaryGap: false, |
| data: this.homeData.providentFund?.xAxis |
| }, |
| yAxis: { |
| type: 'value' |
| }, |
| series: [ |
| { |
| data: this.homeData.providentFund?.yAxis, |
| type: 'line', |
| areaStyle: { |
| color: '#04c9be' |
| }, |
| lineStyle: { |
| color: '#04c9be' |
| } |
| } |
| ] |
| }) |
| } |
| }, |
| created() { |
| this.getHomeData() |
| this.getMessageList() |
| }, |
| mounted() { |
| |
| |
| this.social = echarts.init(this.$refs.social) |
| |
| this.provident = echarts.init(this.$refs.provident) |
| }, |
| methods: { |
| async getHomeData() { |
| this.homeData = await getHomeData() |
| }, |
| async getMessageList() { |
| this.list = await getMessageList() |
| } |
| } |
| } |
| </script> |
复制
第二种方法
| watch: { |
| homeData(newVal, oldVal) { |
| |
| this.drawSocial() |
| this.drawProvident() |
| } |
| }, |
| computed: { |
| ...mapGetters(['avatar', 'name', 'departmentName', 'company']) |
| }, |
| created() { |
| this.loadHomeData() |
| this.loadMessageList() |
| }, |
| mounted() { |
| this.$fn() |
| |
| this.myChartSocial = echarts.init(this.$refs.social) |
| this.myChartProvident = echarts.init(this.$refs.provident) |
| |
| |
| }, |
| methods: { |
| drawProvident() { |
| |
| |
| const option = { |
| title: { |
| text: this.homeData.providentFund.category |
| }, |
| tooltip: {}, |
| legend: { |
| data: ['公积金申报数据'] |
| }, |
| xAxis: { |
| data: this.homeData.providentFund.xAxis |
| }, |
| yAxis: {}, |
| series: [ |
| { |
| name: '金额', |
| type: 'line', |
| data: this.homeData.providentFund.yAxis |
| } |
| ] |
| } |
| |
| this.myChartProvident.setOption(option) |
| }, |
| drawSocial() { |
| |
| |
| const option = { |
| title: { |
| text: 'ECharts 入门示例' |
| }, |
| tooltip: {}, |
| legend: { |
| data: ['销量'] |
| }, |
| xAxis: { |
| data: this.homeData.socialInsurance.xAxis |
| }, |
| yAxis: {}, |
| series: [ |
| { |
| name: '销量', |
| type: 'bar', |
| data: this.homeData.socialInsurance.yAxis |
| } |
| ] |
| } |
| |
| this.myChartSocial.setOption(option) |
| }, |
| async loadMessageList() { |
| const res = await getMessageList() |
| console.log(res) |
| this.messageList = res.data |
| }, |
| async loadHomeData() { |
| const res = await getHomeData() |
| |
| this.homeData = res.data |
| |
| |
| } |
| } |
复制
echarts图表的按需导入(可替换上边的全部导入)

| |
| import * as echarts from 'echarts/core' |
| import { CanvasRenderer } from 'echarts/renderers' |
| |
| import { LineChart } from 'echarts/charts' |
| import { BarChart } from 'echarts/charts' |
| |
| import { TitleComponent } from 'echarts/components' |
| import { TooltipComponent } from 'echarts/components' |
| import { LegendComponent } from 'echarts/components' |
| import { GridComponent } from 'echarts/components' |
| |
| echarts.use([ |
| CanvasRenderer, |
| LineChart, |
| TitleComponent, |
| TooltipComponent, |
| LegendComponent, |
| GridComponent, |
| BarChart |
| ]) |
复制
LegendComponent } from ‘echarts/components’
import { GridComponent } from ‘echarts/components’
echarts.use([
CanvasRenderer,
LineChart,
TitleComponent,
TooltipComponent,
LegendComponent,
GridComponent,
BarChart
])
复制