数据可视化项目---地区销量排行

本文介绍了一个用于展示地区销量排名的组件实现方法。通过使用ECharts库,该组件能够动态展示从服务器获取的数据,并支持鼠标悬停交互及屏幕适配。文章详细介绍了组件的初始化、数据获取与处理过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最终效果如下
在这里插入图片描述

地区销量排行—组件代码

<!--地区销量排行-->
<template>
    <div class="com-container">
        <div class="com-chart" ref="rank_ref"></div>
    </div>
</template>

<script>
    export default {
        name: "Rank",
        data() {
            return {
                chartInstance:null,
                allData:null,
                startValue: 0,//区域缩放的起点值
                endValue: 4,//区域缩放的终点值
                timerId:null,//定时器标识
            }
        },
        mounted(){
            this.initChart();
            this.getData();
            window.addEventListener('resize',this.screenAdapter);
            //在页面加载完成忠厚,主动进行屏幕的适配
            this.screenAdapter();
        },
        destroy(){
            //在组件销毁的时候,将窗口监听器取消掉
            window.removeEventListener('resize',this.screenAdapter);
            clearInterval(this.timerId);
        },
        methods: {
            //初始化echartInstance对象
            initChart(){
                this.chartInstance=this.echarts.init(this.$refs.rank_ref,'chalk');
                const initOption={
                    title:{
                        text:'┃ 地区去向排行',
                        left:20,
                        top:20,
                    },
                    grid:{
                        top:'40%',
                        left:'5%',
                        right:'5%',
                        bottom:'5%',
                        containLabel:true
                    },
                    tooltip:{
                        show:true
                    },
                    xAxis: {
                        type:'category'
                    },
                    yAxis:{
                        type:'value'
                    },
                    series:[
                        {
                            type:'bar'
                        }
                    ]
                };
                this.chartInstance.setOption(initOption);

                //对图表进行鼠标事件的监听
                this.chartInstance.on('mouseover',()=>{
                    clearInterval(this.timerId);
                });
                this.chartInstance.on('mouseout',()=>{
                    this.startInterval();
                });
            },
            //获取服务器数据
            async getData(){
                await this.axios.get("/chart/rank").then(res=>{
                    this.allData=res.data;
                });
                //对allData中的数据从大到小进行排序
                this.allData.sort((a,b)=>{
                    return b.value-a.value;
                });
                this.updateChart();
                this.startInterval();
            },
            //处理数据
            updateChart(){
                const colorArr = [
                    ['#0BA82C', '#4FF778'],
                    ['#2E72BF', '#23E5E5'],
                    ['#5052EE', '#AB6EE5']
                ];
                //所有地区形成的数组
                const RegionArr=this.allData.map(item=>{
                   return item.name
                });
                //所有地区对应额销售金额
                const ValueArr=this.allData.map(item=>{
                    return item.value
                });
                const dataOption={
                    xAxis:{
                        data:RegionArr
                    },
                    dataZoom:{
                        show:false,
                        startValue:this.startValue,
                        endValue:this.endValue,
                    },
                    yAxis:{

                    },
                    series: [
                        {
                            data:ValueArr,
                            itemStyle:{
                                //不同销量不同的颜色渐变
                                color: arg=> {
                                    let targetColorArr=null;
                                    if (arg.value > 300){
                                        targetColorArr=colorArr[0];
                                    }else if (arg.value > 200) {
                                        targetColorArr=colorArr[1];
                                    }else {
                                        targetColorArr=colorArr[2];
                                    }
                                    return new this.echarts.graphic.LinearGradient(0,0,0,1,[
                                        {
                                            offset:0,
                                            color:targetColorArr[0]
                                        },
                                        {
                                            offset:1,
                                            color:targetColorArr[1]
                                        },
                                    ])
                                }
                            }
                        }
                    ]
                };
                this.chartInstance.setOption(dataOption);
            },
            //当浏览器的大小发生变化的时候,调用该方法,来完成屏幕的适配
            screenAdapter(){
                const titleFontSize=this.$refs.rank_ref.offsetWidth/100*3.6;
                //和分辨率大小相关的配置项
                const adapterOption={
                    title:{
                        textStyle:{
                            fontSize:titleFontSize
                        }
                    },
                    series:[
                        {
                            barWidth:titleFontSize,
                            itemStyle: {
                                barBorderRadius:[titleFontSize/2,titleFontSize/2,0,0]
                            }
                        }
                    ]
                };
                this.chartInstance.setOption(adapterOption);
                //手动的调用图表的resize方法
                this.chartInstance.resize();
            },
            startInterval() {
                if (this.timerId) {
                    clearInterval(this.timerId);
                }
                this.timerId=setInterval(()=>{
                    this.startValue++;
                    this.endValue++;
                    if (this.endValue > this.allData.length - 1) {
                        this.startValue=0;
                        this.endValue=4;
                    }
                    this.updateChart();
                },2000);
            }
        }
    }

</script>

<style scoped>
    .com-container{
        width: 100%;
        height: 100%;
        overflow: hidden;
        position: relative;

    }
    .com-chart{
        width: 100%;
        height: 100%;
        overflow: hidden;
        border-radius: 20px;
    }
</style>

客源分析趋势图—图表接口数据

[{"name":"周口中心站","value":"14"},{"name":"周口市中心站","value":"218"},{"name":"周口市淮阳","value":"230"},{"name":"周口市项城","value":"85"},{"name":"周口火车站","value":"567"},{"name":"周口项城","value":"6"}]

客源分析趋势图—设计流程

参考https://blog.csdn.net/liudachu/article/details/109273102

下一篇:数据可视化项目—订单分类占比图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值