EChart处理三维数据作图表、多维legend图例处理

  处理三维数据作图表,好比返回的数据就是一个个list,list里面某几个数据同属于一个维度,项目中的实例效果以下:面试

  上面的khfx会有多个,好比db一、db二、db3等,下面的那些数据也会变化,目前需求就是作的下面的实现单选,可使用echarts的legend的selectedMode实现,而后上面的db那些就是能够复选,默认全显示,选择以后就取消该条数据显示。也就是说至关于须要2层图例组件同时控制下面series的显示。api

  大值考虑的是下面的用legend的图例,而后上面的图例就本身手写,而后经过js方法去实现相似图例的功能。数组

  踩坑记录:echarts

  一、刚开始想的就是经过legend的selected设置为false来达到效果(以前处理翻页使用过这种),可是发现不行,设置某一个name为false,会影响到上面图例全部的数据,由于series里的那个name是同样的,可是这个name又必须和legend里的一一对应,因此放弃ecmascript

  二、考虑先删除好比db1的数据,因此先定义一个存储的数组,来存储删除的数据,由于点击series的线消失,再点击仍是要加进去的,这种实现能够。可是有个问题,就是当删除series为[]一个空数组时,下面的图仍是会有。在echart的demo里面试一下,发现series:[],不会画图fetch

  解决方案:优化

  当选中上面图例时,只把series里面的对象的data置为[]空数组,详情看下面的changeLegrend方法,完美实现效果,代码还没优化,碰到相似处理三维图例的需求时,能够参考下this

<template>
    <div>
    <div class="legend_container">
        <ul>
            <li v-for="item in legendData">
                <button class="btn-link btn-legend-item" @click="changeLegrend(item,$event)"><span class="legend-item-bg" :style="'background-color:'+legendColor[item]"></span>{{item}}</button>
            </li>
        </ul>
    </div>
    <div id="myLine" :style="{width:'100%',height:'300px'}"></div>
    </div>
</template>
<script type="ecmascript-6"> import {getPhyLogiLoadApi} from '@/apis' export default { data(){ return { xData:[], series:[], legendData:[], legendColor:{}, storage:{}, colors:['#5793f3', '#d14a61', '#675bba'] } }, methods:{ drawLine(){ let myLine = this.$echarts.init(document.getElementById('myLine')); let option = { tooltip: { trigger: 'axis' }, legend: { selectedMode:'single' }, grid: { left: '3%', right: '7%', bottom: '3%', containLabel: true }, xAxis: [ { type: 'category', axisTick: { alignWithLabel: true }, data: this.xData, name: '快照时间' } ], yAxis: [ { type: 'value', name: '统计值' } ], series: this.series }; myLine.setOption(option); window.addEventListener("resize", () => { myLine.resize();}); }, fetchData(){ getPhyLogiLoadApi(this.$store.state.inspection.reportInfo.batch_id).then((res) => { if(res.status === 200){ let _dataArray = res.data, len = _dataArray.length; for(let i=0;i<len;i++){ if(!this.legendData.includes(_dataArray[i].instance_name)){ this.legendColor[_dataArray[i].instance_name] = this.colors[this.legendData.length]; this.legendData.push(_dataArray[i].instance_name); } let _obj = { name:_dataArray[i].stat_name, type:'line', smooth:true, dbname:_dataArray[i].instance_name, color:this.legendColor[_dataArray[i].instance_name], data:_dataArray[i].stat_value.split(',') } this.series.push(_obj) } this.xData = _dataArray[0].snap_time.split(',') this.drawLine(); } }) }, changeLegrend(item,e){ let _obj = {}, len = this.series.length, _data = this.series; let thisDom = e.currentTarget; thisDom.classList.toggle('btn-selected'); for(let i = 0; i < len; i++){ if(item === _data[i].dbname){ //若是为空,说明被选,置为存储的数据
                    if(_data[i].data.length === 0){ _data[i].data = this.storage[item][_data[i].name] }else{ //先存数据,再置为空
                        _obj[_data[i].name] = _data[i].data; _data[i].data = [] } } } //存储数据
            this.storage[item] = _obj; this.drawLine(); } }, mounted(){ this.fetchData(); } } </script>
<style scoped lang="stylus" rel="stylesheet"> .legend_container{ text-align center li { list-style none display inline } } .btn-link, .btn-link:active, .btn-link:focus, .btn-link:hover { border-color: transparent; background-color: transparent; box-shadow: none; } .btn-legend-item { color: rgb(72, 72, 72); padding: 0px; } .legend-item-bg { display:inline-block; width: 20px; height: 12px; margin-right: 5px; border-radius: 2px; } .btn-selected{ opacity 0.3 } </style>
相关文章
相关标签/搜索