import { BarChart2, LineChart, Settings2, Table } from 'lucide-react' import { useEffect, useEffectEvent, useMemo } from 'react' import { Popover, PopoverContent, PopoverTrigger, Select, SelectContent, SelectItem, SelectTrigger, SelectValue, Switch, ToggleGroup, ToggleGroupItem, Tooltip, TooltipContent, TooltipTrigger, } from 'ui' import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout' import { MultiSelector, MultiSelectorContent, MultiSelectorItem, MultiSelectorList, MultiSelectorTrigger, } from 'ui-patterns/multi-select' import { ExplorerToolbarAction } from '../ExplorerToolbar' import { type QueryDisplay, type QueryResult } from '../types' import { checkHasNonPositiveValues } from '@/components/ui/QueryBlock/QueryBlock.utils' import { MAX_CHART_Y_SERIES, type ChartConfig } from '@/data/content/notebooks/notebook-schema' interface DisplaySettingsButtonProps { display: QueryDisplay result?: QueryResult columns: string[] disabled: boolean onChange: (display: QueryDisplay) => void } const getLogScaleDisabledReason = (y_series: string[]) => { if (y_series.length === 0) return 'Select a column for the Y axis first' if (y_series.length > 1) return 'Only available with a single Y axis column' return 'Data contains zero or negative values' } export const DisplaySettingsButton = ({ display, result, columns, disabled, onChange, }: DisplaySettingsButtonProps) => { const { view, chart } = display const { type = 'bar', x_column, y_series = [], cumulative = false, show_labels = false, scale = 'linear', } = chart ?? {} const hasNonPositiveValues = useMemo( () => checkHasNonPositiveValues(result?.rows ?? [], y_series[0]), [result, y_series] ) // Logarithmic scale only applies to a single series. const canToggleLogScale = useMemo(() => { if (y_series.length !== 1 || !result || (result.rows ?? []).length === 0) return false return !hasNonPositiveValues }, [hasNonPositiveValues, result, y_series.length]) const onChangeView = (view: 'table' | 'chart') => { onChange({ ...display, view }) } const onUpdateChartConfig = (payload: Partial) => { onChange({ ...display, chart: { type: chart?.type ?? 'bar', x_column: chart?.x_column ?? '', y_series: chart?.y_series ?? [], cumulative: chart?.cumulative ?? false, scale: chart?.scale ?? 'linear', show_labels: chart?.show_labels ?? false, ...payload, }, }) } const resetToLinearScale = useEffectEvent(() => { onUpdateChartConfig({ scale: 'linear' }) }) useEffect(() => { if (scale === 'log' && (hasNonPositiveValues || y_series.length > 1)) { resetToLinearScale() } }, [hasNonPositiveValues, scale, y_series.length]) return ( } tooltip="Result settings" />
{ if (value === 'table' || value === 'chart') onChangeView(value) }} aria-label="View result as" variant="outline" size="tiny" type="single" className="w-full" > Table Chart {view === 'chart' && (
{ if (value === 'bar' || value === 'line') onUpdateChartConfig({ type: value }) }} aria-label="Chart type" variant="default" size="tiny" type="single" className="w-full" > Bar Line { if (values.length > MAX_CHART_Y_SERIES) return onUpdateChartConfig({ y_series: values }) }} className="w-full" > {columns.map((x) => ( = MAX_CHART_Y_SERIES && !y_series.includes(x)} > {x} ))} { if (value === 'linear' || (value === 'log' && canToggleLogScale)) { onUpdateChartConfig({ scale: value }) } }} aria-label="Chart scale" variant="default" size="tiny" type="single" className="w-full" > Linear Log {!canToggleLogScale && ( {getLogScaleDisabledReason(y_series)} )} onUpdateChartConfig({ cumulative })} /> onUpdateChartConfig({ show_labels })} />
)} ) }