Statistic
Highlight a metric such as sales, user count, or completion rate. Supports digit grouping, fixed decimal places, prefixes, suffixes, and animated values.
Import
import { HuiStatistic } from '@nscode/handy-ui'
Basic Usage
value is the number to display; title appears above it. Integer digits are grouped with commas by default.
<template>
<HuiStatistic :value="123456" title="Total users" />
</template>
Precision
precision rounds to a fixed number of decimal places and pads missing digits with zeros. Without it, the value keeps its own decimal places, and very large or small numbers do not use scientific notation. The titles below indicate the displayed formats:
<template>
<HuiSpace direction="vertical" align="start" size="large">
<HuiSpace wrap size="large">
<HuiStatistic :value="12345.6789" :precision="2" title="12,345.68" />
<HuiStatistic :value="100" :precision="2" title="100.00" />
<HuiStatistic :value="-1234.5" :precision="2" title="-1,234.50" />
<HuiStatistic :value="99.5" :precision="0" title="precision 0:100" />
</HuiSpace>
<HuiSpace wrap size="large">
<HuiStatistic :value="12345.6789" title="No precision" />
<HuiStatistic :value="0.0000001" title="Very small number" />
<HuiStatistic :value="123456789012345680000" title="Very large number" />
</HuiSpace>
</HuiSpace>
</template>
Separators
groupSeparator controls digit grouping (, by default; an empty string disables grouping). separator is the decimal separator (. by default).
<template>
<HuiSpace wrap size="large">
<!-- 1,234,567.89 -->
<HuiStatistic :value="1234567.89" title="Default" />
<!-- 1 234 567 -->
<HuiStatistic :value="1234567" group-separator=" " title="Space grouping" />
<!-- 1234567 -->
<HuiStatistic :value="1234567" group-separator="" title="No grouping" />
<!-- 1.234,50 -->
<HuiStatistic
:value="1234.5"
:precision="2"
group-separator="."
separator=","
title="European format"
/>
</HuiSpace>
</template>
Numeric Strings
value also accepts strings. Numeric strings such as an API's '1234.5' are formatted as numbers; nonnumeric strings such as '--' are displayed unchanged.
<template>
<HuiSpace wrap size="large">
<HuiStatistic value="1234.5" :precision="2" title="1,234.50" />
<HuiStatistic value="-0.125" :precision="2" title="-0.13" />
<HuiStatistic value="--" title="No data" />
<HuiStatistic value="--" suffix="%" title="Affixes with nonnumeric values" />
</HuiSpace>
</template>
Title
Set the title prop, or use the title slot for icons, tooltips, or other content. The slot takes precedence.
<template>
<HuiSpace wrap size="large">
<HuiStatistic :value="5680" title="Today's visits" />
<HuiStatistic :value="5680">
<template #title>
<HuiSpace size="small" align="center">
<HuiIcon name="users" />
<span>Today's visitors</span>
</HuiSpace>
</template>
</HuiStatistic>
</HuiSpace>
</template>
Prefix and Suffix
Use prefix and suffix for text. The matching slots accept icons or styled content and take precedence over the props.
<template>
<HuiSpace direction="vertical" align="start" size="large">
<HuiSpace wrap size="large">
<HuiStatistic :value="12345" prefix="¥" title="Sales" />
<HuiStatistic :value="98" suffix="%" title="Satisfaction" />
<HuiStatistic :value="1024" prefix="Total: " suffix=" people" title="Both affixes" />
</HuiSpace>
<HuiSpace wrap size="large">
<HuiStatistic :value="100" title="Growth">
<template #prefix>
<span style="color: var(--hui-color-success)">↑</span>
</template>
<template #suffix>%</template>
</HuiStatistic>
<HuiStatistic :value="12.5" :precision="1" title="Decrease">
<template #prefix>
<span style="color: var(--hui-color-danger)">↓</span>
</template>
<template #suffix>%</template>
</HuiStatistic>
<HuiStatistic :value="4.8" :precision="1" suffix="/ 5" title="Rating">
<template #prefix>
<HuiIcon name="star" color="var(--hui-color-warning)" />
</template>
</HuiStatistic>
</HuiSpace>
</HuiSpace>
</template>
Animation
With animation, the initial value moves from from to value over animationDuration milliseconds. Later value changes transition from the currently displayed number. Animation uses the target's decimal places, or precision when set, to avoid long fractional values. It emits finish when complete.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiStatistic, HuiButton, HuiSpace } from '@nscode/handy-ui'
const value = ref(12345)
const finishCount = ref(0)
</script>
<template>
<HuiSpace wrap size="large" align="center">
<HuiStatistic
:value="value"
animation
:animation-duration="800"
title="Orders"
@finish="finishCount++"
/>
<HuiButton @click="value += 100">Add 100</HuiButton>
<HuiButton @click="value -= 1000">Subtract 1000</HuiButton>
</HuiSpace>
<p style="margin-top: 12px">finish emitted {{ finishCount }} times</p>
</template>
Call startAnimation() through a ref to replay from from, or stopAnimation() to stop at the currently displayed value. The first metric below starts at the default 0; the second uses from of 90000 and two decimal places:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiStatistic, HuiButton, HuiSpace } from '@nscode/handy-ui'
import type { StatisticExpose } from '@nscode/handy-ui'
const statisticRef = ref<StatisticExpose>()
const fromRef = ref<StatisticExpose>()
const replay = () => {
statisticRef.value?.startAnimation()
fromRef.value?.startAnimation()
}
const stop = () => {
statisticRef.value?.stopAnimation()
fromRef.value?.stopAnimation()
}
</script>
<template>
<HuiSpace direction="vertical" align="start" size="large">
<HuiSpace wrap size="large">
<HuiStatistic ref="statisticRef" :value="100000" animation title="Starting at 0" />
<HuiStatistic
ref="fromRef"
:value="100000"
:from="90000"
:precision="2"
prefix="¥"
animation
title="Starting at 90000"
/>
</HuiSpace>
<HuiSpace wrap>
<HuiButton icon="refresh" @click="replay">Replay</HuiButton>
<HuiButton @click="stop">Stop</HuiButton>
</HuiSpace>
</HuiSpace>
</template>
Inside Cards
Place multiple metrics side by side in cards for an overview:
<template>
<div class="overview">
<HuiCard>
<HuiStatistic :value="8846" title="Today's orders" />
</HuiCard>
<HuiCard>
<HuiStatistic :value="126560.5" :precision="2" prefix="¥" title="Today's sales" />
</HuiCard>
<HuiCard>
<HuiStatistic :value="68.2" :precision="1" suffix="%" title="Conversion">
<template #prefix>
<span style="color: var(--hui-color-success)">↑</span>
</template>
</HuiStatistic>
</HuiCard>
<HuiCard>
<HuiStatistic value="--" title="Refunds" />
</HuiCard>
</div>
</template>
<style scoped>
.overview {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
gap: var(--hui-spacing-4);
}
</style>
Custom Formatter
formatter takes precedence over built-in formatting. With animation enabled, it runs on each frame.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiStatistic, HuiButton, HuiSpace } from '@nscode/handy-ui'
const value = ref(1200)
const format = (value: string | number) => '¥ ' + Number(value).toFixed(2)
</script>
<template>
<HuiSpace align="center">
<HuiStatistic title="Amount" :value="value" :formatter="format" animation />
<HuiButton @click="value += 100">Add 100</HuiButton>
</HuiSpace>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| value | number | string | - | Metric value. Numeric strings are formatted; nonnumeric strings are displayed unchanged. NaN, Infinity, and an empty string display nothing. |
| title | string | - | Title. |
| prefix | string | - | Text before the value. |
| suffix | string | - | Text after the value. |
| precision | number | - | Decimal places, rounded and zero-padded. Clamped to 0–20; fractional precision values are rounded down. |
| separator | string | '.' | Decimal separator. |
| groupSeparator | string | ',' | Digit-group separator. An empty string disables grouping. |
| animation | boolean | false | Whether to animate value changes. |
| animationDuration | number | 3000 | Animation duration in milliseconds. |
| from | number | 0 | Starting value on mount and when calling startAnimation(). Later value changes start from the current display. |
| formatter | (value: number | string) => string | - | Custom display formatter, overriding precision and separators; called on every animation frame. |
Events
| Event | Arguments | Description |
|---|---|---|
| finish | - | Emitted when an animation completes. Not emitted when animation is disabled. |
Slots
| Slot | Description |
|---|---|
| title | Title content, taking precedence over title. |
| prefix | Content before the value, taking precedence over prefix. |
| suffix | Content after the value, taking precedence over suffix. |
Methods
Call these through the component ref.
| Method | Parameters | Returns | Description |
|---|---|---|---|
| startAnimation | - | void | Replays the animation from from to value. |
| stopAnimation | - | void | Stops the animation at the current display. |
Types
Use StatisticExpose for public methods. StatisticInstance remains only as a deprecated compatibility alias and will be removed in 1.0.
import type { StatisticProps, StatisticExpose } from '@nscode/handy-ui'
interface StatisticProps {
/** Custom display formatter, called on each animation frame. */
formatter?: (value: number | string) => string
/** Value; numeric strings are formatted, while nonnumeric strings such as '--' display unchanged. */
value?: number | string
/** Title. */
title?: string
/** Prefix. */
prefix?: string
/** Suffix. */
suffix?: string
/** Decimal places, zero-padded when set; range 0–20. */
precision?: number
/** Decimal separator, default '.'. */
separator?: string
/** Digit-group separator, default ','; empty string disables grouping. */
groupSeparator?: string
/** Whether to animate the value. */
animation?: boolean
/** Animation duration in milliseconds. */
animationDuration?: number
/** Starting value for the initial animation. */
from?: number
}
interface StatisticExpose {
/** Replays an animation from from. */
startAnimation: () => void
/** Stops the animation. */
stopAnimation: () => void
}
/** @deprecated Use StatisticExpose; removed in 1.0. */
type StatisticInstance = StatisticExpose