@datafe-open/markdown-chart-kpi

中文文档
0.4.1发布于 2026/10/31MITTypeScriptESM + CJS构建来源已验证近 30 天下载 3732

Strict framework-neutral KPI card renderer for Markdown Chart

原始使用文档

@datafe-open/markdown-chart-kpi

Strict, framework-neutral KPI card renderer for @datafe-open/markdown-chart.

Metrics share a single outer chart frame with whitespace between items, plain status text, and a compact Chart/Data toolbar. Explicit format prefixes and suffixes use smaller type without changing their text or spaces. Line and area sparklines stay 44px high; metrics without trends need no reserved trend space.

import { ChartRendererRegistry } from '@datafe-open/markdown-chart';
import { createKpiRenderer } from '@datafe-open/markdown-chart-kpi';

const registry = new ChartRendererRegistry().register(createKpiRenderer());

The canonical fence keeps default or named datasets separate from renderer-owned field, format, trend, status, and reference configuration:

```markdown-chart
{
  "version": 1,
  "renderer": "kpi",
  "data": {
    "kind": "inline",
    "source": [
      { "day": "2026-08-31", "revenue": 16800000, "tone": "warning" },
      { "day": "2026-09-01", "revenue": 18000000, "tone": "positive" }
    ]
  },
  "spec": {
    "colorConvention": "green-up-red-down",
    "timeField": "day",
    "items": [
      {
        "id": "revenue",
        "title": "Revenue",
        "value": {
          "field": "revenue",
          "reduce": "lastNonNull",
          "format": {
            "style": "currency",
            "currency": "CNY",
            "notation": "compact",
            "maximumFractionDigits": 1
          }
        },
        "status": {
          "text": { "literal": "Above target" },
          "tone": { "field": "tone" }
        },
        "trend": {
          "type": "area",
          "compare": {
            "lag": 1,
            "mode": "relative",
            "label": "vs previous day",
            "polarity": "higher-is-better"
          }
        },
        "references": [
          { "ref": "docs://metrics/revenue", "label": "Revenue definition" }
        ]
      }
    ]
  }
}
```

value.field reads from canonical data and defaults to lastNonNull reduction. Formatting uses a validated subset of Intl.NumberFormat: text, decimal, percent, currency, or unit, plus notation, fraction digits, prefix, suffix, and null display. Functions and arbitrary expressions are rejected.

Items use the default top-level data unless they set dataset to a key in the top-level datasets map. Prefer shared default data for matching grain, filters, and source; use named datasets when those differ. Each selected inline/ref dataset is materialized once and total row/cell limits apply across the selected collection. The default data remains the source of the built-in Data table; a named-only group renders without that single-dataset toggle.

{
  "data": { "kind": "inline", "source": [{ "revenue": 18000000 }] },
  "datasets": {
    "inventory": { "kind": "inline", "source": [{ "stock": 23 }] }
  },
  "spec": {
    "items": [
      { "id": "revenue", "title": "Revenue", "value": { "field": "revenue" } },
      { "id": "inventory", "title": "Inventory", "dataset": "inventory", "value": { "field": "stock" } }
    ]
  }
}

An item may omit trend, or use a line/area sparkline with an exact source row lag, absolute/relative comparison, polarity, and optional zero-inclusive Y scale. Fewer than two valid numeric points hides the trend without failing the KPI. Source order is authoritative; null lag rows are not skipped.

Referenced canonical data remains host-owned. Pass a resolver to this renderer directly or through the React/Vue adapter's kpi option:

const renderer = createKpiRenderer({
  validateDataRef: (ref) => ref.startsWith('dataset://'),
  resolveDataRef: (ref, { signal }) => loadDataset(ref, signal),
  referenceIcon: ({ document }) => createHostReferenceIcon(document),
});

The resolved dataset is used once for values, trends, comparisons, and the shared Data table. The renderer never fetches or interprets refs itself.

References are separate opaque item configuration and only become controls when the host supplies referenceActions:

const referenceActions = {
  canOpen: ({ reference }: { reference: { ref: string } }) =>
    reference.ref.startsWith('docs://'),
  open: ({ reference }: { reference: { ref: string } }) => {
    openDocumentation(reference.ref);
  },
};

referenceIcon is a trusted host-only presentation hook. It receives the opaque reference event plus the control's owner document and returns a newly created decorative HTML/SVG element. If the hook is absent, returns nothing, or throws, the renderer uses its default link glyph. The renderer does not assign domain meaning to either icon.

Hosts can align the renderer with their theme through the --markdown-chart-kpi-* CSS custom properties. Unset properties have light and dark fallbacks selected from the chart mount theme.

Direction colors

Set spec.colorConvention to red-up-green-down for Chinese output or green-up-red-down for English output. Comparison text and its line/area sparkline use the actual delta direction, independent of polarity. Zero uses neutral gray; missing/undefined comparisons keep the ordinary accent sparkline. Status tones still describe business status. Without this optional field, old messages retain polarity-based comparison colors and accent sparklines. The stored convention controls replay; browser language does not change it.

Direction colors support --markdown-chart-kpi-red and --markdown-chart-kpi-green, with light/dark defaults; status color variables are unchanged. Upgrade the renderer before enabling Skill output with this field.

版本与发布记录

@datafe-open/markdown-chart-kpi 共发布 13 个版本,首次发布于 2026/09/03;过去 12 个月发布了 13 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.4.1latest2026/10/3110 天前
0.4.02026/10/2220 天前
0.3.42026/09/221 个月前
0.3.32026/09/211 个月前
0.3.22026/09/201 个月前
0.3.12026/09/181 个月前

关于 @datafe-open/markdown-chart-kpi 的常见问题

@datafe-open/markdown-chart-kpi 是什么?

Strict framework-neutral KPI card renderer for Markdown Chart

如何安装 @datafe-open/markdown-chart-kpi?

在项目目录运行 npm install @datafe-open/markdown-chart-kpi,或使用 pnpm add / yarn add / bun add。

@datafe-open/markdown-chart-kpi 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@datafe-open/markdown-chart-kpi 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@datafe-open/markdown-chart-kpi 可以商用吗?

@datafe-open/markdown-chart-kpi 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@datafe-open/markdown-chart-kpi 的下载量有多少?

根据 npm 官方下载统计,@datafe-open/markdown-chart-kpi 在 2026/10/12 – 2026/11/10 这 30 天内被下载 3,732 次,日均约 124 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@datafe-open/markdown-chart-kpi 还在维护吗?

最新版本 0.4.1 发布于 2026/10/31(10 天前);至今共发布 13 个版本,其中过去 12 个月发布了 13 个;首次发布于 2026/09/03。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。