Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat: add value rendering in tooltip alongside percentage render, add testing for stacked chart utils

TzeYiing2d5602067fc7
diff --git a/package-lock.json b/package-lock.json
index d002b6210bb..2f08bafa685 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -9827,6 +9827,36 @@
"react-dom": "<18.0.0"
}
},
+ "node_modules/@testing-library/react-hooks": {
+ "version": "8.0.1",
+ "resolved": "https://registry.npmjs.org/@testing-library/react-hooks/-/react-hooks-8.0.1.tgz",
+ "integrity": "sha512-Aqhl2IVmLt8IovEVarNDFuJDVWVvhnr9/GCU6UUnrYXwgDFF9h2L2o2P9KBni1AST5sT6riAyoukFLyjQUgD/g==",
+ "dev": true,
+ "dependencies": {
+ "@babel/runtime": "^7.12.5",
+ "react-error-boundary": "^3.1.0"
+ },
+ "engines": {
+ "node": ">=12"
+ },
+ "peerDependencies": {
+ "@types/react": "^16.9.0 || ^17.0.0",
+ "react": "^16.9.0 || ^17.0.0",
+ "react-dom": "^16.9.0 || ^17.0.0",
+ "react-test-renderer": "^16.9.0 || ^17.0.0"
+ },
+ "peerDependenciesMeta": {
+ "@types/react": {
+ "optional": true
+ },
+ "react-dom": {
+ "optional": true
+ },
+ "react-test-renderer": {
+ "optional": true
+ }
+ }
+ },
"node_modules/@testing-library/user-event": {
"version": "13.5.0",
"resolved": "https://registry.npmjs.org/@testing-library/user-event/-/user-event-13.5.0.tgz",
@@ -29842,6 +29872,22 @@
"integrity": "sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w==",
"dev": true
},
+ "node_modules/react-error-boundary": {
+ "version": "3.1.4",
+ "resolved": "https://registry.npmjs.org/react-error-boundary/-/react-error-boundary-3.1.4.tgz",
+ "integrity": "sha512-uM9uPzZJTF6wRQORmSrvOIgt4lJ9MC1sNgEOj2XGsDTRE4kmpWxg7ENK9EWNKJRMAOY9z0MuF4yIfl6gp4sotA==",
+ "dev": true,
+ "dependencies": {
+ "@babel/runtime": "^7.12.5"
+ },
+ "engines": {
+ "node": ">=10",
+ "npm": ">=6"
+ },
+ "peerDependencies": {
+ "react": ">=16.13.1"
+ }
+ },
"node_modules/react-fast-compare": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/react-fast-compare/-/react-fast-compare-2.0.4.tgz",
@@ -37621,8 +37667,9 @@
"@storybook/react": "^6.4.19",
"@storybook/testing-library": "^0.0.9",
"@tailwindcss/typography": "^0.5.2",
- "@testing-library/dom": "^8.11.1",
- "@testing-library/react": "^12.1.2",
+ "@testing-library/dom": "^8.19.0",
+ "@testing-library/react": "^12.1.5",
+ "@testing-library/react-hooks": "^8.0.1",
"@testing-library/user-event": "^13.5.0",
"@types/blueimp-md5": "^2.18.0",
"@types/crypto-js": "^4.0.2",
@@ -44568,6 +44615,16 @@
"@types/react-dom": "<18.0.0"
}
},
+ "@testing-library/react-hooks": {
+ "version": "8.0.1",
+ "resolved": "https://registry.npmjs.org/@testing-library/react-hooks/-/react-hooks-8.0.1.tgz",
+ "integrity": "sha512-Aqhl2IVmLt8IovEVarNDFuJDVWVvhnr9/GCU6UUnrYXwgDFF9h2L2o2P9KBni1AST5sT6riAyoukFLyjQUgD/g==",
+ "dev": true,
+ "requires": {
+ "@babel/runtime": "^7.12.5",
+ "react-error-boundary": "^3.1.0"
+ }
+ },
"@testing-library/user-event": {
"version": "13.5.0",
"resolved": "https://registry.npmjs.org/@testing-library/user-event/-/user-event-13.5.0.tgz",
@@ -60519,6 +60576,15 @@
}
}
},
+ "react-error-boundary": {
+ "version": "3.1.4",
+ "resolved": "https://registry.npmjs.org/react-error-boundary/-/react-error-boundary-3.1.4.tgz",
+ "integrity": "sha512-uM9uPzZJTF6wRQORmSrvOIgt4lJ9MC1sNgEOj2XGsDTRE4kmpWxg7ENK9EWNKJRMAOY9z0MuF4yIfl6gp4sotA==",
+ "dev": true,
+ "requires": {
+ "@babel/runtime": "^7.12.5"
+ }
+ },
"react-fast-compare": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/react-fast-compare/-/react-fast-compare-2.0.4.tgz",
@@ -63325,8 +63391,9 @@
"@tailwindcss/typography": "^0.5.2",
"@tanstack/react-query": "^4.16.1",
"@tanstack/react-query-devtools": "^4.16.1",
- "@testing-library/dom": "^8.11.1",
- "@testing-library/react": "^12.1.2",
+ "@testing-library/dom": "^8.19.0",
+ "@testing-library/react": "^12.1.5",
+ "@testing-library/react-hooks": "*",
"@testing-library/user-event": "^13.5.0",
"@types/blueimp-md5": "^2.18.0",
"@types/crypto-js": "^4.0.2",
diff --git a/studio/components/ui/Charts/Charts.utils.tsx b/studio/components/ui/Charts/Charts.utils.tsx
index ea260c5ff57..af6c33d77db 100644
--- a/studio/components/ui/Charts/Charts.utils.tsx
+++ b/studio/components/ui/Charts/Charts.utils.tsx
@@ -111,20 +111,10 @@ export const useStacked = ({
return acc
}, {} as Record<string, Record<string, number>>)
- const flattened = Object.entries(mapping).map(([x, sMap]) => {
- let map = sMap
- if (variant === 'percentages') {
- const sum = Object.values(sMap).reduce((acc, value) => acc + value, 0)
- map = Object.entries(sMap).reduce((acc, [key, value]) => {
- acc[key] = value !== 0 ? value / sum : 0
- return acc
- }, {} as any)
- }
- return {
- ...map,
- [xAxisKey]: x,
- }
- })
+ const flattened = Object.entries(mapping).map(([x, sMap]) => ({
+ ...sMap,
+ [xAxisKey]: Number.isNaN(Number(x)) ? x : Number(x),
+ }))
return flattened
}, [JSON.stringify(data)])
const dataKeys = useMemo(() => {
@@ -133,5 +123,24 @@ export const useStacked = ({
.sort()
}, [JSON.stringify(stackedData[0] || {})])
- return { dataKeys, stackedData }
+ const percentagesStackedData = useMemo(() => {
+ if (variant !== 'percentages') return
+
+ return stackedData.map((stack) => {
+ const entries = Object.entries(stack) as Array<[string, number]>
+ let map
+ const sum = entries
+ .filter(([key, _value]) => dataKeys.includes(key))
+ .reduce((acc, [_key, value]) => acc + value, 0)
+ map = entries.reduce((acc, [key, value]) => {
+ if (!dataKeys.includes(key)) {
+ return { ...acc, [key]: value }
+ }
+ return { ...acc, [key]: value !== 0 ? value / sum : 0 }
+ }, {} as any)
+ return map
+ })
+ }, [JSON.stringify(stackedData)])
+
+ return { dataKeys, stackedData, percentagesStackedData }
}
diff --git a/studio/components/ui/Charts/StackedBarChart.stories.tsx b/studio/components/ui/Charts/StackedBarChart.stories.tsx
index 12e12d2c2a6..a6703a51727 100644
--- a/studio/components/ui/Charts/StackedBarChart.stories.tsx
+++ b/studio/components/ui/Charts/StackedBarChart.stories.tsx
@@ -36,10 +36,10 @@ const PROPS = {
}
export const StackedBarChart = () => (
<div className="flex flex-col gap-4 ">
- <ExampleName>Stack Colors</ExampleName>
+ <ExampleName>Default Stacked Chart Colors</ExampleName>
<div className="flex flex-row ">
{STACK_COLORS.map((c) => (
- <div className="w-10 h-10" style={{ background: c.base }}></div>
+ <div className="w-10 h-10" key={c.base} style={{ background: c.base }}></div>
))}
</div>
@@ -51,6 +51,10 @@ export const StackedBarChart = () => (
{ title: 'Minimal Header', props: { minimalHeader: true } },
{ title: 'Hide Legend', props: { hideLegend: true } },
{ title: 'Percentage Bars', props: { variant: 'percentages' as const } },
+ {
+ title: 'Percentage Bars with suffix',
+ props: { variant: 'percentages' as const, format: 'mem' },
+ },
{
title: 'Formatter - Auto 2 Precision for floats',
props: { data: DATA.map((d) => ({ ...d, count: d.count + 5.12 })) },
diff --git a/studio/components/ui/Charts/StackedBarChart.tsx b/studio/components/ui/Charts/StackedBarChart.tsx
index ed8b0ea51d9..8f389778d0b 100644
--- a/studio/components/ui/Charts/StackedBarChart.tsx
+++ b/studio/components/ui/Charts/StackedBarChart.tsx
@@ -1,5 +1,5 @@
import { useState } from 'react'
-import { BarChart, Bar, XAxis, Tooltip, Legend, Cell } from 'recharts'
+import { BarChart, Bar, XAxis, Tooltip, Legend, Cell, TooltipProps } from 'recharts'
import ChartHeader from './ChartHeader'
import { CHART_COLORS, STACK_COLORS, DateTimeFormats } from './Charts.constants'
import { CommonChartProps } from './Charts.types'
@@ -33,7 +33,13 @@ const StackedBarChart: React.FC<Props> = ({
hideLegend = false,
}) => {
const { Container } = useChartSize(size)
- const { dataKeys, stackedData } = useStacked({ data, xAxisKey, stackKey, yAxisKey, variant })
+ const { dataKeys, stackedData, percentagesStackedData } = useStacked({
+ data,
+ xAxisKey,
+ stackKey,
+ yAxisKey,
+ variant,
+ })
const [focusDataIndex, setFocusDataIndex] = useState<number | null>(null)
if (data.length === 0) return <NoDataPlaceholder />
return (
@@ -46,7 +52,7 @@ const StackedBarChart: React.FC<Props> = ({
/>
<Container>
<BarChart
- data={stackedData}
+ data={variant === 'percentages' ? percentagesStackedData : stackedData}
margin={{
top: 20,
right: 20,
@@ -56,7 +62,6 @@ const StackedBarChart: React.FC<Props> = ({
className="cursor-pointer overflow-visible"
// mouse hover focusing logic
onMouseMove={(e: any) => {
- console.log(e)
if (e.activeTooltipIndex !== focusDataIndex) {
setFocusDataIndex(e.activeTooltipIndex)
}
@@ -106,18 +111,22 @@ const StackedBarChart: React.FC<Props> = ({
? (label) => timestampFormatter(label, customDateFormat, displayDateInUtc)
: undefined
}
- formatter={(value: number) => {
- let val: string = String(value)
- if (variant === 'percentages') {
- val = precisionFormatter(value * 100, 1) + '%'
+ formatter={(value: number, name: string, props: any) => {
+ const suffix = format || ''
+ if (variant === 'percentages' && percentagesStackedData) {
+ const index = percentagesStackedData.findIndex(
+ (pStack) => pStack === props.payload!
+ )
+ const val = stackedData[index][name]
+ const percentage = precisionFormatter(value * 100, 1) + '%'
+ return `${percentage} (${val}${suffix})`
}
- val += (format || '')
- return val
+ return String(value) + suffix
}}
cursor={false}
labelClassName="text-white"
contentStyle={{ backgroundColor: '#444444', borderColor: '#444444', fontSize: '12px' }}
- wrapperClassName="bg-gray-600 rounded"
+ wrapperClassName="bg-gray-600 rounded min-w-md"
/>
</BarChart>
</Container>
diff --git a/studio/package.json b/studio/package.json
index 7a39cefd0d6..547db163b08 100644
--- a/studio/package.json
+++ b/studio/package.json
@@ -103,8 +103,9 @@
"@storybook/react": "^6.4.19",
"@storybook/testing-library": "^0.0.9",
"@tailwindcss/typography": "^0.5.2",
- "@testing-library/dom": "^8.11.1",
- "@testing-library/react": "^12.1.2",
+ "@testing-library/dom": "^8.19.0",
+ "@testing-library/react": "^12.1.5",
+ "@testing-library/react-hooks": "^8.0.1",
"@testing-library/user-event": "^13.5.0",
"@types/blueimp-md5": "^2.18.0",
"@types/crypto-js": "^4.0.2",
diff --git a/studio/tests/components/ui/Charts/Charts.utils.test.ts b/studio/tests/components/ui/Charts/Charts.utils.test.ts
index 071f19f02d9..4da9e749668 100644
--- a/studio/tests/components/ui/Charts/Charts.utils.test.ts
+++ b/studio/tests/components/ui/Charts/Charts.utils.test.ts
@@ -1,16 +1,52 @@
-import { isFloat, numberFormatter, precisionFormatter } from "components/ui/Charts/Charts.utils"
+import {
+ isFloat,
+ numberFormatter,
+ precisionFormatter,
+ useStacked,
+} from 'components/ui/Charts/Charts.utils'
+import { renderHook } from '@testing-library/react-hooks'
-
-test("isFloat", () => {
- expect(isFloat(123)).toBe(false)
- expect(isFloat(123.123)).toBe(true)
+test('isFloat', () => {
+ expect(isFloat(123)).toBe(false)
+ expect(isFloat(123.123)).toBe(true)
+})
+test('numberFormatter', () => {
+ expect(numberFormatter(123)).toBe('123')
+ expect(numberFormatter(123.123)).toBe('123.12')
})
-test("numberFormatter", () => {
- expect(numberFormatter(123)).toBe("123")
- expect(numberFormatter(123.123)).toBe("123.12")
+
+test('precisionFormatter', () => {
+ expect(precisionFormatter(123, 1)).toBe('123.0')
+ expect(precisionFormatter(123.12345, 4)).toBe('123.1234')
})
-test("precisionFormatter", () => {
- expect(precisionFormatter(123, 1)).toBe("123.0")
- expect(precisionFormatter(123.12345, 4)).toBe("123.1234")
-})
\ No newline at end of file
+test('useStacked', () => {
+ const { result } = renderHook(() =>
+ useStacked({
+ data: [
+ { label: 'a', x: 1, y: 2 },
+ { label: 'b', x: 1, y: 3 },
+ ] as any,
+ xAxisKey: 'x',
+ yAxisKey: 'y',
+ stackKey: 'label',
+ variant: 'percentages',
+ })
+ )
+ expect(result.current).toMatchObject({
+ stackedData: [
+ {
+ a: 2,
+ b: 3,
+ x: 1,
+ },
+ ],
+ percentagesStackedData: [
+ {
+ a: 2 / 5,
+ b: 3 / 5,
+ x: 1,
+ },
+ ],
+ })
+})