koishi-plugin-chat-analyse 1.6.0 → 1.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +22 -14
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -1766,18 +1766,30 @@ var Renderer = class {
|
|
|
1766
1766
|
async *renderWordCloud(data, config) {
|
|
1767
1767
|
const { title, time, words } = data;
|
|
1768
1768
|
if (!words?.length) return;
|
|
1769
|
-
const wordsJson = JSON.stringify(words);
|
|
1770
1769
|
const weights = words.map((w) => w[1]);
|
|
1771
1770
|
const maxWeight = Math.max(...weights, 1);
|
|
1772
1771
|
const minWeight = Math.max(Math.min(...weights), 1);
|
|
1773
|
-
const wordCount = words.length;
|
|
1774
|
-
let calculatedMaxFontSize = Math.round(2560 / Math.pow(wordCount, 0.66));
|
|
1775
|
-
let calculatedMinFontSize = Math.round(calculatedMaxFontSize / 6);
|
|
1776
|
-
const maxFontSize = Math.max(4, Math.min(128, calculatedMaxFontSize));
|
|
1777
|
-
const minFontSize = Math.max(4, Math.min(maxFontSize, calculatedMinFontSize));
|
|
1778
1772
|
const logMaxWeight = Math.log1p(maxWeight);
|
|
1779
1773
|
const logMinWeight = Math.log1p(minWeight);
|
|
1780
|
-
const logWeightRange = logMaxWeight - logMinWeight
|
|
1774
|
+
const logWeightRange = logMaxWeight - logMinWeight;
|
|
1775
|
+
const getRelativeFontSize = /* @__PURE__ */ __name((size) => {
|
|
1776
|
+
if (logWeightRange <= 0) return 1;
|
|
1777
|
+
const normalizedWeight = (Math.log1p(size) - logMinWeight) / logWeightRange;
|
|
1778
|
+
return 0.05 + 0.95 * normalizedWeight;
|
|
1779
|
+
}, "getRelativeFontSize");
|
|
1780
|
+
let estimatedCurrentArea = 0;
|
|
1781
|
+
const relativeFontSizes = words.map((word) => getRelativeFontSize(word[1]));
|
|
1782
|
+
for (let i = 0; i < words.length; i++) {
|
|
1783
|
+
const wordText = words[i][0];
|
|
1784
|
+
const relativeSize = relativeFontSizes[i];
|
|
1785
|
+
estimatedCurrentArea += Math.pow(relativeSize, 2) * wordText.length * 0.6;
|
|
1786
|
+
}
|
|
1787
|
+
const scalingFactor = Math.sqrt(600 * 600 * 0.5 / Math.max(1, estimatedCurrentArea));
|
|
1788
|
+
const wordList = words.map((word, i) => {
|
|
1789
|
+
let finalSize = relativeFontSizes[i] * scalingFactor;
|
|
1790
|
+
finalSize = Math.max(4, Math.min(128, finalSize));
|
|
1791
|
+
return [word[0], finalSize];
|
|
1792
|
+
});
|
|
1781
1793
|
const cardHtml = `
|
|
1782
1794
|
<div class="container" style="width: 600px;">
|
|
1783
1795
|
<div class="header">
|
|
@@ -1792,19 +1804,15 @@ var Renderer = class {
|
|
|
1792
1804
|
<script>
|
|
1793
1805
|
const canvas = document.getElementById('wordcloud-container');
|
|
1794
1806
|
const options = {
|
|
1795
|
-
list: ${wordsJson},
|
|
1796
1807
|
fontFamily: ${JSON.stringify(config.fontFamily)},
|
|
1797
|
-
weightFactor: (size) => {
|
|
1798
|
-
if (${maxWeight} === ${minWeight}) return (${minFontSize} + ${maxFontSize}) / 2;
|
|
1799
|
-
const normalizedWeight = (Math.log1p(size) - ${logMinWeight}) / (${logWeightRange});
|
|
1800
|
-
return ${minFontSize} + normalizedWeight * (${maxFontSize} - ${minFontSize});
|
|
1801
|
-
},
|
|
1802
1808
|
color: ${JSON.stringify(config.color)},
|
|
1803
1809
|
shape: ${JSON.stringify(config.shape)},
|
|
1810
|
+
rotationSteps: ${config.rotationSteps},
|
|
1804
1811
|
ellipticity: ${config.ellipticity},
|
|
1805
1812
|
minRotation: ${config.minRotation},
|
|
1806
1813
|
maxRotation: ${config.maxRotation},
|
|
1807
|
-
|
|
1814
|
+
list: ${JSON.stringify(wordList)},
|
|
1815
|
+
weightFactor: (size) => size,
|
|
1808
1816
|
backgroundColor: 'transparent',
|
|
1809
1817
|
drawOutOfBound: true,
|
|
1810
1818
|
clearCanvas: false,
|