legacyCharts.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435
  1. /**
  2. * v2.9.2 **SVG 图表渲染器**的 Vue 侧接入(P21)。
  3. *
  4. * 旧页在 `app.js:523` 建同一份上下文:
  5. * `const CH = window.__charts({ t, fmt, esc, dv, EN, displayText, verb })`
  6. * 之后用 `CH.rateChart / quadChart / ramChart / waterfall / attribBar` 出 **内联 SVG**。
  7. * 这里保持**同一份 charts.js 原文**(`lib/legacy/charts.js`)与**同一组入参**,
  8. * 使 Vue 版图表与 v2.9.2 逐像素同源(不再用 ECharts 画这几张)。
  9. */
  10. import './legacy/charts.js'
  11. import { t } from '../i18n/zh'
  12. import { fmt } from './render'
  13. import { dv, esc } from './vib'
  14. type ChartFactory = (ctx: Record<string, unknown>) => Record<string, (...a: any[]) => string>
  15. const G: any = (typeof window !== 'undefined') ? window : globalThis
  16. const factory = G.__charts
  17. let VERB = 0
  18. const CH: Record<string, (...a: any[]) => string> = factory
  19. ? factory({ t, fmt, esc, dv, EN: false, displayText: (x: unknown) => (x == null ? '' : String(x)), verb: () => { VERB++ } })
  20. : {}
  21. /** 取一张旧图(返回 SVG 字符串)。缺函数时返回空串,页面自行降级。 */
  22. export function chart(name: 'rateChart' | 'quadChart' | 'ramChart' | 'waterfall' | 'attribBar', ...args: any[]): string {
  23. const fn = CH[name]
  24. if (typeof fn !== 'function') return ''
  25. try { return fn(...args) } catch (e) { return '' }
  26. }
  27. /** 旧页的 `<div class="panel span2"><h3>…</h3>{SVG}<p class="small muted">…</p></div>` 包装。 */
  28. export function chartPanel(title: string, svg: string, note = ''): string {
  29. return `<div class="panel span2"><h3>${title}</h3>${svg}` +
  30. (note ? `<p class="small muted" style="margin-top:6px">${note}</p>` : '') + '</div>'
  31. }