折线图组件封装及数据处理

24 篇文章 1 订阅
该博客介绍了如何使用ECharts库封装一个折线图组件,包括设置图表样式、配置项如颜色、图例、提示框、坐标轴等,并处理数据源以适应不同数量的数据系列。同时,提供了处理数据的方法`processData`,用于计算总和并填充缺失值。文章还展示了组件的实际显示效果。
摘要由CSDN通过智能技术生成
​
//折线图组件封装及数据处理
<template>
    <div class="pancelDiv" :id="id" :style="{ height: height, width: width }"></div>
</template>
<script>
import * as echarts from 'echarts'

export default {
    props: {
        id: {
            type: String,
            default: 'barGraph',
        },
        width: {
            type: String,
            default: '200px',
        },
        height: {
            type: String,
            default: '140px',
        },
        options: {
            type: Array,
            default: () => {},
        },
    },
    data() {
        return {
            defaultOptions: {
                color: ['#0C51A1', '#4182E5', '#3D47FF', '#21FFC7'],
                legend: {
                    textStyle: {
                        fontSize: 12,
                        color: '#fff',
                        width: '30',
                        height: '30',
                    },
                    selector: false,
                    selected: {
                        // 城管: false,
                        // 调处: false,
                    },
                    itemGap: 22,
                    itemWidth: 12,
                    itemHeight: 12,
                    icon: 'rect', //形状  类型包括 circle,rect,line,roundRect,triangle,diamond,pin,arrow,none,
                },
                tooltip: {
                    trigger: 'axis',
                    showContent: true,
                    // confine: true, //将悬浮框限制在图表区域内
                    appendToBody: true, //将 tooltip 的 DOM 节点添加为 HTML 的 <body> 的子节点
                },
                dataset: {
                    source: [
                        ['month', '1月', '2月', '3月', '4月', '5月', '6月'],
                        ['安监', 56.5, 82.1, 88.7, 70.1, 53.4, 85.1],
                        ['查违', 51.1, 51.4, 55.1, 53.3, 73.8, 68.7],
                        ['城管', 40.1, 62.2, 69.5, 36.4, 45.2, 32.5],
                        // ['调处', 25.2, 37.1, 41.2, 18, 33.9, 49.1],
                    ],
                },
                xAxis: {
                    type: 'category',
                    axisLine: {
                        //坐标线
                        lineStyle: {
                            type: 'solid',
                            color: '#d8d8d8', //轴线的颜色
                            width: '1', //坐标线的宽度
                        },
                    },
                    axisLabel: {
                        textStyle: {
                            color: '#878787', //坐标值的具体的颜色
                        },
                        interval: 1,
                    },
                    boundaryGap: false,
                    nameGap: 15,
                    // data:[ '1月', '2月', '3月', '4月', '5月', '6月']
                },
                yAxis: {
                    type: 'value',
                    gridIndex: 0,
                    axisLabel: {
                        textStyle: {
                            color: '#878787', //坐标值具体的颜色
                        },
                    },
                    axisLine: {
                        //线
                        show: false,
                    },
                    axisTick: {
                        //刻度
                        show: false,
                    },
                    // minInterval: 15,//标值的最小间隔
                    splitLine: {
                        //坐标轴风格
                        show: true,
                        lineStyle: {
                            type: 'dashed',
                            color: '#5B6A87',
                        },
                    },
                },
                grid: {
                    top: 50,
                    right: 5,
                    bottom: 5,
                    borderWidth: 0,
                    containLabel: true,
                },
                series: [
                    {
                        type: 'line',
                        smooth: true,
                        seriesLayoutBy: 'row',
                        emphasis: { focus: 'series' },
                        areaStyle: { opacity: 0.3 },
                    },
                    {
                        type: 'line',
                        smooth: true,
                        seriesLayoutBy: 'row',
                        emphasis: { focus: 'series' },
                        areaStyle: { opacity: 0.3 },
                    },
                    {
                        type: 'line',
                        smooth: true,
                        seriesLayoutBy: 'row',
                        emphasis: { focus: 'series' },
                        areaStyle: { opacity: 0.3 },
                    },
                    {
                        type: 'line',
                        smooth: true,
                        seriesLayoutBy: 'row',
                        emphasis: { focus: 'series' },
                        areaStyle: { opacity: 0.3 },
                    },
                ],
            },
            // dataSeq: ['安监', '查违', '城管', '调处', '公安', '人力资源', '其他','其他类','市监','文化广电旅游体育','消防安全','生态环境与水务','三小场所','交通','非警务警情'],
        }
    },
    watch: {
        options: {
            immediate: true,
            handler: function(newValue, oldValue) {
                this.$nextTick(() => {
                    if (newValue && newValue.length > 0) {
                        this.defaultOptions.dataset.source = this.processData(newValue)
                        // 解决分类低于4个时数据显示bug start
                        let sourceLength = Object.keys(this.defaultOptions.dataset.source).length
                        if (sourceLength < 5) {
                            this.defaultOptions.series = this.defaultOptions.series.splice(0, sourceLength - 1)
                        } else {
                            this.defaultOptions.series = [
                                {
                                    type: 'line',
                                    smooth: true,
                                    seriesLayoutBy: 'row',
                                    emphasis: { focus: 'series' },
                                    areaStyle: { opacity: 0.3 },
                                },
                                {
                                    type: 'line',
                                    smooth: true,
                                    seriesLayoutBy: 'row',
                                    emphasis: { focus: 'series' },
                                    areaStyle: { opacity: 0.3 },
                                },
                                {
                                    type: 'line',
                                    smooth: true,
                                    seriesLayoutBy: 'row',
                                    emphasis: { focus: 'series' },
                                    areaStyle: { opacity: 0.3 },
                                },
                                {
                                    type: 'line',
                                    smooth: true,
                                    seriesLayoutBy: 'row',
                                    emphasis: { focus: 'series' },
                                    areaStyle: { opacity: 0.3 },
                                },
                            ]
                        }
                        // end
                        echarts.init(document.getElementById(this.id)).setOption(this.defaultOptions, true)
                    } else {
                        this.defaultOptions.dataset.source = []
                        this.$emit('countTotal', 0)
                        echarts.init(document.getElementById(this.id)).setOption(this.defaultOptions, true)
                    }
                })
            },
        },
    },
    methods: {
        processData(params) {
            var days = this.getLastSixMon()
            let loopData = { day: days }
            let countTotal = 0
            let dataSeq = []
            for (let i = 0, len = params.length; i < len; i++) {
                let { kind, month, total } = params[i]
                countTotal += total
                dataSeq.push(kind)
                let data = loopData[kind]
                if (!data) {
                    data = {}
                    loopData[kind] = data
                }
                data[month] = total
            }
            this.$emit('countTotal', countTotal)
            dataSeq = [...new Set(dataSeq)]
            for (let i = 0, len = dataSeq.length; i < len; i++) {
                let kind = dataSeq[i]
                let data = loopData[kind]
                if (!data) {
                    data = {}
                    loopData[kind] = data
                }
                let totalOfMonth = []
                for (let j = 0, l = days.length; j < l; j++) {
                    let month = days[j]
                    if (data[month]) {
                        totalOfMonth.push(data[month])
                    } else {
                        totalOfMonth.push(0)
                    }
                }
                loopData[kind] = totalOfMonth
            }
            return loopData
        },
        // 获取最近六个月
        getLastSixMon() {
            var data = new Date()
            //获取年
            var year = data.getFullYear()
            //获取月
            var mon = data.getMonth() + 1
            var arry = new Array()
            for (var i = 0; i < 6; i++) {
                if (mon <= 0) {
                    year = year - 1
                    mon = mon + 12
                }
                if (mon < 10) {
                    mon = '0' + mon
                }
                arry[i] = year + '' + mon
                mon = mon - 1
            }
            return arry
        },
    },
}
</script>
<style lang="scss" scoped>
.pancelDiv {
    margin: 0 auto !important;
}
</style>

​

效果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木易66丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值