mixin.js 3.63 KB
import basic from './basic';
import { optionFormatter } from "@/utils/dealChart";
export default {
    props: {
        chartId: {
            type: String
        },
        tval: { //图表按功能划分类型
            type: String
        },
        cdata: {
            type: Object,
            default: () => {}
        }
    },
    watch: {
        cdata: {
            handler(newVal, val) {
                newVal && this.initChartData(newVal);
            },
            deep: true
        }
    },
    components: {
        basic
    },
    data() {
        return {
            chartData: null,
            chart: null,
        }
    },
    methods: {
        getRandomColors(length) {
            let colors = [],
                temp = [];
            while (colors.length < length) {
                temp.push(getColor());
                colors = [...new Set(temp)];
            }

            function getColor() {
                return (
                    "#" +
                    ("00000" + ((Math.random() * 0x1000000) << 0).toString(16)).slice(-6)
                );
            }
            return colors;
        },
        /**
         * 自定义处理图表参数
         * */
        checkOption(preData) {
            if (preData.series && (preData.series.length == 0 || (preData.series[0].data && preData.series[0].data.length == 0))) {
                let lang = localStorage.getItem('lang') || '';
                let nodatatext = '';
                if (lang == 'en_US') {
                    nodatatext = 'No Data'
                } else if (lang == 'zh_TW') {
                    nodatatext = '暫無數據'
                } else {
                    nodatatext = '暂无数据'
                }
                return {
                    isEmpty: true,
                    graphic: [{
                        type: 'image',
                        left: 'center',
                        bottom: '51%',
                        style: {
                            image: '/static/img/no_data.png',
                            width: 60,
                            height: 60,
                        }
                    }, {
                        type: 'text',
                        left: 'center',
                        top: '52%',
                        z: 100,
                        style: {
                            fill: '#444',
                            text: nodatatext,
                            font: '16px "Microsoft YaHei"'
                        }
                    }]
                };
            }
            return preData;
        },
        /**
         * 处理图表参数
         * 说明 preChartData:预处理后端的基础数据
         * dealChartData:处理成echart直接生成图表的数据
         * */
        initChartData(cdata) {
            let chartData = JSON.parse(JSON.stringify(cdata));
            let preData = this.preChartData(chartData);
            const option = preData.noProcess ? this.checkOption(preData) : optionFormatter(preData);
            if (!option) return;
            if (option.isEmpty) {
                option.xAxis = {
                    show: false
                };
                option.yAxis = {
                    show: false
                };
                option.grid = { top: 20, right: 20, bottom: 20, left: 20 };
                this.chartData = option;
                return;
            }
            this.chartData = this.dealChartData(option);
        }
    },
    mounted() {
        this.initChartData(this.cdata);
        this.$refs.basic && this.$refs.basic.chart && (this.chart = this.$refs.basic.chart);
    },
    created: function() {

    }

}