echarts自定义外部legend(原理使数据为0)

setChartsOptions(pjbm, wcd) { let barCharts = Echarts.init(this.$refs.barCharts); let option = { title: { text: '图' }, tooltip: { trigger: 'axis', }, legend: { data: ['销量'] }, xAxis: { data: pjbm }, yAxis: {}, series: [{ name: '销量', type: 'bar', data: wcd }] }; barCharts.setOption(option) }

请求返回的数据格式

data: [{
    pjbm: '建部',
    wcd: 10.0,
    show: true
}, {
    pjbm: '党建部',
    wcd: 90.0,
    show: true
}, {
    pjbm: '发展部',
    wcd: 35.0,
    show: true
}, {
    pjbm: '安监部',
    wcd: 90.0,
    show: true
}, {
    pjbm: '财务部',
    wcd: 40.0,
    show: true
}]
initData() {
    this.$api.chartPage.chartData().then(data = > {
        let pjbm = [];
        let wcd = [];
        data.forEach(item = > {
            pjbm.push(item.pjbm);
            wcd.push(item.wcd);
        });
        this.setChartsOptions(pjbm, wcd);
        this.data = data;
        //拷贝
        this.dataCopy = JSON.parse(JSON.stringify(data));
        this.pjbm = pjbm;
    })
},

页面渲染for循环

{{item.pjbm}}

点击实现

once(item, index) {
    this.data.forEach(p = > {
        if (item.pjbm === p.pjbm) {
            p.show = !item.show
        }
        if (p.show === false) {
            p.wcd = 0;
        }
    });
    this.setChartsOptions(this.pjbm, this.data.map(v = > {
        this.dataCopy.map(p = > {
            if (v.pjbm === p.pjbm && v.show === true) {
                v.wcd = p.wcd;
            }
        });
        return v.wcd;
    }));
},

全部代码




你可能感兴趣的:(echarts自定义外部legend(原理使数据为0))