# Statistic

> 展示具名数值与上下文，支持本地化格式、单位、加载占位和紧凑布局。

网页来源：/components/statistic

版本：@quirekit/ui 0.3.1；源码修订：d79a6b48d00e+worktree。此文档对应当前源码构建，接入方式以开始使用为准。

接入前置：[开始使用](/raw/components/getting-started.md)（Vue、样式、主题与图标 Provider）。

用 `title` 说明统计的对象，`value` 提供数字。组件保留原生名称—值语义；
需要展示完整对象属性时使用 [Descriptions](/raw/components/descriptions.md)。

## 基本用法

`description` 补充统计范围或更新时间。Statistic 自身没有背景和外边距，
可放在 [Card](/raw/components/card.md) 中；多列布局由页面管理。试着更新下方本地工作区摘要，再恢复初始数据。

### 示例：statistic/basic

文件：`basic.vue`

```vue
<script setup lang="ts">
import { ref } from 'vue';
import { QuireButton, QuireCard, QuireStatistic } from '@quirekit/ui/vue';

const updated = ref(false);
</script>

<template>
  <div class="grid gap-qk-surface p-qk-surface" data-statistic-basic>
    <QuireCard as="section" title="文档工作区" description="本月团队共享内容的汇总。">
      <template #trailing>
        <QuireButton variant="outline" @click="updated = !updated">{{ updated ? '恢复初始摘要' : '更新本地摘要' }}</QuireButton>
      </template>
      <div class="grid min-w-0 gap-qk-group sm:grid-cols-3">
        <QuireStatistic title="文档数量" :value="updated ? 1260 : 1248" suffix="份" :description="updated ? '已加入 12 份新文档' : '包含已归档文档'" />
        <QuireStatistic title="共享存储" :value="updated ? 24.8 : 24.5" suffix="GB" :format-options="{ minimumFractionDigits: 1, maximumFractionDigits: 1 }" description="文件和版本记录" />
        <QuireStatistic title="本月归档完成率" :value="updated ? 0.925 : 0.865" :format-options="{ style: 'percent', maximumFractionDigits: 1 }">
          <template #description><span>{{ updated ? '较上次增加 6 个百分点' : '目标为本月全部待归档文档' }}</span></template>
        </QuireStatistic>
      </div>
    </QuireCard>
  </div>
</template>
```

## 数字格式与精度

数字按 [Locale Provider](/raw/components/locale-provider.md) 的语言格式化，默认 `zh-CN`；`formatOptions` 接受 `Intl.NumberFormatOptions`，控制分组、
小数位、百分比、货币和标准单位。百分比传比例值，例如 `0.865` 显示为 `86.5%`。
固定两位小数时同时设置 `minimumFractionDigits` 和 `maximumFractionDigits`。

### 示例：statistic/formatting

文件：`formatting.vue`

```vue
<script setup lang="ts">
import { ref } from 'vue';
import { QuireLocaleProvider, QuireSelect, QuireStatistic, QuireSwitch } from '@quirekit/ui/vue';

const locale = ref('zh-CN');
const precise = ref(true);
const locales = [
  { value: 'zh-CN', label: '简体中文' },
  { value: 'en-US', label: 'English (US)' },
  { value: 'de-DE', label: 'Deutsch' },
];
</script>

<template>
  <div class="grid gap-qk-group p-qk-surface" data-statistic-formatting>
    <div class="flex min-w-0 flex-wrap items-center gap-qk-surface">
      <QuireSelect v-model="locale" :items="locales" label="数字区域格式" width="180px" />
      <QuireSwitch v-model:checked="precise" label="固定两位小数" />
    </div>
    <QuireLocaleProvider :locale="locale">
    <div class="grid min-w-0 gap-qk-group sm:grid-cols-2">
      <QuireStatistic title="文件大小" :value="1234.567" :format-options="{ style: 'unit', unit: 'megabyte', minimumFractionDigits: precise ? 2 : 0, maximumFractionDigits: precise ? 2 : 0 }" description="标准单位按区域格式展示" />
      <QuireStatistic title="已完成比例" :value="0.865" :format-options="{ style: 'percent', minimumFractionDigits: precise ? 2 : 0, maximumFractionDigits: precise ? 2 : 0 }" description="原始比例值为 0.865" />
    </div>
    </QuireLocaleProvider>
  </div>
</template>
```

## 前后缀与自定义内容

`prefix`、`suffix` 追加静态文字，也可使用同名插槽。标准百分比、货币和本地化单位优先用
`formatOptions`，避免重复添加符号；存储单位的换算由调用方处理。
`value` 插槽收到原始 `value` 和格式化后的 `formattedValue`，`description` 插槽可组合趋势说明和独立操作。

### 示例：statistic/affixes

文件：`affixes.vue`

```vue
<script setup lang="ts">
import { ref } from 'vue';
import { QuireButton, QuireStatistic, QuireTag } from '@quirekit/ui/vue';

const details = ref(false);
</script>

<template>
  <div class="grid gap-qk-group p-qk-surface sm:grid-cols-2" data-statistic-affixes>
    <QuireStatistic title="可用共享空间" :value="75.5" prefix="约" suffix="GB" description="总配额 100 GB，数值已经换算" />
    <QuireStatistic title="本周归档" :value="164" suffix="份">
      <template #title><span>本周归档</span></template>
      <template #value="{ formattedValue }"><span>{{ formattedValue }}<span class="text-qk-control font-normal text-qk-text-secondary"> / 200</span></span></template>
      <template #description>
        <div class="grid justify-items-start gap-qk-inline">
          <QuireTag color="success" size="xs">较上周增加 24 份</QuireTag>
          <QuireButton variant="outline" size="xs" :aria-expanded="details" @click="details = !details">{{ details ? '收起归档范围' : '查看归档范围' }}</QuireButton>
          <p v-if="details" class="m-0">统计项目资料与会议记录，不包含草稿和已删除文档。</p>
        </div>
      </template>
    </QuireStatistic>
  </div>
</template>
```

## 加载、缺失与恢复

`loading` 用占位替换数值和前后缀，并提供可访问说明。缺省值、`null`、`NaN` 和
无穷大显示 `emptyValue`（默认 `—`），可访问说明跟随 [Locale Provider](/raw/components/locale-provider.md) 的语言；`0` 仍显示为零。
请求、重试、焦点与动态播报由页面管理，下例第一次刷新失败、第二次恢复。

### 示例：statistic/loading

文件：`loading.vue`

```vue
<script setup lang="ts">
import { nextTick, onBeforeUnmount, ref } from 'vue';
import { QuireButton, QuireCard, QuireStatistic } from '@quirekit/ui/vue';

const value = ref<number | null>(1248);
const loading = ref(false);
const attempts = ref(0);
const feedback = ref('本地示例：第一次刷新失败，再次刷新恢复数据。');
const root = ref<HTMLElement>();
let timer: ReturnType<typeof setTimeout> | undefined;
onBeforeUnmount(() => clearTimeout(timer));

/** Simulates a failed read followed by a successful retry while retaining the trigger. */
function refresh(): void {
  if (loading.value) return;
  loading.value = true;
  attempts.value++;
  feedback.value = '正在刷新摘要…';
  timer = setTimeout(async () => {
    loading.value = false;
    value.value = attempts.value === 1 ? null : 1260;
    feedback.value = value.value === null ? '刷新失败，暂时无法获取统计，请再试一次。' : '摘要已恢复，共 1,260 份文档。';
    await nextTick();
    root.value?.querySelector<HTMLButtonElement>('[data-refresh]')?.focus();
  }, 500);
}

/** Restores the complete initial data and retry sequence for another trial. */
function reset(): void {
  clearTimeout(timer);
  loading.value = false;
  attempts.value = 0;
  value.value = 1248;
  feedback.value = '已恢复初始数据，可以再次体验失败与重试。';
}
</script>

<template>
  <div ref="root" class="grid gap-qk-surface p-qk-surface" data-statistic-loading>
    <QuireCard>
      <div class="grid min-w-0 gap-qk-group sm:grid-cols-3">
        <QuireStatistic title="文档数量" :value="value" :loading="loading" suffix="份" description="读取失败时不显示为零" />
        <QuireStatistic title="待处理文档" :value="0" suffix="份" description="零表示没有待处理文档" />
        <QuireStatistic title="历史版本统计" :value="null" empty-value="未统计" description="暂未启用版本统计" />
      </div>
      <template #footer>
        <div class="flex flex-wrap gap-qk-inline">
          <QuireButton data-refresh variant="outline" :busy="loading" @click="refresh">{{ value === null ? '重试刷新' : '刷新摘要' }}</QuireButton>
          <QuireButton variant="ghost" @click="reset">恢复初始状态</QuireButton>
        </div>
      </template>
    </QuireCard>
    <p role="status" class="m-0 text-qk-helper text-qk-text-secondary">{{ feedback }}</p>
  </div>
</template>
```

## 紧凑尺寸与长数值

`size="sm"` 缩小数值并收紧标题间距，默认 `md`。长数字优先在分组分隔符后换行，不用省略号隐藏关键数据，复制时仍是完整数值。
精确的大整数传 `bigint`；字符串不自动解析。无效的语言标签或格式选项保留 `Intl` 异常，应在接入时修正。

### 示例：statistic/compact

文件：`compact.vue`

```vue
<script setup lang="ts">
import { ref } from 'vue';
import { QuireCard, QuireStatistic, QuireSwitch } from '@quirekit/ui/vue';

const compact = ref(true);
const long = ref(false);
const exactCount = 900719925474099312345n;
</script>

<template>
  <div class="grid gap-qk-surface p-qk-surface" data-statistic-compact>
    <div class="flex flex-wrap gap-qk-surface">
      <QuireSwitch v-model:checked="compact" label="紧凑尺寸" />
      <QuireSwitch v-model:checked="long" label="显示长整数和单位" />
    </div>
    <div class="w-full max-w-xs">
      <QuireCard density="compact">
        <QuireStatistic :size="compact ? 'sm' : 'md'" :title="long ? '跨工作区历史归档的完整字符数量' : '已归档字符'" :value="long ? exactCount : 32048" :suffix="long ? '个字符（包含正文、批注及历史版本）' : '个字符'" description="完整保留数值，窄容器中允许换行" />
      </QuireCard>
    </div>
  </div>
</template>
```

## 可访问性

标题和值使用原生 `dt` / `dd`，没有额外 Tab stop、自动播报或焦点移动。
趋势同时写明变化方向与比较范围，不能只用颜色传递；自定义标题插槽保留清楚的文字名称。

## API

### QuireStatistic

#### Props

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| title | 是 | — | Visible metric name, rendered as a native definition term. |
| value | 否 | — | Numeric value; missing and nonfinite numbers show the empty state. Use bigint for exact large integers. |
| formatOptions | 否 | — | Native grouping, precision, percentage, currency and unit formatting options. |
| prefix | 否 | — | Plain text before a valid value; the prefix slot takes precedence. |
| suffix | 否 | — | Plain text after a valid value; the suffix slot takes precedence. |
| description | 否 | — | Additional context after the value; the description slot takes precedence. |
| size | 否 | "md" | Numeric typography and content spacing. Defaults to md. |
| loading | 否 | false | Replaces the value and affixes with a decorative placeholder. Defaults to false. |
| emptyValue | 否 | "—" | Visible placeholder for missing and nonfinite values. Defaults to —. |

**title**

```ts
string
```

**value**

```ts
number | bigint | null | undefined
```

```ts
undefined | null | number | bigint
```

**formatOptions**

```ts
Intl.NumberFormatOptions | undefined
```

```ts
undefined | {
  localeMatcher?: undefined | "lookup" | "best fit";
  style?: undefined | "decimal" | "percent" | "currency" | "unit";
  currency?: undefined | string;
  currencyDisplay?: undefined | "symbol" | "code" | "name" | "narrowSymbol";
  useGrouping?: undefined | false | true | "min2" | "auto" | "always" | "true" | "false";
  minimumIntegerDigits?: undefined | number;
  minimumFractionDigits?: number | undefined;
  maximumFractionDigits?: number | undefined;
  minimumSignificantDigits?: number | undefined;
  maximumSignificantDigits?: number | undefined;
  numberingSystem?: string | undefined;
  compactDisplay?: undefined | "short" | "long";
  notation?: undefined | "standard" | "scientific" | "engineering" | "compact";
  signDisplay?: undefined | "auto" | "always" | "never" | "exceptZero" | "negative";
  unit?: string | undefined;
  unitDisplay?: undefined | "short" | "long" | "narrow";
  currencySign?: undefined | "standard" | "accounting";
  roundingPriority?: undefined | "auto" | "morePrecision" | "lessPrecision";
  roundingIncrement?: undefined | 1 | 2 | 5 | 10 | 20 | 25 | 50 | 100 | 200 | 250 | 500 | 1000 | 2000 | 2500 | 5000;
  roundingMode?: undefined | "ceil" | "floor" | "expand" | "trunc" | "halfCeil" | "halfFloor" | "halfExpand" | "halfTrunc" | "halfEven";
  trailingZeroDisplay?: undefined | "auto" | "stripIfInteger";
}
```

**prefix**

```ts
string | undefined
```

```ts
undefined | string
```

**suffix**

```ts
string | undefined
```

```ts
undefined | string
```

**description**

```ts
string | undefined
```

```ts
undefined | string
```

**size**

```ts
QuireKitStatisticSize | undefined
```

```ts
undefined | "md" | "sm"
```

**loading**

```ts
boolean | undefined
```

**emptyValue**

```ts
string | undefined
```

```ts
undefined | string
```

#### Events

无。

#### Slots

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| title | 否 | — | Replaces the metric name while retaining native dt semantics. |
| prefix | 否 | — | Content before a valid value; omitted during loading and empty states. |
| value | 否 | — | Replaces the available value's presentation without changing the surrounding dd. |
| suffix | 否 | — | Content after a valid value; omitted during loading and empty states. |
| description | 否 | — | Context, trend explanation or independent actions after the value. |

**title**

```ts
any
```

**prefix**

```ts
any
```

**value**

```ts
QuireKitStatisticValueSlotProps
```

```ts
{
  /** Original finite number or exact integer. */
  value: number | bigint;
  /** Intl.NumberFormat output for the active language and options. */
  formattedValue: string;
}
```

**suffix**

```ts
any
```

**description**

```ts
any
```

#### 公开实例成员

无。

#### 相关类型

```ts
type QuireKitStatisticValueSlotProps = {
  /** Original finite number or exact integer. */
  value: number | bigint;
  /** Intl.NumberFormat output for the active language and options. */
  formattedValue: string;
};
```
