Charts
solid-ui/chartsFreeComponent
A charts component
Live preview
live · rendered by the registry
Rendered by solid-ui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://www.solid-ui.com/r/charts.jsonSource
1import type { Component } from "solid-js"2import { createEffect, createSignal, mergeProps, on, onCleanup, onMount } from "solid-js"3import { unwrap } from "solid-js/store"45import type { Ref } from "@solid-primitives/refs"6import { mergeRefs } from "@solid-primitives/refs"7import type {8 ChartComponent,9 ChartData,10 ChartItem,11 ChartOptions,12 Plugin as ChartPlugin,13 ChartType,14 ChartTypeRegistry,15 TooltipModel16} from "chart.js"17import {18 ArcElement,19 BarController,20 BarElement,21 BubbleController,22 CategoryScale,23 Chart,24 Colors,25 DoughnutController,26 Filler,27 Legend,28 LinearScale,29 LineController,30 LineElement,31 PieController,32 PointElement,33 PolarAreaController,34 RadarController,35 RadialLinearScale,36 ScatterController,37 Tooltip38} from "chart.js"3940type TypedChartProps = {41 data: ChartData42 options?: ChartOptions43 plugins?: ChartPlugin[]44 ref?: Ref<HTMLCanvasElement | null>45 width?: number | undefined46 height?: number | undefined47}4849type ChartProps = TypedChartProps & {50 type: ChartType51}5253type ChartContext = {54 chart: Chart55 tooltip: TooltipModel<keyof ChartTypeRegistry>56}5758const BaseChart: Component<ChartProps> = (rawProps) => {59 const [canvasRef, setCanvasRef] = createSignal<HTMLCanvasElement | null>()60 const [chart, setChart] = createSignal<Chart>()6162 const props = mergeProps(63 {64 width: 512,65 height: 512,66 options: { responsive: true } as ChartOptions,67 plugins: [] as ChartPlugin[]68 },69 rawProps70 )7172 const init = () => {73 const ctx = canvasRef()?.getContext("2d") as ChartItem74 const config = unwrap(props)75 const chart = new Chart(ctx, {76 type: config.type,77 data: config.data,78 options: config.options,79 plugins: config.plugins80 })81 setChart(chart)82 }8384 onMount(() => init())8586 createEffect(87 on(88 () => props.data,89 () => {90 chart()!.data = props.data91 chart()!.update()92 },93 { defer: true }94 )95 )9697 createEffect(98 on(99 () => props.options,100 () => {101 chart()!.options = props.options102 chart()!.update()103 },104 { defer: true }105 )106 )107108 createEffect(109 on(110 [() => props.width, () => props.height],111 () => {112 chart()!.resize(props.width, props.height)113 },114 { defer: true }115 )116 )117118 createEffect(119 on(120 () => props.type,121 () => {122 const dimensions = [chart()!.width, chart()!.height]123 chart()!.destroy()124 init()125// … truncated
What it pulls in
npm packages
Files it writes
More from solid-ui
All 55 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Accordionaccordion | solid-ui | Components | Free | 2 deps | |
| Alertalert | solid-ui | Components | Free | 3 deps | |
| Alert Dialogalert-dialog | solid-ui | Components | Free | 2 deps | |
| Aspect Ratioaspect-ratio | solid-ui | Components | Free | 1 dep | |
| Avataravatar | solid-ui | Components | Free | 2 deps | |
| Badgebadge | solid-ui | Components | Free | 2 deps | |
| Badge Deltabadge-delta | solid-ui | Components | Free | 2 deps | |
| Bar Listbar-list | solid-ui | Components | Free | 1 dep |
