Skip to content

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

feat: add sparkline chart

TzeYiingde105ac4ecf5
diff --git a/studio/components/ui/Charts/Charts.utils.tsx b/studio/components/ui/Charts/Charts.utils.tsx
index af6c33d77db..ce988a2d86b 100644
--- a/studio/components/ui/Charts/Charts.utils.tsx
+++ b/studio/components/ui/Charts/Charts.utils.tsx
@@ -67,8 +67,15 @@ export const timestampFormatter = (
* Hook to create common wrapping components, perform data transformations
* returns a Container component and the minHeight set
*/
-export const useChartSize = (size: CommonChartProps<any>['size'] = 'normal') => {
- const minHeight = { small: 120, normal: 160, large: 280 }[size]
+export const useChartSize = (
+ size: CommonChartProps<any>['size'] = 'normal',
+ sizeMap: {
+ small: number
+ normal: number
+ large: number
+ } = { small: 120, normal: 160, large: 280 }
+) => {
+ const minHeight = sizeMap[size]
const Container: React.FC = useMemo(
() =>
({ children }) =>
diff --git a/studio/components/ui/Charts/Sparkline.stories.tsx b/studio/components/ui/Charts/Sparkline.stories.tsx
new file mode 100644
index 00000000000..e341ff7b86a
--- /dev/null
+++ b/studio/components/ui/Charts/Sparkline.stories.tsx
@@ -0,0 +1,54 @@
+import React from 'react'
+import { ComponentStory, ComponentMeta } from '@storybook/react'
+import dayjs from 'dayjs'
+import Chart from './Sparkline'
+import { StackedChartProps } from './Charts.types'
+import { STACK_COLORS } from './Charts.constants'
+
+export default {
+ title: 'Charts/Sparkline',
+} as ComponentMeta<typeof Chart>
+
+const DATA = [290, 430, 649, 422, 321].flatMap((value, index) => [
+ {
+ count: value,
+ level: 'error',
+ timestamp: dayjs().subtract(index, 'day').toISOString(),
+ },
+])
+
+const PROPS = {
+ title: 'Memory usage',
+ xAxisKey: 'timestamp',
+ yAxisKey: 'count',
+ data: DATA,
+}
+export const Sparkline = () => (
+ <div className="flex flex-col gap-4 ">
+ <ExampleName>Examples</ExampleName>
+ <div className="flex flex-row gap-4 flex-wrap pb-10">
+ {[
+ { title: 'Small', props: { size: 'small' as const } },
+ { title: 'Normal', props: {} },
+ { title: 'Large', props: { size: 'large' as const } },
+ { title: 'No data', props: { data: [] } },
+ ].map(({ title, props }) => (
+ <div className="w-32 h-72">
+ <ExampleName>{title}</ExampleName>
+ <div className="w-full h-full mt-4">
+ <Chart {...PROPS} {...props} />
+ </div>
+ </div>
+ ))}
+ </div>
+ </div>
+)
+
+const ExampleName: React.FC = ({ children }) => (
+ <h1
+ className="font-bold text-scale-
+ 1100"
+ >
+ {children}
+ </h1>
+)
diff --git a/studio/components/ui/Charts/Sparkline.tsx b/studio/components/ui/Charts/Sparkline.tsx
new file mode 100644
index 00000000000..855a9f86383
--- /dev/null
+++ b/studio/components/ui/Charts/Sparkline.tsx
@@ -0,0 +1,46 @@
+import { useState } from 'react'
+import { LineChart, Bar, XAxis, Tooltip, Legend, Cell, TooltipProps, Line } from 'recharts'
+import ChartHeader from './ChartHeader'
+import { CHART_COLORS, STACK_COLORS, DateTimeFormats } from './Charts.constants'
+import { CommonChartProps } from './Charts.types'
+import { timestampFormatter, useChartSize, useStacked } from './Charts.utils'
+import { precisionFormatter } from './Charts.utils'
+import NoDataPlaceholder from './NoDataPlaceholder'
+interface Props extends CommonChartProps<any> {
+ xAxisKey: string
+ yAxisKey: string
+}
+const Sparkline: React.FC<Props> = ({ size, data, xAxisKey, yAxisKey }) => {
+ const { Container } = useChartSize(size, {
+ small: 50,
+ normal: 90,
+ large: 140,
+ })
+ if (data.length === 0) return null
+ return (
+ <Container>
+ <LineChart
+ data={data}
+ margin={{
+ top: 2,
+ right: 10,
+ left: 10,
+ bottom: 2,
+ }}
+ className="overflow-visible max-w-sm"
+ >
+ <XAxis hide dataKey={xAxisKey} />
+ <Line
+ type="monotone"
+ dataKey={yAxisKey}
+ stroke={CHART_COLORS.GREEN_1}
+ dot={false}
+ animationDuration={600}
+ strokeWidth={1.5}
+ />
+ </LineChart>
+ </Container>
+ )
+}
+
+export default Sparkline