houguyin-components.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // 江中猴菇饮 · VOC 报告共享 UI 组件
  2. // 所有子章渲染器通用:VOC 证据卡 / 理论盒 / 决策项 / Tag 云 / 统计条
  3. //
  4. // 设计:不依赖外部图表库,纯 CSS + SVG 实现轻量可视化;
  5. // 所有 pattern-curated 样本自动加「◈ 模式归纳」徽章以保证透明度。
  6. const BRAND = {
  7. JIANG_GREEN: '#2D9C5D',
  8. HOUGU_TAN: '#E8A87C',
  9. DEEP_AMBER: '#C96E3F',
  10. CREAM: '#F5E6D3',
  11. TECH_BLUE: '#0085CA',
  12. WARM_ORANGE: '#FF8C42',
  13. CORAL: '#FF6B6B',
  14. SAGE: '#87A96B',
  15. PURPLE: '#8B5CF6',
  16. ROSE: '#FF4D8D',
  17. };
  18. const PLATFORM_LABELS = {
  19. xhs: { name: '小红书', color: '#FF2442', short: '红' },
  20. douyin: { name: '抖音', color: '#1A1A1A', short: '抖' },
  21. taobao: { name: '淘宝', color: '#FF5000', short: '淘' },
  22. jd: { name: '京东', color: '#E1251B', short: '京' },
  23. zhihu: { name: '知乎', color: '#0084FF', short: '知' },
  24. dingxiang: { name: '丁香医生', color: '#00B368', short: '丁' },
  25. tmall: { name: '天猫', color: '#FF0036', short: '猫' },
  26. unknown: { name: '其他', color: '#888', short: '—' },
  27. };
  28. const SENTIMENT_LABELS = {
  29. positive: { label: '好评', color: '#2D9C5D', icon: '▲' },
  30. neutral: { label: '中性', color: '#888', icon: '●' },
  31. negative: { label: '差评', color: '#FF6B6B', icon: '▼' },
  32. conflicted:{ label: '矛盾', color: '#FF8C42', icon: '◆' },
  33. };
  34. function esc(s) {
  35. return String(s ?? '').replace(/[&<>"']/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' }[c]));
  36. }
  37. function fmtLikes(n) {
  38. if (n == null) return '0';
  39. if (n >= 10000) return (n / 10000).toFixed(1).replace(/\.0$/, '') + 'w';
  40. if (n >= 1000) return (n / 1000).toFixed(1).replace(/\.0$/, '') + 'k';
  41. return String(n);
  42. }
  43. // ================================================================
  44. // VOC 证据卡(单条)· 核心组件
  45. // ================================================================
  46. function renderVocCard(item, opts = {}) {
  47. const { accent = BRAND.HOUGU_TAN, showBasis = false, compact = false } = opts;
  48. const pf = PLATFORM_LABELS[item.platform] || PLATFORM_LABELS.unknown;
  49. const sen = SENTIMENT_LABELS[item.sentiment] || SENTIMENT_LABELS.neutral;
  50. const isSeed = (item.source || '').includes('pattern') || (item.source || '').includes('seed');
  51. const tags = Array.isArray(item.tags) ? item.tags.slice(0, 3) : [];
  52. return `<div style="background:var(--bg-1); border:1px solid var(--border); border-left:3px solid ${accent}; border-radius:var(--radius-sm); padding:${compact ? '12px 14px' : '16px 18px'}; display:flex; flex-direction:column; gap:${compact ? '8px' : '10px'};">
  53. <div style="display:flex; justify-content:space-between; align-items:center; gap:8px; flex-wrap:wrap;">
  54. <div style="display:flex; align-items:center; gap:6px;">
  55. <span style="display:inline-flex; align-items:center; justify-content:center; width:22px; height:22px; border-radius:50%; background:${pf.color}22; color:${pf.color}; font-size:0.62rem; font-weight:700;">${pf.short}</span>
  56. <span style="color:var(--text-1); font-size:0.78rem; font-weight:600;">${esc(item.nickname || '匿名')}</span>
  57. <span style="color:var(--text-3); font-size:0.68rem;">· ${esc(item.ip || '—')}</span>
  58. </div>
  59. <div style="display:flex; align-items:center; gap:6px;">
  60. <span style="color:${sen.color}; font-size:0.7rem; font-weight:600;">${sen.icon} ${sen.label}</span>
  61. <span style="color:var(--text-3); font-size:0.68rem; font-family:var(--font-mono);">♥${fmtLikes(item.likes)}</span>
  62. ${isSeed ? `<span style="padding:1px 6px; background:${BRAND.HOUGU_TAN}18; color:${BRAND.HOUGU_TAN}; border-radius:10px; font-size:0.6rem; font-family:var(--font-mono); font-weight:700;" title="${esc(item.basis || '公开讨论模式归纳')}">◈模式</span>` : `<span style="padding:1px 6px; background:${BRAND.JIANG_GREEN}18; color:${BRAND.JIANG_GREEN}; border-radius:10px; font-size:0.6rem; font-family:var(--font-mono); font-weight:700;">✓真实</span>`}
  63. </div>
  64. </div>
  65. <div style="color:var(--text-1); font-size:${compact ? '0.86rem' : '0.92rem'}; line-height:1.62;">"${esc(item.content || '')}"</div>
  66. ${tags.length ? `<div style="display:flex; flex-wrap:wrap; gap:4px;">
  67. ${tags.map((t) => `<span style="padding:2px 8px; background:${accent}12; color:${accent}; border-radius:3px; font-size:0.68rem; font-weight:600;">${esc(t)}</span>`).join('')}
  68. </div>` : ''}
  69. ${showBasis && item.basis ? `<div style="color:var(--text-3); font-size:0.66rem; font-family:var(--font-mono); letter-spacing:0.05em; padding-top:6px; border-top:1px dashed var(--border);">依据:${esc(item.basis)}</div>` : ''}
  70. </div>`;
  71. }
  72. // ================================================================
  73. // VOC 网格(多条)
  74. // ================================================================
  75. function renderVocGrid(items, opts = {}) {
  76. const { columns = 2, accent = BRAND.HOUGU_TAN, compact = false } = opts;
  77. return `<div style="display:grid; grid-template-columns:repeat(${columns}, 1fr); gap:14px;">
  78. ${items.map((it) => renderVocCard(it, { accent, compact })).join('')}
  79. </div>`;
  80. }
  81. // ================================================================
  82. // Insight Hero · 大洞察摘要条
  83. // ================================================================
  84. function renderInsightHero({ eyebrow, title, subtitle, color = BRAND.HOUGU_TAN }) {
  85. return `<div class="rise-in" style="padding:28px 32px; background:linear-gradient(135deg, ${color}12, ${color}04); border-left:4px solid ${color}; border-radius:var(--radius-md); margin-bottom:32px;">
  86. ${eyebrow ? `<div class="text-mono text-xs" style="color:${color}; letter-spacing:0.18em; margin-bottom:10px; font-weight:700;">${esc(eyebrow)}</div>` : ''}
  87. <div style="color:var(--text-1); font-size:1.18rem; line-height:1.72; font-weight:600;">${title}</div>
  88. ${subtitle ? `<div style="color:var(--text-2); font-size:0.92rem; line-height:1.7; margin-top:10px;">${subtitle}</div>` : ''}
  89. </div>`;
  90. }
  91. // ================================================================
  92. // 决策项列表(Takeaways)
  93. // ================================================================
  94. function renderDecisionList(items, opts = {}) {
  95. const { accent = BRAND.JIANG_GREEN, title = 'DECISION · 落地决策项', columns = 2 } = opts;
  96. return `<div class="fade-up" style="margin-top:24px; padding:22px 26px; background:${accent}08; border:1px solid ${accent}33; border-radius:var(--radius-md);">
  97. <div class="text-mono text-xs" style="color:${accent}; letter-spacing:0.2em; margin-bottom:14px; font-weight:700;">${esc(title)}</div>
  98. <div style="display:grid; grid-template-columns:repeat(${columns}, 1fr); gap:14px;">
  99. ${items.map((it, i) => `
  100. <div style="display:flex; gap:10px; align-items:flex-start;">
  101. <div style="flex-shrink:0; width:22px; height:22px; border-radius:50%; background:${accent}; color:#fff; font-size:0.72rem; font-weight:700; display:flex; align-items:center; justify-content:center;">${i + 1}</div>
  102. <div style="color:var(--text-1); font-size:0.9rem; line-height:1.64;">${it}</div>
  103. </div>`).join('')}
  104. </div>
  105. </div>`;
  106. }
  107. // ================================================================
  108. // 理论映射盒(KANO/JTBD/场景三元素)
  109. // ================================================================
  110. function renderTheoryBox({ theory, mapping, accent = BRAND.PURPLE }) {
  111. return `<div class="fade-up" style="margin-top:24px; padding:22px 26px; background:linear-gradient(135deg, ${accent}0E, transparent); border:1px solid ${accent}30; border-radius:var(--radius-md);">
  112. <div class="text-mono text-xs" style="color:${accent}; letter-spacing:0.2em; margin-bottom:12px; font-weight:700;">THEORY · ${esc(theory)}</div>
  113. <div style="color:var(--text-1); font-size:0.95rem; line-height:1.72;">${mapping}</div>
  114. </div>`;
  115. }
  116. // ================================================================
  117. // Tag 频次云(用于某假设的主题分布)
  118. // ================================================================
  119. function renderTagCloud(tagGroups, opts = {}) {
  120. const { accent = BRAND.HOUGU_TAN, top = 15, title = '主题分布' } = opts;
  121. const list = tagGroups.slice(0, top);
  122. if (!list.length) return '';
  123. const maxCount = Math.max(...list.map((g) => g.count));
  124. return `<div style="padding:20px 22px; background:var(--bg-1); border:1px solid var(--border); border-radius:var(--radius-md);">
  125. <div class="text-mono text-xs" style="color:${accent}; letter-spacing:0.15em; margin-bottom:14px; font-weight:700;">${esc(title)} · TOP ${list.length}</div>
  126. <div style="display:flex; flex-wrap:wrap; gap:8px; align-items:center;">
  127. ${list.map((g) => {
  128. const ratio = g.count / maxCount;
  129. const fs = (0.72 + ratio * 0.45).toFixed(2);
  130. const bg = Math.round(10 + ratio * 30);
  131. return `<span style="padding:6px 12px; background:${accent}${String(bg).padStart(2, '0')}; color:${accent}; border-radius:16px; font-size:${fs}rem; font-weight:${Math.round(500 + ratio * 300)}; letter-spacing:0.02em;">${esc(g.tag)} <span style="opacity:0.7; font-size:0.8em; font-family:var(--font-mono);">×${g.count}</span></span>`;
  132. }).join('')}
  133. </div>
  134. </div>`;
  135. }
  136. // ================================================================
  137. // 数据摘要条(某假设下的统计总览)
  138. // ================================================================
  139. function renderStatStrip(stats, opts = {}) {
  140. const { accent = BRAND.HOUGU_TAN } = opts;
  141. return `<div style="display:grid; grid-template-columns:repeat(${stats.length}, 1fr); gap:12px; padding:18px 22px; background:var(--bg-1); border:1px solid var(--border); border-radius:var(--radius-md);">
  142. ${stats.map((s) => `
  143. <div style="text-align:center;">
  144. <div style="font-family:var(--font-head); font-size:1.8rem; font-weight:800; color:${s.color || accent}; letter-spacing:-0.02em;">${s.num}</div>
  145. <div style="color:var(--text-3); font-size:0.72rem; font-family:var(--font-mono); letter-spacing:0.1em; margin-top:4px;">${esc(s.label)}</div>
  146. ${s.delta ? `<div style="color:${s.deltaColor || accent}; font-size:0.7rem; margin-top:2px;">${esc(s.delta)}</div>` : ''}
  147. </div>`).join('')}
  148. </div>`;
  149. }
  150. // ================================================================
  151. // 水平条形对比图(SVG,适用于多类别对比)
  152. // ================================================================
  153. function renderBarChart(rows, opts = {}) {
  154. const { accent = BRAND.HOUGU_TAN, height = 260, title = '' } = opts;
  155. const max = Math.max(...rows.map((r) => r.value), 1);
  156. const barH = 28;
  157. const gap = 10;
  158. const padL = 160;
  159. const padR = 60;
  160. const padV = 16;
  161. const totalH = rows.length * (barH + gap) + padV * 2;
  162. const W = 900;
  163. return `<div style="padding:18px 22px; background:var(--bg-1); border:1px solid var(--border); border-radius:var(--radius-md);">
  164. ${title ? `<div class="text-mono text-xs" style="color:${accent}; letter-spacing:0.15em; margin-bottom:12px; font-weight:700;">${esc(title)}</div>` : ''}
  165. <svg viewBox="0 0 ${W} ${totalH}" style="width:100%; height:auto; max-height:${height}px;" preserveAspectRatio="xMidYMid meet">
  166. ${rows.map((r, i) => {
  167. const y = padV + i * (barH + gap);
  168. const w = (r.value / max) * (W - padL - padR);
  169. const color = r.color || accent;
  170. return `
  171. <text x="${padL - 10}" y="${y + barH / 2 + 5}" text-anchor="end" font-size="13" font-weight="600" fill="var(--text-1, #333)">${esc(r.label)}</text>
  172. <rect x="${padL}" y="${y}" width="${w}" height="${barH}" rx="4" fill="${color}" opacity="0.85"/>
  173. <text x="${padL + w + 8}" y="${y + barH / 2 + 5}" font-size="13" font-weight="700" fill="${color}" font-family="monospace">${esc(r.valueLabel || r.value)}</text>
  174. `;
  175. }).join('')}
  176. </svg>
  177. </div>`;
  178. }
  179. // ================================================================
  180. // 2×2 矩阵图(四象限,用于未满足需求地图等)
  181. // ================================================================
  182. function renderMatrix2x2({ xLabel, yLabel, xLow, xHigh, yLow, yHigh, quadrants, accent = BRAND.HOUGU_TAN }) {
  183. // quadrants: { q1: {title, color, items, note}, q2: ..., q3: ..., q4: ... }
  184. // q1=右上, q2=左上, q3=左下, q4=右下
  185. const W = 760, H = 520, pad = 60;
  186. const cx = W / 2, cy = H / 2;
  187. const quadStyle = (q) => ({
  188. color: q?.color || accent,
  189. title: q?.title || '',
  190. note: q?.note || '',
  191. items: q?.items || [],
  192. });
  193. const q1 = quadStyle(quadrants.q1);
  194. const q2 = quadStyle(quadrants.q2);
  195. const q3 = quadStyle(quadrants.q3);
  196. const q4 = quadStyle(quadrants.q4);
  197. const renderQuad = (q, x, y) => `
  198. <div style="padding:16px; background:${q.color}08; border:1px solid ${q.color}40; border-radius:6px; height:100%; display:flex; flex-direction:column;">
  199. <div class="text-mono text-xs" style="color:${q.color}; letter-spacing:0.15em; margin-bottom:8px; font-weight:700;">${esc(q.title)}</div>
  200. <ul style="color:var(--text-1); font-size:0.82rem; line-height:1.55; margin:0; padding-left:18px; list-style:disc;">
  201. ${q.items.map((it) => `<li style="margin-bottom:4px;">${it}</li>`).join('')}
  202. </ul>
  203. ${q.note ? `<div style="margin-top:auto; padding-top:10px; color:${q.color}; font-size:0.74rem; font-style:italic;">${q.note}</div>` : ''}
  204. </div>`;
  205. return `<div style="padding:20px 26px; background:var(--bg-1); border:1px solid var(--border); border-radius:var(--radius-md);">
  206. <div style="display:grid; grid-template-columns:52px 1fr 1fr; grid-template-rows:auto 1fr 1fr 40px; gap:12px; height:520px;">
  207. <div style="grid-row:2 / span 2; grid-column:1; display:flex; align-items:center; justify-content:center; transform:rotate(-90deg); white-space:nowrap;">
  208. <span class="text-mono text-xs" style="color:${accent}; letter-spacing:0.2em; font-weight:700;">${esc(yLabel)}</span>
  209. </div>
  210. <div style="grid-row:1; grid-column:2; text-align:center;"><span class="text-mono text-xs" style="color:${q2.color}; font-weight:700;">${esc(yHigh)}</span></div>
  211. <div style="grid-row:1; grid-column:3; text-align:center;"><span class="text-mono text-xs" style="color:${q1.color}; font-weight:700;">${esc(yHigh)}</span></div>
  212. <div style="grid-row:2; grid-column:2;">${renderQuad(q2, 0, 0)}</div>
  213. <div style="grid-row:2; grid-column:3;">${renderQuad(q1, 1, 0)}</div>
  214. <div style="grid-row:3; grid-column:2;">${renderQuad(q3, 0, 1)}</div>
  215. <div style="grid-row:3; grid-column:3;">${renderQuad(q4, 1, 1)}</div>
  216. <div style="grid-row:4; grid-column:2; text-align:center;"><span class="text-mono text-xs" style="color:var(--text-3); letter-spacing:0.15em;">${esc(xLow)} ← ${esc(xLabel)} → ${esc(xHigh)}</span></div>
  217. <div style="grid-row:4; grid-column:3;"></div>
  218. </div>
  219. </div>`;
  220. }
  221. // ================================================================
  222. // 章节 section head(标题区通用)
  223. // ================================================================
  224. function renderSectionHead({ eyebrow, title, subtitle, color = BRAND.HOUGU_TAN }) {
  225. return `<div class="section-head rise-in">
  226. <div class="section-eyebrow" style="color:${color};">${esc(eyebrow)}</div>
  227. <h2 class="text-h1"><span class="headline-anim">${title}</span></h2>
  228. ${subtitle ? `<p class="section-sub">${subtitle}</p>` : ''}
  229. </div>`;
  230. }
  231. module.exports = {
  232. BRAND,
  233. PLATFORM_LABELS,
  234. SENTIMENT_LABELS,
  235. esc,
  236. fmtLikes,
  237. renderVocCard,
  238. renderVocGrid,
  239. renderInsightHero,
  240. renderDecisionList,
  241. renderTheoryBox,
  242. renderTagCloud,
  243. renderStatStrip,
  244. renderBarChart,
  245. renderMatrix2x2,
  246. renderSectionHead,
  247. };