您现在的位置是:首页 >技术杂谈 >[echarts] legend icon 自定义的几种方式网站首页技术杂谈
[echarts] legend icon 自定义的几种方式
简介[echarts] legend icon 自定义的几种方式
一、默认 图例项的 icon
circle, rect, roundRect, triangle, diamond, pin, arrow, none
legend: {
top: '5%',
left: 'center',
itemWidth: 20,
itemHeight: 20,
data: [
{icon: 'circle', name: '搜索引擎'},
{icon: 'rect', name: '直接访问'},
{icon: 'roundRect', name: '邮件营销'},
{icon: 'triangle', name: '联盟广告'},
{icon: 'diamond', name: '视频广告'},
{icon: 'pin', name: 'SEO'},
{icon: 'arrow', name: '定点投放'}
]
}
分别对应以下几种图标
二、使用图片链接或者base64 图片
图片使用前缀为 image://
示例如下:
legend: {
top: '5%',
left: 'center',
itemWidth: 30,
itemHeight: 30,
data: [
{icon: 'image://https://profile.csdnimg.cn/F/0/1/3_rudy_zhou', name: '搜索引擎'},
{icon: 'image://data:image/jpg;base64,iVBORw0KGgoAAAANSUhEUgAAAEsAAABLCAYAAAA4TnrqAAAI2klEQVR4nO2bf2hUVxbHP29mkkwy+Wkm2fywxtRutia7htSa7dYatbBChbBQ7CrL2haCiLTUQqFS2G0Xygp1QSwtVQoWbCtbxK6lloqEyiolWcnqrtVU13Q1MT9M0sT8/p2Zt3+cdzNJ3ozus+9lUnhfuJDMnHfvfZ/ce+65595ogI6r/0ueeHfgxyQXlgW5sCzIhWVBLiwLcmFZkAvLglxYFuTCsiAXlgW5sCzIhWVBLiwL8sW7A/Olzfp5saVDFgUsDRniPqNoCKhpo4RZHODiDksDEoAUIBXwI52aBsaBEaNMEX9gcYflA9KAbKOkI/CmgEGgFxl1g8Zn8VRcYXmQkbQEKAAeAKq2bOGhqir++/XXnD12DD8yoqaAEDIl4yk9XsUHeh7ovwT996CfevVVfbZOv/aavt34Ps+wj2d/lS+Ni/xAPlACrAL26eau7PF4uKTrNAEdiB+Ll+IaZ3kRYKlAtqZFtQl6vaQZdt6F61pULTgszSg+BEAKEEAcezSlGTZ+IIn4AlsQB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rh7XswOOX4/S6VuVPY0FwkuVSRekJXFQ5s2UVhVRbCqCq/Ph8fvJzwxQXh6mt66OjrOnaPp1Cnae3vpRKZ2JzKa+pCI3WlQ6l0W5DKbSjmnEbn7oEqm8XkAWUk9RE51xogk/HqRnUGP8fu9Tobs1oLe/Jt9L0vdyUolAioJZk6ndWQbNI5kSIcRnzRk/KxOhBby2mJcrkmqqam2N+qiWiLme/AqOzpulHhexI3rnVLVAQ8Cz2MU1anZycTF8I8DcYf1Y5L7j04W5MKyIBeWBbmwLMiFZUEuLAtyYVmQC8uCXFgW5MKyIBeWBbmwLMiFZUEuLAtyYVmQC8uCXFgW9D8oBE7AzUnPcAAAAABJRU5ErkJggg==', name: '定点投放'}
]
},
效果如下
在这里插入图片描述
这种方式,能自定义任意UI设计的图标,但是不支持动态置灰,无法跟随默认图表数据颜色
三、使用 矢量 路径
推荐使用方式
我们最常见到的 svg 图片 里面的形状就是使用 矢量路径绘制
echarts 使用 矢量路径 写法前缀为:path://
legend: {
top: '5%',
left: 'center',
itemWidth: 20,
itemHeight: 20,
icon: 'path://M78.625,93.094L66.494,72.081l12.132-21.013h24.264l12.132,21.013l-12.132,21.013H78.625z M95.547,80.377l4.79-8.296 l-4.79-8.296h-9.579l-4.79,8.296l4.79,8.296H95.547z'
},
点击图例 取消显示 置灰效果也能应用置 图标上,这是 使用 图片链接或者base64 达不到的效果
简单来讲就是,使用 矢量路径, 显示效果与默认的几个图标一样,只是图标形状变成了自定义。
矢量路径也支持 复合路径,比如:
legend: {
top: '5%',
left: 'center',
itemWidth: 20,
itemHeight: 20,
data: [
{icon: 'path://M78.625,93.094L66.4....', name: '搜索引擎'},
{icon: 'path://M153.651,224.468c-21....z', name: '直接访问'}
]
},
四、不仅仅是 legend.icon 支持矢量路径
折线 symbol 也支持,点击查看Demo
当然不仅仅只是这些,handle.icon (滚动条)、toolbox.icon (工具图标) 等都支持,大家使用到时,就可以用上面的方式使用矢量路径了。
参考:
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。