在 echarts的tooltip中实现一个select选择器 并在接口中获取到选中的数据

option = {
    xAxis: {
        type: 'category',
        data: ['A', 'B', 'C', 'D', 'E']
    },
    yAxis: {
        type: 'value'
    },
    series: [{
        data: [10, 20, 30, 40, 50],
        type: 'bar'
    }],
    tooltip: {
        trigger: 'item', // 修改为 'item'
        triggerOn:'click',
        formatter: function (params) {
            var html = '<div>';
            for (var i = 0; i < params.length; i++) {
                html += params[i].seriesName + ' : ' + params[i].value + '<br>';
            }
            html += '</div>';
            html += '<select id="select">';
            html += '<option value="A">A</option>';
            html += '<option value="B">B</option>';
            html += '<option value="C">C</option>';
            html += '<option value="D">D</option>';
            html += '<option value="E">E</option>';
            html += '</select>';
            return html;
        }
    }
};

// 设置配置项
myChart.setOption(option);

// // 监听 select 选择器的 change 事件
// document.getElementById('select').addEventListener('change', function () {
//     var selectedValue = this.value;
//     console.log('选中的值:', selectedValue);
//     // 调用接口并获取选中的数据
// });

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的实现: 1. 在 Vue 引入 echarts ``` import echarts from 'echarts/lib/echarts'; import 'echarts/lib/chart/tree'; import 'echarts/lib/component/tooltip'; import 'echarts/lib/component/title'; ``` 2. 在组件定义一个 echarts 实例和数据 ``` <template> <div ref="chart" style="height: 500px;"></div> </template> <script> export default { data() { return { chart: null, // echarts 实例 treeData: [], // 树图数据 }; }, mounted() { this.initChart(); this.getTreeData(); }, methods: { initChart() { this.chart = echarts.init(this.$refs.chart); // 配置 echarts 树图的 option const option = { series: [ { type: 'tree', data: [], // 点击节点时触发 emphasis: { focus: 'descendant', }, // 其他配置... }, ], }; this.chart.setOption(option); }, getTreeData() { // 调用接口获取树图数据 api.getTreeData().then((res) => { this.treeData = res.data; // 更新 echarts 树图数据 this.chart.setOption({ series: [ { data: [this.treeData], }, ], }); }); }, }, }; </script> ``` 3. 在点击节点时调用接口获取下一节点数据,并更新 echarts 树图数据 ``` <template> <div ref="chart" style="height: 500px;"></div> </template> <script> export default { data() { return { chart: null, // echarts 实例 treeData: [], // 树图数据 }; }, mounted() { this.initChart(); this.getTreeData(); }, methods: { initChart() { this.chart = echarts.init(this.$refs.chart); // 配置 echarts 树图的 option const option = { series: [ { type: 'tree', data: [], // 点击节点时触发 emphasis: { focus: 'descendant', }, // 其他配置... }, ], }; this.chart.setOption(option); // 监听 echarts 树图的点击事件 this.chart.on('click', this.handleNodeClick); }, getTreeData(id) { // 调用接口获取树图数据 api.getTreeData(id).then((res) => { this.treeData = res.data; // 更新 echarts 树图数据 this.chart.setOption({ series: [ { data: [this.treeData], }, ], }); }); }, handleNodeClick(params) { const { id } = params.data; this.getTreeData(id); }, }, }; </script> ``` 4. 完整代码示例 ``` <template> <div ref="chart" style="height: 500px;"></div> </template> <script> import echarts from 'echarts/lib/echarts'; import 'echarts/lib/chart/tree'; import 'echarts/lib/component/tooltip'; import 'echarts/lib/component/title'; export default { data() { return { chart: null, // echarts 实例 treeData: [], // 树图数据 }; }, mounted() { this.initChart(); this.getTreeData(); }, methods: { initChart() { this.chart = echarts.init(this.$refs.chart); // 配置 echarts 树图的 option const option = { series: [ { type: 'tree', data: [], // 点击节点时触发 emphasis: { focus: 'descendant', }, // 其他配置... }, ], }; this.chart.setOption(option); // 监听 echarts 树图的点击事件 this.chart.on('click', this.handleNodeClick); }, getTreeData(id) { // 调用接口获取树图数据 api.getTreeData(id).then((res) => { this.treeData = res.data; // 更新 echarts 树图数据 this.chart.setOption({ series: [ { data: [this.treeData], }, ], }); }); }, handleNodeClick(params) { const { id } = params.data; this.getTreeData(id); }, }, }; </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值