-
Notifications
You must be signed in to change notification settings - Fork 383
bar chart support #2005
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
bar chart support #2005
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import { useCallback } from 'react'; | ||
| import { useForm, useWatch } from 'react-hook-form'; | ||
| import { Controller, useForm, useWatch } from 'react-hook-form'; | ||
| import { | ||
| ChartConfigWithDateRange, | ||
| DisplayType, | ||
|
|
@@ -11,6 +11,7 @@ import { | |
| Divider, | ||
| Drawer, | ||
| Group, | ||
| NumberInput, | ||
| Stack, | ||
| } from '@mantine/core'; | ||
|
|
||
|
|
@@ -25,7 +26,9 @@ export type ChartConfigDisplaySettings = Pick< | |
| | 'alignDateRangeToGranularity' | ||
| | 'fillNulls' | ||
| | 'compareToPreviousPeriod' | ||
| >; | ||
| > & { | ||
| maxNumberOfGroups?: number; | ||
| }; | ||
|
|
||
| interface ChartDisplaySettingsDrawerProps { | ||
| opened: boolean; | ||
|
|
@@ -41,13 +44,15 @@ function applyDefaultSettings({ | |
| alignDateRangeToGranularity, | ||
| compareToPreviousPeriod, | ||
| fillNulls, | ||
| maxNumberOfGroups, | ||
| }: ChartConfigDisplaySettings): ChartConfigDisplaySettings { | ||
| return { | ||
| numberFormat: numberFormat ?? DEFAULT_NUMBER_FORMAT, | ||
| alignDateRangeToGranularity: | ||
| alignDateRangeToGranularity == null ? true : alignDateRangeToGranularity, | ||
| fillNulls: fillNulls ?? 0, | ||
| compareToPreviousPeriod: compareToPreviousPeriod ?? false, | ||
| maxNumberOfGroups: maxNumberOfGroups ?? 10, | ||
| }; | ||
| } | ||
|
|
||
|
|
@@ -83,6 +88,7 @@ export default function ChartDisplaySettingsDrawer({ | |
|
|
||
| const isTimeChart = | ||
| displayType === DisplayType.Line || displayType === DisplayType.StackedBar; | ||
| const isBarChart = displayType === DisplayType.Bar; | ||
|
|
||
| return ( | ||
| <Drawer | ||
|
|
@@ -128,6 +134,29 @@ export default function ChartDisplaySettingsDrawer({ | |
| </> | ||
| )} | ||
|
|
||
| {isBarChart && ( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is neat, and would be nice to add to Pie charts too. However, we'd ideally give the user an Order By input as well, so that they can specify which 10 groups to show. There is an order by on the table chart type that might be re-usable
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We also should not show this for raw sql bar charts, since it's ignored. |
||
| <> | ||
| <Box> | ||
| <Controller | ||
| control={control} | ||
| name="maxNumberOfGroups" | ||
| render={({ field }) => ( | ||
| <NumberInput | ||
| size="xs" | ||
| label="Max Number of Groups" | ||
| min={1} | ||
| max={1000} | ||
| value={field.value ?? 10} | ||
| onChange={v => | ||
| field.onChange(typeof v === 'number' ? v : 10) | ||
| } | ||
| /> | ||
| )} | ||
| /> | ||
| </Box> | ||
| <Divider /> | ||
| </> | ||
| )} | ||
| <NumberFormatForm control={control} /> | ||
| <Divider /> | ||
| <Group gap="xs" mt="xs" justify="space-between"> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,11 +51,13 @@ export const isRawSqlDisplayType = ( | |
| | DisplayType.Table | ||
| | DisplayType.Line | ||
| | DisplayType.StackedBar | ||
| | DisplayType.Bar | ||
| | DisplayType.Pie | ||
| | DisplayType.Number => | ||
| displayType === DisplayType.Table || | ||
| displayType === DisplayType.Line || | ||
| displayType === DisplayType.StackedBar || | ||
| displayType === DisplayType.Bar || | ||
| displayType === DisplayType.Pie || | ||
| displayType === DisplayType.Number; | ||
|
|
||
|
|
@@ -236,12 +238,13 @@ export const validateChartForm = ( | |
| }); | ||
| } | ||
|
|
||
| // Validate number and pie charts only have one series | ||
| // Validate number, pie, and bar charts only have one series | ||
| if ( | ||
| !isRawSqlChart && | ||
| Array.isArray(form.series) && | ||
| (form.displayType === DisplayType.Number || | ||
| form.displayType === DisplayType.Pie) && | ||
| form.displayType === DisplayType.Pie || | ||
| form.displayType === DisplayType.Bar) && | ||
| form.series.length > 1 | ||
| ) { | ||
| errors.push({ | ||
|
|
@@ -250,6 +253,18 @@ export const validateChartForm = ( | |
| }); | ||
| } | ||
|
|
||
| // Validate bar charts require a group by | ||
| if ( | ||
| !isRawSqlChart && | ||
| form.displayType === DisplayType.Bar && | ||
| !form.groupBy | ||
| ) { | ||
| errors.push({ | ||
| path: `groupBy`, | ||
| message: 'Group By is required for bar charts', | ||
| }); | ||
| } | ||
|
|
||
|
Comment on lines
+256
to
+267
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we add a unit test covering this check? There are existing unit tests covering this function which can be extended. |
||
| for (const error of errors) { | ||
| console.warn(`Validation error in field ${error.path}: ${error.message}`); | ||
| setError(error.path, { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this display setting intentionally not persisted? All other display settings are persisted as part of the config in the DB / URL Params and available via the API - it is odd if this one is not.
Perhaps instead of adding this new property, we can just use the existing limit property. That way it is persisted, and you don't have to copy back and forth between limit and maxNumberOfGroups in the places where that is happening right now.
normalizeChartConfig could also then be updated to strip
limitfrom non-Bar type chart configs before saving, since the customizable limit is only available for bar charts (though it would be good to add to pie as well)