本文主要是介绍echarts制作词云图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
词云图
由于echarts官网没有词云图的配置,网上太多也不全,这里写一份关于词云图配置的文章,包括自定义图形,透明度等
文章目录
- 词云图
- 一、用到的网站
- 二、全部代码
- 三、结果展示
一、用到的网站
1、阿里巴巴图标库
2、图片转base64网站
3、echarts-wordcloud.min.js文件
4、echarts.min.js文件
二、全部代码
如果要使用自定义图案需要注意:
1、创建图片:var maskImage = new Image();
2、设置图片地址:maskImage.src = url;
3、载入配置:maskImage: maskImage;
4、待图片加载好以后再配置option:maskImage.onload
<!DOCTYPE html>
<html><head><meta charset="utf-8"><title></title><script src="js/echarts.min.js"></script><script src="./js/echarts-wordcloud.min.js"></script><style>#main {width: 700px;height: 500px;box-shadow: 0 0 15px 3px aqua;margin: 100px auto 50px;border-radius: 15px;}</style></head><body><div id="main"></div></body><script>var myChart = echarts.init(document.getElementById('main'));// 数据源var keyWords = [{name: '腾越街道',value: 1,},{name: '西源街道',value: 11,},{name: '固东镇',value: 12,},{name: '滇滩镇',value: 12,},{name: '猴桥镇',value: 18,},{name: '和顺镇',value: 13,},{name: '界头镇',value: 14,},{name: '曲石镇',value: 21,},{name: '明光镇',value: 18,},{name: '中和镇',value: 22,},{name: '芒棒镇',value: 26,},{name: '荷花镇',value: 24,},{name: '北海镇',value: 15,},{name: '清水镇',value: 18,},{name: '明光镇',value: 18,},{name: '中和镇',value: 22,},{name: '芒果镇',value: 26,},{name: '荷花镇',value: 24,},{name: '北海镇',value: 15,},{name: '清水镇',value: 18,},{name: '马站乡',value: 15,},{name: '五合乡',value: 10,},{name: '新华乡',value: 17,},{name: '蒲川乡',value: 15,},{name: '团田乡',value: 16,},{name: '北海乡',value: 1,},{name: '腾越镇',value: 6,},{name: '清水乡',value: 7,},{name: '马站乡',value: 15,},{name: '五合乡',value: 10,},{name: '新华乡',value: 17,},{name: '蒲川乡',value: 15,},{name: '团田乡',value: 16,},{name: '北海乡',value: 1,},{name: '腾越镇',value: 6,},{name: '清水乡',value: 7,}]// 求出最大值用来计算透明度var max = 0// 方法一for (let item of keyWords) {if(item.value > max){max = item.value}}// 方法二,利用map方法// max = Math.max(...keyWords.map(item=>item.value))// 自定义图片var maskImage = new Image();// 图片在iconfont上找,图片的黑色区域表示被填充maskImage.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAAAXNSR0IArs4c6QAAFBtJREFUeF7tnQn0ddUYxp8kQ0KpDGWKBpKZQqaEkpRKiKSIBoooypQSSoNkKJEiSaNWZJZUCMmwIlaGkgxFZsm8fjk397vfHfbeZ9rnnOdd66z7/9a39z7vfvZ5ztnDOywjixEwAjMRWMbYGAEjMBsBE8RPhxGYg4AJ4sfDCJggfgaMQBoC/oKk4eZaA0HABBnIQLubaQiYIGm4udZAEDBBBjLQ7mYaAiZIGm6uNRAETJCBDLS7mYaACZKGm2sNBAETZCAD7W6mIWCCpOHmWgNBwAQZyEC7m2kImCBpuLnWQBAwQQYy0O5mGgImSBpurjUQBEyQgQy0u5mGgAmShptrDQQBE2QgA+1upiFggqTh5loDQcAEGchAu5tpCJggabi51kAQMEEGMtDuZhoCJkgabq41EARMkIEMtLuZhoAJkoabaw0EARNkIAPtbqYhYIKk4eZaA0HABBnIQLubaQiYIGm4udZAEDBBBjLQ7mYaAiZIGm6uNRAETJCBDLS7mYaACZKGm2sNBAETZCAD7W6mIWCCpOHmWgNBwAQZyEBn1M2NJe0p6XaFTp+V9E1J/JaVt0haXdKdJZ0s6TRJfynTqAlSBj3XjUXgfZJ2nlHpw5KeF9vgWPlTJW07Uf8KSVtJ+nZquyZIKnKuF4vAmyW9ZkGlAyS9MbZhSR+aQ66PS9oioc0bq5ggccgtK2mV4lpV0l/HruvH/v5HXLO9L/10SR8L7OVTJH06sCzF9pZ06ILyfFlOj2jzpqImyGzU1i/ePI+XBBkgxh0CQb5c0jnFvPpCSX8KrNfHYmB3rqT1Ajt3sKT9Ass+WdJ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zO32AffugMIpBBk1C22gfGQW7Hhfh4raX9JHCxZjECtCJQhCIqtV5CkiagoRLvjq1GZrX+tyLrxXiBQliAjEDDr2ELSlpJwAKpK/lUk0yS/INd1VTXsdoxACAJVEWT8Xvh+EzSNK/W0FjP1j5gUIUPoMnUiUAdBxvUlksjaYxeur6N/XyXpZ5Imfy/0l6LOIXfbMQjUTZAYXVzWCGSHgAmS3ZBYoZwQMEFyGg3rkh0CJkh2Q2KFckLABMlpNKxLdgiYINkNiRXKCQETJKfRsC7ZIWCCZDckVignBEyQnEbDumSHgAmS3ZBYoZwQMEFyGg3rkh0CJkh2Q2KFckLABMlpNKxLdgiYINkNiRXKCQETJKfRsC7ZIWCCZDckVignBEyQnEbDumSHgAmS3ZBYoZwQMEFyGg3rkh0CJkh2Q2KFckLABMlpNKxLdgiYINkNiRXKCQETJKfRsC7ZIWCCZDckVignBEyQnEbDumSHgAmS3ZBYoZwQMEFyGg3rkh0CJkh2Q2KFckLABMlpNKxLdgiYINkNiRXKCQETJKfRsC7ZIWCCZDckVignBEyQnEbDumSHgAmS3ZBYoZwQMEFyGg3rkh0CJkh2Q2KFckLABMlpNKxLdgiYINkNiRXKCYH/AgR3uRD99Qa9AAAAAElFTkSuQmCC"var option = {backgroundColor: '#fff',tooltip: {show: true},series: [{type: 'wordCloud',// 单词间隔的大小gridSize: 0, // 字体大小范围sizeRange: [8, 38],// 文字旋转角度范围rotationRange: [-45, 45],// 自定义词云图形状maskImage: maskImage,// 形状有(circle)圆形(默认)、(cardioid)心形,(diamond)菱形,// (triangle-forward)三角形向前,(triangle)三角形,(pentagon)五边形和(star)星形。*/shape: 'star',textStyle: {color: function(e) {var a = [Math.round(Math.random() * 255),Math.round(Math.random() * 255),Math.round(Math.random() * 255),(e.data.value / max) < 0.3 ? 0.3 : e.data.value]return 'rgba(' + a.join(',') + ')'}},//设置显示区域的位置以及大小left: 'center',top: 'center',width: '100%',height: '100%',// 源数据data: keyWords,// 鼠标放上样式emphasis: {textStyle: {fontWeight: 'bold',color: '#ff0000',}}}]}// myChart.setOption(option)// 图片加载好以后再设置配置maskImage.onload = function() {myChart.setOption(option)}</script>
</html>
三、结果展示
这篇关于echarts制作词云图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!