动态创建多个echarts图表

效果:

 

<template>
<div class="wrapper">
    <Row v-for="(items, index) in secondeData" :key="index">
        <Col  span="12" v-for="m in items" :key="m">
            <div class="chart" :ref="m.targetName" style="height: 400px;margin: 10px;box-shadow: 0 0 8px"></div>
        </Col>
    </Row>               
</div>
</template>
<script>
var appName='';
//引入Echarts主模块
  let echarts=require('echarts/lib/echarts');
  //引入柱状图
  require('echarts/lib/chart/bar');
  //引入圆饼图
  require('echarts/lib/chart/pie');
  //引入所需组件
  require('echarts/lib/component/tooltip');
  require('echarts/lib/component/legend');

import  'static/js/macarons.js'
export default {

    data(){
        return {
            charts:{},
            echartsData:[],
            appCode:'',
            appName:'',
            monitorAlarmMessage:{
                startAlarmTime:new Date(new Date().getTime()-600000),
                endAlarmTime:new Date()
            }
        }
    },
    computed: {
        // 计算属性的 getter
        secondeData: function () {
            let newData = [], c = this.echartsData.length % 2 == 0, l = c ? this.echartsData.length : this.echartsData.length - 1 ;
            for (let i = 0; i < l; i = i+2) {
                newData.push([this.echartsData[i], this.echartsData[i+1]]);
            }
            if(!c){
                newData.push([this.echartsData[this.echartsData.length - 1]]);
            }
            return newData;
        }
    },
    methods:{
        setData(arrays){
            let _this = this;
            _this.echartsData = arrays;
            _this.$nextTick(function () {
                let newTargets = [];
                for (let index = 0; index < arrays.length; index++) {
                    let item = arrays[index], doms = _this.$refs[item.targetName];
                    newTargets[index] = item.targetName;
                    if(!_this.charts[item.targetName]){
                        console.log("不存在,开始重新绘制div ->" + item.targetName);
                        _this.createChartOne(item.targetName, doms[0]);
                    }
                    _this.chartMonitorTargetUpdate(item.targetName, item); 
                }
                for (const key in _this.charts) {
                    if (newTargets.indexOf(key) < 0) {
                        console.log("删除 ->" + key);
                        delete _this.charts[key];
                    }
                }
            })
        },
        showInfluxDBMonitorAlarmByAppCode(){
            this.$http.post('/influxDBMonitorAnalyze/showInfluxDBMonitorAlarmByAppCode',{//查询请求接口
                appName:this.appName,
                appCode:this.appCode
            }).then((res)=>{
                //alert(JSON.stringify(res.data.data));
                this.setData(res.data.data.filter(function(item){
                    return item != null;
                }));
                //this.echartsDataSize= this.echartsData.length;
                console.log(this.echartsData);
            }).catch((err)=>{
                this.$Modal.error({ title: "请求失败", content: err });
            })
        },
        createChartOne(targetName, ref){
            //this.chartMonitorTarget=echarts.init(this.$refs.chartOne);
            this.charts[targetName] = echarts.init(ref, 'macarons');
            this.charts[targetName].setOption({
                tooltip: {
                    trigger: 'axis'
                },
                grid:{
                    top:100,
                    bottom:40,
                    left:40,
                    right:45
                },
                xAxis:  {
                    name:'时间',               //坐标轴名称
                    type: 'category',
                    nameGap:15,                  //坐标轴名称与轴线之间的距离
                    nameRotate:0,                //坐标轴名字旋转,角度值
                    boundaryGap: true,
                    data: [],
                    axisTick:{          //坐标轴刻度是否朝内,默认朝外。
                        length:5                //坐标轴刻度的长度。
                    },
                    axisLabel: {
                    interval:"auto",
                   rotate:30  
                    } 
                },
                yAxis: {
                    name:'',               //坐标轴名称
                    type: 'value',
                    axisLabel: {
                        formatter: '{value}'
                    }
                },
                series: []
            })
        },
        chartMonitorTargetUpdate(targetName, data){
            this.charts[targetName].setOption({
                title: {
                    text: targetName,
                    textStyle: {
                        fontWeight: 'bolder',              //标题颜色
                        color: '#408829'
                    },
                    subtext: ''
                },
                legend: {
                    left:'center',
                    top:'30',
                    bottom:'auto',
                    orient:'horizontal',
                    data:data.legend.data
                },
                xAxis: {
                    data: data.xaxisData
                },
                series: data.influxDBMonitorAlarm.seriesList
            });
        },
        test(){
            var arrays = [], count = Math.round(Math.random()*10);
            console.log("随机生成个数:" + count);
            for (let index = 0; index < count; index++) {
                //let count = Math.round(Math.random()*10);
                let xaxis = [10];
                for (let k = 0; k < 10; k++) {
                    xaxis[k] = "8:" + Math.round(Math.random()*60);
                }

                let datas = [4];
                for (let j = 0; j < 4; j++) {
                    let is = [10];
                    for (let i = 0; i < 10; i++) {
                        is[i] = Math.round(Math.random()*100);
                    }
                    datas[j] = is;  
                }
                arrays.push({
                    "legend": {
                        "data": ["基线值", "当前值", "基线上浮值", "基线下浮值"]
                    },
                    "targetName": "targetName" + index,
                    "appName": null,
                    "xaxisData": xaxis,
                    "influxDBMonitorAlarm": {
                        "seriesList": [{
                            "name": "基线值",
                            "type": "line",
                            "data": datas[3],
                            "markPoint": {
                                "data": [{
                                    "type": "max",
                                    "name": "最大值"
                                }, {
                                    "type": "min",
                                    "name": "最小值"
                                }]
                            }
                        }, {
                            "name": "当前值",
                            "type": "line",
                            "data": datas[0],
                            "markPoint": {
                                "data": [{
                                    "type": "max",
                                    "name": "最大值"
                                }, {
                                    "type": "min",
                                    "name": "最小值"
                                }]
                            }
                        }, {
                            "name": "基线上浮值",
                            "type": "line",
                            "data": datas[1],
                            "markPoint": {
                                "data": [{
                                    "type": "max",
                                    "name": "最大值"
                                }, {
                                    "type": "min",
                                    "name": "最小值"
                                }]
                            }
                        }, {
                            "name": "基线下浮值",
                            "type": "line",
                            "data": datas[2],
                            "markPoint": {
                                "data": [{
                                    "type": "max",
                                    "name": "最大值"
                                }, {
                                    "type": "min",
                                    "name": "最小值"
                                }]
                            }
                        }]
                    }
                });
            }  
            this.setData(arrays);
        }
    },
    created(){
       
    },
    activated(){
        let _this = this;
        appName=_this.$route.query.appName
        _this.appCode=_this.$route.query.appCode
        _this.appName=_this.$route.query.appName
        _this.showInfluxDBMonitorAlarmByAppCode();
        // _this.test();

        window.setInterval(function(){
            _this.showInfluxDBMonitorAlarmByAppCode();
        }, 5000);
    },destroyed(){
        
    }
    
}
</script>
<style lang="scss" scoped>//-----------------------------------------------------------css样式代码start
//编写样式
.ivu-form-item {
    margin-bottom: 24px;
}
</style>

js:macarons.js

(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define(['exports', 'echarts'], factory);
    } else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') {
        // CommonJS
        factory(exports, require('echarts'));
    } else {
        // Browser globals
        factory({}, root.echarts);
    }
}(this, function (exports, echarts) {
    var log = function (msg) {
        if (typeof console !== 'undefined') {
            console && console.error && console.error(msg);
        }
    };
    if (!echarts) {
        log('ECharts is not Loaded');
        return;
    }

    var colorPalette = [
        '#2ec7c9','#b6a2de','#5ab1ef','#ffb980','#d87a80',
        '#8d98b3','#e5cf0d','#97b552','#95706d','#dc69aa',
        '#07a2a4','#9a7fd1','#588dd5','#f5994e','#c05050',
        '#59678c','#c9ab00','#7eb00a','#6f5553','#c14089'
    ];


    var theme = {
        color: colorPalette,

        title: {
            textStyle: {
                fontWeight: 'normal',
                color: '#008acd'
            }
        },

        visualMap: {
            itemWidth: 15,
            color: ['#5ab1ef','#e0ffff']
        },

        toolbox: {
            iconStyle: {
                normal: {
                    borderColor: colorPalette[0]
                }
            }
        },

        tooltip: {
            backgroundColor: 'rgba(50,50,50,0.5)',
            axisPointer : {
                type : 'line',
                lineStyle : {
                    color: '#008acd'
                },
                crossStyle: {
                    color: '#008acd'
                },
                shadowStyle : {
                    color: 'rgba(200,200,200,0.2)'
                }
            }
        },

        dataZoom: {
            dataBackgroundColor: '#efefff',
            fillerColor: 'rgba(182,162,222,0.2)',
            handleColor: '#008acd'
        },

        grid: {
            borderColor: '#eee'
        },

        categoryAxis: {
            axisLine: {
                lineStyle: {
                    color: '#008acd'
                }
            },
            splitLine: {
                lineStyle: {
                    color: ['#eee']
                }
            }
        },

        valueAxis: {
            axisLine: {
                lineStyle: {
                    color: '#008acd'
                }
            },
            splitArea : {
                show : true,
                areaStyle : {
                    color: ['rgba(250,250,250,0.1)','rgba(200,200,200,0.1)']
                }
            },
            splitLine: {
                lineStyle: {
                    color: ['#eee']
                }
            }
        },

        timeline : {
            lineStyle : {
                color : '#008acd'
            },
            controlStyle : {
                normal : { color : '#008acd'},
                emphasis : { color : '#008acd'}
            },
            symbol : 'emptyCircle',
            symbolSize : 3
        },

        line: {
            smooth : true,
            symbol: 'emptyCircle',
            symbolSize: 3
        },

        candlestick: {
            itemStyle: {
                normal: {
                    color: '#d87a80',
                    color0: '#2ec7c9',
                    lineStyle: {
                        color: '#d87a80',
                        color0: '#2ec7c9'
                    }
                }
            }
        },

        scatter: {
            symbol: 'circle',
            symbolSize: 4
        },

        map: {
            label: {
                normal: {
                    textStyle: {
                        color: '#d87a80'
                    }
                }
            },
            itemStyle: {
                normal: {
                    borderColor: '#eee',
                    areaColor: '#ddd'
                },
                emphasis: {
                    areaColor: '#fe994e'
                }
            }
        },

        graph: {
            color: colorPalette
        },

        gauge : {
            axisLine: {
                lineStyle: {
                    color: [[0.2, '#2ec7c9'],[0.8, '#5ab1ef'],[1, '#d87a80']],
                    width: 10
                }
            },
            axisTick: {
                splitNumber: 10,
                length :15,
                lineStyle: {
                    color: 'auto'
                }
            },
            splitLine: {
                length :22,
                lineStyle: {
                    color: 'auto'
                }
            },
            pointer : {
                width : 5
            }
        }
    };

    echarts.registerTheme('macarons', theme);
}));

 

  • 6
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Echarts是一款功能强大的数据可视化库,旨在以简洁、直观的图表形式展示数据。如果我们想要实现多个图表的轮播效果,可以利用Echarts动态数据更新功能和定时器来实现。 首先,我们需要创建一个包含多个图表的容器,可以使用div元素来实现。然后,根据需要创建多个Echarts实例,并将其分别放入容器中。 接下来,我们可以使用JavaScript中的定时器函数(例如setInterval)来控制图表的切换。在每个定时器的回调函数中,我们可以通过修改Echarts实例的选项和数据来更新图表展示内容。可以根据需要制定每个图表的停留时间,然后在固定的时间间隔后切换到下一个图表。 为了使图表切换更加平滑连续,我们可以使用Echarts提供的数据更新方法(如setOption)来更新图表的数据和选项,而不是销毁并重新创建新的图表实例。 另外,如果希望图表之间的切换有一定的过渡效果,我们还可以通过CSS的过渡属性(如transition)来为图表容器添加过渡效果,使图表之间的切换更加平滑。 总结来说,实现Echarts多个图表的轮播效果需要以下几个步骤:创建包含多个图表的容器、创建多个Echarts实例、使用定时器函数控制切换、通过修改Echarts实例的选项和数据来更新图表内容、为图表容器添加过渡效果等。通过这些步骤的组合,我们可以实现多个图表的轮播效果,使数据以动态的方式进行展示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值