This repository has been archived by the owner on Jun 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(clean the code): clean the code
- Loading branch information
Conglei Shi
committed
Aug 12, 2019
1 parent
861e3db
commit 393733e
Showing
10 changed files
with
292 additions
and
153 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
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
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
92 changes: 92 additions & 0 deletions
92
packages/superset-ui-plugin-chart-table/src/processColumns.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,92 @@ | ||
import { getNumberFormatter, NumberFormats, NumberFormatter } from '@superset-ui/number-format'; | ||
import { getTimeFormatter, TimeFormatter } from '@superset-ui/time-format'; | ||
import { PlainObject } from './types'; | ||
|
||
const DTTM_ALIAS = '__timestamp'; | ||
|
||
export default function processColumns({ | ||
columns, | ||
metrics, | ||
records, | ||
tableTimestampFormat, | ||
datasource, | ||
}: { | ||
columns: string[]; | ||
metrics: string[]; | ||
records: any[]; | ||
tableTimestampFormat: string; | ||
datasource: PlainObject; | ||
}) { | ||
const { columnFormats, verboseMap } = datasource; | ||
|
||
const dataArray: { | ||
[key: string]: any[]; | ||
} = {}; | ||
|
||
metrics.forEach(metric => { | ||
const arr = []; | ||
for (let i = 0; i < records.length; i += 1) { | ||
arr.push(records[i][metric]); | ||
} | ||
|
||
dataArray[metric] = arr; | ||
}); | ||
|
||
const maxes: { | ||
[key: string]: number; | ||
} = {}; | ||
const mins: { | ||
[key: string]: number; | ||
} = {}; | ||
|
||
for (let i = 0; i < metrics.length; i += 1) { | ||
maxes[metrics[i]] = Math.max(...dataArray[metrics[i]]); | ||
mins[metrics[i]] = Math.min(...dataArray[metrics[i]]); | ||
} | ||
|
||
const formatPercent = getNumberFormatter(NumberFormats.PERCENT_3_POINT); | ||
const tsFormatter = getTimeFormatter(tableTimestampFormat); | ||
|
||
const processedColumns = columns.map((key: string) => { | ||
let label = verboseMap[key]; | ||
let formatString = columnFormats && columnFormats[key]; | ||
let formatFunction: NumberFormatter | TimeFormatter | undefined; | ||
let type = 'string'; | ||
|
||
if (key === DTTM_ALIAS) { | ||
formatFunction = tsFormatter; | ||
} | ||
|
||
const extraField: { | ||
[key: string]: any; | ||
} = {}; | ||
|
||
if (metrics.indexOf(key) >= 0) { | ||
formatFunction = getNumberFormatter(formatString); | ||
type = 'metric'; | ||
extraField['maxValue'] = maxes[key]; | ||
extraField['minValue'] = mins[key]; | ||
} | ||
|
||
// Handle verbose names for percents | ||
if (!label) { | ||
if (key[0] === '%') { | ||
const cleanedKey = key.substring(1); | ||
label = `% ${verboseMap[cleanedKey] || cleanedKey}`; | ||
formatFunction = formatPercent; | ||
} else { | ||
label = key; | ||
} | ||
} | ||
|
||
return { | ||
key, | ||
label, | ||
format: formatFunction, | ||
type, | ||
...extraField, | ||
}; | ||
}); | ||
|
||
return processedColumns; | ||
} |
Oops, something went wrong.