|
@@ -1,49 +1,60 @@
|
|
|
import React, { Component } from "react";
|
|
|
import ReactWordCloud from "react-wordcloud";
|
|
|
|
|
|
-class WordCloud extends Component {
|
|
|
- constructor(props) {
|
|
|
- super(props);
|
|
|
- this.state = {
|
|
|
- words: props.words ?? [
|
|
|
- { text: "Apparel", value: 1000 },
|
|
|
- { text: "Fashion", value: 800 },
|
|
|
- { text: "Style", value: 700 },
|
|
|
- { text: "Trend", value: 600 },
|
|
|
- { text: "Wear", value: 500 },
|
|
|
- { text: "Outfit", value: 400 },
|
|
|
- { text: "Clothing", value: 300 },
|
|
|
- { text: "Textile", value: 200 },
|
|
|
- ],
|
|
|
- options: props.options ?? {
|
|
|
- // deterministic: true,
|
|
|
- spiral: "archimedean",
|
|
|
- fontFamily: "sans-serif",
|
|
|
- fontSizes: [30, 80], // 字体范围
|
|
|
- // deterministic: true, // 尝试保持一致
|
|
|
- // enableOptimizations: true, // 优化性能,避免重新布局
|
|
|
- rotations: 0, // 不旋转
|
|
|
- // rotationAngles: [0, 90], // 旋转角度范围
|
|
|
- scale: "sqrt", // 字体缩放方式:linear、sqrt、log
|
|
|
- // spiral: "rectangular", // 词云排列方式:archimedean 或 rectangular
|
|
|
- padding: 20, // 单词之间的间距
|
|
|
- colors: ["#fff"],
|
|
|
- // colors: [
|
|
|
- // "#f87171",
|
|
|
- // "#fb923c",
|
|
|
- // "#facc15",
|
|
|
- // "#4ade80",
|
|
|
- // "#38bdf8",
|
|
|
- // "#a78bfa",
|
|
|
- // "#f472b6",
|
|
|
- // ], // 多彩渐变
|
|
|
- enableTooltip: false, // 鼠标悬停提示
|
|
|
- },
|
|
|
- };
|
|
|
+// 工具函数:生成渐变颜色
|
|
|
+function generateGradientColors(startColor, endColor, steps) {
|
|
|
+ function hexToRgb(hex) {
|
|
|
+ const r = parseInt(hex.slice(1, 3), 16);
|
|
|
+ const g = parseInt(hex.slice(3, 5), 16);
|
|
|
+ const b = parseInt(hex.slice(5, 7), 16);
|
|
|
+ return [r, g, b];
|
|
|
+ }
|
|
|
+
|
|
|
+ function rgbToHex([r, g, b]) {
|
|
|
+ return (
|
|
|
+ "#" +
|
|
|
+ r.toString(16).padStart(2, "0") +
|
|
|
+ g.toString(16).padStart(2, "0") +
|
|
|
+ b.toString(16).padStart(2, "0")
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ const [r1, g1, b1] = hexToRgb(startColor);
|
|
|
+ const [r2, g2, b2] = hexToRgb(endColor);
|
|
|
+
|
|
|
+ const colors = [];
|
|
|
+ for (let i = 0; i < steps; i++) {
|
|
|
+ const t = i / (steps - 1);
|
|
|
+ const r = Math.round(r1 + (r2 - r1) * t);
|
|
|
+ const g = Math.round(g1 + (g2 - g1) * t);
|
|
|
+ const b = Math.round(b1 + (b2 - b1) * t);
|
|
|
+ colors.push(rgbToHex([r, g, b]));
|
|
|
}
|
|
|
+ return colors;
|
|
|
+}
|
|
|
|
|
|
+class WordCloud extends Component {
|
|
|
render() {
|
|
|
- const { words, options } = this.state;
|
|
|
+ const { data } = this.props;
|
|
|
+ // 根据索引生成 words,索引越小,value 越大
|
|
|
+ const words = data.map((item, idx) => ({
|
|
|
+ text: item,
|
|
|
+ value: data.length - idx // 越小索引值越大
|
|
|
+ }));
|
|
|
+
|
|
|
+ const colors = generateGradientColors("#7ECEF4", "#4032A6", words.length);
|
|
|
+
|
|
|
+ const options = {
|
|
|
+ spiral: "archimedean",
|
|
|
+ fontFamily: "sans-serif",
|
|
|
+ fontSizes: [20, 80], // 控制字体范围
|
|
|
+ fontWeight: 'bold',
|
|
|
+ rotations: 0,
|
|
|
+ scale: "sqrt",
|
|
|
+ padding: 10,
|
|
|
+ colors,
|
|
|
+ enableTooltip: false,
|
|
|
+ };
|
|
|
|
|
|
return (
|
|
|
<div style={{ width: "100%", height: "100%" }}>
|