Skip to content

Commit 96ed573

Browse files
committed
Good version 0.1
1 parent bd92927 commit 96ed573

File tree

1 file changed

+39
-19
lines changed

1 file changed

+39
-19
lines changed

src/plugin-word-cloud/component.tsx

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -100,27 +100,15 @@ React.ReactElement<PluginWordCloudProps> {
100100
// --- D3 Word Cloud Logic ---
101101

102102
// Define min/max font sizes
103-
const minFontSize = 12;
104-
const maxFontSize = 48;
103+
const minFontSize = 24;
104+
const maxFontSize = 98;
105105
// Color parameters removed - will use d3.schemeCategory10
106106

107107
// Calculate min and max counts for normalization (still needed for font size)
108108
const counts = Object.values(wordCounts);
109109
const minCount = counts.length > 0 ? Math.min(...counts) : 1;
110110
const maxCount = counts.length > 0 ? Math.max(...counts) : 1;
111111

112-
// Helper function to calculate font size based on count
113-
const calculateFontSize = (count: number): number => {
114-
if (maxCount === minCount) {
115-
return (minFontSize + maxFontSize) / 2; // Average size if all counts are the same
116-
}
117-
// Linear interpolation
118-
const size = minFontSize + ((count - minCount) / (maxCount - minCount)) * (maxFontSize - minFontSize);
119-
return Math.max(minFontSize, Math.min(size, maxFontSize)); // Clamp within bounds
120-
};
121-
122-
// Removed the HSL-based calculateColor function
123-
124112
// Define a categorical color scale using a bright scheme suitable for dark backgrounds
125113
const colorScale = d3.scaleOrdinal(d3.schemeCategory10);
126114

@@ -199,22 +187,54 @@ React.ReactElement<PluginWordCloudProps> {
199187
return;
200188
}
201189

190+
// --- Dynamic Font Size Calculation ---
191+
const numUniqueWords = Object.keys(wordCounts).length;
192+
const wordCountThreshold = 20; // Threshold for adjusting min font size
193+
let effectiveMinFontSize = minFontSize; // Start with the default min size
194+
195+
if (numUniqueWords > 0 && numUniqueWords < wordCountThreshold) {
196+
// If fewer words than threshold, increase the minimum size
197+
// Interpolate between minFontSize and a midpoint based on how few words there are
198+
const midFontSize = (minFontSize + maxFontSize) / 2;
199+
// boostFactor goes from 0 (at threshold-1 words) to 1 (at 1 word)
200+
const boostFactor = (wordCountThreshold - numUniqueWords) / (wordCountThreshold - 1);
201+
effectiveMinFontSize = minFontSize + (midFontSize - minFontSize) * boostFactor;
202+
pluginLogger.debug(`Adjusted min font size to ${effectiveMinFontSize.toFixed(2)} for ${numUniqueWords} words.`);
203+
}
204+
205+
// Calculate min/max counts for normalization (needed for font size scaling)
206+
const counts = Object.values(wordCounts);
207+
const minCount = counts.length > 0 ? Math.min(...counts) : 1;
208+
const maxCount = counts.length > 0 ? Math.max(...counts) : 1;
209+
210+
// Define font size calculation logic *inside* the effect to use effectiveMinFontSize
211+
const calculateDynamicFontSize = (count: number): number => {
212+
if (maxCount === minCount) {
213+
// If all counts are the same, use an average of the effective min and max
214+
return (effectiveMinFontSize + maxFontSize) / 2;
215+
}
216+
// Linear interpolation between effectiveMinFontSize and maxFontSize
217+
const size = effectiveMinFontSize + ((count - minCount) / (maxCount - minCount)) * (maxFontSize - effectiveMinFontSize);
218+
// Clamp within the dynamic bounds (effectiveMinFontSize to maxFontSize)
219+
return Math.max(effectiveMinFontSize, Math.min(size, maxFontSize));
220+
};
221+
202222
// Prepare data for d3-cloud, explicitly typing as WordData[]
203223
const wordsData: WordData[] = Object.entries(wordCounts).map(([text, count]) => ({
204224
text,
205-
size: calculateFontSize(count), // Use existing font size calculation
206-
count, // Keep original count for color calculation
225+
size: calculateDynamicFontSize(count), // Use the new dynamic font size calculation
226+
count, // Keep original count if needed elsewhere
207227
// Initialize d3-cloud properties (optional, layout calculates them)
208228
// x: 0, y: 0, rotate: 0,
209229
}));
210230

211231
const layout = cloud()
212232
.size([layoutWidth, layoutHeight]) // Use calculated layout size
213233
.words(wordsData)
214-
.padding(5) // Padding between words
234+
.padding(2) // Reduced padding for closer words
215235
//.rotate(() => ~~(Math.random() * 2) * 90) // Rotate words 0 or 90 degrees randomly
216236
.rotate(() => (~~(Math.random() * 6) - 3) * 30) // More varied rotation like original example
217-
.font('Impact') // Example font, choose one appropriate
237+
.font('Tahoma') // Example font, choose one appropriate
218238
.fontSize((d: cloud.Word) => d.size || 12) // Added explicit type cloud.Word for 'd'
219239
.on('end', (drawnWords: WordData[]) => draw(drawnWords, width, height, margin)); // Pass dimensions and margin to draw
220240

@@ -298,6 +318,6 @@ React.ReactElement<PluginWordCloudProps> {
298318
}}
299319
/>
300320
);
301-
}
321+
} // <-- Add missing closing brace for the PluginWordCloud function
302322

303323
export default PluginWordCloud;

0 commit comments

Comments
 (0)