Flux 的 Chart 组件是一款轻量、零依赖的工具,可用于在 Livewire 应用中构建图表。它被设计得既简单又极其灵活,便于你按照需求组合并自定义图表样式。
目前,Flux 支持折线图和面积图。更多图表类型将在未来版本中推出。
<flux:chart wire:model="data" class="aspect-3/1">    <flux:chart.svg>        <flux:chart.line field="visitors" class="text-pink-500 dark:text-pink-400" />        <flux:chart.axis axis="x" field="date">            <flux:chart.axis.line />            <flux:chart.axis.tick />        </flux:chart.axis>        <flux:chart.axis axis="y">            <flux:chart.axis.grid />            <flux:chart.axis.tick />        </flux:chart.axis>        <flux:chart.cursor />    </flux:chart.svg>    <flux:chart.tooltip>        <flux:chart.tooltip.heading field="date" :format="['year' => 'numeric', 'month' => 'numeric', 'day' => 'numeric']" />        <flux:chart.tooltip.value field="visitors" label="Visitors" />    </flux:chart.tooltip></flux:chart><?phpuse Livewire\Component;class Dashboard extends Component{    public array $data = [        ['date' => '2025-10-08', 'visitors' => 267],        ['date' => '2025-10-07', 'visitors' => 259],        ['date' => '2025-10-06', 'visitors' => 269],        // ...    ];}<flux:chart wire:model="data" /><flux:chart :value="$this->data" /><flux:chart :value="[1, 2, 3, 4, 5]" /><flux:chart wire:model="data" class="aspect-[3/1]">    <flux:chart.svg>        <flux:chart.line field="memory" class="text-pink-500" />        <flux:chart.point field="memory" class="text-pink-400" />        <flux:chart.axis axis="x" field="date">            <flux:chart.axis.tick />            <flux:chart.axis.line />        </flux:chart.axis>        <flux:chart.axis axis="y" tick-values="[0, 128, 256, 384, 512]" :format="['style' => 'unit', 'unit' => 'megabyte']">            <flux:chart.axis.grid />            <flux:chart.axis.tick />        </flux:chart.axis>    </flux:chart.svg></flux:chart><flux:chart wire:model="data" class="aspect-3/1">    <flux:chart.svg>        <flux:chart.line field="stock" class="text-blue-500 dark:text-blue-400" curve="none" />        <flux:chart.area field="stock" class="text-blue-200/50 dark:text-blue-400/30" curve="none" />        <flux:chart.axis axis="y" position="right" tick-prefix="$" :format="[            'notation' => 'compact',            'compactDisplay' => 'short',            'maximumFractionDigits' => 1,        ]">            <flux:chart.axis.grid />            <flux:chart.axis.tick />        </flux:chart.axis>        <flux:chart.axis axis="x" field="date">            <flux:chart.axis.tick />            <flux:chart.axis.line />        </flux:chart.axis>    </flux:chart.svg></flux:chart><flux:chart wire:model="data">    <flux:chart.viewport class="min-h-[20rem]" >        <flux:chart.svg>            <flux:chart.line field="twitter" class="text-blue-500" curve="none" />            <flux:chart.point field="twitter" class="text-blue-500" r="6" stroke-width="3" />            <flux:chart.line field="facebook" class="text-red-500" curve="none" />            <flux:chart.point field="facebook" class="text-red-500" r="6" stroke-width="3" />            <flux:chart.line field="instagram" class="text-green-500" curve="none" />            <flux:chart.point field="instagram" class="text-green-500" r="6" stroke-width="3" />            <flux:chart.axis axis="x" field="date">                <flux:chart.axis.tick />                <flux:chart.axis.line />            </flux:chart.axis>            <flux:chart.axis axis="y" tick-start="0" tick-end="1" :format="[                'style' => 'percent',                'minimumFractionDigits' => 0,                'maximumFractionDigits' => 0,            ]">                <flux:chart.axis.grid />                <flux:chart.axis.tick />            </flux:chart.axis>        </flux:chart.svg>    </flux:chart.viewport>    <div class="flex justify-center gap-4 pt-4">        <flux:chart.legend label="Instagram">            <flux:chart.legend.indicator class="bg-green-400" />        </flux:chart.legend>        <flux:chart.legend label="Twitter">            <flux:chart.legend.indicator class="bg-blue-400" />        </flux:chart.legend>        <flux:chart.legend label="Facebook">            <flux:chart.legend.indicator class="bg-red-400" />        </flux:chart.legend>    </div></flux:chart>今日
    
昨日
<flux:card>    <flux:chart class="grid gap-6" wire:model="data">        <flux:chart.summary class="flex gap-12">            <div>                <flux:text>Today</flux:text>                <flux:heading size="xl" class="mt-2 tabular-nums">                    <flux:chart.summary.value field="sales" :format="['style' => 'currency', 'currency' => 'USD']" />                </flux:heading>                <flux:text class="mt-2 tabular-nums">                    <flux:chart.summary.value field="date" :format="['hour' => 'numeric', 'minute' => 'numeric', 'hour12' => true]" />                </flux:text>            </div>            <div>                <flux:text>Yesterday</flux:text>                <flux:heading size="lg" class="mt-2 tabular-nums">                    <flux:chart.summary.value field="yesterday" :format="['style' => 'currency', 'currency' => 'USD']" />                </flux:heading>            </div>        </flux:chart.summary>        <flux:chart.viewport class="aspect-[3/1]">            <flux:chart.svg>                <flux:chart.line field="yesterday" class="text-zinc-300 dark:text-white/40" stroke-dasharray="4 4" curve="none" />                <flux:chart.line field="sales" class="text-sky-500 dark:text-sky-400" curve="none" />                <flux:chart.axis axis="x" field="date">                    <flux:chart.axis.grid />                    <flux:chart.axis.tick />                    <flux:chart.axis.line />                </flux:chart.axis>                <flux:chart.axis axis="y">                    <flux:chart.axis.tick />                </flux:chart.axis>                <flux:chart.cursor />            </flux:chart.svg>        </flux:chart.viewport>    </flux:chart></flux:card>| 股票 | 价格 | 涨跌 | 趋势 | 
|---|---|---|---|
| AAPL | $193.45 |  | |
| MSFT | $338.12 |  | |
| TSLA | $242.68 |  | |
| GOOGL | $129.87 |  | 
<flux:chart :value="[15, 18, 16, 19, 22, 25, 28, 25, 29, 28, 32, 35]" class="w-[5rem] aspect-[3/1]">    <flux:chart.svg gutter="0">        <flux:chart.line class="text-green-500 dark:text-green-400" />    </flux:chart.svg></flux:chart>营收
<flux:card class="overflow-hidden min-w-[12rem]">    <flux:text>Revenue</flux:text>    <flux:heading size="xl" class="mt-2 tabular-nums">$12,345</flux:heading>    <flux:chart class="-mx-8 -mb-8 h-[3rem]" :value="[10, 12, 11, 13, 15, 14, 16, 18, 17, 19, 21, 20]">        <flux:chart.svg gutter="0">            <flux:chart.line class="text-sky-200 dark:text-sky-400" />            <flux:chart.area class="text-sky-100 dark:text-sky-400/30" />        </flux:chart.svg>    </flux:chart></flux:card><flux:chart>    <flux:chart.svg gutter="12 0 12 8">        <!-- ... -->    </flux:chart.svg></flux:chart><flux:chart.axis axis="y" scale="linear">    <!-- ... --></flux:chart.axis><flux:chart.svg>    <!-- ... -->    <flux:chart.axis axis="x">        <!-- Horizontal "X" axis line: -->        <flux:chart.axis.line />    </flux:chart.axis>    <flux:chart.axis axis="y">        <!-- Vertical "Y" axis line: -->        <flux:chart.axis.line />    </flux:chart.axis></flux:chart.svg><!-- A dark gray axis line that is 2px wide and has a gray color: --><flux:chart.axis.line class="text-zinc-800" stroke-width="2" /><flux:chart.svg>    <!-- ... -->    <!-- Zero line: -->    <flux:chart.zero-line /></flux:chart.svg><!-- A dark gray zero line that is 2px wide and has a gray color: --><flux:chart.zero-line class="text-zinc-800" stroke-width="2" /><flux:chart.svg>    <!-- ... -->    <flux:chart.axis axis="x">        <!-- Vertical grid lines: -->        <flux:chart.axis.grid />    </flux:chart.axis>    <flux:chart.axis axis="y">        <!-- Horizontal grid lines: -->        <flux:chart.axis.grid />    </flux:chart.axis></flux:chart.svg><!-- A dashed grid line that is 2px wide and has a gray color: --><flux:chart.axis.grid class="text-zinc-200/50" stroke-width="2" stroke-dasharray="4,4" /><flux:chart.svg>    <!-- ... -->    <flux:chart.axis axis="x">        <!-- X axis tick mark lines: -->        <flux:chart.axis.mark />        <!-- X axis tick labels: -->        <flux:chart.axis.tick />    </flux:chart.axis>    <flux:chart.axis axis="y">        <!-- Y axis tick mark lines: -->        <flux:chart.axis.mark />        <!-- Y axis tick labels: -->        <flux:chart.axis.tick />    </flux:chart.axis></flux:chart.svg><!-- A tick mark line that is 10px long, 2px wide, and has a gray color: --><flux:chart.axis.mark class="text-zinc-300" stroke-width="2" y1="0" y2="10" /><!-- A tick label that is 12px, has a blue color, is center aligned, and is 2.5rem from the axis line: --><flux:chart.axis.tick class="text-xs text-blue-500" text-anchor="middle" dy="2.5rem"  /><flux:chart.axis axis="y" tick-count="5">    <!-- ... --></flux:chart.axis><flux:chart.axis axis="y" tick-start="min">    <!-- ... --></flux:chart.axis><flux:chart.axis axis="y" tick-end="max">    <!-- ... --></flux:chart.axis><flux:chart.axis axis="y" tick-values="[0, 128, 256, 384, 512]" tick-suffix="MB">    <!-- ... --></flux:chart.axis><flux:chart.svg>    <!-- ... -->    <!-- Format the X axis tick labels to display the month and day: -->    <flux:chart.axis axis="x" :format="['month' => 'long', 'day' => 'numeric']">        <!-- X axis tick labels: -->        <flux:chart.axis.tick />    </flux:chart.axis>    <!-- Format the Y axis tick labels to display the value in USD: -->    <flux:chart.axis axis="y" :format="['style' => 'currency', 'currency' => 'USD']">        <!-- Y axis tick labels: -->        <flux:chart.axis.tick />    </flux:chart.axis></flux:chart.svg><flux:chart.axis axis="y" tick-prefix="$">    <!-- ... --></flux:chart.axis><flux:chart.axis axis="y" tick-suffix="MB">    <!-- ... --></flux:chart.axis><flux:chart.svg>    <!-- ... -->    <flux:chart.cursor /></flux:chart.svg><!-- A dashed, black cursor that is 1px wide: --><flux:chart.cursor class="text-zinc-800" stroke-width="1" stroke-dasharray="4,4" /><flux:chart>    <flux:chart.svg>        <!-- ... -->    </flux:chart.svg>    <flux:chart.tooltip>        <flux:chart.tooltip.heading field="date" />        <flux:chart.tooltip.value field="visitors" label="Visitors" />        <flux:chart.tooltip.value field="views" label="Views" :format="['notation' => 'compact']" />    </flux:chart.tooltip></flux:chart><flux:chart.tooltip>    <flux:chart.tooltip.heading field="date" />    <flux:chart.tooltip.value field="visitors" label="Visitors" />    <flux:chart.tooltip.value field="views" label="Page Views" :format="['notation' => 'compact']" /></flux:chart.tooltip><flux:chart wire:model="data">    <flux:chart.viewport class="aspect-3/1">        <flux:chart.svg>            <flux:chart.line class="text-blue-500" field="visitors" />            <flux:chart.line class="text-red-500" field="views" />        </flux:chart.svg>    </flux:chart.viewport>    <div class="flex justify-center gap-4 pt-4">        <flux:chart.legend label="Visitors">            <flux:chart.legend.indicator class="bg-blue-400" />        </flux:chart.legend>        <flux:chart.legend label="Views">            <flux:chart.legend.indicator class="bg-red-400" />        </flux:chart.legend>    </div></flux:chart><flux:chart wire:model="data">    <flux:chart.summary>        <flux:chart.summary.value field="visitors" :format="['notation' => 'compact']" />    </flux:chart.summary>    <flux:chart.viewport class="aspect-[3/1]">        <!-- ... -->    </flux:chart.viewport></flux:chart><flux:chart.summary.value field="visitors" fallback="1200" /><flux:chart.axis axis="y" :format="['style' => 'currency', 'currency' => 'USD']" />| 描述 | 示例输入 | 示例输出 | 格式数组 | 
|---|---|---|---|
| 货币(USD) | 1234.56 | $1,234.56 | ['style' => 'currency', 'currency' => 'USD'] | 
| 货币(EUR) | 1234.56 | €1,234.56 | ['style' => 'currency', 'currency' => 'EUR'] | 
| 百分比 | 0.85 | 85% | ['style' => 'percent'] | 
| 紧凑数字 | 1000000 | 1M | ['notation' => 'compact'] | 
| 科学计数法 | 123456789 | 1.235E8 | ['notation' => 'scientific'] | 
| 固定小数 | 3.1415926535 | 3.14 | ['maximumFractionDigits' => 2] | 
| 千分位分隔符 | 1234567 | 1,234,567 | ['useGrouping' => true] | 
| 自定义单位 | 50 | 50 MB | ['style' => 'unit', 'unit' => 'megabyte'] | 
<flux:chart.axis axis="x" field="date" :format="['month' => 'long', 'day' => 'numeric']" />| 描述 | 示例输入 | 示例输出 | 格式数组 | 
|---|---|---|---|
| 完整日期 | 2024-03-15 | Friday, March 15, 2024 | ['dateStyle' => 'full'] | 
| 月份与日期 | 2024-03-15 | March 15 | ['month' => 'long', 'day' => 'numeric'] | 
| 简写月份 | 2024-03-15 | Mar 15 | ['month' => 'short', 'day' => 'numeric'] | 
| 仅显示时间 | 2024-03-15 14:30 | 2:30 PM | ['hour' => 'numeric', 'minute' => 'numeric', 'hour12' => true] | 
| 24 小时制时间 | 2024-03-15 14:30 | 14:30 | ['hour' => '2-digit', 'minute' => '2-digit', 'hour12' => false] | 
| 星期 | 2024-03-15 | Friday | ['weekday' => 'long'] | 
| 短日期格式 | 2024-03-15 | 03/15/24 | ['month' => '2-digit', 'day' => '2-digit', 'year' => '2-digit'] | 
| 仅年份 | 2024-03-15 | 2024 | ['year' => 'numeric'] | 
| 属性 | 说明 | 
|---|---|
| wire:model | 将图表绑定到包含待展示数据的 Livewire 属性。更多信息请参阅 wire:model 文档。 | 
| value | 图表的数据点数组。每个点都应是带命名字段的关联数组,用于未使用 wire:model 绑定的场景。 | 
| curve | 图表中所有折线的默认曲线类型。可选值:smooth(默认)、none。 | 
| 插槽 | 说明 | 
|---|---|
| default | 要渲染的图表组件。通常包含一个定义图表结构的 flux:chart.svg 组件。 | 
| CSS | 说明 | 
|---|---|
| class | 应用到图表容器的额外 CSS 类。常见用法:使用 aspect-3/1 设置宽高比,使用 h-64 固定高度。 | 
| 属性 | 说明 | 
|---|---|
| data-flux-chart | 应用在根元素上,用于样式控制与标识。 | 
用于承载图表 SVG 元素的容器。必须嵌套在 `flux:chart` 内才能渲染图表。
| 插槽 | 说明 | 
|---|---|
| default | 用于渲染图表的可视化组件,如折线、面积、坐标轴及交互元素。 | 
| 属性 | 说明 | 
|---|---|
| field | 要绘制在 y 轴上的数据字段名称。必填。 | 
| curve | 折线的曲线类型。可选值:smooth(默认)、none。 | 
| CSS | 说明 | 
|---|---|
| class | 应用到折线的额外 CSS 类。常见用法:通过 text-{color} 设置线条颜色。 | 
| 属性 | 说明 | 
|---|---|
| field | 要绘制在 y 轴上的数据字段名称。必填。 | 
| curve | 面积的曲线类型。可选值:smooth(默认)、none。 | 
| CSS | 说明 | 
|---|---|
| class | 应用到面积图的额外 CSS 类。常见用法:通过 fill-{color}/20 设置填充颜色。 | 
为折线图或面积图添加点(圆点),用于标记每个数据点。
| 属性 | 说明 | 
|---|---|
| field | 要绘制点位的数据字段名称。必填。 | 
| CSS | 说明 | 
|---|---|
| class | 应用到数据点的额外 CSS 类。常见用法:使用 fill-{color} 设置填充颜色,使用 r 属性设定点的半径。 | 
| 属性 | 说明 | 
|---|---|
| axis | 需要配置的坐标轴。可选值:x、y。必填。 | 
| field | 当 `axis="x"` 时,用于生成标签的数据字段。 | 
| format | 用于坐标轴标签的日期/数字格式化选项。详见 格式化 章节。 | 
为图表添加刻度线,必须在 `flux:chart.axis` 组件内使用。
| 属性 | 说明 | 
|---|---|
| position | 刻度线的位置。可选值:top、bottom、left、right。 | 
| CSS | 说明 | 
|---|---|
| class | 应用到刻度线的额外 CSS 类。常见用法:通过 text-{color} 设置线条颜色,使用 stroke-width="1" 设置线条粗细。 | 
为图表添加坐标轴线,必须在 `flux:chart.axis` 组件内使用。
| CSS | 说明 | 
|---|---|
| class | 应用到坐标轴线的额外 CSS 类。常见用法:通过 text-{color} 设置线条颜色,使用 stroke-width="1" 设置线条粗细。 | 
为图表添加网格线,必须在 `flux:chart.axis` 组件内使用。
| CSS | 说明 | 
|---|---|
| class | 应用到网格线的额外 CSS 类。常见用法:通过 text-{color} 设置线条颜色,使用 stroke-width="{width}" 设置线条粗细。 | 
为坐标轴添加刻度与标签,必须在 `flux:chart.axis` 组件内使用。
| 属性 | 说明 | 
|---|---|
| format | 刻度标签的日期/数字格式化选项。详见 格式化 章节。 | 
| CSS | 说明 | 
|---|---|
| class | 应用到刻度线与标签的额外 CSS 类。常见用法:使用 text-{color} 设置标签颜色,使用 font-{weight} 设置字重。 | 
在 y=0 位置添加一条水平线,适用于同时包含正值和负值的图表。
| CSS | 说明 | 
|---|---|
| class | 应用到零线的额外 CSS 类。常见用法:通过 text-{color} 设置线条颜色,使用 stroke-width="{width}" 设置线条粗细。 | 
添加显示在图表上方的汇总区域,可展示最新数值或光标所在位置的数值。
| 插槽 | 说明 | 
|---|---|
| default | 汇总区域中需要展示的内容,可包含 flux:chart.value 组件以展示特定数据值。 | 
用于显示特定数据值,通常配合 `flux:chart.summary` 组件使用。
| 属性 | 说明 | 
|---|---|
| field | 要展示的数据字段。 | 
| fallback | 当该数值不存在或无法格式化时显示的备用文本。 | 
| format | 日期/数字格式化选项,详见 格式化 章节。 | 
| Prop | Description | 
|---|---|
| field | 图例项所对应的数据字段。 | 
| label | 图例项显示的文本标签。 | 
| format | 日期/数字格式化选项,详见 格式化 章节。 | 
| 插槽 | 说明 | 
|---|---|
| default | 图例中需要展示的内容,可包含任意内容,包括 flux:chart.legend.indicator 组件。 |