Vue3 + echarts 5.4.2 实现图表效果

简介: 从vue2升级到vue3,对原来的柱状图、折线图组件进行了修改,这两个组件的语法保留了vue2,实际使用也没有问题

从vue2升级到vue3,对原来的柱状图、折线图组件进行了修改,这两个组件的语法保留了vue2,实际使用也没有问题。

1、通用柱状图 BarChart.vue

<template>
  <div :class="className" :style="{height: height, width: width}"/>
</template>

<script>
    import * as echarts from 'echarts';
    import 'echarts/theme/macarons.js'

    export default {
   
   
        props: {
   
   
            className: {
   
   
                type: String,
                default: 'chart'
            },
            width: {
   
   
                type: String,
                default: '100%'
            },
            height: {
   
   
                type: String,
                default: '450px'
            },
            title: {
   
   
                type: String,
                required: true
            },
            chartData: {
   
   
                type: Object,
                required: true
            }
        },
        data() {
   
   
            // vue3需要这样写,否则`tooltip`无效
            chart: null
            return {
   
   
                // chart: null
            }
        },
        watch: {
   
   
            chartData: {
   
   
                deep: true,
                handler(val) {
   
   
                    this.setOptions(val)
                }
            }
        },
        mounted() {
   
   
            this.$nextTick(() => {
   
   
                this.initChart()
            })
        },
        beforeDestroy() {
   
   
            if (!this.chart) {
   
   
                return
            }
            this.chart.dispose()
            this.chart = null
        },
        methods: {
   
   
            initChart() {
   
   
                this.chart = echarts.init(this.$el, 'macarons')
                this.chart.setOption({
   
   
                    title: {
   
   
                        text: this.title,
                        textStyle: {
   
   
                            fontSize: 15
                        }
                    },
                    toolbox: {
   
   
                        show: true,
                        orient: 'horizontal',
                        left: 'right',
                        top: 'top',
                        feature: {
   
   
                            mark: {
   
    show: true },
                            magicType: {
   
    show: true, type: ['line', 'bar'] },
                        }
                    },
                    tooltip: {
   
   
                        trigger: 'axis',
                        axisPointer: {
   
   
                            type: 'shadow'
                        }
                    },
                    grid: {
   
   
                        top: '10%',
                        left: '2%',
                        right: '2%',
                        bottom: '4%',
                        containLabel: true
                    },
                    xAxis: [{
   
   
                        type: 'category',
                        axisTick: {
   
    show: false },
                    }],
                    yAxis: [{
   
   
                        type: 'value',
                    }],
                    series: []
                })
            },
            setOptions(data) {
   
   
                let map = data.map
                let series = []
                map.forEach((val, key) => {
   
   
                    let obj = {
   
   
                        name: key,
                        data: Array.from(val),
                        type: 'bar',
                        barWidth: '10%',
                        barGap: 0,
                        emphasis: {
   
   
                            focus: 'series'
                        },
                        animationDuration: 2800,
                        animationEasing: 'quadraticOut'
                    }
                    series.push(obj)
                });

                this.chart.setOption({
   
   
                    xAxis: [{
   
   
                        data: data.date,
                    }],
                    legend: {
   
   
                        data: Array.from(map.keys())
                    },
                    series: series
                })
            }
        }
    }
</script>
AI 代码解读

2、通用的折线图组件 LineChart.vue

<template>
  <div :class="className" :style="{height: height, width: width}"/>
</template>

<script>
    import * as echarts from 'echarts';
    import 'echarts/theme/macarons.js'

    export default {
   
   
        props: {
   
   
            className: {
   
   
                type: String,
                default: 'chart'
            },
            width: {
   
   
                type: String,
                default: '100%'
            },
            height: {
   
   
                type: String,
                default: '450px'
            },
            title: {
   
   
                type: String,
                required: true
            },
            chartData: {
   
   
                type: Object,
                required: true
            }
        },
        data() {
   
   
            // vue3需要这样写,否则`tooltip`无效
            chart: null
            return {
   
   
                // chart: null
            }
        },
        watch: {
   
   
            chartData: {
   
   
                deep: true,
                handler(val) {
   
   
                    this.setOptions(val)
                }
            }
        },
        mounted() {
   
   
            this.$nextTick(() => {
   
   
                this.initChart()
            })
        },
        beforeDestroy() {
   
   
            if (!this.chart) {
   
   
                return
            }
            this.chart.dispose()
            this.chart = null
        },
        methods: {
   
   
            initChart() {
   
   
                this.chart = echarts.init(this.$el, 'macarons')
                this.chart.setOption({
   
   
                    title: {
   
   
                        text: this.title,
                        textStyle: {
   
   
                            fontSize: 15
                        }
                    },
                    xAxis: {
   
   
                        type: 'category',
                        boundaryGap: false,
                    },
                    grid: {
   
   
                        left: 15,
                        right: 10,
                        bottom: 20,
                        top: '10%',
                        containLabel: true
                    },
                    toolbox: {
   
   
                        show: true,
                        orient: 'horizontal',
                        left: 'right',
                        top: 'top',
                        feature: {
   
   
                            mark: {
   
    show: true },
                            magicType: {
   
    show: true, type: ['line', 'bar'] },
                        }
                    },
                    tooltip: {
   
   
                        trigger: 'axis',
                        axisPointer: {
   
   
                            type: 'shadow'
                        },
                    },
                    yAxis: {
   
   
                        type: 'value',
                        axisTick: {
   
   
                            show: false
                        }
                    },
                    series: []
                })
            },
            setOptions(data) {
   
   
                let map = data.map
                let series = []
                map.forEach((val, key) => {
   
   
                    let obj = {
   
   
                        name: key,
                        data: Array.from(val),
                        type: 'line',
                        barWidth: '40',
                        smooth: true,
                        itemStyle: {
   
   
                            normal: {
   
   
                                lineStyle: {
   
   
                                    width: 2
                                }
                            }
                        },
                        animationDuration: 2800,
                        animationEasing: 'quadraticOut'
                    }
                    series.push(obj)
                });

                this.chart.setOption({
   
   
                    xAxis: {
   
   
                        data: data.date,
                    },
                    series: series
                })
            }
        }
    }
</script>
AI 代码解读

3、通用饼图组件 PieChart.vue

<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
    import * as echarts from 'echarts';
    import 'echarts/theme/macarons.js'

    const color = [
        "#37a2da",
        "#32c5e9",
        "#9fe6b8",
        "#ffdb5c",
        "#ff9f7f",
        "#fb7293",
        "#e7bcf3",
        "#8378ea"
    ];
    export default {
   
   
        props: {
   
   
            className: {
   
   
                type: String,
                default: 'chart'
            },
            width: {
   
   
                type: String,
                default: '100%'
            },
            height: {
   
   
                type: String,
                default: '350px'
            },
            title: {
   
   
                type: String,
                required: true
            },
            chartData: {
   
   
                type: Object,
                required: true
            }
        },
        data() {
   
   
            return {
   
   
                chart: null
            }
        },
        watch: {
   
   
            chartData: {
   
   
                deep: true,
                handler(val) {
   
   
                    this.setOptions(val)
                }
            }
        },
        mounted() {
   
   
            this.$nextTick(() => {
   
   
                this.initChart()
            })
        },
        beforeDestroy() {
   
   
            if (!this.chart) {
   
   
                return
            }
            this.chart.dispose()
            this.chart = null
        },
        methods: {
   
   
            initChart() {
   
   
                this.chart = echarts.init(this.$el, 'macarons')
                this.chart.setOption({
   
   
                    title: {
   
   
                        textStyle: {
   
   
                            fontSize: 15
                        }
                    },
                    tooltip: {
   
   
                        trigger: 'item',
                        formatter: '{b} : {c} ({d}%)'
                    },
                    itemStyle: {
   
   
                        normal:{
   
   
                            borderRadius: 10,
                            borderColor: '#fff',
                            borderWidth: 2
                        },
                    },
                    label: {
   
   
                        color: '#32c5e9'
                    },
                    labelLine: {
   
   
                        lineStyle: {
   
   
                            width: 2
                        }
                    },
                    grid: {
   
   
                        top: '10%',
                        left: '2%',
                        right: '2%',
                        bottom: '4%',
                        containLabel: true
                    },
                    series: [{
   
   
                        type: "pie",
                        radius: [50, 110],
                        center: ['50%', '50%'],
                        top: 10,
                        itemStyle: {
   
   
                            normal:{
   
   
                                color: item => {
   
   
                                    return color[item.dataIndex%(color.length)]
                                },
                                borderRadius: 10,
                                borderColor: '#fff',
                                borderWidth: 2
                            },
                        },
                        label: {
   
   
                            color: '#32c5e9'
                        },
                        labelLine: {
   
   
                            lineStyle: {
   
   
                                width: 2
                            }
                        },
                        emphasis: {
   
   
                            label: {
   
   
                                show: true,
                                fontSize: '20',
                                fontWeight: 'bold',
                            }
                        },
                        animationEasing: 'cubicInOut',
                        animationDuration: 2600
                    }]
                })
            },
            setOptions(data) {
   
   
                this.chart.setOption({
   
   
                    title: {
   
   
                        text: this.title,
                    },
                    series: [{
   
   
                        data: data,
                    }]
                })
            }
        }
    }
</script>
AI 代码解读

4、调用示例

<template>
  <div class="dashboard-editor-container">
    <el-row :gutter="32">
      <el-col :xs="24" :sm="24" :lg="24">
        <div class="chart-wrapper">            
          <el-radio-group v-model="period" @change="getData" style="margin: 0 0 20px 0">
            <el-radio-button label="30">近30天</el-radio-button>
            <el-radio-button label="180">近180天</el-radio-button>
            <el-radio-button label="365">近365天</el-radio-button>
          </el-radio-group>

          <line-chart :chart-data="lineChartData" :title="`每日任务接收情况统计`"/>
        </div>
      </el-col>
    </el-row>

    <el-row :gutter="32">
      <el-col :xs="24" :sm="24" :lg="24">
        <div class="chart-wrapper">
          <bar-chart :chart-data="monthlyData" :title="`近半年统计月报`"/>
        </div>
      </el-col>
    </el-row>

    <el-row :gutter="32">
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <pie-chart :chart-data="diagnosePieChartData" :title="`诊断任务统计`"/>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script setup name="Index">
    import LineChart from './LineChart'
    import BarChart from './BarChart'
    import PieChart from './PieChart'

    import {
   
   
        countByMonthly,
        countByReceiptTime,
        countByTypeAndDept
    } from '@/api/xxx'

    const {
   
   proxy} = getCurrentInstance();
    const period = ref(180)
    const monthlyData = ref({
   
   });
    const lineChartData = ref({
   
   });
    const diagnosePieChartData = ref({
   
   });

    function getData() {
   
   
        lineChartData.value = {
   
   }
        countByReceiptTime(period.value).then(res => {
   
   
            let arr = res.data
            let date = arr.map(item => item.time);
            let data = arr.map(item => item.total);

            let map = new Map();
            map.set('每日接收任务', data);

            lineChartData.value = {
   
   
                date,
                map
            };
        })
    }

    function monthly() {
   
   
        monthlyData.value = {
   
   };
        countByMonthly().then(res => {
   
   
            let arr = res.data
            let date = arr.map(item => item.item);
            let data1 = arr.map(item => item["xx"]);
            let data2 = arr.map(item => item["xx"]);
            let data3 = arr.map(item => item["xx"]);
            let data4 = arr.map(item => item["xx"]);
            let data5 = arr.map(item => item["xx"]);

            let map = new Map();
            map.set('任务总数', data1);
            map.set('未开始', data2);
            map.set('进行中', data3);
            map.set('已完成', data4);
            map.set('已发布', data5);

            monthlyData.value = {
   
   
                date,
                map
            };
        })
    }

    function typeAndDept() {
   
   
        diagnosePieChartData.value = {
   
   }
        countByTypeAndDept().then(res => {
   
   
            let arr = res.data;
            diagnosePieChartData.value = arr.filter(item => item.type === 'diagnose').map(item => {
   
   
                return {
   
   "name": item.dept, "value": item.total}
            });
        })
    }

    getData();
    monthly();
    typeAndDept();
</script>

<style lang="scss" scoped>
  .dashboard-editor-container {
   
   
    padding: 32px;
    background-color: rgb(240, 242, 245);
    position: relative;

    .chart-wrapper {
   
   
      background: #fff;
      padding: 16px 16px 0;
      margin-bottom: 32px;
    }
  }

  @media (max-width: 1024px) {
   
   
    .chart-wrapper {
   
   
      padding: 8px;
    }
  }
</style>
AI 代码解读

5、实现效果:

  • 折线图
    image.png
  • 柱状图
    image.png

  • 饼图
    image.png

目录
打赏
0
0
0
2
108
分享
相关文章
|
3月前
一文带你封装Vue3 Echarts
一文带你封装Vue3 Echarts
197 7
一文带你封装Vue3 Echarts
微信小程序图表制作利器:ECharts组件的使用与技巧
微信小程序图表制作利器:ECharts组件的使用与技巧
170 1
echarts柱图前后端代码SpringCloud+Vue3
echarts柱图前后端代码SpringCloud+Vue3
77 1
Vue3使用echarts仪表盘(gauge)
Apache ECharts 是一款强大的数据可视化库,其仪表盘图表(`gauge`)可用于展示度量数据。主要属性包括仪表盘数据源 `gaugeData`(必填)、容器宽度 `width`(默认 100%)及高度 `height`(默认 100%)。数据项需指定名称 `name` 和值 `value`,并支持额外属性。
478 2
Vue3使用echarts仪表盘(gauge)
Vue3使用echarts树图(tree)
本文介绍了如何在 Vue2 项目中使用 Echarts 实现树图(tree)功能。通过按需引入 Echarts 组件和相关依赖,文章详细展示了如何创建一个可自定义配置的树图组件 `TreeChart.vue`,包括树图数据源、容器尺寸、主题色等属性。此外,还提供了在线预览效果及代码示例,帮助读者快速实现树图功能。适用于需要展示层次结构数据的场景。
391 1
Vue3使用echarts树图(tree)
微信小程序使用echarts图表(ec-canvas)
这篇文章介绍了在微信小程序中使用`ec-canvas`集成echarts图表的方法,包括解决加载时报错的问题、配置图表组件、以及在小程序页面中引入和使用这些图表组件的步骤。
622 1
微信小程序使用echarts图表(ec-canvas)
Echart的使用初体验,Echarts的基本使用及语法格式,简单图表绘制和使用及图例添加【学习笔记】
本文介绍了ECharts的基本使用和语法格式,包括如何引入ECharts、创建容器、初始化echarts实例对象、配置option参数和一些基础图表的绘制方法。文章还提供了简单图表绘制和使用图例添加的示例代码,以及对ECharts特性和优势的概述。
Echart的使用初体验,Echarts的基本使用及语法格式,简单图表绘制和使用及图例添加【学习笔记】
|
5月前
|
vue中使用echarts绘制双Y轴图表时,刻度没有对齐的两种解决方法
vue中使用echarts绘制双Y轴图表时,刻度没有对齐的两种解决方法
923 0
在vue页面引入echarts,图表的数据来自数据库 springboot+mybatis+vue+elementui+echarts实现图表的制作
这篇文章介绍了如何在Vue页面中结合SpringBoot、MyBatis、ElementUI和ECharts,实现从数据库获取数据并展示为图表的过程,包括前端和后端的代码实现以及遇到的问题和解决方法。
在vue页面引入echarts,图表的数据来自数据库 springboot+mybatis+vue+elementui+echarts实现图表的制作
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等