Skip to content

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

chore: AreaChart extraction

TzeYiinga0e9acce24f9
diff --git a/studio/components/ui/Charts/AreaChart.stories.tsx b/studio/components/ui/Charts/AreaChart.stories.tsx
new file mode 100644
index 00000000000..2d6914fe9a1
--- /dev/null
+++ b/studio/components/ui/Charts/AreaChart.stories.tsx
@@ -0,0 +1,28 @@
+import React from 'react'
+import { ComponentMeta } from '@storybook/react'
+import Chart from './AreaChart'
+import dayjs from 'dayjs'
+import Divider from '../Divider'
+
+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(),
+}))
+
+export const AreaChart = () => (
+ <div className="flex flex-col gap-4">
+ <ExampleName>Normal</ExampleName>
+ <Chart title="Memory usage" xAxisKey="timestamp" yAxisKey="ram_usage" data={DATA} />
+ <Divider light={false} />
+ <ExampleName>No title prop</ExampleName>
+ <Chart xAxisKey="timestamp" yAxisKey="ram_usage" data={DATA} />
+ </div>
+)
+
+const ExampleName: React.FC = ({ children }) => (
+ <h1 className="font-bold text-scale-1100">{children}</h1>
+)
diff --git a/studio/components/ui/Charts/AreaChart.tsx b/studio/components/ui/Charts/AreaChart.tsx
new file mode 100644
index 00000000000..3586b9341fe
--- /dev/null
+++ b/studio/components/ui/Charts/AreaChart.tsx
@@ -0,0 +1,197 @@
+import { IconBarChart2, Loading } from '@supabase/ui'
+import { useState } from 'react'
+import {
+ BarChart as RechartBarChart,
+ AreaChart as RechartAreaChart,
+ Area,
+ Bar,
+ XAxis,
+ YAxis,
+ Tooltip,
+ ReferenceLine,
+ Cell,
+ ResponsiveContainer,
+} from 'recharts'
+
+import dayjs from 'dayjs'
+import { formatBytes } from 'lib/helpers'
+import { CHART_COLORS, DateTimeFormats } from 'components/ui/Charts/Charts.constants'
+import ChartHeader from './ChartHeader'
+
+function dataCheck(data: any, attribute: any) {
+ const hasData = data && data.find((record: any) => record[attribute])
+ return hasData ? true : false
+}
+
+const CustomTooltip = () => {
+ return null
+}
+
+const DATE_FORMAT__WITH_TIME = 'MMM D, YYYY, hh:mma'
+const DATE_FORMAT__DATE_ONLY = 'MMM D, YYYY'
+
+const NoData = ({ title = 'No data to show', message = 'May take 24 hours for data to show' }) => (
+ <div
+ className="
+ border-scale-600 flex
+ h-full w-full flex-col
+ items-center justify-center space-y-2 border
+ border-dashed text-center
+ "
+ >
+ <IconBarChart2 className="text-scale-800" />
+ <div>
+ <p className="text-scale-1100 text-xs">{title}</p>
+ <p className="text-scale-900 text-xs">{message}</p>
+ </div>
+ </div>
+)
+const total = (data: any, format: any, attribute: any) => {
+ let total = 0
+ data?.map((item: any) => {
+ total = total + Number(item[attribute])
+ })
+ if (format === '%') {
+ return Number(total).toFixed(2)
+ }
+ return numberWithCommas(total)
+}
+
+function numberWithCommas(x: any) {
+ return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')
+}
+
+interface Props {
+ data: any[]
+ yAxisLimit?: any
+ yAxisKey: string
+ xAxisKey: string
+ format?: string
+ timeseries?: boolean
+ customDateFormat?: string
+ title?: string
+ highlightedValue?: string | number
+ displayDateInUtc?: boolean
+}
+
+const AreaChart: React.FC<Props> = ({
+ data,
+ attribute,
+ yAxisLimit,
+ yAxisKey,
+ xAxisKey,
+ format,
+ customDateFormat = DateTimeFormats.FULL,
+ title,
+ highlightedValue,
+ displayDateInUtc,
+}) => {
+ const [focusDataIndex, setFocusDataIndex] = useState<number | null>(null)
+
+ // For future reference: https://github.com/supabase/supabase/pull/5311#discussion_r800852828
+ const chartHeight = 160
+
+ const day = (value: number | string) => (displayDateInUtc ? dayjs(value).utc() : dayjs(value))
+ const highlightedLabel =
+ (focusDataIndex !== null &&
+ data &&
+ data[focusDataIndex] &&
+ day(data[focusDataIndex][xAxisKey]).format(customDateFormat)) ||
+ null
+
+ const resolvedHighlightedValue =
+ highlightedValue || (focusDataIndex !== null ? data[focusDataIndex]?.[yAxisKey] : null)
+
+ return (
+ <Loading active={!data}>
+ {data && (
+ <>
+ <ChartHeader
+ title={title}
+ format={format}
+ customDateFormat={customDateFormat}
+ highlightedValue={resolvedHighlightedValue}
+ highlightedLabel={highlightedLabel}
+ />
+ <div
+ style={{
+ width: '100%',
+ height: `${chartHeight}px`,
+ }}
+ >
+ <>
+ <ResponsiveContainer width="100%" height={chartHeight}>
+ <RechartAreaChart
+ 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={size === 'small' ? 5 : 1}
+ // interval={data ? data.length - 2 : 0}
+ interval={data.length - 2}
+ angle={0}
+ tickCount={2}
+ tickFormatter={(value) =>
+ dayjs(data[0][xAxisKey]).format(
+ customDateFormat ? customDateFormat : DATE_FORMAT__WITH_TIME
+ )
+ }
+ // hide the tick
+ tick={{ fontSize: '0px' }}
+ // color the axis
+ axisLine={{ stroke: CHART_COLORS.AXIS }}
+ tickLine={{ stroke: CHART_COLORS.AXIS }}
+ />
+ {yAxisLimit && <YAxis type="number" domain={[0, yAxisLimit]} hide />}
+ <Tooltip content={<CustomTooltip />} />
+ <Area
+ type="monotone"
+ dataKey={yAxisKey}
+ stroke={CHART_COLORS.GREEN_1}
+ fillOpacity={1}
+ fill="url(#colorUv)"
+ />
+ </RechartAreaChart>
+ </ResponsiveContainer>
+ {data && (
+ <div className="text-scale-900 -mt-5 flex items-center justify-between text-xs">
+ <span>
+ {dayjs(data[0][xAxisKey]).format(
+ customDateFormat ? customDateFormat : DATE_FORMAT__WITH_TIME
+ )}
+ </span>
+ <span>
+ {dayjs(data[data?.length - 1]?.[xAxisKey]).format(
+ customDateFormat ? customDateFormat : DATE_FORMAT__WITH_TIME
+ )}
+ </span>
+ </div>
+ )}
+ </>
+ </div>
+ </>
+ )}
+ </Loading>
+ )
+}
+export default AreaChart
diff --git a/studio/components/ui/Charts/Chart.types.tsx b/studio/components/ui/Charts/Chart.types.tsx
new file mode 100644
index 00000000000..f7bcf93138d
--- /dev/null
+++ b/studio/components/ui/Charts/Chart.types.tsx
@@ -0,0 +1,19 @@
+export type HeaderType<Datum> = {
+ attribute: string
+ focus: number | null
+ format?: string
+ highlightedValue?: number
+ data?: Datum[]
+ customDateFormat?: string
+ label: string
+ minimalHeader?: boolean
+ displayDateInUtc?: boolean
+}
+
+export interface Datum {
+ [attribute: string]: number | string
+}
+
+export interface TimeseriesDatum extends Datum {
+ timestamp: string
+}
diff --git a/studio/components/ui/Charts/Chart.utils.ts b/studio/components/ui/Charts/Chart.utils.ts
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/studio/components/ui/Charts/ChartBase.tsx b/studio/components/ui/Charts/ChartBase.tsx
new file mode 100644
index 00000000000..5a1a92921f6
--- /dev/null
+++ b/studio/components/ui/Charts/ChartBase.tsx
@@ -0,0 +1,8 @@
+// interface Props {}
+
+// const ChartBase = ({ header }) => {}
+
+// export default ChartBase
+const ChartBase = () => <>ChartBase</>
+
+export default ChartBase
diff --git a/studio/components/ui/Charts/ChartHeader.tsx b/studio/components/ui/Charts/ChartHeader.tsx
new file mode 100644
index 00000000000..c9a18062ff3
--- /dev/null
+++ b/studio/components/ui/Charts/ChartHeader.tsx
@@ -0,0 +1,80 @@
+import dayjs from 'dayjs'
+import { DateTimeFormats } from './Charts.constants'
+
+const DATE_FORMAT__WITH_TIME = 'MMM D, YYYY, hh:mma'
+const DATE_FORMAT__DATE_ONLY = 'MMM D, YYYY'
+export interface ChartHeaderProps {
+ title?: string
+ format?: string
+ highlightedValue: number | string
+ customDateFormat?: string
+ minimalHeader?: boolean
+ displayDateInUtc?: boolean
+ highlightedLabel?: string
+}
+const ChartHeader: React.FC<ChartHeaderProps> = ({
+ format,
+ highlightedValue,
+ highlightedLabel,
+ customDateFormat,
+ title,
+ minimalHeader = false,
+ displayDateInUtc = false,
+}) => {
+ let FOCUS_FORMAT = customDateFormat
+ ? customDateFormat
+ : format == '%'
+ ? DateTimeFormats.FULL
+ : DateTimeFormats.DATE_ONLY
+
+ const chartTitle = (
+ <h3 className={'text-scale-900 ' + (minimalHeader ? 'text-xs' : 'text-sm')}>{title}</h3>
+ )
+
+ // let highlightedLabel = ''
+
+ // if (focus) {
+ // if (data) {
+ // highlightedLabel = data[focus]?.[attribute]?.toLocaleString()
+ // } else if (highlightedValue) {
+ // highlightedLabel = highlightedValue?.toLocaleString()
+ // }
+ // }
+
+ const highlighted = (
+ <h5
+ className={`text-scale-1200 text-xl font-normal ${minimalHeader ? 'text-base' : 'text-2xl'}`}
+ >
+ {highlightedValue}
+ <span className="text-lg">{format}</span>
+ </h5>
+ )
+ const date = (
+ <h5 className="text-scale-900 text-xs">
+ {highlightedLabel}
+ {/* {focus && data && data[focus] && day(data[focus].period_start).format(FOCUS_FORMAT)} */}
+ </h5>
+ )
+
+ if (minimalHeader) {
+ return (
+ <div className="flex flex-row items-center gap-x-4" style={{ minHeight: '1.8rem' }}>
+ {chartTitle}
+ <div className="flex flex-row items-baseline gap-x-2">
+ {highlighted}
+ {date}
+ </div>
+ </div>
+ )
+ }
+
+ return (
+ <div className="h-16">
+ {chartTitle}
+ {highlighted}
+ {date}
+ </div>
+ )
+}
+
+export default ChartHeader
diff --git a/studio/components/ui/Charts/Charts.constants.ts b/studio/components/ui/Charts/Charts.constants.ts
index 902c3861a77..d423e9129e9 100644
--- a/studio/components/ui/Charts/Charts.constants.ts
+++ b/studio/components/ui/Charts/Charts.constants.ts
@@ -17,3 +17,8 @@ export const USAGE_COLORS = {
404: 'var(--colors-amber7)',
500: 'var(--colors-red9)',
}
+
+export enum DateTimeFormats {
+ FULL = 'MMM D, YYYY, hh:mma',
+ DATE_ONLY = 'MMM D, YYYY',
+}
diff --git a/studio/package.json b/studio/package.json
index 1b9790d0711..4f2431992de 100644
--- a/studio/package.json
+++ b/studio/package.json
@@ -113,6 +113,7 @@
"@types/react-virtualized-auto-sizer": "^1.0.1",
"@types/react-window": "^1.8.5",
"@types/react-window-infinite-loader": "^1.0.5",
+ "@types/recharts": "^1.8.23",
"@types/semver": "^7.3.9",
"@types/sqlstring": "^2.3.0",
"@types/uuid": "^8.3.4",