rankChart.vue 13.8 KB
<template>
	<view class="rankchart">
		<div :id='bindId' class="chart"></div>
	</view>
</template>

<script>
	export default {
	  data () {
		return {
		}
	  },
	  mounted() {
	  },
	  watch:{
		  bindId:function(newValue,oldValue){
		  },
		  chartData:{
			handler(newValue,oldValue){
				this.drawRank();
			},
			deep:true
		  },
	  },
	  props:{
			bindId:{
				type:String,
				default:''
			},
		   chartData: {
                type: Object,
                default: function(){
					return {}
				}
            }
	  },
	  methods: {
	  	 drawRank() {
			let myChart = {};
            let chartDom = document.getElementById(this.bindId);
            myChart = this.echarts.init(chartDom);
            myChart.showLoading('default', {
                text: '',
                color: '#409eff'
            });
            var option = {
				title:{
					show:true,
					 text:this.chartData.title,
					 left: '13px',
					 top: '13px',
					 textStyle: {
						 fontWeight: "normal",
						 fontSize: 14,
						 color: '#444'
					 }
				},
                tooltip: {
                    trigger: 'axis',
                    backgroundColor: '#fff',
                    padding: [0, 0, 0, 0],
                    textStyle: {
                        color: '#333333'
                    },
                    axisPointer: {
                        type: 'line',
                        lineStyle: {
                            color: 'transparent'
                        }
                    },
                    extraCssText: 'box-shadow: 0px 0px 10px -4px rgba(3, 3, 3, .4)',
                },
				grid:{
				    left: '56px',
				    right: '36px',
				    top: '66px',
				    bottom: '20px'
				},
                yAxis: JSON.parse(JSON.stringify(this.chartData.yaxis)),
                xAxis: {
                    axisLabel: {
                        show: false
                    },
                    axisLine: {
                        show: false
                    },
                    axisTick: {
                        show: false
                    },
                    splitLine: {
                        show: false
                    }
                },
				series:JSON.parse(JSON.stringify(this.chartData.series))
            }
            try {
                this.autoComplete(option.yAxis.data.length, option.yAxis.data, 'string');
            } catch (error) { }
            let dataArr = [], valArr = [];
            try {
                option.yAxis.axisTick = {};
                option.yAxis.axisTick.show = false;
                option.yAxis.inverse = true;
                option.yAxis.axisLine = {
                    show: false
                }
                option.yAxis.splitLine = {
                    normal: {
                        show: false
                    },
                    emphasis: {
                        show: false
                    }
                }
                var rankWidths = '';
				uni.getSystemInfo({
					success:(res)=>{
						rankWidths=res.windowWidth-150;
					}
				})
                var rankTop = -1;
                var barWidth = 6;
                let labelPos = 0;
				if(option.yAxis.data.length > labelPos) {
				    labelPos = option.yAxis.data.length;
				}
                option.yAxis.axisLabel = {
                    margin: 40,
                    interval: 0,
                    padding: [0, 0, 10, 0],
                    verticalAlign: 'bottom',
                    textStyle: {
                        align: 'left',
                        color: '#666666'
                    },
                    formatter: function(value, index) {
                        var key = index + 1;
                        var htmls = '';
                        if(key == 1) {
                            htmls = '{firstLabel|' + key + '}' + " " + '{valueLabel|' + value + '}'
                        } else if(key == 2) {
                            htmls = '{secondLabel|' + key + '}' + " " + '{valueLabel|' + value + '}'
                        } else if(key == 3) {
                            htmls = '{thirdLabel|' + key + '}' + " " + '{valueLabel|' + value + '}'
                        } else {
                            htmls = '{normalLabel|' + key + '}' + " " + '{valueLabel|' + value + '}'
                        }
                        return htmls;
                    },
                    rich: {
                        firstLabel: {
                            width: 18,
                            height: 18,
                            align: 'center',
                            color: '#fff',
                            fontSize: 12,
                            backgroundColor: '#EB6100'
                        },
                        secondLabel: {
                            width: 18,
                            height: 18,
                            align: 'center',
                            color: '#fff',
                            fontSize: 12,
                            backgroundColor: '#FF9631'
                        }, 
                        thirdLabel:{
                            width: 18,
                            height: 18,
                            align: 'center',
                            color: '#fff',
                            fontSize: 12,
                            backgroundColor: '#FFC62E'
                        },
                        normalLabel: {
                            width: 18,
                            height: 18,
                            fontSize: 12,
                            align: 'center',
                            color: '#666666'
                        },
                        valueLabel: {
                            height: 18,
                            padding: [0, 0, 0, 20],
                            align: 'left',
                            color: '#666',
                            fontSize: 12,
                        }
                    }
                };
            } catch (error) { }
            // var colorsArr = ['#ffc62f', '#6c6fc0', '#00b9e2', '#aae3f6', '#aae3f6', '#aae3f6', '#aae3f6', '#aae3f6', '#aae3f6', '#aae3f6', '#aae3f6', '#aae3f6']
            var colorsArr = ['#3BB8FF', '#3BB8FF', '#3BB8FF', '#3BB8FF', '#3BB8FF', '#3BB8FF', '#3BB8FF', '#3BB8FF', '#3BB8FF', '#3BB8FF', '#3BB8FF', '#3BB8FF']
            if(!option.series) return;
            let dataLen = 0;
            for (var i = 0; i < option.series.length; i++) {
                this.autoComplete(option.series[i].data.length, option.series[i].data);
                option.series[i].label = {
                    normal: {
                        show: true,
                        position: [rankWidths, -20],
                        fontSize: 12,
                        textStyle: {
                            color: '#666666'
                        }
                    },
                    emphasis: {
                        show: true,
                        position: [rankWidths, -20],
                        fontSize: 12,
                        textStyle: {
                            color: '#666666'
                        }
                    }
                }
                option.series[i].itemStyle = {
                    normal: {
                        // barBorderRadius: [18, 18, 18, 18],
                        barBorderRadius: 18,
                        color: '#3BB8FF'
                        // color: function(params) {
                        //     return colorsArr[params.dataIndex];
                        // }
                    },
                    emphasis: {
                        barBorderRadius: 18,
                        color: '#0069FF'
                    }
                }
                option.tooltip.formatter = function (params, ticket, callback) {
                    if(!params[0].data) return;
                    let _seriesNum = '', num = '', unit = '';
                    num = (params[0].data || params[0].data == 0) ? (params[0].data >= 100000 ? (params[0].data / 10000).toFixed(2) : params[0].data) : '--';
                    if(/^costomer_ranking$/.test(option.seriesText) || /^format_costomer_ranking$/.test(option.seriesText) || /^trifficRank$/.test(option.seriesText) || /^format_traffic_ranking$/.test(option.seriesText) || /^trafficRank$/.test(option.seriesText)) {
                        _seriesNum = '客流量';
                        unit = params[0].data >= 100000 ? '万人次' : '人次';
                        // unit = '人次';
                    } else if(/^sale_ranking$/.test(option.seriesText) || /^format_sell_ranking$/.test(option.seriesText) || /^saleRank$/.test(option.seriesText) || /^format_sale_ranking$/.test(option.seriesText)) {
                        _seriesNum = '销售额';
                        unit = params[0].data >= 100000 ? '万元' : '元';
                        // unit = '元';
                    } else if(/^areaEffRank$/.test(option.seriesText) || /^perSquareMeterRank$/.test(option.seriesText)) {
                        _seriesNum = '坪效';
                        unit = params[0].data >= 100000 ? '万元/平米' : '元/平米';
                        // unit = '元/平米';
                    } else if(/^durationtimeRank$/.test(option.seriesText) || /^dwellTimeRank$/.test(option.seriesText) || /^durationTimeRank$/.test(option.seriesText)) {
                        _seriesNum = '滞留时间';
                        num = params[0].data >= 1 ? params[0].data : params[0].data * 60;
                        unit = params[0].data >= 1 ? '小时' : '分';
                        // unit = '小时';
                    } else if(/^order_ranking$/.test(option.seriesText)) {
                        _seriesNum = '成单数';
                        unit = params[0].data >= 100000 ? '万次' : '次';
                        // unit = '次';
                    } else if(/^depth_ranking$/.test(option.seriesText) || /^depthRank$/.test(option.seriesText)) {
                        _seriesNum = '游逛深度';
                        unit = params[0].data >= 100000 ? '万个' : '个';
                        // unit = '个';
                    } else if(/^preprice_ranking$/.test(option.seriesText) || /^prePriceRank$/.test(option.seriesText) || /^perTransactionRank$/.test(option.seriesText)) {
                        _seriesNum = '客单价';
                        unit = params[0].data >= 100000 ? '万元/人' : '元/人';
                        // unit = '元/人';
                    } else if(/^turnoverRank$/.test(option.seriesText)) {
                        _seriesNum = '成交量';
                        unit = params[0].data >= 100000 ? '万单' : '单';
                        // unit = '单';
                    } else if(/^handbagRateRank$/.test(option.seriesText)) {
                        _seriesNum = '提袋率';
                        unit = '%';
                    } 
                    // else if() {
                    //     _seriesNum = '销售额';
                    //     unit = '元';
                    // } else if(/^format_costomer_ranking$/.test(option.seriesText)) {
                    //     _seriesNum = '客流量';
                    //     unit = '人次';
                    // } 
                    else if(/^format_enteringrate_ranking$/.test(option.seriesText) || /^enteringRateRank$/.test(option.seriesText)) {
                        _seriesNum = '进店率';
                        unit = '%';
                    } else {
                        _seriesNum = '--';
                        unit = '--';
                    }
                    var htmls = '';
                    htmls += '<div style="font-size:16px;height:31px;color:#0068FF;border-radius:4px;line-height:31px;padding-left:15px; padding-right: 15px;">' + params[0].name + '</div>'
                        + '<div>'
                        + '<p style="font-size:14px;padding:4px 15px 6px;color:#444444;">' + _seriesNum + ' : ' + num + unit + '</p>'
                        + '</div>';
                    return htmls;
                }
                option.series[i].barWidth = barWidth;
            }
            var maxData = 0, defaultData = 100;
            for (var i = 0; i < option.series[0].data.length; i++) {
                if(typeof option.series[0].data[i] === 'string' && !maxData) {
                    maxData = defaultData;
                } else if (maxData < option.series[0].data[i]){
                    maxData = option.series[0].data[i];
                }
            }
            for (var i = 0; i < option.series[0].data.length; i++) {
                dataArr[i] = maxData;
            }

            var shadowSeries = {
                type: 'bar',
                // legendHoverLink: false,
                z: 0,
                itemStyle: {
                    normal: {
                        color: '#f5f5f5',
                        barBorderRadius: 18
                    },
                    emphasis: {
                        color: '#f5f5f5',
                        barBorderRadius: 18
                    }
                },
                label: {
                    show: false
                },
                barWidth: barWidth,
                barGap: '-100%',
                barCategoryGap: '40%',
                data: dataArr,
                animation: false
            } 
            option.series.push(shadowSeries)
            option.xAxis.max = 'dataMax';
            myChart.clear();
            setTimeout(() => {
                myChart.hideLoading();
            }, 500);
            myChart.setOption(option);
        },
		autoComplete(dataLen, data, type) {
			if(dataLen < 10) {
				for(let i = 0; i < 10; i++) {
					if(!data[i] && data[i] != 0) {
						data[i] = '';
					}
				}
			}
            return data;
        }
	  }
	}
</script>

<style>
	.chart{
		width:706.52upx;
		height:750.4upx;
		margin: 0 auto;
		box-shadow:0px 0px 7px 0px rgba(0,0,0,0.14);
		border-radius:4px;
		background: #FFFFFF;
		margin-bottom: 18.11upx;;
	}
</style>