Skip to content

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

SQL Chart fixes (#32769)

Jordi Enric265b4cddd747
diff --git a/apps/studio/components/interfaces/SQLEditor/UtilityPanel/ChartConfig.tsx b/apps/studio/components/interfaces/SQLEditor/UtilityPanel/ChartConfig.tsx
index e7bf396d3c0..bb21c0f4d6a 100644
--- a/apps/studio/components/interfaces/SQLEditor/UtilityPanel/ChartConfig.tsx
+++ b/apps/studio/components/interfaces/SQLEditor/UtilityPanel/ChartConfig.tsx
@@ -1,9 +1,9 @@
-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'
import { ArrowUpDown } from 'lucide-react'
import { useMemo } from 'react'
+
+import BarChart from 'components/ui/Charts/BarChart'
+import NoDataPlaceholder from 'components/ui/Charts/NoDataPlaceholder'
import {
Button,
Checkbox_Shadcn_,
@@ -51,7 +51,12 @@ type ChartConfigProps = {
config: ChartConfig
onConfigChange: (config: ChartConfig) => void
}
-export function ChartConfig({ results = { rows: [] }, config, onConfigChange }: ChartConfigProps) {
+
+export const ChartConfig = ({
+ results = { rows: [] },
+ config,
+ onConfigChange,
+}: ChartConfigProps) => {
// If a result key is not valid, it will be filtered out
const resultKeys = useMemo(() => {
return Object.keys(results.rows[0] || {}).filter((key) => {
@@ -87,131 +92,134 @@ export function ChartConfig({ results = { rows: [] }, config, onConfigChange }:
return (
<ResizablePanelGroup direction="horizontal" className="flex-grow h-full">
- <>
- <ResizablePanel className="p-4 h-full" defaultSize={75}>
- {config.type === 'bar' && (
- <BarChart
- showLegend
- size="normal"
- xAxisIsDate={xKeyDateFormat === 'date'}
- data={resultToRender as any}
- 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 && (
- <Button
- type="text"
- onClick={() => {
- const currentX = config.xKey
- const currentY = config.yKey
- onConfigChange({ ...config, xKey: currentY, yKey: currentX })
- }}
- title="Swap X and Y axis"
- icon={<ArrowUpDown size="15" className="text-foreground-lighter" />}
- >
- Flip
- </Button>
- )}
- </div>
-
- <Select_Shadcn_
- value={config.xKey}
- onValueChange={(value) => {
- onConfigChange({ ...config, xKey: value })
+ <ResizablePanel className="p-4 h-full" defaultSize={75}>
+ {config.type === 'bar' && (
+ <BarChart
+ showLegend
+ size="normal"
+ xAxisIsDate={xKeyDateFormat === 'date'}
+ data={resultToRender}
+ xAxisKey={config.xKey}
+ yAxisKey={config.yKey}
+ emptyStateMessage="Execute a query and configure the chart options"
+ showGrid={config.showGrid}
+ XAxisProps={{
+ angle: 0,
+ interval: 'preserveStart',
+ 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(),
+ hide: !config.showLabels,
+ domain: [0, 'dataMax'],
+ }}
+ />
+ )}
+ </ResizablePanel>
+ <ResizableHandle withHandle />
+ <ResizablePanel
+ defaultSize={25}
+ minSize={15}
+ className="px-3 py-3 space-y-4 !overflow-y-auto"
+ >
+ <div className="flex justify-between items-center h-5">
+ <h2 className="text-sm text-foreground-lighter">Chart options</h2>
+ {config.xKey && config.yKey && (
+ <Button
+ type="text"
+ onClick={() => {
+ const currentX = config.xKey
+ const currentY = config.yKey
+ onConfigChange({ ...config, xKey: currentY, yKey: currentX })
}}
+ title="Swap X and Y axis"
+ icon={<ArrowUpDown size="15" className="text-foreground-lighter" />}
>
- <SelectTrigger_Shadcn_>
- X Axis {config.xKey && `- ${config.xKey}`}
- </SelectTrigger_Shadcn_>
- <SelectContent_Shadcn_>
- <SelectGroup_Shadcn_>
- {resultKeys.map((key) => (
- <SelectItem_Shadcn_ value={key} key={key}>
- {key}
- </SelectItem_Shadcn_>
- ))}
- </SelectGroup_Shadcn_>
- </SelectContent_Shadcn_>
- </Select_Shadcn_>
- <Select_Shadcn_
- value={config.yKey}
- onValueChange={(value) => {
- onConfigChange({ ...config, yKey: value })
- }}
- >
- <SelectTrigger_Shadcn_>
- Y Axis {config.yKey && `- ${config.yKey}`}
- </SelectTrigger_Shadcn_>
- <SelectContent_Shadcn_>
- <SelectGroup_Shadcn_>
- {resultKeys.map((key) => (
- <SelectItem_Shadcn_ value={key} key={key}>
- {key}
- </SelectItem_Shadcn_>
- ))}
- </SelectGroup_Shadcn_>
- </SelectContent_Shadcn_>
- </Select_Shadcn_>
- <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>
- </>
+ Flip
+ </Button>
+ )}
+ </div>
+
+ <div>
+ <Label_Shadcn_ className="text-xs text-foreground-light">X Axis</Label_Shadcn_>
+ <Select_Shadcn_
+ value={config.xKey}
+ onValueChange={(value) => {
+ onConfigChange({ ...config, xKey: value })
+ }}
+ >
+ <SelectTrigger_Shadcn_>{config.xKey || 'Select X Axis'}</SelectTrigger_Shadcn_>
+ <SelectContent_Shadcn_>
+ <SelectGroup_Shadcn_>
+ {resultKeys.map((key) => (
+ <SelectItem_Shadcn_ value={key} key={key}>
+ {key}
+ </SelectItem_Shadcn_>
+ ))}
+ </SelectGroup_Shadcn_>
+ </SelectContent_Shadcn_>
+ </Select_Shadcn_>
+ </div>
+
+ <div>
+ <Label_Shadcn_ className="text-xs text-foreground-light">Y Axis</Label_Shadcn_>
+ <Select_Shadcn_
+ value={config.yKey}
+ onValueChange={(value) => {
+ onConfigChange({ ...config, yKey: value })
+ }}
+ >
+ <SelectTrigger_Shadcn_>{config.yKey || 'Select Y Axis'}</SelectTrigger_Shadcn_>
+ <SelectContent_Shadcn_>
+ <SelectGroup_Shadcn_>
+ {resultKeys.map((key) => (
+ <SelectItem_Shadcn_ value={key} key={key}>
+ {key}
+ </SelectItem_Shadcn_>
+ ))}
+ </SelectGroup_Shadcn_>
+ </SelectContent_Shadcn_>
+ </Select_Shadcn_>
+ </div>
+ <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>
+ </ResizablePanel>
</ResizablePanelGroup>
)
}
diff --git a/apps/studio/components/ui/Charts/BarChart.tsx b/apps/studio/components/ui/Charts/BarChart.tsx
index aa2ec9766b6..e4d00370887 100644
--- a/apps/studio/components/ui/Charts/BarChart.tsx
+++ b/apps/studio/components/ui/Charts/BarChart.tsx
@@ -2,14 +2,13 @@ import dayjs from 'dayjs'
import { ComponentProps, useState } from 'react'
import {
Bar,
+ CartesianGrid,
Cell,
Legend,
BarChart as RechartBarChart,
Tooltip,
XAxis,
- Label,
YAxis,
- CartesianGrid,
} from 'recharts'
import { CHART_COLORS, DateTimeFormats } from 'components/ui/Charts/Charts.constants'
@@ -155,7 +154,7 @@ const BarChart = ({
dataKey={yAxisKey}
fill={CHART_COLORS.GREEN_1}
animationDuration={300}
- // max bar size required to prevent bars from expanding to max width.
+ // Max bar size required to prevent bars from expanding to max width.
maxBarSize={48}
>
{data?.map((_entry: Datum, index: any) => (