Vue 封装echarts柱状图(Bar)组件

目的:减少重复代码,便于维护

显示效果

组件代码

javascript 复制代码
<template>
    <div class="ldw-data-content-box">
        <div class="ldw-chilren-box">
            <div class="title" v-if="title">{{ title }}</div>
            <div style="width:100%;flex:1;">
                <div :id="'canvas-box'+number" style="width: 100%;height:100%;"></div>
            </div>
        </div>
    </div>
</template>

<script>
var echarts = require("echarts");
const total = function(data){
    return data.reduce((prev,cur)=>{
        return prev+cur.value
    },0)
}
export default {
    props:{
        title:"",
        data:{
            require:true,
            type:Array,
            default:()=>[]
        },
        w:{
            type:String,
            default:"auto"
        },
        column:{
            type:Boolean,
            default:true
        }
    },
    data(){
        return{
            bg:["#0090FF","#31CFB8","#E55240"],
            number:null,
            top:0,
            h:100,
            myChart:null
        }
    },
    watch: {
        data: {
            //深度监听,可监听到对象、数组的变化
            handler(val, oldVal) {
                this.initData();
            },
            deep: true, //true 深度监听
        }
    },
    created(){
        this.number = Math.random(1000)+1;
    },
    mounted(){
        this.initData()
    },
    methods:{
        initData(){
            let that = this
            let canvas = document.getElementById(`canvas-box${this.number}`)
            this.myChart = echarts.init(canvas);
            
            this.myChart.on("click", function(params) {
                that.$emit('eClick',params)
            });

            let option = {
                title: {
                },
                grid: {
                    top: "8%",
                    left: "3%",
                    right: "4%",
                    bottom: "8%",
                    containLabel: true
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow'
                    }
                },
                legend: {
                    show:false
                },
                xAxis: [
                    {
                        type: 'category',
                        axisTick: {
                            show: false
                        },
                        axisLine:{
                            show:false
                        },
                        axisLabel: {
                          interval:0,
                          rotate:40  //倾斜的程度
                        },
                        splitLine:{show:false},
                        data:this.data.map((res)=>{
                            return res.type
                        })
                    }
                ],
                yAxis: [
                    {
                        type: 'value',
                        axisTick: {
                            show: false
                        },
                        axisLine:{
                            show:false
                        },
                        splitLine:{show:false},
                    }
                ],
                series: [
                    {

                        name: '总计',
                        type: 'bar',
                        barWidth:35,
                        colorBy:"series",
                        label: {
                            // 柱图头部显示值
                            show: true,
                            textStyle:{
                                fontSize:14,
                                fontWeight:600
                            },
                            position: "top",
                            color:"#E55240",
                            fontSize: "14px",
                        },
                        emphasis:{
                            itemStyle:{
                                color:"#F89387"
                            },
                        },
                        itemStyle:{
                            color:{
                                type: 'linear',
                                colorStops: [{
                                    offset: 0,color:"#F89387" // 0% 处的颜色
                                }, {
                                    offset: 1,color:"#E55240" // 100% 处的颜色
                                }],
                                global: false // 缺省为 false
                            },
                        },
                        data: this.data.map((res)=>{
                            return res.value
                        })
                    },
                ]
            };

            this.myChart.setOption(option)
        },
        resize(){
            this.myChart.resize()
        },
        colorFormat(arr){
            arr.forEach((item)=>{
                if(item.ldwColor){
                    item.itemStyle = {
                        color:{
                            type: 'linear',
                            x: 0,
                            y: 0,
                            x2: 0,
                            y2: 1,
                            colorStops: [{
                                offset: 0, color: item.ldwColor[0] // 0% 处的颜色
                            }, {
                                offset: 1, color: item.ldwColor[1] // 100% 处的颜色
                            }],
                            global: false // 缺省为 false
                        }
                    }
                }
            })
            return arr
        }
    }
}
</script>

<style scoped>
.ldw-data-content-box{
    width:100%;
    height:100%;
    display: flex;
}

.ldw-data-content-box>.ldw-chilren-box{
    width:100%;
    height:100%;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-flow: column;
    overflow: hidden;
}
.ldw-data-content-box>.ldw-chilren-box>.title{
    font-size: 18px;
    color:#000;
    text-align: center;
    padding:24px 0;
}

.ldw-bg-box{
    background: rgba(255,255,255,0.5);
    border: 1px solid #F4FDFE;
    border-radius: 20px;
}

.ldw-text-text{
	display: inline-block;
	text-align: justify;
	line-height: 0;
	margin-left: 20px;
}

.ldw-text-text::after{
	content:"";
	display: inline-block;
	width:100%;
	overflow:hidden;
	height:0;
}

.ldw-quan-quan{
    display: flex;
    align-items: center;
}

.ldw-w{
    margin-top:6px;
    position: relative;
}

.ldw-quan-box{
    width: 13px;
    height: 13px;
    border-radius: 2px;
    margin-right: 20px;
}

.flex-column{
    width:100%;
    display: flex;
    justify-content: space-around;
}

.flex-column .ldw-w{
    width: auto;
}

.flex-column .ldw-quan-box{
    margin-right: 10px;
}
</style>

调用代码

javascript 复制代码
<template>
    <div class="root flex flex-col border-box">
        <div  style="width: 400px; height: 400px;"  >
            <Bar  :title="'统计'" :barType="'x'" :data="chartData" ></Bar>
        </div>
    </div>
</template>

<script>
    import Bar from '@/components/echarts/barTwoInfo.vue'

    export default{
        name:'',
        created() {
        },
        components: {Bar},
        data() {
            return {
                chartData:
                [
                    {value:100, type:'一季度'},
                    {value:105, type:'二季度'},
                    {value:201, type:'三季度'},
                    {value:167, type:'四季度'},
                ]
            }
        },
        methods:{
        }
    }
</script>
相关推荐
谢道韫6661 小时前
今日总结 2024-12-24
javascript·vue.js·elementui
一朵好运莲1 小时前
React引入Echart水球图
开发语言·javascript·ecmascript
梦境之冢2 小时前
axios 常见的content-type、responseType有哪些?
前端·javascript·http
racerun2 小时前
vue VueResource & axios
前端·javascript·vue.js
J总裁的小芒果2 小时前
THREE.js 入门(六) 纹理、uv坐标
开发语言·javascript·uv
m0_548514772 小时前
前端Pako.js 压缩解压库 与 Java 的 zlib 压缩与解压 的互通实现
java·前端·javascript
Calm5502 小时前
Vue3:uv-upload图片上传
前端·vue.js
浮游本尊2 小时前
Nginx配置:如何在一个域名下运行两个网站
前端·javascript
新中地GIS开发老师2 小时前
《Vue进阶教程》(12)ref的实现详细教程
前端·javascript·vue.js·arcgis·前端框架·地理信息科学·地信
漫天转悠2 小时前
Vue3中404页面捕获(图文详情)
vue.js