{"title":"Car Analysis","description":"","template":"node","files":{".npmrc":"legacy-peer-deps=true\n","index.html":"\n\n \n \n \n Car Analysis\n \n \n \n \n","src/index.less":"#loading {\n animation: spin 1s infinite linear;\n height: 2rem;\n margin: 40vh auto;\n width: 2rem;\n &::before {\n content: url('data:image/svg+xml,');\n }\n}\n\n@keyframes spin {\n from {\n transform: rotate(0);\n }\n to {\n transform: rotate(360deg);\n }\n}\n\nbody {\n display: flex;\n height: 100vh;\n}\n\naside {\n background: #ddd;\n flex: 0;\n padding: 0.5rem 0.5rem 0;\n}\n\nmain {\n background: #fff;\n flex: 1;\n max-height: 100vh;\n padding: 0.5rem;\n}\n\ninput {\n height: 1.5rem;\n margin: 0 0.25rem 0 0;\n vertical-align: bottom;\n}\n\nhr {\n margin: 0.5rem 0 0.1rem;\n}\n\ntable {\n width: 100%;\n table-layout: fixed;\n font-size: inherit;\n line-height: inherit;\n border-collapse: collapse;\n align-self: flex-start;\n margin: 0.5rem;\n caption {\n text-align: left;\n height: 1.75rem;\n button {\n border: 0;\n margin-right: 0.25rem;\n }\n }\n th,\n td {\n overflow: hidden;\n white-space: nowrap;\n text-overflow: ellipsis;\n }\n th {\n padding: 0.25rem;\n cursor: pointer;\n border: solid #ddd;\n border-width: 1px 0;\n text-align: left;\n }\n td {\n border-bottom: 1px solid #eee;\n span {\n border-left: 2px solid transparent;\n padding: 0.25rem;\n line-height: 1.75rem;\n }\n }\n}\n\nsvg {\n stroke: #666;\n stroke-width: 1;\n fill: #333;\n text {\n stroke: #fff;\n stroke-width: 4;\n paint-order: stroke;\n font-size: 12;\n }\n path {\n fill: none;\n }\n circle {\n stroke-width: 2;\n fill: #fff;\n opacity: 0;\n & + text {\n text-anchor: middle;\n display: none;\n }\n &:hover {\n opacity: 1;\n & + text {\n display: block;\n }\n }\n }\n}\n\n.measure(@color) {\n stroke-width: 2;\n stroke: @color;\n border-color: @color;\n fill: @color;\n}\n\n.MPG {\n .measure(#FFB300);\n}\n.Cylinders {\n .measure(#803e75);\n}\n.Displacement {\n .measure(#FF6800);\n}\n.Horsepower {\n .measure(#A6BDD7);\n}\n.Weight {\n .measure(#C10020);\n}\n.Acceleration {\n .measure(#98ce62);\n}\n\nselect {\n border: 1px solid #ccc;\n display: block;\n font: inherit;\n letter-spacing: inherit;\n width: 10rem;\n option {\n border-left: 2px solid transparent;\n }\n}\n\n@font-face {\n font-family: Inter;\n src: url(https://tinybase.org/fonts/inter.woff2) format(\"woff2\");\n}\n\n* {\n box-sizing: border-box;\n}\n\nbody {\n color: #333;\n font-family: Inter, sans-serif;\n letter-spacing: -0.04rem;\n font-size: 0.8rem;\n line-height: 1.4rem;\n margin: 0;\n user-select: none;\n}","src/main.jsx":"import './index.less';\n\nimport { useCallback, useEffect, useMemo, useRef, useState } from 'react';\nimport React from 'react';\nimport { createRoot } from 'react-dom/client';\nimport { createQueries, createStore } from 'tinybase';\nimport {\n Provider,\n useCreateQueries,\n useCreateStore,\n useParamValueState,\n useQueries,\n useResultCell,\n useResultSortedRowIds,\n useResultTable,\n} from 'tinybase/ui-react';\nimport { ResultSortedTableInHtmlTable } from 'tinybase/ui-react-dom';\nimport { Inspector } from 'tinybase/ui-react-inspector';\n\nconst DIMENSION_CELL_IDS = ['Manufacturer', 'Name', 'Year', 'Region'];\n\nconst MEASURE_CELL_IDS = [\n 'MPG',\n 'Cylinders',\n 'Displacement',\n 'Horsepower',\n 'Weight',\n 'Acceleration',\n];\n\nconst AGGREGATES = {\n Maximum: 'max',\n Average: 'avg',\n Minimum: 'min',\n};\n\nconst GRAPH_FONT = 11;\nconst GRAPH_PADDING = 5;\nconst NUMERIC = /^[\\d\\.]+$/;\n\nconst round = (value) => Math.round(value * 100) / 100;\n\nconst App = () => {\n const store = useCreateStore(createStore);\n const queries = useCreateQueries(store, () =>\n createQueries(store)\n // ...\n\n // ...\n .setQueryDefinition(\n 'cars',\n 'cars',\n ({ select, where, group, param }) => {\n param('dimensions').forEach((cellId) => select(cellId));\n param('measures').forEach((cellId) => {\n select(cellId);\n group(cellId, AGGREGATES[param('aggregate')]);\n });\n },\n {\n dimensions: ['Manufacturer'],\n measures: ['MPG', 'Horsepower'],\n aggregate: 'Average',\n },\n ),\n );\n\n // ...\n const [isLoading, setIsLoading] = useState(true);\n useMemo(async () => {\n await loadTable(store);\n setIsLoading(false);\n }, []);\n\n return (\n \n {isLoading ? : }\n \n \n );\n};\n\nconst Loading = () =>
;\n\naddEventListener('load', () => createRoot(document.body).render());\n\nconst loadTable = async (store) => {\n const rows = (\n await (await fetch(`https://tinybase.org/assets/cars.tsv`)).text()\n ).split('\\n');\n const cellIds = rows.shift().split('\\t');\n store.transaction(() =>\n rows.forEach((row, rowId) => {\n const cells = row.split('\\t');\n if (cells.length == cellIds.length) {\n cells.forEach((cell, c) => {\n if (cell != '') {\n if (NUMERIC.test(cell)) {\n cell = parseFloat(cell);\n }\n store.setCell('cars', rowId, cellIds[c], cell);\n }\n });\n }\n }),\n );\n};\n\nconst Body = () => {\n const [showTable, setShowTable] = useState(false);\n // ...\n\n const [dimensions, setDimensions] = useParamValueState('cars', 'dimensions');\n const [measures, setMeasures] = useParamValueState('cars', 'measures');\n const [aggregate, setAggregate] = useParamValueState('cars', 'aggregate');\n\n return (\n <>\n \n\n {showTable ? (\n \n ) : (\n \n )}\n \n );\n};\n\nconst ResultTable = ({ columns }) => (\n \n Object.fromEntries(\n columns.map((column) => [column, { component: CustomCell }]),\n ),\n [...columns],\n )}\n />\n);\n\n// ...\nconst CustomCell = ({ rowId, cellId }) => {\n const cell = useResultCell('cars', rowId, cellId);\n return (\n {Number.isFinite(cell) ? round(cell) : cell}\n );\n};\n\nconst ResultGraph = ({ dimensions, measures }) => {\n const ref = useRef(null);\n const [{ width = 0, height = 0 }, setDimensions] = useState({});\n useEffect(() => {\n const observer = new ResizeObserver(([{ contentRect }]) =>\n setDimensions(contentRect),\n );\n observer.observe(ref.current);\n return () => observer.disconnect();\n }, [ref]);\n // ...\n\n // ...\n const [xAllLabels, yValueSets, yMax] = useGraphData(dimensions, measures);\n // ...\n\n // ...\n const [xToPixel, yToPixel, xLabels, yLabels, xRange] = useGraphSetup(\n width,\n height,\n xAllLabels,\n yMax,\n );\n // ...\n\n // ...\n if (width == 0 || height == 0) {\n return
;\n }\n // ...\n\n // ...\n return (\n
\n \n \n\n <>\n {xLabels.map((xLabel, x) => {\n const textX = xToPixel(x) - GRAPH_FONT / 2;\n const textY = yToPixel(0) + GRAPH_PADDING;\n return (\n xLabel && (\n \n {xLabel}\n \n )\n );\n })}\n \n\n <>\n {yLabels.map((yLabel) => {\n const textX = xToPixel(0) - GRAPH_PADDING;\n const textY = yToPixel(yLabel) + GRAPH_FONT / 2;\n return (\n \n {yLabel}\n \n );\n })}\n \n\n {yValueSets.map((yValueSet, s) => (\n \n \n `${x == 0 ? 'M' : 'L'}${xToPixel(x)} ${yToPixel(y)}`,\n )\n .join('')}\n />\n {yValueSet.map((y, x) => (\n <>\n \n \n {xAllLabels[x]} {measures[s]}: {round(y)}\n \n \n ))}\n \n ))}\n \n
\n );\n};\n\nconst useGraphData = (dimensions, measures) => {\n const table = useResultTable('cars');\n const sortedRowIds = useResultSortedRowIds('cars', measures[0] ?? undefined);\n return useMemo(() => {\n const yAll = [1];\n const xAllLabels = [];\n const yValueSets = measures.map(() => []);\n sortedRowIds.forEach((rowId) => {\n const row = table[rowId];\n xAllLabels.push(\n dimensions.map((dimensionColumn) => row[dimensionColumn]).join(', '),\n );\n measures.forEach((measureColumn, m) => {\n yAll.push(row[measureColumn]);\n yValueSets[m].push(row[measureColumn]);\n });\n });\n return [xAllLabels, yValueSets, Math.max(...yAll)];\n }, [table, sortedRowIds, measures, dimensions]);\n};\n\nconst useGraphSetup = (width, height, xAllLabels, yMax) =>\n useMemo(() => {\n const xOffset = height / 4;\n const yOffset = width / 15;\n const xWidth = width - yOffset - GRAPH_PADDING;\n const yHeight = height - xOffset - GRAPH_PADDING;\n\n const xRange = xAllLabels.length - 1;\n const xLabels = xAllLabels.map((label, x) =>\n x % Math.ceil((GRAPH_FONT * xRange) / xWidth) == 0 ? label : null,\n );\n\n const yMaxMagnitude = Math.pow(10, Math.floor(Math.log10(yMax)));\n const yRange = Math.ceil(yMax / yMaxMagnitude) * yMaxMagnitude;\n const yMajorSteps = Math.ceil(yMax / yMaxMagnitude);\n const yMinorSteps = yMajorSteps <= 2 ? 5 : yMajorSteps <= 5 ? 2 : 1;\n const yLabels = Array(yMinorSteps * yMajorSteps + 1)\n .fill()\n .map((_, i) => (i * yMaxMagnitude) / yMinorSteps);\n\n return [\n (x) => yOffset + (x * xWidth) / xRange,\n (y) => GRAPH_PADDING + yHeight - (y * yHeight) / yRange,\n xLabels,\n yLabels,\n xRange,\n yRange,\n ];\n }, [width, height, xAllLabels, yMax]);\n\nconst Select = ({ options, selected, onOptionsChange, multiple = true }) => {\n const handleOptionsChange = useCallback(\n ({ target }) =>\n onOptionsChange(\n multiple\n ? [...target.selectedOptions].map((option) => option.value)\n : target.value,\n ),\n [onOptionsChange],\n );\n return (\n \n {options.map((option) => (\n \n {option}\n \n ))}\n \n );\n};","package.json":"{\n \"name\": \"car-analysis\",\n \"private\": true,\n \"version\": \"0.0.0\",\n \"type\": \"module\",\n \"scripts\": {\n \"dev\": \"vite\",\n \"build\": \"vite build\",\n \"preview\": \"vite preview\"\n },\n \"dependencies\": {\n \"react\": \"^19.3.1\",\n \"react-dom\": \"^19.3.1\",\n \"tinybase\": \"10.1.0\"\n },\n \"devDependencies\": {\n \"vite\": \"^8.3.8\",\n \"less\": \"^4.9.1\",\n \"@vitejs/plugin-react\": \"^6.3.0\"\n },\n \"stackblitz\": {\n \"startCommand\": \"npm run dev\"\n }\n}","vite.config.js":"import { defineConfig } from 'vite';\nimport react from '@vitejs/plugin-react';\n\nexport default defineConfig({\n plugins: [react()],\n});"},"options":{"file":"src/main.jsx","hidedevtools":"1","showSidebar":"1","terminalHeight":"18"}}