Skip to content

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

feat(reports): experiment with line charts FDBKIN-43258 (#51926)

Jordi Enricef759800a5b0
diff --git a/apps/studio/components/interfaces/Reports/renderers/ApiRenderers.tsx b/apps/studio/components/interfaces/Reports/renderers/ApiRenderers.tsx
index e2d14bffddc..b1d69125423 100644
--- a/apps/studio/components/interfaces/Reports/renderers/ApiRenderers.tsx
+++ b/apps/studio/components/interfaces/Reports/renderers/ApiRenderers.tsx
@@ -1,3 +1,4 @@
+import { useFlag } from 'common'
import { geoCentroid } from 'd3-geo'
import sumBy from 'lodash/sumBy'
import { ChevronRightIcon } from 'lucide-react'
@@ -50,6 +51,7 @@ export const NetworkTrafficRenderer = (
ingress: number
}>
) => {
+ const shouldDefaultToLineCharts = useFlag('defaultToLineCharts')
const { data: organization } = useSelectedOrganizationQuery({ enabled: IS_PLATFORM })
const { data, error, isError } = useFillTimeseriesSorted({
data: props.data,
@@ -84,6 +86,7 @@ export const NetworkTrafficRenderer = (
return (
<div className="flex flex-col gap-12 w-full">
<BarChart
+ chartStyle={shouldDefaultToLineCharts ? 'line' : 'bar'}
size="small"
title="Ingress"
highlightedValue={sumBy(props.data, 'ingress_mb')}
@@ -119,6 +122,7 @@ export const TotalRequestsChartRenderer = (
count: number
}>
) => {
+ const shouldDefaultToLineCharts = useFlag('defaultToLineCharts')
const total = props.data.reduce((acc, datum) => {
return acc + datum.count
}, 0)
@@ -148,6 +152,7 @@ export const TotalRequestsChartRenderer = (
return (
<BarChart
+ chartStyle={shouldDefaultToLineCharts ? 'line' : 'bar'}
size="small"
minimalHeader
highlightedValue={total}
@@ -248,6 +253,7 @@ export const ErrorCountsChartRenderer = (
count: number
}>
) => {
+ const shouldDefaultToLineCharts = useFlag('defaultToLineCharts')
const total = props.data.reduce((acc, datum) => {
return acc + datum.count
}, 0)
@@ -278,6 +284,7 @@ export const ErrorCountsChartRenderer = (
return (
<BarChart
+ chartStyle={shouldDefaultToLineCharts ? 'line' : 'bar'}
size="small"
minimalHeader
className="w-full"
@@ -296,6 +303,7 @@ export const ResponseSpeedChartRenderer = (
avg: number
}>
) => {
+ const shouldDefaultToLineCharts = useFlag('defaultToLineCharts')
const transformedData = props.data.map((datum) => ({
timestamp: datum.timestamp,
avg: datum.avg,
@@ -329,6 +337,7 @@ export const ResponseSpeedChartRenderer = (
return (
<BarChart
+ chartStyle={shouldDefaultToLineCharts ? 'line' : 'bar'}
size="small"
highlightedValue={lastAvg}
format="ms"
diff --git a/apps/studio/components/interfaces/Reports/renderers/StorageRenderers.tsx b/apps/studio/components/interfaces/Reports/renderers/StorageRenderers.tsx
index 3659b64a2a4..2324959dbfa 100644
--- a/apps/studio/components/interfaces/Reports/renderers/StorageRenderers.tsx
+++ b/apps/studio/components/interfaces/Reports/renderers/StorageRenderers.tsx
@@ -1,3 +1,4 @@
+import { useFlag } from 'common'
import { Fragment } from 'react'
import { safeDecodeURIComponent } from '../Reports.utils'
@@ -13,6 +14,7 @@ export const CacheHitRateChartRenderer = (
miss_count: number
}>
) => {
+ const shouldDefaultToLineCharts = useFlag('defaultToLineCharts')
const stackedData = props.data.flatMap((datum) => [
{
timestamp: +datum.timestamp / 1000,
@@ -28,6 +30,7 @@ export const CacheHitRateChartRenderer = (
return (
<StackedBarChart
+ chartStyle={shouldDefaultToLineCharts ? 'line' : 'bar'}
hideHeader
variant="percentages"
data={stackedData}
diff --git a/apps/studio/components/ui/Charts/BarChart.tsx b/apps/studio/components/ui/Charts/BarChart.tsx
index ee94de47981..d4b574b3644 100644
--- a/apps/studio/components/ui/Charts/BarChart.tsx
+++ b/apps/studio/components/ui/Charts/BarChart.tsx
@@ -4,7 +4,9 @@ import {
CartesianGrid,
Cell,
Legend,
+ Line,
BarChart as RechartBarChart,
+ ComposedChart as RechartComposedChart,
Tooltip,
XAxis,
YAxis,
@@ -31,6 +33,7 @@ export interface BarChartProps<D = Datum> extends CommonChartProps<D> {
XAxisProps?: ComponentProps<typeof XAxis>
YAxisProps?: ComponentProps<typeof YAxis>
showGrid?: boolean
+ chartStyle?: 'bar' | 'line'
syncId?: string
}
@@ -56,7 +59,9 @@ function BarChart<D extends Datum = Datum>({
YAxisProps,
showGrid = false,
syncId,
+ chartStyle = 'bar',
}: BarChartProps<D>) {
+ const Chart = chartStyle === 'line' ? RechartComposedChart : RechartBarChart
const { hoveredIndex, isHovered, isCurrentChart, setHover, clearHover } =
useChartHoverState('default')
const { Container } = useChartSize(size)
@@ -143,7 +148,7 @@ function BarChart<D extends Datum = Datum>({
attributes={[]}
/>
<Container>
- <RechartBarChart
+ <Chart
data={transformedData}
className="overflow-visible"
onMouseMove={(e: any) => {
@@ -192,26 +197,36 @@ function BarChart<D extends Datum = Datum>({
) : null
}
/>
- <Bar
- dataKey={yAxisKey}
- fill={CHART_COLORS.GREEN_1}
- animationDuration={300}
- maxBarSize={48}
- >
- {data?.map((_entry: D, index: number) => (
- <Cell
- key={`cell-${index}`}
- className={`transition-all duration-300 ${onBarClick ? 'cursor-pointer' : ''}`}
- fill={
- focusDataIndex === index || focusDataIndex === null
- ? CHART_COLORS.GREEN_1
- : CHART_COLORS.GREEN_2
- }
- enableBackground={12}
- />
- ))}
- </Bar>
- </RechartBarChart>
+ {chartStyle === 'line' ? (
+ <Line
+ dataKey={yAxisKey}
+ type="linear"
+ stroke={CHART_COLORS.GREEN_1}
+ dot={false}
+ animationDuration={300}
+ />
+ ) : (
+ <Bar
+ dataKey={yAxisKey}
+ fill={CHART_COLORS.GREEN_1}
+ animationDuration={300}
+ maxBarSize={48}
+ >
+ {data?.map((_entry: D, index: number) => (
+ <Cell
+ key={`cell-${index}`}
+ className={`transition-all duration-300 ${onBarClick ? 'cursor-pointer' : ''}`}
+ fill={
+ focusDataIndex === index || focusDataIndex === null
+ ? CHART_COLORS.GREEN_1
+ : CHART_COLORS.GREEN_2
+ }
+ enableBackground={12}
+ />
+ ))}
+ </Bar>
+ )}
+ </Chart>
</Container>
{data && (
<div className="text-foreground-lighter -mt-10 flex items-center justify-between text-[10px] font-mono">
diff --git a/apps/studio/components/ui/Charts/ComposedChart.tsx b/apps/studio/components/ui/Charts/ComposedChart.tsx
index 757556976d4..b5274738ec7 100644
--- a/apps/studio/components/ui/Charts/ComposedChart.tsx
+++ b/apps/studio/components/ui/Charts/ComposedChart.tsx
@@ -1,3 +1,4 @@
+import { useFlag } from 'common'
import { useTheme } from 'next-themes'
import { ComponentProps, useEffect, useMemo, useState } from 'react'
import {
@@ -141,6 +142,8 @@ export function ComposedChart({
showNewBadge,
normalizeVisibleStackToPercent = false,
}: ComposedChartProps) {
+ const shouldDefaultToLineCharts = useFlag('defaultToLineCharts')
+ const effectiveChartStyle = shouldDefaultToLineCharts ? 'line' : chartStyle
const { resolvedTheme } = useTheme()
const { hoveredIndex, syncTooltip, setHover, clearHover } = useChartHoverState(
syncId || 'default'
@@ -190,14 +193,14 @@ export function ComposedChart({
width: 0,
}
const yAxisPadding = useMemo(() => {
- const needsTopPadding = normalizeVisibleStackToPercent && chartStyle !== 'bar'
+ const needsTopPadding = normalizeVisibleStackToPercent && effectiveChartStyle !== 'bar'
if (!needsTopPadding) return _YAxisProps.padding
return {
..._YAxisProps.padding,
top: Math.max(8, _YAxisProps.padding?.top ?? 0),
}
- }, [_YAxisProps.padding, chartStyle, normalizeVisibleStackToPercent])
+ }, [_YAxisProps.padding, effectiveChartStyle, normalizeVisibleStackToPercent])
function getHeaderLabel() {
if (!xAxisIsDate) {
@@ -446,8 +449,8 @@ export function ComposedChart({
highlightedValue={formatHighlightedValue(resolvedHighlightedValue)}
highlightedLabel={resolvedHighlightedLabel}
minimalHeader={minimalHeader}
- hideChartType={hideChartType}
- chartStyle={chartStyle}
+ hideChartType={hideChartType || shouldDefaultToLineCharts}
+ chartStyle={effectiveChartStyle}
onChartStyleChange={onChartStyleChange}
showMaxValue={_showMaxValue}
setShowMaxValue={maxAttribute ? setShowMaxValue : undefined}
@@ -557,7 +560,7 @@ export function ComposedChart({
))}
</defs>
- {chartStyle === 'bar'
+ {effectiveChartStyle === 'bar'
? visibleAttributes.map((attribute) => (
<Bar
key={attribute.name}
diff --git a/apps/studio/components/ui/Charts/StackedBarChart.tsx b/apps/studio/components/ui/Charts/StackedBarChart.tsx
index 444b3f67970..47f9e7b5710 100644
--- a/apps/studio/components/ui/Charts/StackedBarChart.tsx
+++ b/apps/studio/components/ui/Charts/StackedBarChart.tsx
@@ -1,5 +1,5 @@
import { useState } from 'react'
-import { Bar, BarChart, Cell, Legend, Tooltip, XAxis } from 'recharts'
+import { Bar, BarChart, Cell, ComposedChart, Legend, Line, Tooltip, XAxis } from 'recharts'
import { ChartHeader } from './ChartHeader'
import {
@@ -26,6 +26,7 @@ interface Props extends CommonChartProps<any> {
hideLegend?: boolean
hideHeader?: boolean
stackColors?: ValidStackColor[]
+ chartStyle?: 'bar' | 'line'
syncId?: string
}
const StackedBarChart: React.FC<Props> = ({
@@ -49,7 +50,9 @@ const StackedBarChart: React.FC<Props> = ({
hideHeader = false,
stackColors = DEFAULT_STACK_COLORS,
syncId,
+ chartStyle = 'bar',
}) => {
+ const Chart = chartStyle === 'line' ? ComposedChart : BarChart
const { Container } = useChartSize(size)
const { hoveredIndex, syncTooltip, setHover, clearHover } = useChartHoverState(
syncId || 'default'
@@ -118,7 +121,7 @@ const StackedBarChart: React.FC<Props> = ({
/>
)}
<Container>
- <BarChart
+ <Chart
data={variant === 'percentages' ? percentagesStackedData : stackedData}
margin={{
top: 20,
@@ -157,27 +160,40 @@ const StackedBarChart: React.FC<Props> = ({
axisLine={{ stroke: CHART_COLORS.AXIS }}
tickLine={{ stroke: CHART_COLORS.AXIS }}
/>
- {dataKeys.map((datum, stackIndex) => (
- <Bar
- key={stackIndex}
- dataKey={datum}
- type="monotone"
- legendType="circle"
- fill={stackColorScales[stackIndex].base}
- stackId={1}
- animationDuration={300}
- maxBarSize={48}
- className={onBarClick ? 'cursor-pointer' : ''}
- >
- {stackedData?.map((_entry: unknown, index: any) => (
- <Cell
- key={`cell-${index}`}
- className={`transition-all duration-300`}
- opacity={focusDataIndex === index ? 0.85 : 1}
- />
- ))}
- </Bar>
- ))}
+ {chartStyle === 'line' &&
+ dataKeys.map((datum, stackIndex) => (
+ <Line
+ key={datum}
+ dataKey={datum}
+ type="linear"
+ legendType="circle"
+ stroke={stackColorScales[stackIndex].base}
+ dot={false}
+ animationDuration={300}
+ />
+ ))}
+ {chartStyle === 'bar' &&
+ dataKeys.map((datum, stackIndex) => (
+ <Bar
+ key={stackIndex}
+ dataKey={datum}
+ type="monotone"
+ legendType="circle"
+ fill={stackColorScales[stackIndex].base}
+ stackId={1}
+ animationDuration={300}
+ maxBarSize={48}
+ className={onBarClick ? 'cursor-pointer' : ''}
+ >
+ {stackedData?.map((_entry: unknown, index: any) => (
+ <Cell
+ key={`cell-${index}`}
+ className={`transition-all duration-300`}
+ opacity={focusDataIndex === index ? 0.85 : 1}
+ />
+ ))}
+ </Bar>
+ ))}
<Tooltip
labelFormatter={
xAxisFormatAsDate ? (label) => formatChartDate(label as number | string) : undefined
@@ -204,7 +220,7 @@ const StackedBarChart: React.FC<Props> = ({
wrapperClassName="bg-gray-600 rounded-sm min-w-md"
active={!!syncId && syncTooltip && hoveredIndex !== null}
/>
- </BarChart>
+ </Chart>
</Container>
{stackedData && stackedData[0] && (
<div className="text-foreground-lighter -mt-5 flex items-center justify-between text-xs">