1
1
import { Code , Play } from 'lucide-react'
2
2
import { DragEvent , ReactNode , useEffect , useMemo , useState } from 'react'
3
- import { Bar , BarChart , CartesianGrid , XAxis } from 'recharts'
3
+ import { Bar , BarChart , CartesianGrid , XAxis , YAxis } from 'recharts'
4
4
import { toast } from 'sonner'
5
5
6
6
import { useParams } from 'common'
@@ -9,6 +9,7 @@ import { ChartConfig } from 'components/interfaces/SQLEditor/UtilityPanel/ChartC
9
9
import Results from 'components/interfaces/SQLEditor/UtilityPanel/Results'
10
10
import { usePrimaryDatabase } from 'data/read-replicas/replicas-query'
11
11
import { QueryResponseError , useExecuteSqlMutation } from 'data/sql/execute-sql-mutation'
12
+ import dayjs from 'dayjs'
12
13
import { Parameter , parseParameters } from 'lib/sql-parameters'
13
14
import { Dashboards } from 'types'
14
15
import { ChartContainer , ChartTooltip , ChartTooltipContent , cn , CodeBlock , SQL_ICON } from 'ui'
@@ -133,9 +134,14 @@ export const QueryBlock = ({
133
134
const formattedQueryResult = useMemo ( ( ) => {
134
135
// Make sure Y axis values are numbers
135
136
return queryResult ?. map ( ( row ) => {
136
- return Object . fromEntries ( Object . entries ( row ) . map ( ( [ key , value ] ) => [ key , Number ( value ) ] ) )
137
+ return Object . fromEntries (
138
+ Object . entries ( row ) . map ( ( [ key , value ] ) => {
139
+ if ( key === yKey ) return [ key , Number ( value ) ]
140
+ else return [ key , value ]
141
+ } )
142
+ )
137
143
} )
138
- } , [ queryResult ] )
144
+ } , [ queryResult , yKey ] )
139
145
140
146
const [ parameterValues , setParameterValues ] = useState < Record < string , string > > ( { } )
141
147
const [ showWarning , setShowWarning ] = useState < 'hasWriteOperation' | 'hasUnknownFunctions' > ( )
@@ -151,6 +157,10 @@ export const QueryBlock = ({
151
157
const postgresConnectionString = primaryDatabase ?. connectionString
152
158
const readOnlyConnectionString = primaryDatabase ?. connection_string_read_only
153
159
160
+ const chartData = chartSettings . cumulative
161
+ ? getCumulativeResults ( { rows : formattedQueryResult ?? [ ] } , chartSettings )
162
+ : formattedQueryResult
163
+
154
164
const { mutate : execute , isLoading : isExecuting } = useExecuteSqlMutation ( {
155
165
onSuccess : ( data ) => {
156
166
onResults ?.( data . result )
@@ -166,6 +176,14 @@ export const QueryBlock = ({
166
176
} ,
167
177
} )
168
178
179
+ const getDateFormat = ( key : any ) => {
180
+ const value = chartData ?. [ 0 ] ?. [ key ] || ''
181
+ if ( typeof value === 'number' ) return 'number'
182
+ if ( dayjs ( value ) . isValid ( ) ) return 'date'
183
+ return 'string'
184
+ }
185
+ const xKeyDateFormat = getDateFormat ( xKey )
186
+
169
187
const handleExecute = ( ) => {
170
188
if ( ! sql || isLoading ) return
171
189
@@ -372,21 +390,22 @@ export const QueryBlock = ({
372
390
>
373
391
< BarChart
374
392
accessibilityLayer
375
- margin = { { left : 0 , right : 0 } }
376
- data = {
377
- chartSettings . cumulative
378
- ? getCumulativeResults ( { rows : formattedQueryResult ?? [ ] } , chartSettings )
379
- : formattedQueryResult
380
- }
393
+ margin = { { left : - 20 , right : 0 , top : 10 } }
394
+ data = { chartData }
381
395
>
382
396
< CartesianGrid vertical = { false } />
383
397
< XAxis
398
+ tickLine
384
399
dataKey = { xKey }
385
- tickLine = { false }
386
400
axisLine = { false }
387
- tickMargin = { 8 }
401
+ interval = "preserveStartEnd"
402
+ tickMargin = { 4 }
388
403
minTickGap = { 32 }
404
+ tickFormatter = { ( value ) =>
405
+ xKeyDateFormat === 'date' ? dayjs ( value ) . format ( 'MMM D YYYY HH:mm' ) : value
406
+ }
389
407
/>
408
+ < YAxis tickLine = { false } axisLine = { false } tickMargin = { 4 } />
390
409
< ChartTooltip content = { < ChartTooltipContent className = "w-[150px]" /> } />
391
410
< Bar dataKey = { yKey } fill = "var(--chart-1)" radius = { 4 } />
392
411
</ BarChart >
0 commit comments