moi-computer 0.11.0 → 0.11.1-dev.1c52199
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/client/AGENTS.md +2 -2
- package/client/api/app-config.ts +2 -1
- package/client/app/AppRouter.tsx +2 -2
- package/client/app/routes/WorkspaceRoute.tsx +33 -27
- package/client/app/shell/SidebarLayout.tsx +18 -15
- package/client/components/shared/BottomPanel.tsx +26 -14
- package/client/components/shared/PanelHeader.tsx +1 -1
- package/client/components/shared/ReorderableList.tsx +7 -2
- package/client/components/shared/WorkspaceIcon.tsx +92 -0
- package/client/components/ui/alert-dialog.tsx +1 -0
- package/client/components/ui/button.tsx +1 -53
- package/client/components/ui/checkbox.tsx +1 -0
- package/client/components/ui/collapsible.tsx +1 -15
- package/client/components/ui/dialog.tsx +1 -58
- package/client/components/ui/dropdown-menu.tsx +1 -254
- package/client/components/ui/hover-card.tsx +1 -44
- package/client/components/ui/inline-input.tsx +84 -0
- package/client/components/ui/input.tsx +1 -21
- package/client/components/ui/popover.tsx +1 -88
- package/client/components/ui/resizable.tsx +1 -40
- package/client/components/ui/skeleton.tsx +1 -13
- package/client/components/ui/slider.tsx +1 -70
- package/client/components/ui/spinner.tsx +1 -18
- package/client/components/ui/switch.tsx +1 -23
- package/client/components/ui/tabs.tsx +1 -0
- package/client/components/ui/textarea.tsx +1 -18
- package/client/components/ui/toast.tsx +2 -2
- package/client/components/ui/tooltip.tsx +1 -51
- package/client/features/applets/AppletMount.tsx +5 -2
- package/client/features/applets/applet-cache.ts +3 -3
- package/client/features/applets/applet-runtime.ts +158 -108
- package/client/features/applets/applet-styles.ts +2 -2
- package/client/features/applets/applet-thumbnail.ts +6 -8
- package/client/features/applets/useApplet.ts +10 -9
- package/client/features/chat/ChatPanel.tsx +24 -15
- package/client/features/chat/ChatPopup.tsx +9 -5
- package/client/features/chat/applet-chat-intents.ts +179 -0
- package/client/features/chat/chat-send.ts +116 -64
- package/client/features/chat/chat-store.ts +51 -62
- package/client/features/chat/composer/ChatComposer.tsx +43 -128
- package/client/features/chat/composer/ModelPicker.tsx +47 -60
- package/client/features/chat/composer/attachments/AttachmentChip.tsx +99 -0
- package/client/features/chat/composer/attachments/DrawingAttachmentChip.tsx +37 -0
- package/client/features/chat/composer/attachments/FileAttachmentChip.tsx +22 -0
- package/client/features/chat/composer/attachments/TextAttachmentChip.tsx +11 -0
- package/client/features/chat/composer/attachments/draft-attachments.ts +168 -0
- package/client/features/chat/composer/attachments/types.ts +25 -0
- package/client/features/chat/composer/attachments/uploads.ts +26 -0
- package/client/features/chat/composer/banners/ErrorBanner.tsx +20 -12
- package/client/features/chat/composer/model-order.ts +15 -0
- package/client/features/chat/composer/useWorkspaceComposerState.tsx +15 -7
- package/client/features/chat/{chat-connection.ts → connection/chat-connection.ts} +1 -1
- package/client/features/chat/{chat-frames.ts → connection/chat-frames.ts} +10 -8
- package/client/features/chat/{ChatEmptyState.tsx → messages/ChatEmptyState.tsx} +16 -11
- package/client/features/chat/{MarkdownContent.tsx → messages/MarkdownContent.tsx} +16 -1
- package/client/features/chat/{TurnView.tsx → messages/TurnView.tsx} +27 -19
- package/client/features/chat/{group-turns.ts → messages/group-turns.ts} +12 -0
- package/client/features/chat/{interleave-notices.ts → messages/interleave-notices.ts} +7 -11
- package/client/features/chat/{tool-group → messages/tool-group}/SubagentCard.tsx +1 -1
- package/client/features/chat/{tool-group → messages/tool-group}/TimelineRow.tsx +2 -2
- package/client/features/chat/{tool-group → messages/tool-group}/ToolCallGroup.tsx +2 -5
- package/client/features/chat/{tool-group → messages/tool-group}/format/codex.ts +4 -1
- package/client/features/chat/{ChatSelector.tsx → sessions/ChatSelector.tsx} +3 -3
- package/client/features/chat/{api.ts → sessions/api.ts} +5 -14
- package/client/features/chat/sessions/session-view.ts +53 -0
- package/client/features/chat/{useSelectedSession.ts → sessions/useSelectedSession.ts} +64 -20
- package/client/features/chat/useChat.ts +46 -24
- package/client/features/collab/DevCollabIdentity.tsx +170 -0
- package/client/features/collab/DevCollabKit.tsx +501 -0
- package/client/features/collab/DevCollabPage.tsx +25 -0
- package/client/features/collab/WorkspaceCollabControls.tsx +313 -0
- package/client/features/collab/client.ts +146 -0
- package/client/features/collab/components.tsx +307 -0
- package/client/features/collab/dev-persona.ts +66 -0
- package/client/features/collab/engine.ts +96 -0
- package/client/features/collab/entry.tsx +35 -0
- package/client/features/collab/facehash-avatar.ts +132 -0
- package/client/features/collab/fake-engine.ts +130 -0
- package/client/features/collab/hooks.tsx +253 -0
- package/client/features/collab/identity.ts +305 -0
- package/client/features/collab/index.ts +45 -0
- package/client/features/collab/people.ts +102 -0
- package/client/features/collab/personal-state.ts +61 -0
- package/client/features/collab/presence-publisher.ts +29 -0
- package/client/features/collab/presence-target.ts +10 -0
- package/client/features/collab/primitives.tsx +398 -0
- package/client/features/collab/proxy-identity.ts +24 -0
- package/client/features/collab/store.ts +156 -0
- package/client/features/dev/ChatStatesPage.tsx +8 -7
- package/client/features/dev/DevIndexPage.tsx +10 -0
- package/client/features/dev/DevRoutes.tsx +16 -1
- package/client/features/dev/HarnessDebugPage.tsx +11 -8
- package/client/features/dev/ToolCallsPage.tsx +1 -1
- package/client/features/dev/UiComponentPreviews.tsx +855 -0
- package/client/features/dev/UiComponentsPage.tsx +60 -0
- package/client/features/dev/chat-states-fixtures.ts +13 -2
- package/client/features/drawings/useChatAnnotation.ts +17 -17
- package/client/features/drawings/useDrawingLayer.ts +7 -9
- package/client/features/drawings/useViewBuilderSketch.ts +18 -15
- package/client/features/home/HomePage.tsx +8 -7
- package/client/features/home/WorkspacePreview.tsx +2 -2
- package/client/features/home/workspace-presentation.tsx +1 -11
- package/client/features/home/workspace-setup/DemoDialog.tsx +3 -2
- package/client/features/home/workspace-setup/ImportWorkspaceDialog.tsx +5 -8
- package/client/features/home/workspace-setup/WorkspaceAgentStep.tsx +2 -4
- package/client/features/home/workspace-setup/WorkspaceDialogContent.tsx +7 -20
- package/client/features/overview/HiddenPanel.tsx +109 -0
- package/client/features/overview/Overview.tsx +302 -0
- package/client/features/overview/WidgetCanvas.tsx +164 -0
- package/client/features/{widgets → overview}/WidgetFrame.tsx +25 -17
- package/client/features/overview/WorkspaceName.tsx +60 -0
- package/client/features/overview/api.ts +30 -0
- package/client/features/overview/default/ViewsWidget.tsx +252 -0
- package/client/features/overview/default/registry.tsx +36 -0
- package/client/features/{widgets → overview}/grid.ts +36 -0
- package/client/features/overview/useGridReconcile.ts +94 -0
- package/client/features/scratchpad/Scratchpad.tsx +7 -8
- package/client/features/scratchpad/ScratchpadControls.tsx +0 -1
- package/client/features/scratchpad/useScratchpadSnapshot.tsx +23 -13
- package/client/features/settings/GeneralSettings.tsx +55 -58
- package/client/features/settings/SettingsLayout.tsx +2 -2
- package/client/features/settings/WorkspaceIconPicker.tsx +318 -0
- package/client/features/settings/WorkspaceSettingsDialog.tsx +65 -0
- package/client/features/settings/api.ts +48 -24
- package/client/features/views/ViewBuilder.tsx +5 -4
- package/client/features/views/ViewManager.tsx +28 -22
- package/client/features/views/api.ts +36 -2
- package/client/features/views/useViewBuilderActions.ts +11 -5
- package/client/features/views/useViewBuilderDrafts.ts +23 -21
- package/client/features/views/view-presentation.ts +13 -0
- package/client/features/workspace/{CustomizePanel.tsx → ThemePanel.tsx} +26 -31
- package/client/features/workspace/UnavailablePage.tsx +21 -0
- package/client/features/workspace/WorkspaceLayoutContext.tsx +22 -10
- package/client/features/workspace/WorkspaceScreen.tsx +228 -172
- package/client/features/workspace/WorkspaceSplitLayout.tsx +7 -2
- package/client/features/workspace/WorkspaceTabs.tsx +2 -0
- package/client/features/workspace/api.ts +5 -31
- package/client/features/workspace/layout-save.ts +13 -0
- package/client/features/workspace/moi-context.ts +15 -11
- package/client/features/workspace/tab-resolution.ts +10 -33
- package/client/features/workspace/useWorkspaceNavigation.ts +173 -101
- package/client/index.css +23 -30
- package/client/index.tsx +4 -3
- package/client/lib/app-icon-registry.ts +2 -0
- package/client/lib/use-latest-ref.ts +11 -0
- package/client/main.tsx +1 -1
- package/client/runtime/useWorkspaceEvents.ts +59 -14
- package/client/theme.css +5 -5
- package/dist/chunk-1ftessc8.js +2 -0
- package/dist/chunk-3802vh2p.js +2 -0
- package/dist/chunk-542a2m9m.js +1 -0
- package/dist/chunk-5745d83v.js +2 -0
- package/dist/chunk-5zxdsjym.js +1 -0
- package/dist/{chunk-tqd3pkg8.css → chunk-60q353jq.css} +1 -1
- package/dist/chunk-7r4z77ry.js +2 -0
- package/dist/chunk-9cm0wjah.js +79 -0
- package/dist/{chunk-11cab5dq.css → chunk-cq2r11fg.css} +1 -1
- package/dist/chunk-cvgdm1hq.js +2 -0
- package/dist/chunk-gptm4jpd.js +546 -0
- package/dist/chunk-nf2wkvf9.js +14 -0
- package/dist/chunk-wnmf38ta.js +5 -0
- package/dist/chunk-xaehhr1j.css +1 -0
- package/dist/chunk-xpt063fx.js +1 -0
- package/dist/index.html +1 -1
- package/lib/app-icons.ts +1 -0
- package/lib/applet-selector.ts +24 -0
- package/lib/chat-title.ts +6 -1
- package/lib/collab/colors.ts +24 -0
- package/lib/collab/protocol.ts +83 -0
- package/lib/collab/types.ts +63 -0
- package/lib/default-widgets.ts +32 -0
- package/lib/experimental.ts +14 -0
- package/lib/format.ts +67 -7
- package/lib/message-attachments.ts +40 -0
- package/lib/moi-attachments.ts +204 -0
- package/lib/moi-context.ts +27 -35
- package/lib/navigation.ts +84 -0
- package/lib/themes.ts +6 -6
- package/lib/types.ts +103 -40
- package/lib/workspace-icon.ts +20 -0
- package/lib/workspace-layout.ts +22 -2
- package/lib/workspace-tabs.ts +9 -33
- package/package.json +44 -32
- package/registry.json +656 -0
- package/server/AGENTS.md +2 -0
- package/server/agent.ts +1 -1
- package/server/api.ts +162 -68
- package/server/app-config.ts +127 -34
- package/server/applet-typecheck.ts +100 -0
- package/server/applets/applet-css.ts +55 -0
- package/server/{bundler → applets}/build-applet.ts +21 -240
- package/server/applets/build-worker.ts +178 -0
- package/server/applets/config.ts +226 -0
- package/server/applets/dependencies.ts +206 -0
- package/server/{applets.ts → applets/index.ts} +44 -189
- package/server/applets/runtime/base.ts +4 -0
- package/server/applets/runtime/moi.ts +42 -0
- package/server/applets/runtime/rpc.ts +17 -0
- package/server/applets/view-source.ts +67 -0
- package/server/attachment-message.ts +95 -0
- package/server/chat-selection.ts +23 -0
- package/server/cli-ui-components.ts +28 -33
- package/server/cli.ts +226 -120
- package/server/collab/applet-module.ts +50 -0
- package/server/collab/cloudflare-access.ts +77 -0
- package/server/collab/config.ts +18 -0
- package/server/collab/ipc.ts +10 -0
- package/server/collab/manager.ts +407 -0
- package/server/collab/service.ts +137 -0
- package/server/collab/skill/collab-env.d.ts +92 -0
- package/server/collab/skill/index.ts +59 -0
- package/server/collab/worker.ts +52 -0
- package/server/control.ts +63 -42
- package/server/data-dir.ts +1 -1
- package/server/harness/README.md +45 -50
- package/server/harness/acp/NOTES.md +426 -0
- package/server/harness/acp/adapter.ts +7 -21
- package/server/harness/acp/discovery.ts +27 -30
- package/server/harness/acp/model-state.ts +57 -0
- package/server/harness/acp/session.ts +49 -56
- package/server/harness/claude-code/NOTES.md +54 -13
- package/server/harness/claude-code/adapter.ts +8 -25
- package/server/harness/claude-code/cli.ts +40 -0
- package/server/harness/claude-code/index.ts +13 -5
- package/server/harness/claude-code/models.ts +72 -19
- package/server/harness/claude-code/session.ts +447 -343
- package/server/harness/claude-code/sessions.ts +9 -4
- package/server/harness/codex/NOTES.md +222 -350
- package/server/harness/codex/adapter.ts +102 -52
- package/server/harness/codex/auth.ts +1 -3
- package/server/harness/codex/client.ts +169 -249
- package/server/harness/codex/discovery.ts +9 -17
- package/server/harness/codex/index.ts +3 -1
- package/server/harness/codex/pagination.ts +24 -0
- package/server/harness/codex/permissions.ts +32 -8
- package/server/harness/codex/probe.ts +10 -13
- package/server/harness/codex/session.ts +567 -181
- package/server/harness/codex/transport.ts +184 -0
- package/server/harness/debug.ts +1 -1
- package/server/harness/hermes/NOTES.md +19 -1
- package/server/harness/hermes/discovery.ts +20 -0
- package/server/harness/hermes/index.ts +13 -0
- package/server/harness/hermes/models.ts +28 -8
- package/server/harness/openclaw/NOTES.md +1 -1
- package/server/harness/openclaw/adapter.ts +22 -4
- package/server/harness/openclaw/gateway.ts +5 -3
- package/server/harness/openclaw/session.ts +27 -27
- package/server/harness/types.ts +9 -5
- package/server/layout.ts +24 -16
- package/server/moi-scaffold.ts +22 -14
- package/server/navigation-relay.ts +110 -0
- package/server/preview.ts +3 -1
- package/server/registry.ts +71 -41
- package/server/scratchpad-executor.ts +79 -56
- package/server/scratchpad-shape-defaults.ts +82 -0
- package/server/selected-session.ts +24 -4
- package/server/skill-update.ts +5 -3
- package/server/skill-version.ts +4 -7
- package/server/skills-template.ts +60 -9
- package/server/tabs.ts +19 -33
- package/server/tldraw-package.d.ts +4 -0
- package/server/ui-components.ts +101 -434
- package/server/uploads.ts +0 -0
- package/server/view-builders.ts +20 -2
- package/server/views.ts +126 -6
- package/server/web.ts +108 -30
- package/server/widgets.ts +12 -5
- package/server/workspace-config.ts +13 -9
- package/server/workspace-env.ts +3 -2
- package/tsconfig.json +0 -1
- package/ui-components/accordion.tsx +71 -0
- package/ui-components/alert-dialog.tsx +165 -0
- package/ui-components/alert.tsx +68 -0
- package/ui-components/attachment.tsx +195 -0
- package/ui-components/avatar.tsx +91 -0
- package/ui-components/badge.tsx +49 -0
- package/ui-components/button-group.tsx +78 -0
- package/ui-components/button.tsx +55 -0
- package/ui-components/calendar.tsx +180 -0
- package/ui-components/card.tsx +84 -0
- package/ui-components/carousel.tsx +235 -0
- package/ui-components/chart.tsx +338 -0
- package/ui-components/checkbox.tsx +28 -0
- package/ui-components/collapsible.tsx +15 -0
- package/ui-components/combobox.tsx +267 -0
- package/ui-components/context-menu.tsx +244 -0
- package/ui-components/dialog.tsx +138 -0
- package/ui-components/docs/accordion.md +463 -0
- package/ui-components/docs/alert-dialog.md +481 -0
- package/ui-components/docs/alert.md +301 -0
- package/ui-components/docs/attachment.md +689 -0
- package/ui-components/docs/avatar.md +495 -0
- package/ui-components/docs/badge.md +258 -0
- package/ui-components/docs/button-group.md +879 -0
- package/ui-components/docs/button.md +26 -0
- package/ui-components/docs/calendar.md +862 -0
- package/ui-components/docs/card.md +265 -0
- package/ui-components/docs/carousel.md +627 -0
- package/ui-components/docs/chart.md +1256 -0
- package/ui-components/docs/checkbox.md +477 -0
- package/ui-components/docs/collapsible.md +446 -0
- package/ui-components/docs/combobox.md +1003 -0
- package/ui-components/docs/context-menu.md +860 -0
- package/ui-components/docs/data-table.md +1580 -0
- package/ui-components/docs/date-picker.md +614 -0
- package/ui-components/docs/dialog.md +414 -0
- package/ui-components/docs/drawer.md +51 -0
- package/ui-components/docs/dropdown-menu.md +1000 -0
- package/ui-components/docs/field.md +1192 -0
- package/ui-components/docs/hover-card.md +275 -0
- package/ui-components/docs/input-group.md +866 -0
- package/ui-components/docs/input.md +456 -0
- package/ui-components/docs/kbd.md +191 -0
- package/ui-components/docs/label.md +295 -0
- package/ui-components/docs/pagination.md +416 -0
- package/ui-components/docs/popover.md +348 -0
- package/ui-components/docs/progress.md +214 -0
- package/ui-components/docs/radio-group.md +389 -0
- package/ui-components/docs/resizable.md +265 -0
- package/ui-components/docs/select.md +616 -0
- package/ui-components/docs/separator.md +216 -0
- package/ui-components/docs/skeleton.md +205 -0
- package/ui-components/docs/slider.md +226 -0
- package/ui-components/docs/spinner.md +332 -0
- package/ui-components/docs/switch.md +236 -0
- package/ui-components/docs/table.md +551 -0
- package/ui-components/docs/tabs.md +379 -0
- package/ui-components/docs/textarea.md +187 -0
- package/ui-components/docs/toggle-group.md +370 -0
- package/ui-components/docs/toggle.md +91 -0
- package/ui-components/docs/tooltip.md +290 -0
- package/ui-components/drawer.tsx +213 -0
- package/ui-components/dropdown-menu.tsx +263 -0
- package/ui-components/field.tsx +224 -0
- package/ui-components/hover-card.tsx +46 -0
- package/ui-components/input-group.tsx +146 -0
- package/ui-components/input.tsx +20 -0
- package/ui-components/kbd.tsx +32 -0
- package/ui-components/label.tsx +17 -0
- package/ui-components/pagination.tsx +124 -0
- package/ui-components/popover.tsx +81 -0
- package/ui-components/progress.tsx +63 -0
- package/ui-components/radio-group.tsx +38 -0
- package/ui-components/resizable.tsx +40 -0
- package/ui-components/scoped-portal.tsx +31 -0
- package/ui-components/select.tsx +195 -0
- package/ui-components/separator.tsx +18 -0
- package/ui-components/skeleton.tsx +15 -0
- package/ui-components/slider.tsx +75 -0
- package/ui-components/spinner.tsx +20 -0
- package/ui-components/switch.tsx +27 -0
- package/ui-components/table.tsx +86 -0
- package/ui-components/tabs.tsx +75 -0
- package/ui-components/textarea.tsx +17 -0
- package/ui-components/toggle-group.tsx +85 -0
- package/ui-components/toggle.tsx +42 -0
- package/ui-components/tooltip.tsx +61 -0
- package/ui-components/utils.ts +8 -0
- package/workspace/.claude/skills/moi-workspace/SKILL.md +295 -248
- package/workspace/.claude/skills/moi-workspace/references/COLLABORATIVE.md +151 -0
- package/workspace/.claude/skills/moi-workspace/references/DESIGN.md +100 -152
- package/workspace/.claude/skills/moi-workspace/references/INTENTS.md +156 -0
- package/workspace/.claude/skills/moi-workspace/references/UI-COMPONENTS.md +67 -203
- package/client/features/chat/attachment-staging.ts +0 -153
- package/client/features/chat/uploads.ts +0 -18
- package/client/features/chat/useAppletChatMessage.ts +0 -68
- package/client/features/settings/IconPicker.tsx +0 -442
- package/client/features/settings/WorkspaceSettings.tsx +0 -92
- package/client/features/settings/render-icon.ts +0 -173
- package/client/features/widgets/HiddenPanel.tsx +0 -80
- package/client/features/widgets/WidgetGrid.tsx +0 -123
- package/client/features/widgets/Widgets.tsx +0 -264
- package/client/features/widgets/useGridReconcile.ts +0 -73
- package/dist/chunk-104m57qw.js +0 -2
- package/dist/chunk-59zf0m6n.js +0 -1
- package/dist/chunk-bj3akjs2.js +0 -1
- package/dist/chunk-c9pjqx9m.js +0 -14
- package/dist/chunk-j2w2vak4.js +0 -546
- package/dist/chunk-k2r9c95m.js +0 -10
- package/dist/chunk-kmbxhgwv.js +0 -1
- package/dist/chunk-p1yqa71k.js +0 -83
- package/dist/chunk-pcnahct1.js +0 -5
- package/dist/chunk-r5xrenjy.css +0 -1
- package/dist/chunk-tny85yt2.js +0 -2
- package/dist/chunk-v7wr1df4.js +0 -2
- package/server/bundler/applet-css.ts +0 -155
- /package/client/features/chat/{ChatPromptBubbles.tsx → messages/ChatPromptBubbles.tsx} +0 -0
- /package/client/features/chat/{ShimmerText.tsx → messages/ShimmerText.tsx} +0 -0
- /package/client/features/chat/{preview-turn.ts → messages/preview-turn.ts} +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/CodeBlock.tsx +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/Collapse.tsx +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/ReadImagePreview.tsx +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/ToolOutput.tsx +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/detect.ts +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/fixtures.ts +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/format/claude.ts +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/format/hermes.ts +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/format/index.ts +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/format/mcp.ts +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/format/openclaw.ts +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/format/shared.ts +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/format/shell.ts +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/index.ts +0 -0
- /package/client/features/chat/{useStickToBottom.ts → messages/useStickToBottom.ts} +0 -0
|
@@ -0,0 +1,1256 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Chart
|
|
3
|
+
description: Beautiful charts. Built using Recharts. Copy and paste into your apps.
|
|
4
|
+
base: base
|
|
5
|
+
component: true
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
<Callout className="mt-4">
|
|
9
|
+
|
|
10
|
+
**Updated:** The `chart` component now uses Recharts v3. If you're upgrading existing chart code, see [Updating to Recharts v3](#updating-to-recharts-v3).
|
|
11
|
+
|
|
12
|
+
</Callout>
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
'use client'
|
|
16
|
+
|
|
17
|
+
import * as React from 'react'
|
|
18
|
+
import { Bar, BarChart, CartesianGrid, XAxis } from 'recharts'
|
|
19
|
+
|
|
20
|
+
import {
|
|
21
|
+
Card,
|
|
22
|
+
CardContent,
|
|
23
|
+
CardDescription,
|
|
24
|
+
CardHeader,
|
|
25
|
+
CardTitle
|
|
26
|
+
} from '@/registry/new-york-v4/ui/card'
|
|
27
|
+
import {
|
|
28
|
+
ChartContainer,
|
|
29
|
+
ChartTooltip,
|
|
30
|
+
ChartTooltipContent,
|
|
31
|
+
type ChartConfig
|
|
32
|
+
} from '@/registry/new-york-v4/ui/chart'
|
|
33
|
+
|
|
34
|
+
export const description = 'An interactive bar chart'
|
|
35
|
+
|
|
36
|
+
const chartData = [
|
|
37
|
+
{ date: '2024-04-01', desktop: 222, mobile: 150 },
|
|
38
|
+
{ date: '2024-04-02', desktop: 97, mobile: 180 },
|
|
39
|
+
{ date: '2024-04-03', desktop: 167, mobile: 120 },
|
|
40
|
+
{ date: '2024-04-04', desktop: 242, mobile: 260 },
|
|
41
|
+
{ date: '2024-04-05', desktop: 373, mobile: 290 },
|
|
42
|
+
{ date: '2024-04-06', desktop: 301, mobile: 340 },
|
|
43
|
+
{ date: '2024-04-07', desktop: 245, mobile: 180 },
|
|
44
|
+
{ date: '2024-04-08', desktop: 409, mobile: 320 },
|
|
45
|
+
{ date: '2024-04-09', desktop: 59, mobile: 110 },
|
|
46
|
+
{ date: '2024-04-10', desktop: 261, mobile: 190 },
|
|
47
|
+
{ date: '2024-04-11', desktop: 327, mobile: 350 },
|
|
48
|
+
{ date: '2024-04-12', desktop: 292, mobile: 210 },
|
|
49
|
+
{ date: '2024-04-13', desktop: 342, mobile: 380 },
|
|
50
|
+
{ date: '2024-04-14', desktop: 137, mobile: 220 },
|
|
51
|
+
{ date: '2024-04-15', desktop: 120, mobile: 170 },
|
|
52
|
+
{ date: '2024-04-16', desktop: 138, mobile: 190 },
|
|
53
|
+
{ date: '2024-04-17', desktop: 446, mobile: 360 },
|
|
54
|
+
{ date: '2024-04-18', desktop: 364, mobile: 410 },
|
|
55
|
+
{ date: '2024-04-19', desktop: 243, mobile: 180 },
|
|
56
|
+
{ date: '2024-04-20', desktop: 89, mobile: 150 },
|
|
57
|
+
{ date: '2024-04-21', desktop: 137, mobile: 200 },
|
|
58
|
+
{ date: '2024-04-22', desktop: 224, mobile: 170 },
|
|
59
|
+
{ date: '2024-04-23', desktop: 138, mobile: 230 },
|
|
60
|
+
{ date: '2024-04-24', desktop: 387, mobile: 290 },
|
|
61
|
+
{ date: '2024-04-25', desktop: 215, mobile: 250 },
|
|
62
|
+
{ date: '2024-04-26', desktop: 75, mobile: 130 },
|
|
63
|
+
{ date: '2024-04-27', desktop: 383, mobile: 420 },
|
|
64
|
+
{ date: '2024-04-28', desktop: 122, mobile: 180 },
|
|
65
|
+
{ date: '2024-04-29', desktop: 315, mobile: 240 },
|
|
66
|
+
{ date: '2024-04-30', desktop: 454, mobile: 380 }
|
|
67
|
+
]
|
|
68
|
+
|
|
69
|
+
const chartConfig = {
|
|
70
|
+
views: {
|
|
71
|
+
label: 'Page Views'
|
|
72
|
+
},
|
|
73
|
+
desktop: {
|
|
74
|
+
label: 'Desktop',
|
|
75
|
+
color: 'var(--chart-2)'
|
|
76
|
+
},
|
|
77
|
+
mobile: {
|
|
78
|
+
label: 'Mobile',
|
|
79
|
+
color: 'var(--chart-1)'
|
|
80
|
+
}
|
|
81
|
+
} satisfies ChartConfig
|
|
82
|
+
|
|
83
|
+
export function ChartDemo() {
|
|
84
|
+
const [activeChart, setActiveChart] = React.useState<keyof typeof chartConfig>('desktop')
|
|
85
|
+
|
|
86
|
+
const total = React.useMemo(
|
|
87
|
+
() => ({
|
|
88
|
+
desktop: chartData.reduce((acc, curr) => acc + curr.desktop, 0),
|
|
89
|
+
mobile: chartData.reduce((acc, curr) => acc + curr.mobile, 0)
|
|
90
|
+
}),
|
|
91
|
+
[]
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
return (
|
|
95
|
+
<Card className="py-0 pb-4">
|
|
96
|
+
<CardHeader className="flex flex-col items-stretch border-b p-0! sm:flex-row">
|
|
97
|
+
<div className="flex flex-1 flex-col justify-center gap-1 px-6 pt-4 pb-3 sm:py-0!">
|
|
98
|
+
<CardTitle>Bar Chart - Interactive</CardTitle>
|
|
99
|
+
<CardDescription>Showing total visitors for the last 3 months</CardDescription>
|
|
100
|
+
</div>
|
|
101
|
+
<div className="flex">
|
|
102
|
+
{['desktop', 'mobile'].map(key => {
|
|
103
|
+
const chart = key as keyof typeof chartConfig
|
|
104
|
+
return (
|
|
105
|
+
<button
|
|
106
|
+
key={chart}
|
|
107
|
+
data-active={activeChart === chart}
|
|
108
|
+
className="relative z-30 flex flex-1 flex-col justify-center gap-1 border-t px-6 py-4 text-left even:border-l data-[active=true]:bg-muted/50 sm:border-t-0 sm:border-l sm:px-8 sm:py-6"
|
|
109
|
+
onClick={() => setActiveChart(chart)}
|
|
110
|
+
>
|
|
111
|
+
<span className="text-xs text-muted-foreground">{chartConfig[chart].label}</span>
|
|
112
|
+
<span className="text-lg leading-none font-bold sm:text-3xl">
|
|
113
|
+
{total[key as keyof typeof total].toLocaleString()}
|
|
114
|
+
</span>
|
|
115
|
+
</button>
|
|
116
|
+
)
|
|
117
|
+
})}
|
|
118
|
+
</div>
|
|
119
|
+
</CardHeader>
|
|
120
|
+
<CardContent className="px-2 sm:p-6">
|
|
121
|
+
<ChartContainer config={chartConfig} className="aspect-auto h-[250px] w-full">
|
|
122
|
+
<BarChart
|
|
123
|
+
accessibilityLayer
|
|
124
|
+
data={chartData}
|
|
125
|
+
margin={{
|
|
126
|
+
left: 12,
|
|
127
|
+
right: 12
|
|
128
|
+
}}
|
|
129
|
+
>
|
|
130
|
+
<CartesianGrid vertical={false} />
|
|
131
|
+
<XAxis
|
|
132
|
+
dataKey="date"
|
|
133
|
+
tickLine={false}
|
|
134
|
+
axisLine={false}
|
|
135
|
+
tickMargin={8}
|
|
136
|
+
minTickGap={32}
|
|
137
|
+
tickFormatter={value => {
|
|
138
|
+
const date = new Date(value)
|
|
139
|
+
return date.toLocaleDateString('en-US', {
|
|
140
|
+
month: 'short',
|
|
141
|
+
day: 'numeric'
|
|
142
|
+
})
|
|
143
|
+
}}
|
|
144
|
+
/>
|
|
145
|
+
<ChartTooltip
|
|
146
|
+
content={
|
|
147
|
+
<ChartTooltipContent
|
|
148
|
+
className="w-[150px]"
|
|
149
|
+
nameKey="views"
|
|
150
|
+
labelFormatter={value => {
|
|
151
|
+
return new Date(value).toLocaleDateString('en-US', {
|
|
152
|
+
month: 'short',
|
|
153
|
+
day: 'numeric',
|
|
154
|
+
year: 'numeric'
|
|
155
|
+
})
|
|
156
|
+
}}
|
|
157
|
+
/>
|
|
158
|
+
}
|
|
159
|
+
/>
|
|
160
|
+
<Bar dataKey={activeChart} fill={`var(--color-${activeChart})`} />
|
|
161
|
+
</BarChart>
|
|
162
|
+
</ChartContainer>
|
|
163
|
+
</CardContent>
|
|
164
|
+
</Card>
|
|
165
|
+
)
|
|
166
|
+
}
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
Introducing **Charts**. A collection of chart components that you can copy and paste into your apps.
|
|
170
|
+
|
|
171
|
+
Charts are designed to look great out of the box. They work well with the other components and are fully customizable to fit your project.
|
|
172
|
+
|
|
173
|
+
[Browse the Charts Library](/charts).
|
|
174
|
+
|
|
175
|
+
## Component
|
|
176
|
+
|
|
177
|
+
We use [Recharts](https://recharts.org/) under the hood.
|
|
178
|
+
|
|
179
|
+
We designed the `chart` component with composition in mind. **You build your charts using Recharts components and only bring in custom components, such as `ChartTooltip`, when and where you need it**.
|
|
180
|
+
|
|
181
|
+
```tsx showLineNumbers /ChartContainer/ /ChartTooltipContent/
|
|
182
|
+
import { Bar, BarChart } from 'recharts'
|
|
183
|
+
|
|
184
|
+
import { ChartContainer, ChartTooltipContent } from '@/components/ui/chart'
|
|
185
|
+
|
|
186
|
+
export function MyChart() {
|
|
187
|
+
return (
|
|
188
|
+
<ChartContainer>
|
|
189
|
+
<BarChart data={data}>
|
|
190
|
+
<Bar dataKey="value" />
|
|
191
|
+
<ChartTooltip content={<ChartTooltipContent />} />
|
|
192
|
+
</BarChart>
|
|
193
|
+
</ChartContainer>
|
|
194
|
+
)
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
We do not wrap Recharts. This means you're not locked into an abstraction. When a new Recharts version is released, you can follow the official upgrade path to upgrade your charts.
|
|
199
|
+
|
|
200
|
+
**The components are yours**.
|
|
201
|
+
|
|
202
|
+
## Updating to Recharts v3
|
|
203
|
+
|
|
204
|
+
If you're updating older chart code to Recharts v3:
|
|
205
|
+
|
|
206
|
+
- Use `var(--chart-1)` instead of `hsl(var(--chart-1))` when you reference chart tokens from your CSS variables.
|
|
207
|
+
- Use `ChartTooltip.defaultIndex` for initial tooltip state only. Keep persistent active shapes in your own chart state.
|
|
208
|
+
- Remove `layout` from `<Bar>` when the parent `<BarChart>` already defines it.
|
|
209
|
+
- Keep a height, `min-h-*`, or `aspect-*` on `ChartContainer` so `ResponsiveContainer` can measure on first render.
|
|
210
|
+
|
|
211
|
+
## Installation
|
|
212
|
+
|
|
213
|
+
<CodeTabs>
|
|
214
|
+
|
|
215
|
+
<TabsList>
|
|
216
|
+
<TabsTrigger value="cli">Command</TabsTrigger>
|
|
217
|
+
<TabsTrigger value="manual">Manual</TabsTrigger>
|
|
218
|
+
</TabsList>
|
|
219
|
+
<TabsContent value="cli">
|
|
220
|
+
|
|
221
|
+
```bash
|
|
222
|
+
moi ui-components add chart
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
</TabsContent>
|
|
226
|
+
|
|
227
|
+
<TabsContent value="manual">
|
|
228
|
+
|
|
229
|
+
<Steps className="mb-0 pt-2">
|
|
230
|
+
|
|
231
|
+
<Step>Install the following dependencies:</Step>
|
|
232
|
+
|
|
233
|
+
```bash
|
|
234
|
+
npm install recharts
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
<Step>Copy and paste the following code into `components/ui/chart.tsx`.</Step>
|
|
238
|
+
|
|
239
|
+
<ComponentSource
|
|
240
|
+
name="chart"
|
|
241
|
+
title="components/ui/chart.tsx"
|
|
242
|
+
styleName="base-nova"
|
|
243
|
+
/>
|
|
244
|
+
|
|
245
|
+
<Step>Add the following colors to your CSS file</Step>
|
|
246
|
+
|
|
247
|
+
```css title="app/globals.css" showLineNumbers
|
|
248
|
+
@layer base {
|
|
249
|
+
:root {
|
|
250
|
+
--chart-1: oklch(0.646 0.222 41.116);
|
|
251
|
+
--chart-2: oklch(0.6 0.118 184.704);
|
|
252
|
+
--chart-3: oklch(0.398 0.07 227.392);
|
|
253
|
+
--chart-4: oklch(0.828 0.189 84.429);
|
|
254
|
+
--chart-5: oklch(0.769 0.188 70.08);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
.dark {
|
|
258
|
+
--chart-1: oklch(0.488 0.243 264.376);
|
|
259
|
+
--chart-2: oklch(0.696 0.17 162.48);
|
|
260
|
+
--chart-3: oklch(0.769 0.188 70.08);
|
|
261
|
+
--chart-4: oklch(0.627 0.265 303.9);
|
|
262
|
+
--chart-5: oklch(0.645 0.246 16.439);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
</Steps>
|
|
268
|
+
|
|
269
|
+
</TabsContent>
|
|
270
|
+
|
|
271
|
+
</CodeTabs>
|
|
272
|
+
|
|
273
|
+
## Your First Chart
|
|
274
|
+
|
|
275
|
+
Let's build your first chart. We'll build a bar chart, add a grid, axis, tooltip and legend.
|
|
276
|
+
|
|
277
|
+
<Steps>
|
|
278
|
+
|
|
279
|
+
<Step>Start by defining your data</Step>
|
|
280
|
+
|
|
281
|
+
The following data represents the number of desktop and mobile users for each month.
|
|
282
|
+
|
|
283
|
+
<Callout className="mt-4">
|
|
284
|
+
|
|
285
|
+
**Note:** Your data can be in any shape. You are not limited to the shape of the data below. Use the `dataKey` prop to map your data to the chart.
|
|
286
|
+
|
|
287
|
+
</Callout>
|
|
288
|
+
|
|
289
|
+
```tsx title="components/example-chart.tsx" showLineNumbers
|
|
290
|
+
const chartData = [
|
|
291
|
+
{ month: 'January', desktop: 186, mobile: 80 },
|
|
292
|
+
{ month: 'February', desktop: 305, mobile: 200 },
|
|
293
|
+
{ month: 'March', desktop: 237, mobile: 120 },
|
|
294
|
+
{ month: 'April', desktop: 73, mobile: 190 },
|
|
295
|
+
{ month: 'May', desktop: 209, mobile: 130 },
|
|
296
|
+
{ month: 'June', desktop: 214, mobile: 140 }
|
|
297
|
+
]
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
<Step>Define your chart config</Step>
|
|
301
|
+
|
|
302
|
+
The chart config holds configuration for the chart. This is where you place human-readable strings, such as labels, icons and color tokens for theming.
|
|
303
|
+
|
|
304
|
+
```tsx title="components/example-chart.tsx" showLineNumbers
|
|
305
|
+
import { type ChartConfig } from '@/components/ui/chart'
|
|
306
|
+
|
|
307
|
+
const chartConfig = {
|
|
308
|
+
desktop: {
|
|
309
|
+
label: 'Desktop',
|
|
310
|
+
color: '#2563eb'
|
|
311
|
+
},
|
|
312
|
+
mobile: {
|
|
313
|
+
label: 'Mobile',
|
|
314
|
+
color: '#60a5fa'
|
|
315
|
+
}
|
|
316
|
+
} satisfies ChartConfig
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
<Step>Build your chart</Step>
|
|
320
|
+
|
|
321
|
+
You can now build your chart using Recharts components.
|
|
322
|
+
|
|
323
|
+
<Callout className="mt-4 bg-amber-50 border-amber-200 dark:bg-amber-950/50 dark:border-amber-950">
|
|
324
|
+
|
|
325
|
+
**Important:** Remember to set a `min-h-[VALUE]` on the `ChartContainer` component. This is required for the chart to be responsive.
|
|
326
|
+
|
|
327
|
+
</Callout>
|
|
328
|
+
|
|
329
|
+
```tsx
|
|
330
|
+
'use client'
|
|
331
|
+
|
|
332
|
+
import { Bar, BarChart } from 'recharts'
|
|
333
|
+
|
|
334
|
+
import { ChartContainer, type ChartConfig } from '@/components/ui/chart'
|
|
335
|
+
|
|
336
|
+
const chartData = [
|
|
337
|
+
{ month: 'January', desktop: 186, mobile: 80 },
|
|
338
|
+
{ month: 'February', desktop: 305, mobile: 200 },
|
|
339
|
+
{ month: 'March', desktop: 237, mobile: 120 },
|
|
340
|
+
{ month: 'April', desktop: 73, mobile: 190 },
|
|
341
|
+
{ month: 'May', desktop: 209, mobile: 130 },
|
|
342
|
+
{ month: 'June', desktop: 214, mobile: 140 }
|
|
343
|
+
]
|
|
344
|
+
|
|
345
|
+
const chartConfig = {
|
|
346
|
+
desktop: {
|
|
347
|
+
label: 'Desktop',
|
|
348
|
+
color: '#2563eb'
|
|
349
|
+
},
|
|
350
|
+
mobile: {
|
|
351
|
+
label: 'Mobile',
|
|
352
|
+
color: '#60a5fa'
|
|
353
|
+
}
|
|
354
|
+
} satisfies ChartConfig
|
|
355
|
+
|
|
356
|
+
export function ChartExample() {
|
|
357
|
+
return (
|
|
358
|
+
<ChartContainer config={chartConfig} className="min-h-[200px] w-full">
|
|
359
|
+
<BarChart accessibilityLayer data={chartData}>
|
|
360
|
+
<Bar dataKey="desktop" fill="var(--color-desktop)" radius={4} />
|
|
361
|
+
<Bar dataKey="mobile" fill="var(--color-mobile)" radius={4} />
|
|
362
|
+
</BarChart>
|
|
363
|
+
</ChartContainer>
|
|
364
|
+
)
|
|
365
|
+
}
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
</Steps>
|
|
369
|
+
|
|
370
|
+
### Add a Grid
|
|
371
|
+
|
|
372
|
+
Let's add a grid to the chart.
|
|
373
|
+
|
|
374
|
+
<Steps className="mb-0 pt-2">
|
|
375
|
+
|
|
376
|
+
<Step>Import the `CartesianGrid` component.</Step>
|
|
377
|
+
|
|
378
|
+
```tsx /CartesianGrid/
|
|
379
|
+
import { Bar, BarChart, CartesianGrid } from 'recharts'
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
<Step>Add the `CartesianGrid` component to your chart.</Step>
|
|
383
|
+
|
|
384
|
+
```tsx showLineNumbers {3}
|
|
385
|
+
<ChartContainer config={chartConfig} className="min-h-[200px] w-full">
|
|
386
|
+
<BarChart accessibilityLayer data={chartData}>
|
|
387
|
+
<CartesianGrid vertical={false} />
|
|
388
|
+
<Bar dataKey="desktop" fill="var(--color-desktop)" radius={4} />
|
|
389
|
+
<Bar dataKey="mobile" fill="var(--color-mobile)" radius={4} />
|
|
390
|
+
</BarChart>
|
|
391
|
+
</ChartContainer>
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
```tsx
|
|
395
|
+
'use client'
|
|
396
|
+
|
|
397
|
+
import { Bar, BarChart, CartesianGrid } from 'recharts'
|
|
398
|
+
|
|
399
|
+
import { ChartContainer, type ChartConfig } from '@/components/ui/chart'
|
|
400
|
+
|
|
401
|
+
const chartData = [
|
|
402
|
+
{ month: 'January', desktop: 186, mobile: 80 },
|
|
403
|
+
{ month: 'February', desktop: 305, mobile: 200 },
|
|
404
|
+
{ month: 'March', desktop: 237, mobile: 120 },
|
|
405
|
+
{ month: 'April', desktop: 73, mobile: 190 },
|
|
406
|
+
{ month: 'May', desktop: 209, mobile: 130 },
|
|
407
|
+
{ month: 'June', desktop: 214, mobile: 140 }
|
|
408
|
+
]
|
|
409
|
+
|
|
410
|
+
const chartConfig = {
|
|
411
|
+
desktop: {
|
|
412
|
+
label: 'Desktop',
|
|
413
|
+
color: '#2563eb'
|
|
414
|
+
},
|
|
415
|
+
mobile: {
|
|
416
|
+
label: 'Mobile',
|
|
417
|
+
color: '#60a5fa'
|
|
418
|
+
}
|
|
419
|
+
} satisfies ChartConfig
|
|
420
|
+
|
|
421
|
+
export function ChartBarDemoGrid() {
|
|
422
|
+
return (
|
|
423
|
+
<ChartContainer config={chartConfig} className="min-h-[200px] w-full">
|
|
424
|
+
<BarChart accessibilityLayer data={chartData}>
|
|
425
|
+
<CartesianGrid vertical={false} />
|
|
426
|
+
<Bar dataKey="desktop" fill="var(--color-desktop)" radius={4} />
|
|
427
|
+
<Bar dataKey="mobile" fill="var(--color-mobile)" radius={4} />
|
|
428
|
+
</BarChart>
|
|
429
|
+
</ChartContainer>
|
|
430
|
+
)
|
|
431
|
+
}
|
|
432
|
+
```
|
|
433
|
+
|
|
434
|
+
</Steps>
|
|
435
|
+
|
|
436
|
+
### Add an Axis
|
|
437
|
+
|
|
438
|
+
To add an x-axis to the chart, we'll use the `XAxis` component.
|
|
439
|
+
|
|
440
|
+
<Steps className="mb-0 pt-2">
|
|
441
|
+
|
|
442
|
+
<Step>Import the `XAxis` component.</Step>
|
|
443
|
+
|
|
444
|
+
```tsx /XAxis/
|
|
445
|
+
import { Bar, BarChart, CartesianGrid, XAxis } from 'recharts'
|
|
446
|
+
```
|
|
447
|
+
|
|
448
|
+
<Step>Add the `XAxis` component to your chart.</Step>
|
|
449
|
+
|
|
450
|
+
```tsx showLineNumbers {4-10}
|
|
451
|
+
<ChartContainer config={chartConfig} className="h-[200px] w-full">
|
|
452
|
+
<BarChart accessibilityLayer data={chartData}>
|
|
453
|
+
<CartesianGrid vertical={false} />
|
|
454
|
+
<XAxis
|
|
455
|
+
dataKey="month"
|
|
456
|
+
tickLine={false}
|
|
457
|
+
tickMargin={10}
|
|
458
|
+
axisLine={false}
|
|
459
|
+
tickFormatter={value => value.slice(0, 3)}
|
|
460
|
+
/>
|
|
461
|
+
<Bar dataKey="desktop" fill="var(--color-desktop)" radius={4} />
|
|
462
|
+
<Bar dataKey="mobile" fill="var(--color-mobile)" radius={4} />
|
|
463
|
+
</BarChart>
|
|
464
|
+
</ChartContainer>
|
|
465
|
+
```
|
|
466
|
+
|
|
467
|
+
```tsx
|
|
468
|
+
'use client'
|
|
469
|
+
|
|
470
|
+
import { Bar, BarChart, CartesianGrid, XAxis } from 'recharts'
|
|
471
|
+
|
|
472
|
+
import { ChartContainer, type ChartConfig } from '@/components/ui/chart'
|
|
473
|
+
|
|
474
|
+
const chartData = [
|
|
475
|
+
{ month: 'January', desktop: 186, mobile: 80 },
|
|
476
|
+
{ month: 'February', desktop: 305, mobile: 200 },
|
|
477
|
+
{ month: 'March', desktop: 237, mobile: 120 },
|
|
478
|
+
{ month: 'April', desktop: 73, mobile: 190 },
|
|
479
|
+
{ month: 'May', desktop: 209, mobile: 130 },
|
|
480
|
+
{ month: 'June', desktop: 214, mobile: 140 }
|
|
481
|
+
]
|
|
482
|
+
|
|
483
|
+
const chartConfig = {
|
|
484
|
+
desktop: {
|
|
485
|
+
label: 'Desktop',
|
|
486
|
+
color: '#2563eb'
|
|
487
|
+
},
|
|
488
|
+
mobile: {
|
|
489
|
+
label: 'Mobile',
|
|
490
|
+
color: '#60a5fa'
|
|
491
|
+
}
|
|
492
|
+
} satisfies ChartConfig
|
|
493
|
+
|
|
494
|
+
export function ChartBarDemoAxis() {
|
|
495
|
+
return (
|
|
496
|
+
<ChartContainer config={chartConfig} className="min-h-[200px] w-full">
|
|
497
|
+
<BarChart accessibilityLayer data={chartData}>
|
|
498
|
+
<CartesianGrid vertical={false} />
|
|
499
|
+
<XAxis
|
|
500
|
+
dataKey="month"
|
|
501
|
+
tickLine={false}
|
|
502
|
+
tickMargin={10}
|
|
503
|
+
axisLine={false}
|
|
504
|
+
tickFormatter={value => value.slice(0, 3)}
|
|
505
|
+
/>
|
|
506
|
+
<Bar dataKey="desktop" fill="var(--color-desktop)" radius={4} />
|
|
507
|
+
<Bar dataKey="mobile" fill="var(--color-mobile)" radius={4} />
|
|
508
|
+
</BarChart>
|
|
509
|
+
</ChartContainer>
|
|
510
|
+
)
|
|
511
|
+
}
|
|
512
|
+
```
|
|
513
|
+
|
|
514
|
+
</Steps>
|
|
515
|
+
|
|
516
|
+
### Add Tooltip
|
|
517
|
+
|
|
518
|
+
So far we've only used components from Recharts. They look great out of the box thanks to some customization in the `chart` component.
|
|
519
|
+
|
|
520
|
+
To add a tooltip, we'll use the custom `ChartTooltip` and `ChartTooltipContent` components from `chart`.
|
|
521
|
+
|
|
522
|
+
<Steps className="mb-0 pt-2">
|
|
523
|
+
|
|
524
|
+
<Step>Import the `ChartTooltip` and `ChartTooltipContent` components.</Step>
|
|
525
|
+
|
|
526
|
+
```tsx
|
|
527
|
+
import { ChartTooltip, ChartTooltipContent } from '@/components/ui/chart'
|
|
528
|
+
```
|
|
529
|
+
|
|
530
|
+
<Step>Add the components to your chart.</Step>
|
|
531
|
+
|
|
532
|
+
```tsx showLineNumbers {11}
|
|
533
|
+
<ChartContainer config={chartConfig} className="h-[200px] w-full">
|
|
534
|
+
<BarChart accessibilityLayer data={chartData}>
|
|
535
|
+
<CartesianGrid vertical={false} />
|
|
536
|
+
<XAxis
|
|
537
|
+
dataKey="month"
|
|
538
|
+
tickLine={false}
|
|
539
|
+
tickMargin={10}
|
|
540
|
+
axisLine={false}
|
|
541
|
+
tickFormatter={value => value.slice(0, 3)}
|
|
542
|
+
/>
|
|
543
|
+
<ChartTooltip content={<ChartTooltipContent />} />
|
|
544
|
+
<Bar dataKey="desktop" fill="var(--color-desktop)" radius={4} />
|
|
545
|
+
<Bar dataKey="mobile" fill="var(--color-mobile)" radius={4} />
|
|
546
|
+
</BarChart>
|
|
547
|
+
</ChartContainer>
|
|
548
|
+
```
|
|
549
|
+
|
|
550
|
+
```tsx
|
|
551
|
+
'use client'
|
|
552
|
+
|
|
553
|
+
import { Bar, BarChart, CartesianGrid, XAxis } from 'recharts'
|
|
554
|
+
|
|
555
|
+
import {
|
|
556
|
+
ChartContainer,
|
|
557
|
+
ChartTooltip,
|
|
558
|
+
ChartTooltipContent,
|
|
559
|
+
type ChartConfig
|
|
560
|
+
} from '@/components/ui/chart'
|
|
561
|
+
|
|
562
|
+
const chartData = [
|
|
563
|
+
{ month: 'January', desktop: 186, mobile: 80 },
|
|
564
|
+
{ month: 'February', desktop: 305, mobile: 200 },
|
|
565
|
+
{ month: 'March', desktop: 237, mobile: 120 },
|
|
566
|
+
{ month: 'April', desktop: 73, mobile: 190 },
|
|
567
|
+
{ month: 'May', desktop: 209, mobile: 130 },
|
|
568
|
+
{ month: 'June', desktop: 214, mobile: 140 }
|
|
569
|
+
]
|
|
570
|
+
|
|
571
|
+
const chartConfig = {
|
|
572
|
+
desktop: {
|
|
573
|
+
label: 'Desktop',
|
|
574
|
+
color: '#2563eb'
|
|
575
|
+
},
|
|
576
|
+
mobile: {
|
|
577
|
+
label: 'Mobile',
|
|
578
|
+
color: '#60a5fa'
|
|
579
|
+
}
|
|
580
|
+
} satisfies ChartConfig
|
|
581
|
+
|
|
582
|
+
export function ChartBarDemoTooltip() {
|
|
583
|
+
return (
|
|
584
|
+
<ChartContainer config={chartConfig} className="min-h-[200px] w-full">
|
|
585
|
+
<BarChart accessibilityLayer data={chartData}>
|
|
586
|
+
<CartesianGrid vertical={false} />
|
|
587
|
+
<XAxis
|
|
588
|
+
dataKey="month"
|
|
589
|
+
tickLine={false}
|
|
590
|
+
tickMargin={10}
|
|
591
|
+
axisLine={false}
|
|
592
|
+
tickFormatter={value => value.slice(0, 3)}
|
|
593
|
+
/>
|
|
594
|
+
<ChartTooltip content={<ChartTooltipContent />} />
|
|
595
|
+
<Bar dataKey="desktop" fill="var(--color-desktop)" radius={4} />
|
|
596
|
+
<Bar dataKey="mobile" fill="var(--color-mobile)" radius={4} />
|
|
597
|
+
</BarChart>
|
|
598
|
+
</ChartContainer>
|
|
599
|
+
)
|
|
600
|
+
}
|
|
601
|
+
```
|
|
602
|
+
|
|
603
|
+
Hover to see the tooltips. Easy, right? Two components, and we've got a beautiful tooltip.
|
|
604
|
+
|
|
605
|
+
</Steps>
|
|
606
|
+
|
|
607
|
+
### Add Legend
|
|
608
|
+
|
|
609
|
+
We'll do the same for the legend. We'll use the `ChartLegend` and `ChartLegendContent` components from `chart`.
|
|
610
|
+
|
|
611
|
+
<Steps className="mb-0 pt-2">
|
|
612
|
+
|
|
613
|
+
<Step>Import the `ChartLegend` and `ChartLegendContent` components.</Step>
|
|
614
|
+
|
|
615
|
+
```tsx
|
|
616
|
+
import { ChartLegend, ChartLegendContent } from '@/components/ui/chart'
|
|
617
|
+
```
|
|
618
|
+
|
|
619
|
+
<Step>Add the components to your chart.</Step>
|
|
620
|
+
|
|
621
|
+
```tsx showLineNumbers {12}
|
|
622
|
+
<ChartContainer config={chartConfig} className="h-[200px] w-full">
|
|
623
|
+
<BarChart accessibilityLayer data={chartData}>
|
|
624
|
+
<CartesianGrid vertical={false} />
|
|
625
|
+
<XAxis
|
|
626
|
+
dataKey="month"
|
|
627
|
+
tickLine={false}
|
|
628
|
+
tickMargin={10}
|
|
629
|
+
axisLine={false}
|
|
630
|
+
tickFormatter={value => value.slice(0, 3)}
|
|
631
|
+
/>
|
|
632
|
+
<ChartTooltip content={<ChartTooltipContent />} />
|
|
633
|
+
<ChartLegend content={<ChartLegendContent />} />
|
|
634
|
+
<Bar dataKey="desktop" fill="var(--color-desktop)" radius={4} />
|
|
635
|
+
<Bar dataKey="mobile" fill="var(--color-mobile)" radius={4} />
|
|
636
|
+
</BarChart>
|
|
637
|
+
</ChartContainer>
|
|
638
|
+
```
|
|
639
|
+
|
|
640
|
+
```tsx
|
|
641
|
+
'use client'
|
|
642
|
+
|
|
643
|
+
import { Bar, BarChart, CartesianGrid, XAxis } from 'recharts'
|
|
644
|
+
|
|
645
|
+
import {
|
|
646
|
+
ChartContainer,
|
|
647
|
+
ChartLegend,
|
|
648
|
+
ChartLegendContent,
|
|
649
|
+
ChartTooltip,
|
|
650
|
+
ChartTooltipContent,
|
|
651
|
+
type ChartConfig
|
|
652
|
+
} from '@/components/ui/chart'
|
|
653
|
+
|
|
654
|
+
const chartData = [
|
|
655
|
+
{ month: 'January', desktop: 186, mobile: 80 },
|
|
656
|
+
{ month: 'February', desktop: 305, mobile: 200 },
|
|
657
|
+
{ month: 'March', desktop: 237, mobile: 120 },
|
|
658
|
+
{ month: 'April', desktop: 73, mobile: 190 },
|
|
659
|
+
{ month: 'May', desktop: 209, mobile: 130 },
|
|
660
|
+
{ month: 'June', desktop: 214, mobile: 140 }
|
|
661
|
+
]
|
|
662
|
+
|
|
663
|
+
const chartConfig = {
|
|
664
|
+
desktop: {
|
|
665
|
+
label: 'Desktop',
|
|
666
|
+
color: '#2563eb'
|
|
667
|
+
},
|
|
668
|
+
mobile: {
|
|
669
|
+
label: 'Mobile',
|
|
670
|
+
color: '#60a5fa'
|
|
671
|
+
}
|
|
672
|
+
} satisfies ChartConfig
|
|
673
|
+
|
|
674
|
+
export function ChartBarDemoLegend() {
|
|
675
|
+
return (
|
|
676
|
+
<ChartContainer config={chartConfig} className="min-h-[200px] w-full">
|
|
677
|
+
<BarChart accessibilityLayer data={chartData}>
|
|
678
|
+
<CartesianGrid vertical={false} />
|
|
679
|
+
<XAxis
|
|
680
|
+
dataKey="month"
|
|
681
|
+
tickLine={false}
|
|
682
|
+
tickMargin={10}
|
|
683
|
+
axisLine={false}
|
|
684
|
+
tickFormatter={value => value.slice(0, 3)}
|
|
685
|
+
/>
|
|
686
|
+
<ChartTooltip content={<ChartTooltipContent />} />
|
|
687
|
+
<ChartLegend content={<ChartLegendContent />} />
|
|
688
|
+
<Bar dataKey="desktop" fill="var(--color-desktop)" radius={4} />
|
|
689
|
+
<Bar dataKey="mobile" fill="var(--color-mobile)" radius={4} />
|
|
690
|
+
</BarChart>
|
|
691
|
+
</ChartContainer>
|
|
692
|
+
)
|
|
693
|
+
}
|
|
694
|
+
```
|
|
695
|
+
|
|
696
|
+
</Steps>
|
|
697
|
+
|
|
698
|
+
Done. You've built your first chart! What's next?
|
|
699
|
+
|
|
700
|
+
- [Themes and Colors](/docs/components/chart#theming)
|
|
701
|
+
- [Tooltip](/docs/components/chart#tooltip)
|
|
702
|
+
- [Legend](/docs/components/chart#legend)
|
|
703
|
+
|
|
704
|
+
## Chart Config
|
|
705
|
+
|
|
706
|
+
The chart config is where you define the labels, icons and colors for a chart.
|
|
707
|
+
|
|
708
|
+
It is intentionally decoupled from chart data.
|
|
709
|
+
|
|
710
|
+
This allows you to share config and color tokens between charts. It can also work independently for cases where your data or color tokens live remotely or in a different format.
|
|
711
|
+
|
|
712
|
+
```tsx showLineNumbers /ChartConfig/
|
|
713
|
+
import { IconDeviceDesktop } from '@tabler/icons-react'
|
|
714
|
+
|
|
715
|
+
import { type ChartConfig } from '@/components/ui/chart'
|
|
716
|
+
|
|
717
|
+
const chartConfig = {
|
|
718
|
+
desktop: {
|
|
719
|
+
label: 'Desktop',
|
|
720
|
+
icon: IconDeviceDesktop,
|
|
721
|
+
// A color like 'hsl(220, 98%, 61%)' or 'var(--color-name)'
|
|
722
|
+
color: '#2563eb',
|
|
723
|
+
// OR a theme object with 'light' and 'dark' keys
|
|
724
|
+
theme: {
|
|
725
|
+
light: '#2563eb',
|
|
726
|
+
dark: '#dc2626'
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
} satisfies ChartConfig
|
|
730
|
+
```
|
|
731
|
+
|
|
732
|
+
## Theming
|
|
733
|
+
|
|
734
|
+
Charts have built-in support for theming. You can use css variables (recommended) or color values in any color format, such as hex, hsl or oklch.
|
|
735
|
+
|
|
736
|
+
### CSS Variables
|
|
737
|
+
|
|
738
|
+
<Steps className="mb-0 pt-2">
|
|
739
|
+
|
|
740
|
+
<Step>Define your colors in your css file</Step>
|
|
741
|
+
|
|
742
|
+
```css title="app/globals.css" showLineNumbers
|
|
743
|
+
@layer base {
|
|
744
|
+
:root {
|
|
745
|
+
--chart-1: oklch(0.646 0.222 41.116);
|
|
746
|
+
--chart-2: oklch(0.6 0.118 184.704);
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
.dark {
|
|
750
|
+
--chart-1: oklch(0.488 0.243 264.376);
|
|
751
|
+
--chart-2: oklch(0.696 0.17 162.48);
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
```
|
|
755
|
+
|
|
756
|
+
<Step>Add the color to your `chartConfig`</Step>
|
|
757
|
+
|
|
758
|
+
```tsx title="components/example-chart.tsx" showLineNumbers
|
|
759
|
+
const chartConfig = {
|
|
760
|
+
desktop: {
|
|
761
|
+
label: 'Desktop',
|
|
762
|
+
color: 'var(--chart-1)'
|
|
763
|
+
},
|
|
764
|
+
mobile: {
|
|
765
|
+
label: 'Mobile',
|
|
766
|
+
color: 'var(--chart-2)'
|
|
767
|
+
}
|
|
768
|
+
} satisfies ChartConfig
|
|
769
|
+
```
|
|
770
|
+
|
|
771
|
+
</Steps>
|
|
772
|
+
|
|
773
|
+
### hex, hsl or oklch
|
|
774
|
+
|
|
775
|
+
You can also define your colors directly in the chart config. Use the color format you prefer.
|
|
776
|
+
|
|
777
|
+
```tsx title="components/example-chart.tsx" showLineNumbers
|
|
778
|
+
const chartConfig = {
|
|
779
|
+
desktop: {
|
|
780
|
+
label: 'Desktop',
|
|
781
|
+
color: '#2563eb'
|
|
782
|
+
},
|
|
783
|
+
mobile: {
|
|
784
|
+
label: 'Mobile',
|
|
785
|
+
color: 'hsl(220, 98%, 61%)'
|
|
786
|
+
},
|
|
787
|
+
tablet: {
|
|
788
|
+
label: 'Tablet',
|
|
789
|
+
color: 'oklch(0.5 0.2 240)'
|
|
790
|
+
},
|
|
791
|
+
laptop: {
|
|
792
|
+
label: 'Laptop',
|
|
793
|
+
color: 'var(--chart-2)'
|
|
794
|
+
}
|
|
795
|
+
} satisfies ChartConfig
|
|
796
|
+
```
|
|
797
|
+
|
|
798
|
+
### Using Colors
|
|
799
|
+
|
|
800
|
+
To use the theme colors in your chart, reference the colors using the format `var(--color-KEY)`.
|
|
801
|
+
|
|
802
|
+
#### Components
|
|
803
|
+
|
|
804
|
+
```tsx
|
|
805
|
+
<Bar dataKey="desktop" fill="var(--color-desktop)" />
|
|
806
|
+
```
|
|
807
|
+
|
|
808
|
+
#### Chart Data
|
|
809
|
+
|
|
810
|
+
```tsx title="components/example-chart.tsx" showLineNumbers
|
|
811
|
+
const chartData = [
|
|
812
|
+
{ browser: 'chrome', visitors: 275, fill: 'var(--color-chrome)' },
|
|
813
|
+
{ browser: 'safari', visitors: 200, fill: 'var(--color-safari)' }
|
|
814
|
+
]
|
|
815
|
+
```
|
|
816
|
+
|
|
817
|
+
#### Tailwind
|
|
818
|
+
|
|
819
|
+
```tsx title="components/example-chart.tsx"
|
|
820
|
+
<LabelList className="fill-(--color-desktop)" />
|
|
821
|
+
```
|
|
822
|
+
|
|
823
|
+
## Tooltip
|
|
824
|
+
|
|
825
|
+
A chart tooltip contains a label, name, indicator and value. You can use a combination of these to customize your tooltip.
|
|
826
|
+
|
|
827
|
+
```tsx
|
|
828
|
+
'use client'
|
|
829
|
+
|
|
830
|
+
import * as React from 'react'
|
|
831
|
+
import { cn } from 'cn'
|
|
832
|
+
|
|
833
|
+
export function ChartTooltipDemo() {
|
|
834
|
+
return (
|
|
835
|
+
<div className="grid aspect-video w-full max-w-md justify-center text-foreground md:grid-cols-2 [&>div]:relative [&>div]:flex [&>div]:h-[137px] [&>div]:w-[224px] [&>div]:items-center [&>div]:justify-center [&>div]:p-4">
|
|
836
|
+
<div>
|
|
837
|
+
<div className="absolute top-[45px] left-[-35px] z-10 text-sm font-medium">Label</div>
|
|
838
|
+
<svg
|
|
839
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
840
|
+
viewBox="0 0 193 40"
|
|
841
|
+
width="50"
|
|
842
|
+
height="12"
|
|
843
|
+
fill="none"
|
|
844
|
+
className="absolute top-[50px] left-[5px] z-10"
|
|
845
|
+
>
|
|
846
|
+
<g clipPath="url(#a)">
|
|
847
|
+
<path
|
|
848
|
+
fill="currentColor"
|
|
849
|
+
d="M173.928 21.13C115.811 44.938 58.751 45.773 0 26.141c4.227-4.386 7.82-2.715 10.567-1.88 21.133 5.64 42.9 6.266 64.457 7.101 31.066 1.253 60.441-5.848 89.183-17.335 1.268-.418 2.325-1.253 4.861-2.924-14.582-2.924-29.165 2.089-41.845-3.76.212-.835.212-1.879.423-2.714 9.51-.627 19.231-1.253 28.742-2.089 9.51-.835 18.808-1.88 28.318-2.506 6.974-.418 9.933 2.924 7.397 9.19-3.17 8.145-7.608 15.664-11.623 23.391-.423.836-1.057 1.88-1.902 2.298-2.325.835-4.65 1.044-7.186 1.67-.422-2.088-1.479-4.386-1.268-6.265.423-2.506 1.902-4.595 3.804-9.19Z"
|
|
850
|
+
/>
|
|
851
|
+
</g>
|
|
852
|
+
<defs>
|
|
853
|
+
<clipPath id="a">
|
|
854
|
+
<path fill="currentColor" d="M0 0h193v40H0z" />
|
|
855
|
+
</clipPath>
|
|
856
|
+
</defs>
|
|
857
|
+
</svg>
|
|
858
|
+
<TooltipDemo
|
|
859
|
+
label="Page Views"
|
|
860
|
+
payload={[
|
|
861
|
+
{ name: 'Desktop', value: 186, fill: 'var(--chart-1)' },
|
|
862
|
+
{ name: 'Mobile', value: 80, fill: 'var(--chart-2)' }
|
|
863
|
+
]}
|
|
864
|
+
className="w-[8rem]"
|
|
865
|
+
/>
|
|
866
|
+
</div>
|
|
867
|
+
<div className="items-end">
|
|
868
|
+
<div className="absolute top-[0px] left-[122px] z-10 text-sm font-medium">Name</div>
|
|
869
|
+
<svg
|
|
870
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
871
|
+
width="35"
|
|
872
|
+
height="42"
|
|
873
|
+
fill="none"
|
|
874
|
+
viewBox="0 0 122 148"
|
|
875
|
+
className="absolute top-[10px] left-[85px] z-10 -scale-x-100"
|
|
876
|
+
>
|
|
877
|
+
<g clipPath="url(#ab)">
|
|
878
|
+
<path
|
|
879
|
+
fill="currentColor"
|
|
880
|
+
d="M0 2.65c6.15-4.024 12.299-2.753 17.812-.847a115.56 115.56 0 0 1 21.84 10.59C70.4 32.727 88.849 61.744 96.483 97.54c1.908 9.108 2.544 18.639 3.817 29.017 8.481-4.871 12.934-14.402 21.416-19.909 1.061 4.236-1.06 6.989-2.756 9.319-6.998 9.531-14.207 19.062-21.63 28.382-3.604 4.448-6.36 4.871-10.177 1.059-8.058-7.837-12.935-17.368-14.42-28.382 0-.424.636-1.059 1.485-2.118 9.118 2.33 6.997 13.979 14.843 18.215 3.393-14.614.848-28.593-2.969-42.149-4.029-14.19-9.33-27.746-17.812-39.82-8.27-11.86-18.66-21.392-30.11-30.287C26.93 11.758 14.207 6.039 0 2.65Z"
|
|
881
|
+
/>
|
|
882
|
+
</g>
|
|
883
|
+
<defs>
|
|
884
|
+
<clipPath id="ab">
|
|
885
|
+
<path fill="currentColor" d="M0 0h122v148H0z" />
|
|
886
|
+
</clipPath>
|
|
887
|
+
</defs>
|
|
888
|
+
</svg>
|
|
889
|
+
<TooltipDemo
|
|
890
|
+
label="Browser"
|
|
891
|
+
hideLabel
|
|
892
|
+
payload={[
|
|
893
|
+
{ name: 'Chrome', value: 1286, fill: 'var(--chart-3)' },
|
|
894
|
+
{ name: 'Firefox', value: 1000, fill: 'var(--chart-4)' }
|
|
895
|
+
]}
|
|
896
|
+
indicator="dashed"
|
|
897
|
+
className="w-[8rem]"
|
|
898
|
+
/>
|
|
899
|
+
</div>
|
|
900
|
+
<div className="hidden! md:flex!">
|
|
901
|
+
<TooltipDemo
|
|
902
|
+
label="Page Views"
|
|
903
|
+
payload={[{ name: 'Desktop', value: 12486, fill: 'var(--chart-3)' }]}
|
|
904
|
+
className="w-[9rem]"
|
|
905
|
+
indicator="line"
|
|
906
|
+
/>
|
|
907
|
+
</div>
|
|
908
|
+
<div className="items-start! justify-start!">
|
|
909
|
+
<div className="absolute top-[60px] left-[50px] z-10 text-sm font-medium">Indicator</div>
|
|
910
|
+
<TooltipDemo
|
|
911
|
+
label="Browser"
|
|
912
|
+
hideLabel
|
|
913
|
+
payload={[{ name: 'Chrome', value: 1286, fill: 'var(--chart-1)' }]}
|
|
914
|
+
indicator="dot"
|
|
915
|
+
className="w-[8rem]"
|
|
916
|
+
/>
|
|
917
|
+
<svg
|
|
918
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
919
|
+
width="15"
|
|
920
|
+
height="34"
|
|
921
|
+
fill="none"
|
|
922
|
+
viewBox="0 0 75 175"
|
|
923
|
+
className="absolute top-[38px] left-[30px] z-10 rotate-[-40deg]"
|
|
924
|
+
>
|
|
925
|
+
<g clipPath="url(#abc)">
|
|
926
|
+
<path
|
|
927
|
+
fill="currentColor"
|
|
928
|
+
d="M20.187 175c-4.439-2.109-7.186-2.531-8.032-4.008-3.17-5.484-6.763-10.968-8.454-17.084-5.073-16.242-4.439-32.694-1.057-49.146 5.707-28.053 18.388-52.942 34.24-76.565 1.692-2.531 3.171-5.063 4.862-7.805 0-.21-.211-.632-.634-1.265-4.65 1.265-9.511 2.53-14.161 3.585-2.537.422-5.496.422-8.032-.421-1.48-.422-3.593-2.742-3.593-4.219 0-1.898 1.48-4.218 2.747-5.906 1.057-1.054 2.96-1.265 4.65-1.687C35.406 7.315 48.088 3.729 60.98.776c10.99-2.53 14.584 1.055 13.95 11.812-.634 11.18-.846 22.358-1.268 33.326-.212 3.375-.846 6.96-1.268 10.757-8.878-4.007-8.878-4.007-12.048-38.177C47.03 33.259 38.153 49.289 29.91 65.741 21.667 82.193 16.17 99.49 13.212 117.84c-2.959 18.984.634 36.912 6.975 57.161Z"
|
|
929
|
+
/>
|
|
930
|
+
</g>
|
|
931
|
+
<defs>
|
|
932
|
+
<clipPath id="abc">
|
|
933
|
+
<path fill="currentColor" d="M0 0h75v175H0z" />
|
|
934
|
+
</clipPath>
|
|
935
|
+
</defs>
|
|
936
|
+
</svg>
|
|
937
|
+
</div>
|
|
938
|
+
</div>
|
|
939
|
+
)
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
function TooltipDemo({
|
|
943
|
+
indicator = 'dot',
|
|
944
|
+
label,
|
|
945
|
+
payload,
|
|
946
|
+
hideLabel,
|
|
947
|
+
hideIndicator,
|
|
948
|
+
className
|
|
949
|
+
}: {
|
|
950
|
+
label: string
|
|
951
|
+
hideLabel?: boolean
|
|
952
|
+
hideIndicator?: boolean
|
|
953
|
+
indicator?: 'line' | 'dot' | 'dashed'
|
|
954
|
+
payload: {
|
|
955
|
+
name: string
|
|
956
|
+
value: number
|
|
957
|
+
fill: string
|
|
958
|
+
}[]
|
|
959
|
+
nameKey?: string
|
|
960
|
+
labelKey?: string
|
|
961
|
+
} & React.ComponentProps<'div'>) {
|
|
962
|
+
const tooltipLabel = hideLabel ? null : <div className="font-medium">{label}</div>
|
|
963
|
+
|
|
964
|
+
if (!payload?.length) {
|
|
965
|
+
return null
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
const nestLabel = payload.length === 1 && indicator !== 'dot'
|
|
969
|
+
|
|
970
|
+
return (
|
|
971
|
+
<div
|
|
972
|
+
className={cn(
|
|
973
|
+
'grid min-w-[8rem] items-start gap-1.5 rounded-lg border border-border/50 bg-background px-2.5 py-1.5 text-xs shadow-xl transition-all ease-in-out hover:-translate-y-0.5',
|
|
974
|
+
className
|
|
975
|
+
)}
|
|
976
|
+
>
|
|
977
|
+
{!nestLabel ? tooltipLabel : null}
|
|
978
|
+
<div className="grid gap-1.5">
|
|
979
|
+
{payload.map((item, index) => {
|
|
980
|
+
const indicatorColor = item.fill
|
|
981
|
+
|
|
982
|
+
return (
|
|
983
|
+
<div
|
|
984
|
+
key={index}
|
|
985
|
+
className={cn(
|
|
986
|
+
'flex w-full items-stretch gap-2 [&>svg]:h-2.5 [&>svg]:w-2.5 [&>svg]:text-muted-foreground',
|
|
987
|
+
indicator === 'dot' && 'items-center'
|
|
988
|
+
)}
|
|
989
|
+
>
|
|
990
|
+
<>
|
|
991
|
+
{!hideIndicator && (
|
|
992
|
+
<div
|
|
993
|
+
className={cn(
|
|
994
|
+
'shrink-0 rounded-[2px] border-(--color-border) bg-(--color-bg)',
|
|
995
|
+
{
|
|
996
|
+
'h-2.5 w-2.5': indicator === 'dot',
|
|
997
|
+
'w-1': indicator === 'line',
|
|
998
|
+
'w-0 border-[1.5px] border-dashed bg-transparent': indicator === 'dashed',
|
|
999
|
+
'my-0.5': nestLabel && indicator === 'dashed'
|
|
1000
|
+
}
|
|
1001
|
+
)}
|
|
1002
|
+
style={
|
|
1003
|
+
{
|
|
1004
|
+
'--color-bg': indicatorColor,
|
|
1005
|
+
'--color-border': indicatorColor
|
|
1006
|
+
} as React.CSSProperties
|
|
1007
|
+
}
|
|
1008
|
+
/>
|
|
1009
|
+
)}
|
|
1010
|
+
<div
|
|
1011
|
+
className={cn(
|
|
1012
|
+
'flex flex-1 justify-between leading-none',
|
|
1013
|
+
nestLabel ? 'items-end' : 'items-center'
|
|
1014
|
+
)}
|
|
1015
|
+
>
|
|
1016
|
+
<div className="grid gap-1.5">
|
|
1017
|
+
{nestLabel ? tooltipLabel : null}
|
|
1018
|
+
<span className="text-muted-foreground">{item.name}</span>
|
|
1019
|
+
</div>
|
|
1020
|
+
<span className="font-mono font-medium text-foreground tabular-nums">
|
|
1021
|
+
{item.value.toLocaleString()}
|
|
1022
|
+
</span>
|
|
1023
|
+
</div>
|
|
1024
|
+
</>
|
|
1025
|
+
</div>
|
|
1026
|
+
)
|
|
1027
|
+
})}
|
|
1028
|
+
</div>
|
|
1029
|
+
</div>
|
|
1030
|
+
)
|
|
1031
|
+
}
|
|
1032
|
+
```
|
|
1033
|
+
|
|
1034
|
+
You can turn on/off any of these using the `hideLabel`, `hideIndicator` props and customize the indicator style using the `indicator` prop.
|
|
1035
|
+
|
|
1036
|
+
Use `labelKey` and `nameKey` to use a custom key for the tooltip label and name.
|
|
1037
|
+
|
|
1038
|
+
Chart comes with the `<ChartTooltip>` and `<ChartTooltipContent>` components. You can use these two components to add custom tooltips to your chart.
|
|
1039
|
+
|
|
1040
|
+
```tsx title="components/example-chart.tsx"
|
|
1041
|
+
import { ChartTooltip, ChartTooltipContent } from '@/components/ui/chart'
|
|
1042
|
+
```
|
|
1043
|
+
|
|
1044
|
+
```tsx title="components/example-chart.tsx"
|
|
1045
|
+
<ChartTooltip content={<ChartTooltipContent />} />
|
|
1046
|
+
```
|
|
1047
|
+
|
|
1048
|
+
### Props
|
|
1049
|
+
|
|
1050
|
+
Use the following props to customize the tooltip.
|
|
1051
|
+
|
|
1052
|
+
| Prop | Type | Description |
|
|
1053
|
+
| :-------------- | :----------------------- | :------------------------------------------- |
|
|
1054
|
+
| `labelKey` | string | The config or data key to use for the label. |
|
|
1055
|
+
| `nameKey` | string | The config or data key to use for the name. |
|
|
1056
|
+
| `indicator` | `dot` `line` or `dashed` | The indicator style for the tooltip. |
|
|
1057
|
+
| `hideLabel` | boolean | Whether to hide the label. |
|
|
1058
|
+
| `hideIndicator` | boolean | Whether to hide the indicator. |
|
|
1059
|
+
|
|
1060
|
+
### Colors
|
|
1061
|
+
|
|
1062
|
+
Colors are automatically referenced from the chart config.
|
|
1063
|
+
|
|
1064
|
+
### Custom
|
|
1065
|
+
|
|
1066
|
+
To use a custom key for tooltip label and names, use the `labelKey` and `nameKey` props.
|
|
1067
|
+
|
|
1068
|
+
```tsx showLineNumbers /browser/
|
|
1069
|
+
const chartData = [
|
|
1070
|
+
{ browser: 'chrome', visitors: 187, fill: 'var(--color-chrome)' },
|
|
1071
|
+
{ browser: 'safari', visitors: 200, fill: 'var(--color-safari)' }
|
|
1072
|
+
]
|
|
1073
|
+
|
|
1074
|
+
const chartConfig = {
|
|
1075
|
+
visitors: {
|
|
1076
|
+
label: 'Total Visitors'
|
|
1077
|
+
},
|
|
1078
|
+
chrome: {
|
|
1079
|
+
label: 'Chrome',
|
|
1080
|
+
color: 'var(--chart-1)'
|
|
1081
|
+
},
|
|
1082
|
+
safari: {
|
|
1083
|
+
label: 'Safari',
|
|
1084
|
+
color: 'var(--chart-2)'
|
|
1085
|
+
}
|
|
1086
|
+
} satisfies ChartConfig
|
|
1087
|
+
```
|
|
1088
|
+
|
|
1089
|
+
```tsx title="components/example-chart.tsx"
|
|
1090
|
+
<ChartTooltip content={<ChartTooltipContent labelKey="visitors" nameKey="browser" />} />
|
|
1091
|
+
```
|
|
1092
|
+
|
|
1093
|
+
This will use `Total Visitors` for label and `Chrome` and `Safari` for the tooltip names.
|
|
1094
|
+
|
|
1095
|
+
## Legend
|
|
1096
|
+
|
|
1097
|
+
You can use the custom `<ChartLegend>` and `<ChartLegendContent>` components to add a legend to your chart.
|
|
1098
|
+
|
|
1099
|
+
```tsx title="components/example-chart.tsx"
|
|
1100
|
+
import { ChartLegend, ChartLegendContent } from '@/components/ui/chart'
|
|
1101
|
+
```
|
|
1102
|
+
|
|
1103
|
+
```tsx title="components/example-chart.tsx"
|
|
1104
|
+
<ChartLegend content={<ChartLegendContent />} />
|
|
1105
|
+
```
|
|
1106
|
+
|
|
1107
|
+
### Colors
|
|
1108
|
+
|
|
1109
|
+
Colors are automatically referenced from the chart config.
|
|
1110
|
+
|
|
1111
|
+
### Custom
|
|
1112
|
+
|
|
1113
|
+
To use a custom key for legend names, use the `nameKey` prop.
|
|
1114
|
+
|
|
1115
|
+
```tsx showLineNumbers /browser/
|
|
1116
|
+
const chartData = [
|
|
1117
|
+
{ browser: 'chrome', visitors: 187, fill: 'var(--color-chrome)' },
|
|
1118
|
+
{ browser: 'safari', visitors: 200, fill: 'var(--color-safari)' }
|
|
1119
|
+
]
|
|
1120
|
+
|
|
1121
|
+
const chartConfig = {
|
|
1122
|
+
chrome: {
|
|
1123
|
+
label: 'Chrome',
|
|
1124
|
+
color: 'var(--chart-1)'
|
|
1125
|
+
},
|
|
1126
|
+
safari: {
|
|
1127
|
+
label: 'Safari',
|
|
1128
|
+
color: 'var(--chart-2)'
|
|
1129
|
+
}
|
|
1130
|
+
} satisfies ChartConfig
|
|
1131
|
+
```
|
|
1132
|
+
|
|
1133
|
+
```tsx title="components/example-chart.tsx"
|
|
1134
|
+
<ChartLegend content={<ChartLegendContent nameKey="browser" />} />
|
|
1135
|
+
```
|
|
1136
|
+
|
|
1137
|
+
This will use `Chrome` and `Safari` for the legend names.
|
|
1138
|
+
|
|
1139
|
+
## Accessibility
|
|
1140
|
+
|
|
1141
|
+
You can turn on the `accessibilityLayer` prop to add an accessible layer to your chart.
|
|
1142
|
+
|
|
1143
|
+
This prop adds keyboard access and screen reader support to your charts.
|
|
1144
|
+
|
|
1145
|
+
```tsx title="components/example-chart.tsx"
|
|
1146
|
+
<LineChart accessibilityLayer />
|
|
1147
|
+
```
|
|
1148
|
+
|
|
1149
|
+
## RTL
|
|
1150
|
+
|
|
1151
|
+
```tsx
|
|
1152
|
+
'use client'
|
|
1153
|
+
|
|
1154
|
+
import { Bar, BarChart, CartesianGrid, XAxis } from 'recharts'
|
|
1155
|
+
|
|
1156
|
+
import { useTranslation, type Translations } from '@/components/language-selector'
|
|
1157
|
+
import {
|
|
1158
|
+
ChartContainer,
|
|
1159
|
+
ChartLegend,
|
|
1160
|
+
ChartLegendContent,
|
|
1161
|
+
ChartTooltip,
|
|
1162
|
+
ChartTooltipContent,
|
|
1163
|
+
type ChartConfig
|
|
1164
|
+
} from '@/components/ui/chart'
|
|
1165
|
+
|
|
1166
|
+
const translations: Translations = {
|
|
1167
|
+
en: {
|
|
1168
|
+
dir: 'ltr',
|
|
1169
|
+
values: {
|
|
1170
|
+
january: 'January',
|
|
1171
|
+
february: 'February',
|
|
1172
|
+
march: 'March',
|
|
1173
|
+
april: 'April',
|
|
1174
|
+
may: 'May',
|
|
1175
|
+
june: 'June',
|
|
1176
|
+
desktop: 'Desktop',
|
|
1177
|
+
mobile: 'Mobile'
|
|
1178
|
+
}
|
|
1179
|
+
},
|
|
1180
|
+
ar: {
|
|
1181
|
+
dir: 'rtl',
|
|
1182
|
+
values: {
|
|
1183
|
+
january: 'يناير',
|
|
1184
|
+
february: 'فبراير',
|
|
1185
|
+
march: 'مارس',
|
|
1186
|
+
april: 'أبريل',
|
|
1187
|
+
may: 'مايو',
|
|
1188
|
+
june: 'يونيو',
|
|
1189
|
+
desktop: 'سطح المكتب',
|
|
1190
|
+
mobile: 'الجوال'
|
|
1191
|
+
}
|
|
1192
|
+
},
|
|
1193
|
+
he: {
|
|
1194
|
+
dir: 'rtl',
|
|
1195
|
+
values: {
|
|
1196
|
+
january: 'ינואר',
|
|
1197
|
+
february: 'פברואר',
|
|
1198
|
+
march: 'מרץ',
|
|
1199
|
+
april: 'אפריל',
|
|
1200
|
+
may: 'מאי',
|
|
1201
|
+
june: 'יוני',
|
|
1202
|
+
desktop: 'מחשב',
|
|
1203
|
+
mobile: 'נייד'
|
|
1204
|
+
}
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
const chartData = [
|
|
1209
|
+
{ month: 'january', desktop: 186, mobile: 80 },
|
|
1210
|
+
{ month: 'february', desktop: 305, mobile: 200 },
|
|
1211
|
+
{ month: 'march', desktop: 237, mobile: 120 },
|
|
1212
|
+
{ month: 'april', desktop: 73, mobile: 190 },
|
|
1213
|
+
{ month: 'may', desktop: 209, mobile: 130 },
|
|
1214
|
+
{ month: 'june', desktop: 214, mobile: 140 }
|
|
1215
|
+
]
|
|
1216
|
+
|
|
1217
|
+
export function ChartRtl() {
|
|
1218
|
+
const { t, dir } = useTranslation(translations, 'ar')
|
|
1219
|
+
|
|
1220
|
+
const chartConfig = {
|
|
1221
|
+
desktop: {
|
|
1222
|
+
label: t.desktop,
|
|
1223
|
+
color: 'var(--chart-2)'
|
|
1224
|
+
},
|
|
1225
|
+
mobile: {
|
|
1226
|
+
label: t.mobile,
|
|
1227
|
+
color: 'var(--chart-1)'
|
|
1228
|
+
}
|
|
1229
|
+
} satisfies ChartConfig
|
|
1230
|
+
|
|
1231
|
+
return (
|
|
1232
|
+
<ChartContainer config={chartConfig} className="min-h-[200px] w-full">
|
|
1233
|
+
<BarChart accessibilityLayer data={chartData}>
|
|
1234
|
+
<CartesianGrid vertical={false} orientation={dir === 'rtl' ? 'right' : 'left'} />
|
|
1235
|
+
<XAxis
|
|
1236
|
+
dataKey="month"
|
|
1237
|
+
tickLine={false}
|
|
1238
|
+
tickMargin={10}
|
|
1239
|
+
axisLine={false}
|
|
1240
|
+
tickFormatter={value => (t[value as keyof typeof t] as string).slice(0, 3)}
|
|
1241
|
+
reversed={dir === 'rtl'}
|
|
1242
|
+
/>
|
|
1243
|
+
<ChartTooltip
|
|
1244
|
+
content={
|
|
1245
|
+
<ChartTooltipContent labelFormatter={value => t[value as keyof typeof t] as string} />
|
|
1246
|
+
}
|
|
1247
|
+
labelClassName="w-32"
|
|
1248
|
+
/>
|
|
1249
|
+
<ChartLegend content={<ChartLegendContent />} />
|
|
1250
|
+
<Bar dataKey="desktop" fill="var(--color-desktop)" radius={4} />
|
|
1251
|
+
<Bar dataKey="mobile" fill="var(--color-mobile)" radius={4} />
|
|
1252
|
+
</BarChart>
|
|
1253
|
+
</ChartContainer>
|
|
1254
|
+
)
|
|
1255
|
+
}
|
|
1256
|
+
```
|