Skip to content

Ft wordcloud perf #206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vue-data-ui",
"private": false,
"version": "2.10.1",
"version": "2.10.2-beta.2",
"type": "module",
"description": "A user-empowering data visualization Vue 3 components library for eloquent data storytelling",
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ const components = ref([ //------|
* Modify the index to display a component
* [0] = VueUiXy
*/
const selectedComponent = ref(components.value[54]);
const selectedComponent = ref(components.value[37]);

/**
* Legacy testing arena where some non chart components can be tested
Expand Down
18 changes: 14 additions & 4 deletions src/components/vue-ui-word-cloud.vue
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ function generateWordCloud() {
words: scaledWords,
svg: svg.value,
proximity: FINAL_CONFIG.value.style.chart.words.proximity,
strictPixelPadding: FINAL_CONFIG.value.strictPixelPadding
});
}

Expand Down Expand Up @@ -499,18 +500,27 @@ function useTooltip(word) {
<g
:transform="`translate(${(svg.width <= 0 ? 10 : svg.width) / 2}, ${(svg.height <= 0 ? 10 : svg.height) / 2})`">
<g v-for="(word, index) in positionedWords">
<text
<rect
v-if="word.minX !== undefined"
data-cy="datapoint-word"
:x="word.x + word.minX"
:y="word.y + (word.minY * 1.25)"
:width="word.maxX - word.minX"
:height="word.maxY - word.minY"
fill="transparent"
pointer-events="visiblePainted"
@mouseover="useTooltip(word)"
@mouseleave="selectedWord = null; isTooltip = false"
/>
<text
:fill="word.color"
:font-weight="FINAL_CONFIG.style.chart.words.bold ? 'bold' : 'normal'" :key="index"
:x="word.x" :y="word.y" :font-size="word.fontSize"
:transform="`translate(${word.width / 2}, ${word.height / 2})`"
:class="{'animated': FINAL_CONFIG.useCssAnimation, 'word-selected': selectedWord && selectedWord === word.id && mutableConfig.showTooltip, 'word-not-selected': selectedWord && selectedWord !== word.id && mutableConfig.showTooltip }"
text-anchor="middle"
dominant-baseline="central"
@mouseover="useTooltip(word)"
@mouseleave="selectedWord = null; isTooltip = false"
:style="`animation-delay:${index * FINAL_CONFIG.animationDelayMs}ms !important;`"
:style="`animation-delay:${index * FINAL_CONFIG.animationDelayMs}ms !important; pointer-events:none;`"
>
{{ word.name }}
</text>
Expand Down
1 change: 1 addition & 0 deletions src/useConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -3645,6 +3645,7 @@ export function useConfig() {
customPalette: [],
useCssAnimation: true,
animationDelayMs: 20,
strictPixelPadding: false, // If true, strict per-pixel padding is used (dilateWordMask); if false, just rectangular bounding box (or pad).
userOptions: USER_OPTIONS({
tooltip: true,
pdf: true,
Expand Down
41 changes: 34 additions & 7 deletions src/wordcloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@
* - position words inside a given area using a spiral placement algorithm (the actual function of this file used in the component @generateWordCloud)
*/

function getTightBoundingBox(canvas, ctx) {
const { width, height } = canvas;
const img = ctx.getImageData(0, 0, width, height);
const data = img.data;
let minX = width, minY = height, maxX = 0, maxY = 0;
let found = false;
for (let y = 0; y < height; y += 1) {
for (let x = 0; x < width; x += 1) {
const alpha = data[(y * width + x) * 4 + 3];
if (alpha > 1) {
found = true;
if (x < minX) minX = x;
if (x > maxX) maxX = x;
if (y < minY) minY = y;
if (y > maxY) maxY = y;
}
}
}
if (!found) return [0, 0, 0, 0];
return [minX, minY, maxX, maxY];
}

/**
* Generates a bitmap and mask representing a word, given font size and padding.
* @param {Object} params
Expand Down Expand Up @@ -52,9 +74,11 @@ export function getWordBitmap({
if (data[(y * textW + x) * 4 + 3] > 1) wordMask.push([x, y]);
}
}

const [minX, minY, maxX, maxY] = getTightBoundingBox(canvas, ctx);
ctx.restore();

return { w: textW, h: textH, wordMask };
return { w: textW, h: textH, wordMask, minX, minY, maxX, maxY };
}

/**
Expand Down Expand Up @@ -135,13 +159,14 @@ export function positionWords({
words,
proximity = 0,
svg,
strictPixelPadding
}) {
const { width, height } = svg;
const maskW = Math.round(width);
const maskH = Math.round(height);
const minFontSize = 1;
const configMinFontSize = svg.minFontSize;
const maxFontSize = svg.maxFontSize;
const maxFontSize = Math.min(svg.maxFontSize, 100);
const values = words.map(w => w.value);
const minValue = Math.min(...values);
const maxValue = Math.max(...values);
Expand Down Expand Up @@ -169,7 +194,7 @@ export function positionWords({
let fontSize = targetFontSize;

while (!placed && fontSize >= minFontSize) {
let { w, h, wordMask } = getWordBitmap({
let { w, h, wordMask, minX, minY, maxX, maxY } = getWordBitmap({
word: wordRaw,
fontSize,
pad: proximity,
Expand All @@ -178,7 +203,9 @@ export function positionWords({
svg
});

wordMask = dilateWordMask({ wordMask, w, h, dilation: 2 });
if (strictPixelPadding) {
wordMask = dilateWordMask({ wordMask, w, h, dilation: 1 });
}

let r = 0;
let attempts = 0;
Expand All @@ -190,7 +217,7 @@ export function positionWords({
const py = Math.round(cy + r * Math.sin(theta * Math.PI / 180) - h / 2);
if (px < 0 || py < 0 || px + w > maskW || py + h > maskH) continue;
if (canPlaceAt({ mask, maskW, maskH, wx:px, wy:py, wordMask})) {
positionedWords.push({ ...wordRaw, x: px - maskW / 2, y: py - maskH / 2, fontSize, width: w, height: h, angle: 0 });
positionedWords.push({ ...wordRaw, x: px - maskW / 2, y: py - maskH / 2, fontSize, width: w, height: h, angle: 0, minX, minY, maxX, maxY });
markMask({ mask, maskW, maskH, wx: px, wy: py, wordMask });
placed = true;
break;
Expand All @@ -203,7 +230,7 @@ export function positionWords({

if (!placed && fontSize < minFontSize) {
fontSize = minFontSize;
const { w, h, wordMask } = getWordBitmap({
const { w, h, wordMask, minX, minY, maxX, maxY } = getWordBitmap({
word: wordRaw,
fontSize,
pad: proximity,
Expand All @@ -222,7 +249,7 @@ export function positionWords({
const py = Math.round(cy + r * Math.sin(theta * Math.PI / 180) - h / 2);
if (px < 0 || py < 0 || px + w > maskW || py + h > maskH) continue;
if (canPlaceAt({ mask, maskW, maskH, wx: px, wy: py, wordMask })) {
positionedWords.push({ ...wordRaw, x: px - maskW / 2, y: py - maskH / 2, fontSize, width: w, height: h, angle: 0 });
positionedWords.push({ ...wordRaw, x: px - maskW / 2, y: py - maskH / 2, fontSize, width: w, height: h, angle: 0, minX, minY, maxX, maxY });
markMask({ mask, maskW, maskH, wx: px, wy: py, wordMask });
placed = true;
break;
Expand Down
34 changes: 0 additions & 34 deletions tests/wordcloud.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,6 @@ describe("getWordBitmap", () => {
});
expect(ctx.font.startsWith("bold ")).toBe(true);
});

test("pads the word bitmap correctly", () => {
const canvas = createMockCanvas();
const ctx = createMockContext2D();
const word = { name: "pad" };
const fontSize = 12;
const pad = 5;
const svg = { style: {} };
const res1 = getWordBitmap({ word, fontSize, pad: 0, canvas, ctx, svg });
const res2 = getWordBitmap({ word, fontSize, pad, canvas, ctx, svg });
expect(res2.w).toBeGreaterThan(res1.w);
expect(res2.h).toBeGreaterThan(res1.h);
});
});

describe("canPlaceAt", () => {
Expand Down Expand Up @@ -339,25 +326,4 @@ describe("positionWords", () => {
expect(result[0].fontSize).toBe(svg.minFontSize);
expect(result[1].fontSize).toBe(svg.minFontSize);
});

test("proximity increases bounding box size", () => {
const words = [
{ name: "A", value: 15 },
{ name: "B", value: 10 },
];
const svg = {
width: 100,
height: 100,
minFontSize: 10,
maxFontSize: 20,
style: {},
};
const result1 = positionWords({ words, svg, proximity: 0 });
const result2 = positionWords({ words, svg, proximity: 10 });

expect(result2[0].width).toBeGreaterThan(result1[0].width);
expect(result2[1].width).toBeGreaterThan(result1[1].width);
expect(result2[0].height).toBeGreaterThan(result1[0].height);
expect(result2[1].height).toBeGreaterThan(result1[1].height);
});
});
1 change: 1 addition & 0 deletions types/vue-data-ui.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5480,6 +5480,7 @@ declare module "vue-data-ui" {
userOptions?: ChartUserOptions;
useCssAnimation?: boolean;
animationDelayMs?: number;
strictPixelPadding?: boolean;
style?: {
fontFamily?: string;
chart?: {
Expand Down