Chart
Beautiful charts drawn directly in GPUI — no chart library.
Overview
分组柱状 + CartesianGrid + X 轴 + 悬停 tooltip + 带 totals 的 legend(文档页同款交互)。
Installation
Chart 是单个 Rust 文件(src/chart.rs)—— 无图表库依赖:网格、柱、线、面积、环形全用 GPUI 的 div 与 canvas/PathBuilder 自绘。
[dependencies]gpui-kit = "0.6.6"# chart.rs is self-contained — no chart library dependency.# Download the runnable example crate, then:cargo run --locked -- --chart# Other demos:cargo run --locked -- --chart stackedcargo run --locked -- --chart linecargo run --locked -- --chart areacargo run --locked -- --chart donutUsage
Chart::new(kind) 建实体,.config(..) 传 ChartSeries(ChartConfig 等价物:label + light/dark 色),.data(..) 传 (类目, 各系列值) 行。悬停即出 tooltip,无需接线。
use justdo_command::chart::{Chart, ChartKind, ChartSeries};let chart = cx.new(|_| { Chart::new(ChartKind::Bar) .title("Bar Chart - Interactive", "…last 6 months") .config(vec![ ChartSeries::new("Desktop", 0x2563eb, 0x3b82f6), ChartSeries::new("Mobile", 0x60a5fa, 0x93c5fd), ]) .data(vec![ ("Jan", vec![186., 80.]), ("Feb", vec![305., 200.]), ("Mar", vec![237., 120.]), // … ]) .dark(dark)});网页预览与桌面端运行同一份 Rust / GPUI 组件。
Composition
组合结构对齐 shadcn/ui 的 ChartContainer / ChartTooltip / ChartLegend(Recharts)。
Chart card-styled container — title + caption + plot + legend├── plot relative box, 220px tall│ ├── canvas grid lines / line+area paths / donut slices│ ├── bars flex bands (Bar | Stacked)│ ├── hover bands one transparent flex_1 column per x category│ └── tooltip absolute, follows the cursor├── x axis per-category flex_1 labels└── legend dot + label + series totalNotes
Chart config. ChartSeries 解耦 label 与 light/dark 色值 —— 与 shadcn 的 ChartConfig 对应,数据行只带数值。
// ChartSeries is the ChartConfig equivalent — label plus// light/dark colors, decoupled from the data rows.const DESKTOP: (u32, u32) = (0x2563eb, 0x3b82f6); // light, darkChartSeries::new("Desktop", DESKTOP.0, DESKTOP.1)Tooltip. 悬停命中按 x 分带(cartesian)或按极角+半径(donut)判定;.indicator(IndicatorStyle::Line) 换线性指示条,默认圆点。donut 悬停会把其余切片降到 35% 透明。
Scale. y 轴归一取单列最大值(Stacked 取列总和)。网格 4 条水平线、无纵向网格(对齐 CartesianGrid vertical={false})。
Not ported. Recharts 的动画入场、刷选缩放、雷达/径向图未实现;RTL 文字依赖阿拉伯字形(Geist 不含)。
API Reference
Chart 实体与配置。
| API | Signature | Notes |
|---|---|---|
Chart::new(kind) | ChartKind | Bar | Stacked | Line | Area | Donut. |
.title(.., ..) | &str, &str | Card-style heading + caption above the plot. |
.config(..) | Vec<ChartSeries> | Series (or donut slice) labels + light/dark colors. |
.data(..) | Vec<(&str, Vec<f32>)> | (category, per-series values); donut rows are slices. |
.indicator(..) | IndicatorStyle | Tooltip indicator: Dot (default) | Line. |
.legend(..) | bool | Legend row with series totals. |
.center_label(..) | &str | Donut hole label (e.g. total). |
.dark(..) | bool | Dark theme (colors read from series' dark slots). |
Source & inspiration
结构与视觉参考 shadcn/ui Chart (Recharts) 。本页交互由 Rust / GPUI 绘制,基础样式来自 gpui-kit 。