Skip to content

Commit

Permalink
feat(bi): Added support for decimal numbers in BI (#24103)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilbert09 authored Jul 31, 2024
1 parent 89789af commit 9001b68
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ const YSeries = ({ series, index }: { series: AxisSeries<number>; index: number
<LemonField name="suffix" label="Suffix">
<LemonInput placeholder="USD" />
</LemonField>
<LemonField name="decimalPlaces" label="Decimal places">
<LemonInput type="number" min={0} />
</LemonField>
</Form>
}
visible={isSettingsOpen}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,16 @@ export const ySeriesLogic = kea<ySeriesLogicType>([
prefix: props.series?.settings?.formatting?.prefix ?? '',
suffix: props.series?.settings?.formatting?.suffix ?? '',
style: props.series?.settings?.formatting?.style ?? 'none',
decimalPlaces: props.series?.settings?.formatting?.decimalPlaces ?? '',
},
submit: async (format) => {
actions.updateYSeries(props.seriesIndex, props.series.column.name, {
formatting: {
prefix: format.prefix,
suffix: format.suffix,
style: format.style,
decimalPlaces:
format.decimalPlaces === '' ? undefined : parseInt(format.decimalPlaces.toString(), 10),
},
})
actions.setSettingsOpen(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,16 @@ const DefaultAxisSettings: AxisSeriesSettings = {
}

export const formatDataWithSettings = (data: number, settings?: AxisSeriesSettings): string => {
let dataAsString = `${data}`
const decimalPlaces = settings?.formatting?.decimalPlaces

let dataAsString = `${decimalPlaces ? data.toFixed(decimalPlaces) : data}`

if (settings?.formatting?.style === 'number') {
dataAsString = data.toLocaleString()
dataAsString = data.toLocaleString(undefined, { maximumFractionDigits: decimalPlaces })
}

if (settings?.formatting?.style === 'percent') {
dataAsString = `${(data * 100).toLocaleString()}%`
dataAsString = `${data.toLocaleString(undefined, { maximumFractionDigits: decimalPlaces })}%`
}

if (settings?.formatting?.prefix) {
Expand Down Expand Up @@ -273,7 +275,20 @@ export const dataVisualizationLogic = kea<dataVisualizationLogicType>([
column,
data: data.map((n) => {
try {
return parseInt(n[column.dataIndex], 10)
const multiplier = series.settings.formatting?.style === 'percent' ? 100 : 1

if (series.settings.formatting?.decimalPlaces) {
return parseFloat(
(parseFloat(n[column.dataIndex]) * multiplier).toFixed(
series.settings.formatting.decimalPlaces
)
)
}

const isInt = Number.isInteger(n[column.dataIndex])
return isInt
? parseInt(n[column.dataIndex], 10) * multiplier
: parseFloat(n[column.dataIndex]) * multiplier
} catch {
return 0
}
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/queries/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2046,6 +2046,9 @@
"ChartSettingsFormatting": {
"additionalProperties": false,
"properties": {
"decimalPlaces": {
"type": "number"
},
"prefix": {
"type": "string"
},
Expand Down
1 change: 1 addition & 0 deletions frontend/src/queries/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ export interface ChartSettingsFormatting {
prefix?: string
suffix?: string
style?: 'none' | 'number' | 'percent'
decimalPlaces?: number
}

export interface ChartSettings {
Expand Down
1 change: 1 addition & 0 deletions posthog/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ class ChartSettingsFormatting(BaseModel):
model_config = ConfigDict(
extra="forbid",
)
decimalPlaces: Optional[float] = None
prefix: Optional[str] = None
style: Optional[Style] = None
suffix: Optional[str] = None
Expand Down

0 comments on commit 9001b68

Please sign in to comment.