最近在react项目中遇到词云图,在echarts中关于词云图的demo较少,以下为实现代码:
import React, { Component } from "react";
import ReactEcharts from "echarts-for-react";
import { Row, Col } from "antd";
import * as echarts from "echarts";
import "echarts-wordcloud";
export default class Wordcloud extends Component {
wordOption = () => {
let maskImage = new Image();
// 此为词云图呈现形状的图片base64码,可选,可以自定义图片
maskImage.src =
"data:image/svg+xml;utf8;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iaXNvLTg4NTktMSI/Pgo8IS0tIEdlbmVyYXRvcjogQWRvYmUgSWxsdXN0cmF0b3IgMTYuMC4wLCBTVkcgRXhwb3J0IFBsdWctSW4gLiBTVkcgVmVyc2lvbjogNi4wMCBCdWlsZCAwKSAgLS0+CjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy8xLjEvRFREL3N2ZzExLmR0ZCI+CjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgdmVyc2lvbj0iMS4xIiBpZD0iQ2FwYV8xIiB4PSIwcHgiIHk9IjBweCIgd2lkdGg9IjI1NnB4IiBoZWlnaHQ9IjI1NnB4IiB2aWV3Qm94PSIwIDAgNTQ4LjE3NiA1NDguMTc2IiBzdHlsZT0iZW5hYmxlLWJhY2tncm91bmQ6bmV3IDAgMCA1NDguMTc2IDU0OC4xNzY7IiB4bWw6c3BhY2U9InByZXNlcnZlIj4KPGc+Cgk8cGF0aCBkPSJNNTI0LjE4MywyOTcuMDY1Yy0xNS45ODUtMTkuODkzLTM2LjI2NS0zMi42OTEtNjAuODE1LTM4LjM5OWM3LjgxLTExLjk5MywxMS43MDQtMjUuMTI2LDExLjcwNC0zOS4zOTkgICBjMC0yMC4xNzctNy4xMzktMzcuNDAxLTIxLjQwOS01MS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",
value: 6181
},
{
name: "Amy Schumer",
value: 4386
},
{
name: "Jurassic World",
value: 4055
},
{
name: "Charter Communications",
value: 2467
},
{
name: "Chick Fil A",
value: 2244
},
{
name: "Planet Fitness",
value: 1898
},
{
name: "Pitch Perfect",
value: 1484
},
{
name: "Express",
value: 1112
},
{
name: "Home",
value: 965
},
{
name: "Johnny Depp",
value: 847
},
{
name: "Lena Dunham",
value: 582
},
{
name: "Lewis Hamilton",
value: 555
},
{
name: "KXAN",
value: 550
},
{
name: "Point Break",
value: 265
}
];
let option = {
backgroundColor: "#fff",
tooltip: {
pointFormat: "{series.name}: <b>{point.percentage:.1f}%</b>"
},
series: [
{
type: "wordCloud",
gridSize: 1,
// Text size range which the value in data will be mapped to.
// Default to have minimum 12px and maximum 60px size.
sizeRange: [12, 55],
// Text rotation range and step in degree. Text will be rotated randomly in range [-90, 90] by rotationStep 45
rotationRange: [-45, 0, 45, 90], // 词语呈现的角度,翻转的角度
maskImage: maskImage, // 呈现形状图片, 可选
textStyle: {
normal: { // 词语呈现的颜色
color: function() {
return (
"rgb(" +
Math.round(Math.random() * 255) +
", " +
Math.round(Math.random() * 255) +
", " +
Math.round(Math.random() * 255) +
")"
);
}
}
},
// Folllowing left/top/width/height/right/bottom are used for positioning the word cloud
// Default to be put in the center and has 75% x 80% size.
left: "center",
top: "center",
right: null,
bottom: null,
width: "90%",
height: "110%",
data: wordData
}
]
};
return option;
};
render() {
return (
<div style={{ marginTop: "50px", width: "82%" }}>
<Row>
<Col md={24}>
<ReactEcharts
// style={{ "56%" }}
option={this.wordOption()}
theme="ThemeStyle"
/>
</Col>
</Row>
</div>
);
}
}