echarts圓餅圖設置默認選中項並在中間顯示文字


效果:

 

代碼:

var myChart = echarts.init(document.getElementById('quanshi-echarts-two'));
    option = {
      grid:{ //設置圖表撐滿整個畫布
        top:"12px",
        left:"12px",
        right:"16px",
        bottom:"12px",
        containLabel:true
      },
      series: [
        {
          name:'訪問來源',
          type:'pie',
          radius: ['50%', '70%'],
          avoidLabelOverlap: false,
          label: {
              normal: {
                  show: false,
                  position: 'center',
                  formatter: function(data){ // 設置圓餅圖中間文字排版
                    return data.value+"\n"+"用戶數"
                  }
              },
              emphasis: {
                  show: true, //文字至於中間時,這里需為true
                  textStyle: { //設置文字樣式
                      fontSize: '12',
                      color:"#333"
                  }
              }
          },
          labelLine: {
              normal: {
                  show: false
              }
          },
          data:[
              {value:335, name:'優秀',itemStyle:{color:"#3de16b"}},
              {value:310, name:'良好',itemStyle:{color:"#27baff"}},
              {value:234, name:'一般',itemStyle:{color:"#5865e5"}},
              {value:135, name:'較差',itemStyle:{color:"#fea51a"}},
              {value:1548, name:'糟糕',itemStyle:{color:"#ef5e31"}}
          ]
        }
      ]
    };
    myChart.setOption(option);
    //設置默認選中高亮部分
    myChart.dispatchAction({type: 'highlight',seriesIndex: 0,dataIndex: 0});
    // 當鼠標移入時,如果不是第一項,則把當前項置為選中,如果是第一項,則設置第一項為當前項
    myChart.on('mouseover',function(e){
      myChart.dispatchAction({type: 'downplay',seriesIndex: 0,dataIndex:0}); 
      if(e.dataIndex != index){            
        myChart.dispatchAction({type: 'downplay', seriesIndex: 0, dataIndex: index  });     
      }
      if(e.dataIndex == 0){
        myChart.dispatchAction({type: 'highlight',seriesIndex: 0,dataIndex:e.dataIndex});
      }
    });

    //當鼠標離開時,把當前項置為選中 
    myChart.on('mouseout',function(e){
        index = e.dataIndex;
        myChart.dispatchAction({type: 'highlight',seriesIndex: 0,dataIndex: e.dataIndex});    
    });

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。