mirror of
https://github.com/supabase/supabase.git
synced 2026-10-06 18:05:11 +03:00
## Context Related to Explorer/Notebooks - Adds support for log scale in the charts <img width="980" height="477" alt="image" src="https://github.com/user-attachments/assets/b3764023-bd9a-4edd-92d4-1b61df93b97e" /> Prevents setting to log scale if y axis has yet to be selected, or if the data set contains non positive values <img width="393" height="188" alt="image" src="https://github.com/user-attachments/assets/57695d9e-9724-47ed-aa2a-c104c16837b2" /> <img width="408" height="159" alt="image" src="https://github.com/user-attachments/assets/2290bb69-0ddb-4449-afad-aabae6ace669" /> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Added logarithmic Y-axis scaling for line and bar charts. - Added logarithmic tick formatting and appropriate chart bounds. - Added contextual guidance when logarithmic scaling is unavailable. - **Bug Fixes** - Automatically switches charts back to linear scaling when data contains values that cannot support logarithmic scaling. - Prevented unnecessary stacking for single-series area charts. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
275 lines
9.2 KiB
TypeScript
275 lines
9.2 KiB
TypeScript
import { BarChart2, Settings2, Table } from 'lucide-react'
|
|
import { useEffect, useEffectEvent, useMemo } from 'react'
|
|
import {
|
|
Checkbox,
|
|
Popover,
|
|
PopoverContent,
|
|
PopoverSeparator,
|
|
PopoverTrigger,
|
|
Select,
|
|
SelectContent,
|
|
SelectItem,
|
|
SelectTrigger,
|
|
SelectValue,
|
|
ToggleGroup,
|
|
ToggleGroupItem,
|
|
Tooltip,
|
|
TooltipContent,
|
|
TooltipTrigger,
|
|
} from 'ui'
|
|
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
|
|
import { type Snapshot } from 'valtio'
|
|
|
|
import { ExplorerToolbarAction } from '../ExplorerToolbar'
|
|
import { type QueryResult } from '../types'
|
|
import { checkHasNonPositiveValues } from '@/components/ui/QueryBlock/QueryBlock.utils'
|
|
import { type DatabaseCell as DatabaseCellSchema } from '@/data/content/notebooks/notebook-schema'
|
|
import { useCurrentNotebook, useNotebooksStateSnapshot } from '@/state/notebooks/notebooks-state'
|
|
|
|
interface DisplaySettingsButtonProps {
|
|
cell: Snapshot<DatabaseCellSchema>
|
|
result?: QueryResult
|
|
columns: string[]
|
|
disabled: boolean
|
|
}
|
|
|
|
// [Joshen] TODO support multiple y axis charts
|
|
|
|
export const DisplaySettingsButton = ({
|
|
cell,
|
|
result,
|
|
columns,
|
|
disabled,
|
|
}: DisplaySettingsButtonProps) => {
|
|
const snap = useNotebooksStateSnapshot()
|
|
const currentNotebook = useCurrentNotebook()
|
|
const cells = currentNotebook?.notebook.content?.cells ?? []
|
|
|
|
const { view, chart } = cell
|
|
const {
|
|
type = 'bar',
|
|
x_column,
|
|
y_columns = [],
|
|
cumulative = false,
|
|
show_labels = false,
|
|
scale = 'linear',
|
|
} = chart ?? {}
|
|
|
|
const hasNonPositiveValues = useMemo(
|
|
() => checkHasNonPositiveValues(result?.rows ?? [], y_columns[0]),
|
|
[result, y_columns]
|
|
)
|
|
|
|
const canToggleLogScale = useMemo(() => {
|
|
if (y_columns.length === 0 || !result || (result.rows ?? []).length === 0) return false
|
|
return !hasNonPositiveValues
|
|
}, [hasNonPositiveValues, result, y_columns.length])
|
|
|
|
const onChangeView = (view: 'table' | 'chart') => {
|
|
const notebookId = currentNotebook?.notebook.id
|
|
if (!notebookId) return
|
|
|
|
const nextCells = cells.map((c) =>
|
|
c.id === cell.id && c._tag === 'database_cell' ? { ...c, view } : c
|
|
)
|
|
snap.updateCells({ id: notebookId, cells: nextCells })
|
|
}
|
|
|
|
const onUpdateChartConfig = (
|
|
payload:
|
|
| { type: 'bar' | 'line' }
|
|
| { x_column: string }
|
|
| { y_columns: string[] }
|
|
| { cumulative: boolean }
|
|
| { show_labels: boolean }
|
|
| { scale: 'linear' | 'log' }
|
|
) => {
|
|
const notebookId = currentNotebook?.notebook.id
|
|
if (!notebookId) return
|
|
|
|
const nextCells = cells.map((c) => {
|
|
if (c.id !== cell.id || c._tag !== 'database_cell') return c
|
|
|
|
return {
|
|
...c,
|
|
chart: {
|
|
type: c.chart?.type ?? 'bar',
|
|
x_column: c.chart?.x_column ?? '',
|
|
y_columns: c.chart?.y_columns ?? [],
|
|
cumulative: c.chart?.cumulative ?? false,
|
|
scale: c.chart?.scale ?? 'linear',
|
|
show_labels: c.chart?.show_labels ?? false,
|
|
...payload,
|
|
},
|
|
}
|
|
})
|
|
snap.updateCells({ id: notebookId, cells: nextCells })
|
|
}
|
|
|
|
const resetToLinearScale = useEffectEvent(() => {
|
|
onUpdateChartConfig({ scale: 'linear' })
|
|
})
|
|
|
|
useEffect(() => {
|
|
if (hasNonPositiveValues && scale === 'log') {
|
|
resetToLinearScale()
|
|
}
|
|
}, [hasNonPositiveValues, scale])
|
|
|
|
return (
|
|
<Popover>
|
|
<PopoverTrigger asChild>
|
|
<ExplorerToolbarAction disabled={disabled} icon={<Settings2 />} tooltip="Result settings" />
|
|
</PopoverTrigger>
|
|
<PopoverContent side="bottom" className="flex flex-col gap-y-3 p-0 py-3 mr-8">
|
|
<div className="flex flex-col gap-y-3 px-3">
|
|
<p className="text-xs tracking-tighter uppercase font-mono text-foreground-lighter">
|
|
Result display settings
|
|
</p>
|
|
|
|
<FormItemLayout isReactForm={false} label="View data as">
|
|
<ToggleGroup
|
|
value={view}
|
|
onValueChange={(value) => {
|
|
if (value === 'table' || value === 'chart') onChangeView(value)
|
|
}}
|
|
variant="outline"
|
|
type="single"
|
|
>
|
|
<ToggleGroupItem value="table" className="w-full gap-x-2 data-[state=on]:bg-accent">
|
|
<Table size={14} />
|
|
<p>Table</p>
|
|
</ToggleGroupItem>
|
|
<ToggleGroupItem value="chart" className="w-full gap-x-2 data-[state=on]:bg-accent">
|
|
<BarChart2 size={14} />
|
|
<p>Chart</p>
|
|
</ToggleGroupItem>
|
|
</ToggleGroup>
|
|
</FormItemLayout>
|
|
</div>
|
|
|
|
{view === 'chart' && (
|
|
<>
|
|
<PopoverSeparator />
|
|
|
|
<FormItemLayout isReactForm={false} label="Render chart as" className="px-3 mb-1">
|
|
<ToggleGroup
|
|
value={type}
|
|
onValueChange={(value) => {
|
|
if (value === 'bar' || value === 'line') onUpdateChartConfig({ type: value })
|
|
}}
|
|
variant="outline"
|
|
type="single"
|
|
>
|
|
<ToggleGroupItem value="bar" className="w-full data-[state=on]:bg-accent">
|
|
Bar
|
|
</ToggleGroupItem>
|
|
<ToggleGroupItem value="line" className="w-full data-[state=on]:bg-accent">
|
|
Line
|
|
</ToggleGroupItem>
|
|
</ToggleGroup>
|
|
</FormItemLayout>
|
|
|
|
<div className="px-3 flex flex-col gap-y-2">
|
|
<FormItemLayout isReactForm={false} layout="flex-row-reverse" label="X axis">
|
|
<Select
|
|
value={x_column}
|
|
onValueChange={(x_column) => onUpdateChartConfig({ x_column })}
|
|
>
|
|
<SelectTrigger className="w-32">
|
|
<SelectValue placeholder="Select a column" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
{columns.map((x) => (
|
|
<SelectItem key={x} value={x}>
|
|
{x}
|
|
</SelectItem>
|
|
))}
|
|
</SelectContent>
|
|
</Select>
|
|
</FormItemLayout>
|
|
|
|
<FormItemLayout isReactForm={false} layout="flex-row-reverse" label="Y Axis">
|
|
<Select
|
|
value={y_columns?.[0]}
|
|
onValueChange={(y_column) => onUpdateChartConfig({ y_columns: [y_column] })}
|
|
>
|
|
<SelectTrigger className="w-32">
|
|
<SelectValue placeholder="Select a column" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
{columns.map((x) => (
|
|
<SelectItem key={x} value={x}>
|
|
{x}
|
|
</SelectItem>
|
|
))}
|
|
</SelectContent>
|
|
</Select>
|
|
</FormItemLayout>
|
|
|
|
<FormItemLayout isReactForm={false} layout="flex-row-reverse" label="Scale">
|
|
<Select
|
|
value={scale}
|
|
onValueChange={(scale) =>
|
|
onUpdateChartConfig({ scale: scale as 'log' | 'linear' })
|
|
}
|
|
>
|
|
<SelectTrigger className="w-32">
|
|
<SelectValue placeholder="Select a column" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value="linear">Linear</SelectItem>
|
|
<Tooltip>
|
|
<TooltipTrigger asChild>
|
|
<SelectItem
|
|
disabled={!canToggleLogScale}
|
|
value="log"
|
|
className={!canToggleLogScale ? '!pointer-events-auto' : undefined}
|
|
>
|
|
Logarithmic
|
|
</SelectItem>
|
|
</TooltipTrigger>
|
|
{!canToggleLogScale && (
|
|
<TooltipContent side="right">
|
|
{y_columns.length === 0
|
|
? 'Select a column for the Y axis first'
|
|
: 'Data contains zero or negative values'}
|
|
</TooltipContent>
|
|
)}
|
|
</Tooltip>
|
|
</SelectContent>
|
|
</Select>
|
|
</FormItemLayout>
|
|
|
|
<FormItemLayout id="cumulative" isReactForm={false} layout="flex" label="Cumulative">
|
|
<Checkbox
|
|
id="cumulative"
|
|
checked={cumulative}
|
|
onCheckedChange={(cumulative) =>
|
|
onUpdateChartConfig({ cumulative: Boolean(cumulative) })
|
|
}
|
|
/>
|
|
</FormItemLayout>
|
|
|
|
<FormItemLayout
|
|
id="show_labels"
|
|
isReactForm={false}
|
|
layout="flex"
|
|
label="Show labels"
|
|
>
|
|
<Checkbox
|
|
id="show_labels"
|
|
checked={show_labels}
|
|
onCheckedChange={(show_labels) =>
|
|
onUpdateChartConfig({ show_labels: Boolean(show_labels) })
|
|
}
|
|
/>
|
|
</FormItemLayout>
|
|
</div>
|
|
</>
|
|
)}
|
|
</PopoverContent>
|
|
</Popover>
|
|
)
|
|
}
|