wordCloud.js 4.5 KB

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