UNPKG

wcz-layout

Version:

1 lines • 9.81 kB
{"version":3,"file":"generatedChart-k7NEdi5Z.mjs","names":["{ node }: Readonly<{ node: Record<string, unknown> }>","isRecord(node.props) ? node.props : {}","props","defineChart({\n ...(spec as object),\n marks: (Array.isArray(nodes) ? nodes : []).flatMap(markOf),\n scales: { x: positionScale(x), y: positionScale(y) },\n color: {\n range: SERIES,\n ...colors,\n ...(legend\n ? {\n legend: colorLegend(\n isRecord(legend) ? (legend as Parameters<typeof colorLegend>[0]) : {},\n ),\n }\n : {}),\n } as ChartColorOptions,\n tooltip,\n })","children","isRecord(scales) ? scales : {}","isRecord(color) ? color : {}","Array.isArray(nodes) ? nodes : []","typeof ariaDescription === \"string\" && { ariaDescription }","typeof aspectRatio === \"number\"\n ? { aspectRatio }\n : { height: typeof height === \"number\" ? height : 280 }","<Box sx={(theme) => ({ ...palette(LIGHT), ...theme.applyStyles(\"dark\", palette(DARK)) })}>\n <Chart\n definition={definition}\n ariaLabel={typeof ariaLabel === \"string\" ? ariaLabel : \"Chart\"}\n {...(typeof ariaDescription === \"string\" && { ariaDescription })}\n {...(typeof aspectRatio === \"number\"\n ? { aspectRatio }\n : { height: typeof height === \"number\" ? height : 280 })}\n />\n </Box>","typeof ariaLabel === \"string\" ? ariaLabel : \"Chart\"","(theme) => ({ ...palette(LIGHT), ...theme.applyStyles(\"dark\", palette(DARK)) })","_c"],"sources":["../src/components/chat/generatedChart.tsx"],"sourcesContent":["import { Box } from \"@mui/material\";\nimport type { ChartColorOptions, ChartMark, ChartPositionScaleOptions } from \"@tanstack/charts\";\nimport {\n areaX,\n areaY,\n arrow,\n bandX,\n bandY,\n barX,\n barY,\n boxX,\n boxY,\n cell,\n colorLegend,\n crosshair,\n defineChart,\n differenceX,\n differenceY,\n dot,\n frame,\n hexagon,\n linearRegressionX,\n linearRegressionY,\n lineX,\n lineY,\n link,\n rect,\n ridgelineX,\n ridgelineY,\n ruleX,\n ruleY,\n text,\n tickX,\n tickY,\n vector,\n violinX,\n violinY,\n waffleX,\n waffleY,\n} from \"@tanstack/charts\";\nimport { Chart } from \"@tanstack/charts/react\";\nimport { scaleBand } from \"@tanstack/charts/scales/band\";\nimport { scaleLinear } from \"@tanstack/charts/scales/linear\";\nimport { scalePoint } from \"@tanstack/charts/scales/point\";\nimport { tooltip } from \"@tanstack/charts/tooltip\";\nimport type { ChartMarkName } from \"~/lib/ai/tools/ui\";\n\nconst marks = {\n areaX,\n areaY,\n arrow,\n bandX,\n bandY,\n barX,\n barY,\n boxX,\n boxY,\n cell,\n crosshair,\n differenceX,\n differenceY,\n dot,\n frame,\n hexagon,\n lineX,\n lineY,\n linearRegressionX,\n linearRegressionY,\n link,\n rect,\n ridgelineX,\n ridgelineY,\n ruleX,\n ruleY,\n text,\n tickX,\n tickY,\n vector,\n violinX,\n violinY,\n waffleX,\n waffleY,\n} satisfies Record<ChartMarkName, unknown>;\n\n// This order is what passes the colour-blindness checks, on light and on dark paper.\nconst LIGHT = [\n \"#2a78d6\",\n \"#eb6834\",\n \"#1baf7a\",\n \"#eda100\",\n \"#e87ba4\",\n \"#008300\",\n \"#6250d6\",\n \"#e34948\",\n];\nconst DARK = [\n \"#3987e5\",\n \"#d95926\",\n \"#199e70\",\n \"#c98500\",\n \"#d55181\",\n \"#008300\",\n \"#9085e9\",\n \"#e66767\",\n];\nconst SERIES = LIGHT.map((_, index) => `var(--ts-chart-${index + 1})`);\nconst palette = (colors: Array<string>) =>\n Object.fromEntries(colors.map((color, index) => [`--ts-chart-${index + 1}`, color]));\n\nconst BARS = { maxThickness: 24, radius: { end: 4 } };\n\nconst isRecord = (value: unknown): value is Record<string, unknown> =>\n typeof value === \"object\" && value !== null && !Array.isArray(value);\n\nfunction positionScale(spec: unknown): ChartPositionScaleOptions | null {\n if (spec === null) return null;\n const { scale, padding, domain, ...options } = isRecord(spec) ? spec : {};\n const gap = typeof padding === \"number\" ? padding : undefined;\n const make = () => {\n const made =\n scale === \"band\"\n ? scaleBand().padding(gap ?? 0.2)\n : scale === \"point\"\n ? scalePoint().padding(gap ?? 0.5)\n : scaleLinear();\n return Array.isArray(domain) ? made.domain(domain as never) : made;\n };\n return { ...options, scale: Array.isArray(domain) ? make() : make } as ChartPositionScaleOptions;\n}\n\ntype AnyMark = ChartMark<any, any, any, any, any, any, any>;\n\nfunction markOf(node: unknown): Array<AnyMark> {\n if (!isRecord(node) || typeof node.type !== \"string\" || !Object.hasOwn(marks, node.type))\n return [];\n const type = node.type as ChartMarkName;\n const { data, ...options } = isRecord(node.props) ? node.props : {};\n const make = marks[type] as (...args: Array<unknown>) => AnyMark;\n if (type === \"crosshair\" || type === \"frame\") return [make(options)];\n const rows = Array.isArray(data) ? data : [];\n return [make(rows, type === \"barX\" || type === \"barY\" ? { ...BARS, ...options } : options)];\n}\n\n/** A `Chart` node: its mark children drawn on its scales, with the default tooltip. */\nexport function GeneratedChart({ node }: Readonly<{ node: Record<string, unknown> }>) {\n const props = isRecord(node.props) ? node.props : {};\n const { scales, color, ariaLabel, ariaDescription, height, aspectRatio, children, ...spec } =\n props;\n const { x, y } = isRecord(scales) ? scales : {};\n const { legend, ...colors } = isRecord(color) ? color : {};\n const nodes = node.children ?? children;\n\n const definition = defineChart({\n ...(spec as object),\n marks: (Array.isArray(nodes) ? nodes : []).flatMap(markOf),\n scales: { x: positionScale(x), y: positionScale(y) },\n color: {\n range: SERIES,\n ...colors,\n ...(legend\n ? {\n legend: colorLegend(\n isRecord(legend) ? (legend as Parameters<typeof colorLegend>[0]) : {},\n ),\n }\n : {}),\n } as ChartColorOptions,\n tooltip,\n });\n\n return (\n <Box sx={(theme) => ({ ...palette(LIGHT), ...theme.applyStyles(\"dark\", palette(DARK)) })}>\n <Chart\n definition={definition}\n ariaLabel={typeof ariaLabel === \"string\" ? ariaLabel : \"Chart\"}\n {...(typeof ariaDescription === \"string\" && { ariaDescription })}\n {...(typeof aspectRatio === \"number\"\n ? { aspectRatio }\n : { height: typeof height === \"number\" ? height : 280 })}\n />\n </Box>\n );\n}\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