Skip to content

Commit

Permalink
Add filter accuracy to INumberDesc
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkh committed Nov 17, 2020
1 parent 25cc8b7 commit 1ac0955
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
3 changes: 2 additions & 1 deletion demo/small_numbers.html
Expand Up @@ -68,7 +68,8 @@
.label('Column with small numbers')
.width(250)
.mapping('log', [minN, maxN], [1, 0])
.custom('numberFormat', 'e'),
.custom('numberFormat', 'e')
.custom('filterAccuracy', 0.00001),
);


Expand Down
7 changes: 7 additions & 0 deletions src/model/INumberColumn.ts
Expand Up @@ -107,6 +107,13 @@ export interface INumberDesc extends IMapAbleDesc {
* @default .3n
*/
numberFormat?: string;

/**
* The accuracy defines the deviation of values to the applied filter boundary.
* Use an accuracy closer to 0 for columns with smaller numbers (e.g., 1e-9).
* @default 0.001
*/
filterAccuracy?: number;
}

/**
Expand Down
14 changes: 12 additions & 2 deletions src/model/NumberColumn.ts
Expand Up @@ -69,11 +69,17 @@ export default class NumberColumn extends ValueColumn<number> implements INumber

/**
* currently active filter
* @type {{min: number, max: number}}
* @private
*/
private currentFilter: INumberFilter = noNumberFilter();

/**
* The accuracy defines the deviation of values to the applied filter boundary.
* Use an accuracy closer to 0 for columns with smaller numbers (e.g., 1e-9).
* @private
*/
private readonly filterAccuracy: number = 0.001;

private readonly numberFormat: (n: number) => string = format('.2f');

private currentGroupThresholds: number[] = [];
Expand All @@ -92,6 +98,10 @@ export default class NumberColumn extends ValueColumn<number> implements INumber
if (desc.numberFormat) {
this.numberFormat = format(desc.numberFormat);
}

if (desc.filterAccuracy) {
this.filterAccuracy = desc.filterAccuracy;
}
}

getNumberFormat() {
Expand Down Expand Up @@ -281,7 +291,7 @@ export default class NumberColumn extends ValueColumn<number> implements INumber

setFilter(value: INumberFilter | null) {
value = value || {min: -Infinity, max: +Infinity, filterMissing: false};
if (isEqualNumberFilter(value, this.currentFilter)) {
if (isEqualNumberFilter(value, this.currentFilter, this.filterAccuracy)) {
return;
}
const bak = this.getFilter();
Expand Down
4 changes: 2 additions & 2 deletions src/model/internalNumber.ts
Expand Up @@ -60,8 +60,8 @@ export function noNumberFilter() {
}

/** @internal */
export function isEqualNumberFilter(a: INumberFilter, b: INumberFilter) {
return similar(a.min, b.min, 0.001) && similar(a.max, b.max, 0.001) && a.filterMissing === b.filterMissing;
export function isEqualNumberFilter(a: INumberFilter, b: INumberFilter, delta = 0.001) {
return similar(a.min, b.min, delta) && similar(a.max, b.max, delta) && a.filterMissing === b.filterMissing;
}

/** @internal */
Expand Down

0 comments on commit 1ac0955

Please sign in to comment.