vue2 + echats树状图 点击按钮 展开所有节点/收起所有节点

vue2 + echats树状图 点击按钮 展开所有节点/收起所有节点

<template>
    <div>
        <button @click="expandAll(isExpanded)">
            {{ isExpanded ? '一键收起' : '一键展开' }}
        </button>
        <div ref="echartsTree" style="width: 600px; height: 400px;"></div>
    </div>
</template>

<script>
import * as echarts from 'echarts';

export default {
    data() {
        return {
            isExpanded: true,
            myChart: null,
            option: {}
        };
    },
    methods: {
        getList() {
            this.option = {
                series: [
                    {
                        type: 'tree',
                        data: [
                            {
                                name: 'root',
                                children: [
                                    { name: 'child1' },
                                    { name: 'child2', children: [{ name: 'grandchild1' }] }
                                ]
                            }
                        ],
                        expandAndCollapse: true // 开启节点展开折叠
                    }
                ]
            }
        },
        // 一键展开
        expandAll(e) {
            if (e == false) {
                this.handleClose();
                this.isExpanded = !e;
            } else {
                this.option.series = {
                    type: 'tree',
                    data: [{
                        name: 'root'
                    }]
                }
                this.myChart.setOption(this.option);
                this.isExpanded = !e;
            }
        },
        // 一键关闭
        handleClose() {
            this.getList();
            this.myChart.setOption(this.option);
        },
        initChart() {
            this.myChart = echarts.init(this.$refs.echartsTree);
            this.myChart.setOption(this.option);
        }
    },
    mounted() {
        this.getList();
        this.$nextTick(() => {
            this.initChart()
        })
    }
};
</script>
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现vue+echarts树状图,可以按照以下步骤操作: 1. 安装echarts:在项目中安装echarts依赖,可以使用npm或yarn命令进行安装。 2. 引入echarts:在组件中引入echarts,并创建一个echarts实例。 3. 获取据:获取需要展示的树状图据,可以使用ajax或axios等方式进行获取。 4. 处理据:将获取到的据进行递归处理,生成符合echarts树状图要求的据格式。 5. 配置echarts:设置树状图的布局、样式、节点名称等配置。 6. 渲染树状图:将配置好的echarts实例渲染到DOM中。 以下是一个简单的vue+echarts树状图示例代码: ``` <template> <div ref="treeChart" style="height: 500px;"></div> </template> <script> import * as echarts from 'echarts'; export default { mounted() { this.initTreeChart(); }, methods: { initTreeChart() { const treeChart = echarts.init(this.$refs.treeChart); // 模拟据 const data = { name: 'root', children: [ { name: 'node1', children: [ { name: 'node1.1' }, { name: 'node1.2' } ] }, { name: 'node2' } ] }; // 处理据 const option = { series: [ { type: 'tree', data: [data], top: '1%', left: '7%', bottom: '1%', right: '20%', symbolSize: 7, label: { position: 'left', verticalAlign: 'middle', align: 'right' }, leaves: { label: { position: 'right', verticalAlign: 'middle', align: 'left' } }, expandAndCollapse: true, animationDuration: 550, animationDurationUpdate: 750 } ] }; // 渲染树状图 treeChart.setOption(option); } } } </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值