wordCloud.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  2. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  3. return new (P || (P = Promise))(function (resolve, reject) {
  4. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  5. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  6. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  7. step((generator = generator.apply(thisArg, _arguments || [])).next());
  8. });
  9. };
  10. var __rest = (this && this.__rest) || function (s, e) {
  11. var t = {};
  12. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
  13. t[p] = s[p];
  14. if (s != null && typeof Object.getOwnPropertySymbols === "function")
  15. for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  16. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
  17. t[p[i]] = s[p[i]];
  18. }
  19. return t;
  20. };
  21. import { min, max } from 'd3-array';
  22. import { tagCloud } from './utils/d3-cloud';
  23. import { flow } from './utils/flow';
  24. const DEFAULT_OPTIONS = {
  25. size: [500, 500],
  26. fontSize: [14, 28],
  27. };
  28. export function processImageMask(img) {
  29. return new Promise((res, rej) => {
  30. if (img instanceof HTMLImageElement) {
  31. res(img);
  32. return;
  33. }
  34. if (typeof img === 'string') {
  35. const image = new Image();
  36. image.crossOrigin = 'anonymous';
  37. image.src = img;
  38. image.onload = () => res(image);
  39. image.onerror = () => {
  40. console.error(`'image ${img} load failed !!!'`);
  41. rej();
  42. };
  43. return;
  44. }
  45. rej();
  46. });
  47. }
  48. export function normalizeFontSize(fontSize, range) {
  49. if (typeof fontSize === 'function')
  50. return fontSize;
  51. if (Array.isArray(fontSize)) {
  52. const [fMin, fMax] = fontSize;
  53. if (!range)
  54. return () => (fMax + fMin) / 2;
  55. const [min, max] = range;
  56. if (max === min)
  57. return () => (fMax + fMin) / 2;
  58. return ({ value }) => ((fMax - fMin) / (max - min)) * (value - min) + fMin;
  59. }
  60. return () => fontSize;
  61. }
  62. export const WordCloud = (options) => {
  63. return (data) => __awaiter(void 0, void 0, void 0, function* () {
  64. const cloudOptions = Object.assign({}, DEFAULT_OPTIONS, options);
  65. const layout = tagCloud();
  66. yield flow(layout, cloudOptions)
  67. .set('fontSize', (v) => {
  68. const arr = data.map((d) => d.value);
  69. return normalizeFontSize(v, [min(arr), max(arr)]);
  70. })
  71. .set('font')
  72. .set('fontStyle')
  73. .set('fontWeight')
  74. .set('padding')
  75. .set('rotate')
  76. .set('size')
  77. .set('spiral')
  78. .set('timeInterval')
  79. .set('random')
  80. .set('text')
  81. .set('on')
  82. .setAsync('imageMask', processImageMask, layout.createMask);
  83. layout.words([...data]);
  84. const result = layout.start();
  85. const [cw, ch] = cloudOptions.size;
  86. const defaultBounds = [
  87. { x: 0, y: 0 },
  88. { x: cw, y: ch },
  89. ];
  90. const { _bounds: bounds = defaultBounds, _tags, hasImage } = result;
  91. const tags = _tags.map((_a) => {
  92. var { x, y } = _a, rest = __rest(_a, ["x", "y"]);
  93. return (Object.assign(Object.assign({}, rest), { x: x + cw / 2, y: y + ch / 2 }));
  94. });
  95. // Append two data to replace the corner of top-left and bottom-right, avoid calculate the actual bounds will occur some error.
  96. const [{ x: tlx, y: tly }, { x: brx, y: bry }] = bounds;
  97. const invisibleText = { text: '', value: 0, opacity: 0, fontSize: 0 };
  98. tags.push(Object.assign(Object.assign({}, invisibleText), { x: hasImage ? 0 : tlx, y: hasImage ? 0 : tly }), Object.assign(Object.assign({}, invisibleText), { x: hasImage ? cw : brx, y: hasImage ? ch : bry }));
  99. return tags;
  100. });
  101. };
  102. WordCloud.props = {};
  103. //# sourceMappingURL=wordCloud.js.map