Skip to content

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

Add labels and grid to sql editor charts (#27787)

Jordi Enric6608f8765bad
diff --git a/apps/studio/components/interfaces/SQLEditor/UtilityPanel/ChartConfig.tsx b/apps/studio/components/interfaces/SQLEditor/UtilityPanel/ChartConfig.tsx
index dc9febca068..e7bf396d3c0 100644
--- a/apps/studio/components/interfaces/SQLEditor/UtilityPanel/ChartConfig.tsx
+++ b/apps/studio/components/interfaces/SQLEditor/UtilityPanel/ChartConfig.tsx
@@ -1,3 +1,4 @@
+import { Checkbox } from '@ui/components/shadcn/ui/checkbox'
import BarChart from 'components/ui/Charts/BarChart'
import NoDataPlaceholder from 'components/ui/Charts/NoDataPlaceholder'
import dayjs from 'dayjs'
@@ -24,6 +25,8 @@ export type ChartConfig = {
cumulative: boolean
xKey: string
yKey: string
+ showLabels?: boolean
+ showGrid?: boolean
}
const getCumulativeResults = (results: Results, config: ChartConfig) => {
@@ -95,12 +98,30 @@ export function ChartConfig({ results = { rows: [] }, config, onConfigChange }:
xAxisKey={config.xKey}
yAxisKey={config.yKey}
emptyStateMessage="Execute a query and configure the chart options"
+ showGrid={config.showGrid}
+ XAxisProps={{
+ angle: 0,
+ interval: 0,
+ hide: !config.showLabels,
+ tickFormatter: (idx: string) => {
+ const value = resultToRender[+idx][config.xKey]
+ if (xKeyDateFormat === 'date') {
+ return dayjs(value).format('MMM D YYYY')
+ }
+ return value
+ },
+ }}
+ YAxisProps={{
+ tickFormatter: (value: number) => value.toLocaleString(),
+ label: <></>,
+ hide: !config.showLabels,
+ }}
/>
)}
</ResizablePanel>
<ResizableHandle withHandle />
<ResizablePanel defaultSize={25} minSize={15} className="px-3 py-3 space-y-4">
- <>
+ <form className="grid gap-4">
<div className="flex justify-between items-center h-5">
<h2 className="text-sm text-foreground-lighter">Chart options</h2>
{config.xKey && config.yKey && (
@@ -157,18 +178,38 @@ export function ChartConfig({ results = { rows: [] }, config, onConfigChange }:
</SelectGroup_Shadcn_>
</SelectContent_Shadcn_>
</Select_Shadcn_>
- <div className="flex items-center">
- <Checkbox_Shadcn_
- id="cumulative"
- name="cumulative"
- checked={config.cumulative}
- onClick={() => onConfigChange({ ...config, cumulative: !config.cumulative })}
- />
- <Label_Shadcn_ className="text-foreground-light p-1.5" htmlFor="cumulative">
+ <div className="*:flex *:gap-2 *:items-center grid gap-2 *:text-foreground-light *:p-1.5 *:pl-0">
+ <Label_Shadcn_ className="" htmlFor="cumulative">
+ <Checkbox_Shadcn_
+ id="cumulative"
+ name="cumulative"
+ checked={config.cumulative}
+ onClick={() => onConfigChange({ ...config, cumulative: !config.cumulative })}
+ />
Cumulative
</Label_Shadcn_>
+
+ <Label_Shadcn_ htmlFor="showLabels">
+ <Checkbox_Shadcn_
+ id="showLabels"
+ name="showLabels"
+ checked={config.showLabels}
+ onClick={() => onConfigChange({ ...config, showLabels: !config.showLabels })}
+ />
+ Show labels
+ </Label_Shadcn_>
+
+ <Label_Shadcn_ htmlFor="showGrid">
+ <Checkbox_Shadcn_
+ id="showGrid"
+ name="showGrid"
+ checked={config.showGrid}
+ onClick={() => onConfigChange({ ...config, showGrid: !config.showGrid })}
+ />
+ Show grid
+ </Label_Shadcn_>
</div>
- </>
+ </form>
</ResizablePanel>
</>
</ResizablePanelGroup>
diff --git a/apps/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityPanel.tsx b/apps/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityPanel.tsx
index b8f444dc39a..989aaa159d1 100644
--- a/apps/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityPanel.tsx
+++ b/apps/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityPanel.tsx
@@ -22,12 +22,14 @@ export type UtilityPanelProps = {
onDebug: () => void
}
-const DEFAULT_CHART_CONFIG = {
+const DEFAULT_CHART_CONFIG: ChartConfig = {
type: 'bar',
cumulative: false,
xKey: '',
yKey: '',
-} as const
+ showLabels: false,
+ showGrid: false,
+}
const UtilityPanel = ({
id,
diff --git a/apps/studio/components/ui/Charts/BarChart.tsx b/apps/studio/components/ui/Charts/BarChart.tsx
index 7e76bb82e6c..5820707abc9 100644
--- a/apps/studio/components/ui/Charts/BarChart.tsx
+++ b/apps/studio/components/ui/Charts/BarChart.tsx
@@ -1,6 +1,16 @@
import dayjs from 'dayjs'
-import { useState } from 'react'
-import { Bar, Cell, Legend, BarChart as RechartBarChart, Tooltip, XAxis } from 'recharts'
+import { ComponentProps, useState } from 'react'
+import {
+ Bar,
+ Cell,
+ Legend,
+ BarChart as RechartBarChart,
+ Tooltip,
+ XAxis,
+ Label,
+ YAxis,
+ CartesianGrid,
+} from 'recharts'
import { CHART_COLORS, DateTimeFormats } from 'components/ui/Charts/Charts.constants'
import type { CategoricalChartState } from 'recharts/types/chart/generateCategoricalChart'
@@ -18,6 +28,9 @@ export interface BarChartProps<D = Datum> extends CommonChartProps<D> {
emptyStateMessage?: string
showLegend?: boolean
xAxisIsDate?: boolean
+ XAxisProps?: ComponentProps<typeof XAxis>
+ YAxisProps?: ComponentProps<typeof YAxis>
+ showGrid?: boolean
}
const BarChart = ({
@@ -38,10 +51,25 @@ const BarChart = ({
onBarClick,
showLegend = false,
xAxisIsDate = true,
+ XAxisProps,
+ YAxisProps,
+ showGrid = false,
}: BarChartProps) => {
const { Container } = useChartSize(size)
const [focusDataIndex, setFocusDataIndex] = useState<number | null>(null)
+ // Default props
+ const _XAxisProps = XAxisProps || {
+ interval: data.length - 2,
+ angle: 0,
+ tick: false,
+ }
+
+ const _YAxisProps = YAxisProps || {
+ tickFormatter: (value) => numberFormatter(value, valuePrecision),
+ tick: false,
+ }
+
const day = (value: number | string) => (displayDateInUtc ? dayjs(value).utc() : dayjs(value))
function getHeaderLabel() {
@@ -114,15 +142,18 @@ const BarChart = ({
}}
>
{showLegend && <Legend />}
+ {showGrid && <CartesianGrid stroke={CHART_COLORS.AXIS} />}
+ <YAxis
+ {..._YAxisProps}
+ axisLine={{ stroke: CHART_COLORS.AXIS }}
+ tickLine={{ stroke: CHART_COLORS.AXIS }}
+ key={yAxisKey}
+ />
<XAxis
- dataKey={xAxisKey}
- interval={data.length - 2}
- angle={0}
- // hide the tick
- tick={false}
- // color the axis
+ {..._XAxisProps}
axisLine={{ stroke: CHART_COLORS.AXIS }}
tickLine={{ stroke: CHART_COLORS.AXIS }}
+ key={xAxisKey}
/>
<Tooltip content={() => null} />
<Bar
diff --git a/apps/studio/types/userContent.ts b/apps/studio/types/userContent.ts
index bb652467872..bf6af911173 100644
--- a/apps/studio/types/userContent.ts
+++ b/apps/studio/types/userContent.ts
@@ -45,6 +45,8 @@ export namespace SqlSnippets {
cumulative: boolean
xKey: string
yKey: string
+ showLabels?: boolean
+ showGrid?: boolean
}
}
}