The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
feat: add in new barchart
TzeYiing69cc2fd99391
diff --git a/studio/components/to-be-cleaned/Charts/ChartRenderer.tsx b/studio/components/to-be-cleaned/Charts/ChartRenderer.tsxindex f6bd2b5798c..11df39dd8cf 100644--- a/studio/components/to-be-cleaned/Charts/ChartRenderer.tsx+++ b/studio/components/to-be-cleaned/Charts/ChartRenderer.tsx@@ -126,6 +126,9 @@ const Header = ({)}+/**+ * @deprecated please use studio/components/ui/Charts/BarChart.tsx instead+ */export function BarChart({data,attribute,@@ -268,4 +271,4 @@ export function BarChart({</div></Loading>)-}\ No newline at end of file+}diff --git a/studio/components/ui/Charts/BarChart.stories.tsx b/studio/components/ui/Charts/BarChart.stories.tsxnew file mode 100644index 00000000000..a0f18a840e3--- /dev/null+++ b/studio/components/ui/Charts/BarChart.stories.tsx@@ -0,0 +1,49 @@+import React from 'react'+import { ComponentMeta } from '@storybook/react'+import Chart, { BarChartProps } from './BarChart'+import dayjs from 'dayjs'++export default {+ title: 'Charts',+} as ComponentMeta<typeof Chart>++const DATA = [290, 430, 649, 422, 321, 893, 111].map((value, index) => ({+ ram_usage: value,+ timestamp: dayjs().subtract(index, 'day').toISOString(),+}))++const PROPS: BarChartProps<typeof DATA[number]> = {+ title: 'Memory usage',+ xAxisKey: 'timestamp',+ yAxisKey: 'ram_usage',+ data: DATA,+}++export const BarChart = () => (+ <div className="flex flex-row gap-4 flex-wrap p-10">+ {[+ { title: 'Normal', props: {} },+ { title: 'No title', props: { title: undefined } },+ { title: 'Minimal Header', props: { minimalHeader: true } },+ { title: 'Minimal Highlighted Value', props: { minimalHeader: true, highlightedValue: 123 } },+ { title: 'Highlighted Value', props: { highlightedValue: 123, highlightedLabel: "some custom label" } },+ { title: 'Formatter - Auto 2 Precision for floats', props: { data: DATA.map(d=> ({...d, ram_usage: d.ram_usage + 0.1231233})) } },+ { title: 'UTC Dates', props: { displayDateInUtc: true } },+ { title: 'Custom date format', props: { customDateFormat: 'HH:mm' } },+ { title: 'Value format', props: { format: 'c' } },+ { title: 'Minimal Value format', props: { minimalHeader: true, format: 'c' } },+ { title: 'No data', props: { data: [] } },+ ].map(({ title, props }) => (+ <div className="w-72 h-72">+ <ExampleName>{title}</ExampleName>+ <div className="w-full h-full mt-4">+ <Chart {...PROPS} {...props} />+ </div>+ </div>+ ))}+ </div>+)++const ExampleName: React.FC = ({ children }) => (+ <h1 className="font-bold text-scale-1100">{children}</h1>+)diff --git a/studio/components/ui/Charts/BarChart.tsx b/studio/components/ui/Charts/BarChart.tsxnew file mode 100644index 00000000000..a38bd9a372b--- /dev/null+++ b/studio/components/ui/Charts/BarChart.tsx@@ -0,0 +1,151 @@+import { useState } from 'react'+import {+ BarChart as RechartBarChart,+ Area,+ XAxis,+ YAxis,+ Tooltip,+ ResponsiveContainer,+ Bar,+ Cell,+} from 'recharts'+import dayjs from 'dayjs'+import { CHART_COLORS, DateTimeFormats } from 'components/ui/Charts/Charts.constants'+import ChartHeader from './ChartHeader'+import { Datum, CommonChartProps } from './Charts.types'+import utc from 'dayjs/plugin/utc'+import ChartNoData from './NoDataPlaceholder'+import { numberFormatter } from './Charts.utils'+dayjs.extend(utc)++export interface BarChartProps<D = Datum> extends CommonChartProps<D> {+ yAxisKey: string+ xAxisKey: string+ format?: string+ customDateFormat?: string+ displayDateInUtc?: boolean+ onDatumClick?: (datum: Datum) => void+}++const BarChart: React.FC<BarChartProps> = ({+ data,+ yAxisKey,+ xAxisKey,+ format,+ customDateFormat = DateTimeFormats.FULL,+ title,+ highlightedValue,+ highlightedLabel,+ displayDateInUtc,+ minimalHeader,+ className = '',+ size = 'normal',+ onDatumClick,+}) => {+ const [focusDataIndex, setFocusDataIndex] = useState<number | null>(null)++ // For future reference: https://github.com/supabase/supabase/pull/5311#discussion_r800852828+ const chartHeight = {+ tiny: 76,+ small: 96,+ normal: 160,+ }[size as string] as number++ if (data.length === 0) return <ChartNoData className={className} />++ const day = (value: number | string) => (displayDateInUtc ? dayjs(value).utc() : dayjs(value))+ const resolvedHighlightedLabel =+ (focusDataIndex !== null &&+ data &&+ data[focusDataIndex] &&+ day(data[focusDataIndex][xAxisKey]).format(customDateFormat)) ||+ highlightedLabel++ const resolvedHighlightedValue =+ (focusDataIndex !== null ? data[focusDataIndex]?.[yAxisKey] : null) || highlightedValue++ return (+ <div className={['flex flex-col gap-3', className].join(' ')}>+ <ChartHeader+ title={title}+ format={format}+ customDateFormat={customDateFormat}+ highlightedValue={+ typeof resolvedHighlightedValue === 'number'+ ? numberFormatter(resolvedHighlightedValue)+ : resolvedHighlightedValue+ }+ highlightedLabel={resolvedHighlightedLabel}+ minimalHeader={minimalHeader}+ />+ <ResponsiveContainer width="100%" height={chartHeight}>+ <RechartBarChart+ data={data}+ margin={{+ top: 0,+ right: 0,+ left: 0,+ bottom: 0,+ }}+ className="overflow-visible"+ // mouse hover focusing logic+ onMouseMove={(e: any) => {+ if (e.activeTooltipIndex !== focusDataIndex) {+ setFocusDataIndex(e.activeTooltipIndex)+ }+ }}+ onMouseLeave={() => setFocusDataIndex(null)}+ >+ <defs>+ <linearGradient id="colorUv" x1="0" y1="0" x2="0" y2="1">+ <stop offset="5%" stopColor={CHART_COLORS.GREEN_1} stopOpacity={0.8} />+ <stop offset="95%" stopColor={CHART_COLORS.GREEN_1} stopOpacity={0} />+ </linearGradient>+ </defs>+ <XAxis+ dataKey={xAxisKey}+ interval={data.length - 2}+ angle={0}+ // hide the tick+ tick={{ fontSize: '0px' }}+ // color the axis+ axisLine={{ stroke: CHART_COLORS.AXIS }}+ tickLine={{ stroke: CHART_COLORS.AXIS }}+ />+ <Tooltip content={() => null} />+ <Bar+ dataKey={yAxisKey}+ fill={CHART_COLORS.GREEN_1}+ animationDuration={300}+ // max bar size required to prevent bars from expanding to max width.+ maxBarSize={48}+ >+ {data?.map((_entry: Datum, index: any) => (+ <Cell+ key={`cell-${index}`}+ className={`transition-all duration-300 ${onDatumClick ? 'cursor-pointer' : ''}`}+ fill={focusDataIndex !== index ? CHART_COLORS.GREEN_1 : CHART_COLORS.GREEN_2}+ enableBackground={12}+ />+ ))}+ </Bar>++ <Area+ type="monotone"+ dataKey={yAxisKey}+ stroke={CHART_COLORS.GREEN_1}+ fillOpacity={1}+ fill="url(#colorUv)"+ />+ </RechartBarChart>+ </ResponsiveContainer>+ {data && (+ <div className="text-scale-900 -mt-5 flex items-center justify-between text-xs">+ <span>{dayjs(data[0][xAxisKey]).format(customDateFormat)}</span>+ <span>{dayjs(data[data?.length - 1]?.[xAxisKey]).format(customDateFormat)}</span>+ </div>+ )}+ </div>+ )+}+export default BarChart