index.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import React, { Component } from "react";
  2. import ReactWordCloud from "react-wordcloud";
  3. class WordCloud extends Component {
  4. constructor(props) {
  5. super(props);
  6. this.state = {
  7. words: props.words ?? [
  8. { text: "Apparel", value: 1000 },
  9. { text: "Fashion", value: 800 },
  10. { text: "Style", value: 700 },
  11. { text: "Trend", value: 600 },
  12. { text: "Wear", value: 500 },
  13. { text: "Outfit", value: 400 },
  14. { text: "Clothing", value: 300 },
  15. { text: "Textile", value: 200 },
  16. ],
  17. options: props.options ?? {
  18. // deterministic: true,
  19. spiral: "archimedean",
  20. fontFamily: "sans-serif",
  21. fontSizes: [30, 80], // 字体范围
  22. // deterministic: true, // 尝试保持一致
  23. // enableOptimizations: true, // 优化性能,避免重新布局
  24. rotations: 0, // 不旋转
  25. // rotationAngles: [0, 90], // 旋转角度范围
  26. scale: "sqrt", // 字体缩放方式:linear、sqrt、log
  27. // spiral: "rectangular", // 词云排列方式:archimedean 或 rectangular
  28. padding: 20, // 单词之间的间距
  29. colors: ["#fff"],
  30. // colors: [
  31. // "#f87171",
  32. // "#fb923c",
  33. // "#facc15",
  34. // "#4ade80",
  35. // "#38bdf8",
  36. // "#a78bfa",
  37. // "#f472b6",
  38. // ], // 多彩渐变
  39. enableTooltip: false, // 鼠标悬停提示
  40. },
  41. };
  42. }
  43. render() {
  44. const { words, options } = this.state;
  45. return (
  46. <div style={{ width: "100%", height: "100%" }}>
  47. <ReactWordCloud words={words} options={options} />
  48. </div>
  49. );
  50. }
  51. }
  52. export default WordCloud;