Echarts工具栏(自定义按钮)

本文介绍了如何在Echarts中添加自定义工具栏按钮,重点在于SVG图标的应用和配置,通过修改`myEcharts.js`,设置CSS及HTML元素,实现Echarts图表上的自定义操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

myEcharts.js

var myChart = echarts.init(document.getElementById('main'));
var option = {
    title : {
        text: '某地区蒸发量和降水量',
        subtext: '纯属虚构'
    },
    tooltip : {
        trigger: 'axis'
    },
    legend: {
        data:['蒸发量','降水量']
    },
    toolbox: {
        padding:30,
        show : true,
        feature : {
            mark : {show: true},
            dataView : {show: true, readOnly: false},
            magicType : {show: true, type: ['line', 'bar']},
            restore : {show: true},
            saveAsImage : {show: true},
            //敲黑板,重点!!!
            myTool2:{
  
  //自定义按钮 danielinbiti,这里增加,selfbuttons可以随便取名字    
                   show:true,//是否显示    
                   title:'查看全部', //鼠标移动上去显示的文字    
                   icon:'path://M525.4 721.2H330.9c-9 0-18.5-7.7-18.5-18.1V311c0-9 9.3-18.1 18.5-18.1h336.6c9.3 0 18.5 9.1 18.5 18.1v232.7c0 6 8.8 12.1 15 12.1 6.2 0 15-6 15-12.1V311c0-25.6-25.3-48.9-50.1-48.9h-335c-26.2 0-50.1 23.3-50.1 48.9v389.1c0 36.3 20 51.5 50.1 51.5h197.6c6.2 0 9.3-7.5 9.3-15.1 0-6-6.2-15.3-12.4-15.3zM378.8 580.6c-6.2 0-12.3 8.6-12.3 14.6s6.2 14.6 12.3 14.6h141.4c6.2 0 12.3-5.8 12.3-13.4 0.3-9.5-6.2-15.9-12.3-15.9H378.8z m251.6-91.2c0-6-6.2-14.6-12.3-14.6H375.7c-6.2 0-12.4 8.6-12.4 14.6s6.2 14.6 12.4 14.6h240.8c6.2 0.1 13.9-8.5 13.9-14.6z m-9.2-120.5H378.8c-6.2 0-12.3 8.6-12.3 14.6s6.2 14.6 12.3 14.6h240.8c7.7 0 13.9-8.6 13.9-14.6s-6.2-14.6-12.3-14.6z m119.4 376.6L709 714.1c9.2-12 14.6-27 14.6-43.2 0-39.4-32.1-71.4-71.8-71.4-39.7 0-71.8 32-71.8 71.4s32.1 71.4 71.8 71.4c16.3 0 31.3-5.4 43.4-14.5l31.6 31.5c3.8 3.8 10 3.8 13.8 0 3.8-3.8 3.8-10 0-13.8z m-88.8-23.6c-28.3 0-51.3-22.8-51.3-51s23-51 51.3-51c28.3 0 51.3 22.8 51.3 51s-23 51-51.3 51z', //图标    
                  onclick:function() {
  
  //点击事件,这里的option1是chart的option信息    
                           $('.big').css('display','block');
                         }    
                   }
        }
    },
    calculable : true,
    xAxis : [
        {
            type : 'category',
            data : ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']
        }
   
### 实现 ECharts 自定义按钮ECharts 中,可以利用 `toolbox` 组件来自定义工具栏中的按钮。为了向 Toolbox 添加自定义按钮,需修改 `feature` 属性下的配置项。 #### 配置自定义按钮属性 通过指定 `name`, `title`, 和 `icon` 来创建一个新的按钮对象,并将其加入到 `toolbox.feature` 的数组中[^1]: ```javascript option = { toolbox: { feature: { customButton: { show: true, title: "Custom Action", icon: 'path://M497.8 ...', // 或者使用 image://URL 形式的图标路径 onclick: function () { alert('You clicked the custom button!'); } }, saveAsImage: {} } } }; ``` 对于希望使用图像作为图标的场景,应当采用 `'image://http://example.com/icon.png'` 这样的形式来设定 `icon` 值[^3]。 #### 动态响应交互行为 如果需要让新添加的按钮执行特定逻辑操作,则可以在对应的回调函数内编写相应代码片段。比如,在点击时触发某些数据处理流程或是页面跳转等动作[^2]。 #### 完整实例展示 下面给出一段完整的 JavaScript 代码用于初始化带有自定义按钮的图表实例: ```javascript var chartDom = document.getElementById('main'); var myChart = echarts.init(chartDom); var option; // 初始化选项设置 option = { tooltip: {}, xAxis: { data: ["shirt", "cardign", "chiffon shirt", "pants", "heels", "socks"] }, yAxis: {}, series: [{ name: '销量', type: 'bar', data: [5, 20, 36, 10, 10, 20] }], toolbox: { feature: { customAction: { show: true, title: 'Execute Custom Function', icon: 'image://https://www.example.com/custom-icon.png', onclick: function() { console.log('Executing custom action...'); // 执行任何想要的操作... } }, restore: {}, // 恢复默认布局 saveAsImage: {} // 另存为图片文件 } } }; if (option && typeof option === 'object') { myChart.setOption(option); } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值