-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(legacy-plugin-chart-pivot-table): pivot table chart string aggreg…
…ation empty values (#880) * fix(legacy-plugin-chart-pivot-table): empty value for string aggregate * fix(plugin): bug fix * fix(plugin): fix test * fix(test): fixted function name * lint fixed * lint * handle null values * nit picks * js to ts * nit picks * null values fixed * eq fixed
- Loading branch information
1 parent
b5e8c29
commit d3bc089
Showing
3 changed files
with
190 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
..._superset_ui/superset-ui/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import { formatNumber } from '@superset-ui/core'; | ||
|
||
function formatCellValue( | ||
i: number, | ||
cols: string[], | ||
tdText: string, | ||
columnFormats: any, | ||
numberFormat: string, | ||
dateRegex: RegExp, | ||
dateFormatter: any, | ||
) { | ||
const metric: string = cols[i]; | ||
const format: string = columnFormats[metric] || numberFormat || '.3s'; | ||
let textContent: string = tdText; | ||
let sortAttributeValue: any = tdText; | ||
|
||
if (parseFloat(tdText)) { | ||
const parsedValue = parseFloat(tdText); | ||
textContent = formatNumber(format, parsedValue); | ||
sortAttributeValue = parsedValue; | ||
} else { | ||
const regexMatch = dateRegex.exec(tdText); | ||
if (regexMatch) { | ||
const date = new Date(parseFloat(regexMatch[1])); | ||
textContent = dateFormatter(date); | ||
sortAttributeValue = date; | ||
} else if (tdText === 'null') { | ||
textContent = ''; | ||
sortAttributeValue = Number.NEGATIVE_INFINITY; | ||
} | ||
} | ||
// @ts-ignore | ||
const attr = ('data-sort', sortAttributeValue); | ||
|
||
return { textContent, attr }; | ||
} | ||
|
||
function formatDateCellValue(text: string, verboseMap: any, dateRegex: RegExp, dateFormatter: any) { | ||
const regexMatch = dateRegex.exec(text); | ||
let cellValue; | ||
if (regexMatch) { | ||
const date = new Date(parseFloat(regexMatch[1])); | ||
cellValue = dateFormatter(date); | ||
} else { | ||
cellValue = verboseMap[text] || text; | ||
} | ||
return cellValue; | ||
} | ||
|
||
export { formatCellValue, formatDateCellValue }; |
91 changes: 91 additions & 0 deletions
91
...y_superset_ui/superset-ui/plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import { getTimeFormatterForGranularity } from '@superset-ui/core'; | ||
import { formatCellValue } from '../src/utils/formatCells'; | ||
|
||
describe('pivot table plugin format cells', () => { | ||
const i = 0; | ||
const cols = ['SUM']; | ||
let tdText = '2222222'; | ||
const columnFormats = {}; | ||
const numberFormat = 'SMART_NUMBER'; | ||
const dateRegex = /^__timestamp:(-?\d*\.?\d*)$/; | ||
const dateFormatter = getTimeFormatterForGranularity('P1D'); | ||
|
||
it('render number', () => { | ||
const { textContent, attr } = formatCellValue( | ||
i, | ||
cols, | ||
tdText, | ||
columnFormats, | ||
numberFormat, | ||
dateRegex, | ||
dateFormatter, | ||
); | ||
expect(textContent).toEqual('2.22M'); | ||
expect(attr).toEqual(('data-sort', 2222222)); | ||
}); | ||
|
||
it('render date', () => { | ||
tdText = '__timestamp:-126230400000.0'; | ||
|
||
const { textContent } = formatCellValue( | ||
i, | ||
cols, | ||
tdText, | ||
columnFormats, | ||
numberFormat, | ||
dateRegex, | ||
dateFormatter, | ||
); | ||
expect(textContent).toEqual('1966-01-01'); | ||
}); | ||
|
||
it('render string', () => { | ||
tdText = 'some-text'; | ||
|
||
const { textContent, attr } = formatCellValue( | ||
i, | ||
cols, | ||
tdText, | ||
columnFormats, | ||
numberFormat, | ||
dateRegex, | ||
dateFormatter, | ||
); | ||
expect(textContent).toEqual(tdText); | ||
expect(attr).toEqual(('data-sort', tdText)); | ||
}); | ||
|
||
it('render null', () => { | ||
tdText = 'null'; | ||
|
||
const { textContent, attr } = formatCellValue( | ||
i, | ||
cols, | ||
tdText, | ||
columnFormats, | ||
numberFormat, | ||
dateRegex, | ||
dateFormatter, | ||
); | ||
expect(textContent).toEqual(''); | ||
expect(attr).toEqual(('data-sort', Number.NEGATIVE_INFINITY)); | ||
}); | ||
}); |