Skip to content

Commit

Permalink
fix: handle better mixed values when accumulating
Browse files Browse the repository at this point in the history
  • Loading branch information
edoardo committed Oct 11, 2024
1 parent 4e93148 commit 7de9aab
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/components/PivotTable/styles/PivotTable.style.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ export const cell = css`
text-align: right;
}
.N_A {
text-align: center;
color: ${colors.grey600};
}
Expand Down
20 changes: 14 additions & 6 deletions src/modules/pivotTable/PivotTableEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
NUMBER_TYPE_ROW_PERCENTAGE,
NUMBER_TYPE_VALUE,
VALUE_TYPE_NA,
VALUE_NA,
} from './pivotTableConstants.js'

const dataFields = [
Expand Down Expand Up @@ -247,7 +248,7 @@ const applyTotalAggregationType = (
) => {
switch (overrideTotalAggregationType || totalAggregationType) {
case AGGREGATE_TYPE_NA:
return 'N/A'
return VALUE_NA
case AGGREGATE_TYPE_AVERAGE:
return (
((numerator || value) * multiplier) /
Expand Down Expand Up @@ -433,9 +434,12 @@ export class PivotTableEngine {
})

if (cumulativeValue !== undefined && cumulativeValue !== null) {
// force to NUMBER for accumulated values
// force to TEXT for N/A (accumulated) values
// force to NUMBER for accumulated values if no valueType present
rawCell.valueType =
valueType === undefined || valueType === null
cumulativeValue === VALUE_NA
? VALUE_TYPE_NA
: valueType === undefined || valueType === null
? VALUE_TYPE_NUMBER
: valueType
rawCell.empty = false
Expand Down Expand Up @@ -1087,7 +1091,7 @@ export class PivotTableEngine {

// only accumulate numeric (except for PERCENTAGE and UNIT_INTERVAL) and boolean values
// accumulating other value types like text values does not make sense
if (isCumulativeValueType(valueType)) {
if (acc !== VALUE_NA && isCumulativeValueType(valueType)) {
// initialise to 0 for cumulative types
// (||= is not transformed correctly in Babel with the current setup)
acc || (acc = 0)
Expand All @@ -1104,10 +1108,14 @@ export class PivotTableEngine {

acc += parseValue(rawValue)
}

this.accumulators.rows[row][column] = acc
} else {
// show N/A from the first non-cumulative type and onwards
// only if a previous value is present (this is to avoid filling empty rows with N/A)
acc = acc ? VALUE_NA : ''
}

this.accumulators.rows[row][column] = acc

return acc
}, '')
})
Expand Down
2 changes: 2 additions & 0 deletions src/modules/pivotTable/pivotTableConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ export const WRAPPED_TEXT_JUSTIFY_BUFFER = 25
export const WRAPPED_TEXT_LINE_HEIGHT = 1.0

export const CLIPPED_AXIS_PARTITION_SIZE_PX = 1000

export const VALUE_NA = 'N/A'

0 comments on commit 7de9aab

Please sign in to comment.