Echarts绘制带箭头的线

Echarts绘制带箭头的线
在这里插入图片描echarts述
这个图主要参考echarts官网的关系图绘制的,难点在于去掉其余导向图的箭头,只保留最后一个节点的箭头,以及处理值为null时箭头的指向问题,代码如下:

<template>
    <div class="invoicechart">
      <div class="invoicechart_box" :id="elId"> </div>
    </div>
<!-- :id="echartId"  -->
</template>
<script>
  import uuidv1 from 'uuid/v1'
  export default {
  name: 'r-echarts-line-mul',
  data () {
    return {
            elId: 'echartid11',
            // myChart实例数组
            myChart:null,
            legendShow:false,
        }
    },
    props: {
        data: {//具体数据
            type: Object,
            default(){
              return{

              }
            }

        }
    },
    created(){
      this.elId = uuidv1() //获取随机id
    },
    mounted () {
        this.setEchart();

    },
    watch:{
        data: {//监听数值是否有变化
            handler(newValue, oldValue) {
              if (!this.myChart) {
                this.setEchart();
              }else{
                this.chartChange();
              }

            },
            deep: true//深度
        }
    },
    computed: {
        origin() {
            return this.data;
        },
        option() {
            let that = this;
            // console.log('color',this.data.color)
            let obj = {
                title: {
                    text: that.origin.title?that.origin.title:''
                },
                color:['#6fac45','#f5bb94','#a2c6e7','#aeaeae'],
                tooltip: { },
                legend: { },
                xAxis: {
                    type: 'category',
                    // boundaryGap: true,
                    axisLine:{
                        lineStyle:{
                            color:'#292727'
                        }
                    },
                    axisLabel:{
                        textStyle:{
                            color:'#000000',
                            fontFamily:'Gotham-Bold'
                        }
                    },
                    data: that.origin.xAxisData?that.origin.xAxisData:[],
                },
                yAxis: {
                    type: 'value',
                    show: true,
                    min:that.origin.min?that.origin.min:0,
                    axisLine:{
                        lineStyle:{
                            color:'#292727'
                        }
                    },
                    axisLabel: {
                        show: true,
                        position: 'inside',
                        interval: 'auto',
                        textStyle:{
                            color:'#000000',
                            fontFamily:'Gotham-Bold'
                        },
                        // formatter: '{value}%'
                    }

                },
                series: this.dealSeries
            }
            return obj;
        },
        dealSeries(){
            let data=[];
            let objdata={};
            let links={};
            let color=this.data.color?this.data.color:['#6fac45','#ff6800','#0083ff','#ff0202'];
            if(this.data.seriesData){
                this.data.seriesData.forEach((element,index) => {
                  // console.log('1',element.data)
                  //过滤null,将所有值为null的转为字符串‘null’
                  element.data = element.data.map((item,index) => {
                    return item==null?'null':item;
                  })
                  // console.log('2',element.data)
                  //倒序遍历数组,查找值为null的值,并将其删除,知道值为数字则跳出循环
                  for(var i = element.data.length-1;i!=-1;i--){
                    if(element.data[i] == 'null'){
                      //符合条件,移除
                      element.data.pop();
                    }else{
                      break;
                    }
                  }
                  // console.log('3',element.data)
                  //箭头指向,值为null时指向置为空,将线的宽度置为0,隐藏线
                  links = element.data.map(function (item, i) {
                      if(item=='null'){
                        return {
                          source:'',
                          target: '',
                          lineStyle: {
                            normal: {
                              width: 0,
                              curveness: 0.2
                            }
                          }
                        }
                      }else{
                        return {
                          source: i,
                          target: i + 1
                        }
                      }
                    });
                    links.pop();
                    let list = links.pop();
                    list.symbolSize=[0,10]
                    links.push(list);
                    //绘制实线
                    if(element.name=='true_value'){
                      objdata={
                        type: 'line',
                        data: element.data,
                        symbol: 'circle',     //设定为实心点
                        symbolSize: 5,   //设定实心点的大小
                        color:'green',
                        lineStyle: {
                          normal: {
                            type:'solid',
                            color:'green',
                            width:3
                          }
                        }
                      }
                    }else{
                     //绘制虚线
                      objdata={
                        type: 'graph',
                        layout: 'none',
                        coordinateSystem: 'cartesian2d',
                        symbolSize: 0,
                        edgeSymbol: ['circle', 'arrow'],
                        edgeSymbolSize: [0, 0],
                        data:element.data,
                        links:links,
                        color:color[index],
                        lineStyle: {
                          normal: {
                            type:'dashed',
                            color:color[index],
                            width:2,
                          }
                        }
                      };
                    }
                    // console.log(objdata)
                    if(element&&Object.prototype.toString.call(element)=="[object Object]"){
                        for(let key in element){
                            if(!objdata[key]){
                                objdata[key]=element[key];
                            }
                        }
                    }
                    data.push(objdata);
                });
            }
            return data;
        }
    },
    methods: {
        setEchart () {
            let _this=this;
            this.myChart = this.$echarts.init(document.getElementById(_this.elId));
            this.myChart.setOption(this.option);
            this.myChart.group = _this.data.name;
            this.$echarts.connect(_this.data.name);
            window.addEventListener('resize',function(){//图表自适应
                _this.myChart.resize();
            });
        },
        chartChange () {
            this.myChart.setOption(this.option);
        },

    },
    beforeDestroy(){
        this.myChart.clear();
    }
}
</script>

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值