echarts饼图点击高亮显示,刷新数据后选中部分还是高亮显示


    var index = -1; highlightIndex = 0; var deptName = '';
    var datagridType = '';
    var selectIndex = -1;


               
        $.ajax({
            type: "post",
            url: '',
            dataType: 'json',
            success: function (json) {
                var carProfile =[
										    {
										      "value": 2,
										      "name": "货车(2辆)"
										    },
										    {
										      "value": 120,
										      "name": "人车(120辆)"
										    }
										  ];
                deptName = carProfile;
                //人员情况
                myChart2.setOption({
                    legend: {
                        icon: "circle",
                        formatter: function (name) {
                            var target;
                            for (var j = 0; j < carProfile.length; j++) {
                                if (carProfile[j].name === name) {
                                    target = carProfile[j].value
                                }
                            }
                            var arr = [name + "(" + target + "辆)"];
                            return arr

                        }
                    },
                    series: [
                        {
                            
                            data: carProfile,
                            label: {
                                normal: {
                                    show: true,
                                    position:'center',
                                    formatter: '总类型\r\n' + carProfile.length + '种',
                                    textStyle: {
                                        fontSize: 20,
                                        color: '#d0d1d8'
                                    }
                                }
                            }
                        }
                    ]
                });
                if (datagridType != '') {
                    for (var i = 0; i < deptName.length; i++) {
                        if (datagridType == deptName[i].name) {
                            selectIndex = i;
                            index = i;
                        }
                    }
                    myChart2.dispatchAction({ type: 'highlight', seriesIndex: 0, dataIndex: parseInt(selectIndex) });//选中高亮
                }
                myChart2.on('click', function (params) {
                    if (params.dataIndex != index) {
                        highlightIndex = 1;
                        index = params.dataIndex;
                        myChart2.dispatchAction({ type: 'highlight', seriesIndex: 0, dataIndex: params.dataIndex });//选中高亮
                        for (var i = 0; i < carProfile.length; i++) {
                            if (i != index) {
                                myChart2.dispatchAction({ type: 'downplay', seriesIndex: 0, dataIndex: i });//取消高亮
                            }
                        }
                        datagridType = params.name;
                        //;
                    } else {
                        highlightIndex = 0;
                        datagridType = '';
                        myChart2.dispatchAction({ type: 'downplay', seriesIndex: 0, dataIndex: index });//取消高亮
                        index = -1
                    }
                    float(datagridType);
                });
                myChart2.on('mouseout', function (params) {
                    if (highlightIndex != 0) {
                        myChart2.setOption({
                            tooltip:
                            {
                                alwaysShowContent: true,
                            }
                        });
                        myChart2.dispatchAction({ type: 'highlight', seriesIndex: 0, dataIndex: index });//选中高亮
                    } else {
                        index = -1;
                        myChart2.setOption({
                            tooltip:
                            {
                                alwaysShowContent: false
                            }
                        });
                    }
                });



            },
            error: function (s) {
                $.messager.alert("提示", "操作失败", "error");
            }
        });

你可能感兴趣的:(前端,echarts)