Echart實現鼠標移入移出事件


下面以一個環形圖爲例,現在想做的效果是圖例初始化時默認顯示第一個圖的文字,上代碼

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript" src="js/echarts.js" ></script>
</head>
<body>
<div id="main" style="width: 800px;height: 500px;"></div>
<script type="text/javascript">
var myChart = echarts.init(document.getElementById('main'));
var ass = {
                normal: {
                    show: false,
                    position: 'center',
                    textStyle: {
                    fontSize : '30',
                    fontWeight: 'bold'
                    }
                },
                emphasis: {
                    show: true,
                    textStyle: {
                        fontSize: '30',
                        fontWeight: 'bold'
                    }
                }
          };          
var resultData =  [
                {value:335, name:'直接訪問'},
                {value:310, name:'郵件營銷'},
                {value:234, name:'聯盟廣告'},
                {value:135, name:'視頻廣告'},
                {value:1548, name:'搜索引擎'}
            ];       
var option = {
    legend: {
        orient: 'vertical',
        x: 'left',
        data:['直接訪問','郵件營銷','聯盟廣告','視頻廣告','搜索引擎']
    },
    series: [
        {
            name:'訪問來源',
            type:'pie',
            radius: ['50%', '70%'],
            avoidLabelOverlap: false,
            label: {
                normal: {
                    show: false,
                    position: 'center',
                    textStyle: {
                    fontSize : '30',
                    fontWeight: 'bold'
                    }
                },
                emphasis: {
                    show: true,
                    textStyle: {
                        fontSize: '30',
                        fontWeight: 'bold'
                    }
                }
            },
            labelLine: {
                normal: {
                    show: false
                }
            },
            data:resultData
        }
    ]
};
function emphasis(){
    var dataLen = option.series[0].data.length; 
    // 高亮當前圖形
    myChart.dispatchAction({
        type: 'highlight',
        seriesIndex: 0,
        dataIndex: 0
    });
}
function unemphasis(){
var dataLen = option.series[0].data.length;
// 取消之前高亮的圖形
    myChart.dispatchAction({
        type: 'downplay',
        seriesIndex: 0,
        dataIndex: 0
    });
}

myChart.setOption(option);
emphasis();
    myChart.on('mouseover', function(a){
    if(a.dataIndex!=0){
    unemphasis();
    }    
    })
    myChart.on('mouseout', function(){
    emphasis();
    })
</script>
</body>

</html>

效果


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章