A reusable, SVG-based word cloud component for Angular. Renders weighted text items in a spiral or rectangular layout with collision detection. Supports multiple color modes using MJ design tokens, optional entry animation, and click/hover interaction.
npm install @memberjunction/ng-word-cloud
The component is standalone and can be imported directly:
import { MJWordCloudComponent } from '@memberjunction/ng-word-cloud';
@Component({
imports: [MJWordCloudComponent],
template: `
<mj-word-cloud
[Items]="Words"
[MaxFontSize]="56"
ColorMode="categorical"
(ItemClick)="OnWordClick($event)">
</mj-word-cloud>
`
})
export class MyComponent {
Words: WordCloudItem[] = [
{ Text: 'Machine Learning', Weight: 1.0 },
{ Text: 'Data Science', Weight: 0.8, Category: 'analytics' },
{ Text: 'Neural Networks', Weight: 0.6, Category: 'ai' },
];
OnWordClick(event: WordCloudItemEvent): void {
console.log('Clicked:', event.Item.Text);
}
}
mj-word-cloud
| Input | Type | Default | Description |
|---|---|---|---|
Items |
WordCloudItem[] |
[] |
Data items to display in the cloud |
MinFontSize |
number |
12 |
Minimum font size in pixels for the lowest-weighted item |
MaxFontSize |
number |
48 |
Maximum font size in pixels for the highest-weighted item |
Layout |
'spiral' | 'rectangular' |
'spiral' |
Layout algorithm for word placement |
ColorMode |
'brand' | 'categorical' | 'weight-gradient' |
'brand' |
How colors are assigned to words (see Color Modes below) |
Interactive |
boolean |
true |
Whether words are clickable and hoverable |
MaxItems |
number |
100 |
Maximum number of items to display (heaviest items are kept) |
Animate |
boolean |
true |
Whether to animate words in with a staggered fade |
| Output | Type | Description |
|---|---|---|
ItemClick |
EventEmitter<WordCloudItemEvent> |
Emitted when a word is clicked |
ItemHover |
EventEmitter<WordCloudItemEvent> |
Emitted when the pointer enters a word |
ItemLeave |
EventEmitter<WordCloudItemEvent> |
Emitted when the pointer leaves a word |
interface WordCloudItem {
Text: string; // Display text
Weight: number; // Importance (0.0 - 1.0), determines font size
Category?: string; // Optional category for color grouping
Metadata?: Record<string, unknown>; // Optional metadata passed through events
}
interface WordCloudItemEvent {
Item: WordCloudItem; // The item that was interacted with
Event: MouseEvent; // The original DOM event
}
brandAll words use --mj-brand-primary. Opacity varies with weight (range 0.4--1.0), giving higher-weighted words more visual prominence.
categoricalWords with the same Category share a color from a fixed palette of MJ design tokens: --mj-brand-primary, --mj-status-success, --mj-status-warning, --mj-status-error, --mj-status-info, and others. Words without a Category each get their own color.
weight-gradientInterpolates between --mj-text-muted (low weight) and --mj-brand-primary (high weight) using color-mix(). Provides a smooth gradient that adapts to both light and dark themes.
The component renders entirely in SVG using an <svg> element with a computed viewBox and preserveAspectRatio="xMidYMid meet" so the cloud scales to fill its container.
MaxItems.MinFontSize and MaxFontSize based on weight.viewBox is computed to fit all placed items with 20px padding.Text width is estimated using an average character width ratio of 0.6x the font size (no DOM measurement required), making the layout engine fully deterministic and SSR-compatible.
All colors use MJ semantic design tokens exclusively. No hardcoded hex values are used in the component's styles or color logic. The component adapts automatically to light and dark themes via the --mj-* CSS custom properties.
Tokens used:
--mj-brand-primary, --mj-brand-primary-hover -- brand color mode--mj-status-success, --mj-status-warning, --mj-status-error, --mj-status-info -- categorical palette--mj-text-muted -- weight-gradient low end--mj-font-family -- inherited font familyThe component uses ChangeDetectionStrategy.OnPush for performance. The layout is recomputed whenever any @Input changes (via ngOnChanges).