This repository was archived by the owner on Dec 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 265
fix(legacy-plugin-chart-pivot-table): pivot table chart string aggregation empty values #880
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8ef584e
fix(legacy-plugin-chart-pivot-table): empty value for string aggregate
mayurnewase 2df0795
fix(plugin): bug fix
mayurnewase be282ed
fix(plugin): fix test
mayurnewase dc4fe16
fix(test): fixted function name
mayurnewase d18453c
lint fixed
mayurnewase 9b22902
lint
mayurnewase 2ff977e
handle null values
mayurnewase 870c0af
nit picks
mayurnewase 725a56a
js to ts
mayurnewase 015ba7f
nit picks
mayurnewase 95cd261
null values fixed
mayurnewase 786e2df
eq fixed
mayurnewase File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.ts
This file contains hidden or 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
plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.ts
This file contains hidden or 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)); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Eh... I don't think this is a valid JavaScript syntax....