mirror of
https://github.com/chartjs/Chart.js.git
synced 2026-03-06 08:24:05 +01:00
Fix filterBetween (#7211)
* Fix filterBetween * Update with review suggestions
This commit is contained in:
committed by
Evert Timberg
parent
efdbea9e9a
commit
dcb57388df
@@ -68,3 +68,26 @@ export function _rlookupByKey(table, key, value) {
|
||||
|
||||
return {lo, hi};
|
||||
}
|
||||
|
||||
/**
|
||||
* Return subset of `values` between `min` and `max` inclusive.
|
||||
* Values are assumed to be in sorted order.
|
||||
* @param {number[]} values - sorted array of values
|
||||
* @param {number} min - min value
|
||||
* @param {number} max - max value
|
||||
*/
|
||||
export function _filterBetween(values, min, max) {
|
||||
let start = 0;
|
||||
let end = values.length;
|
||||
|
||||
while (start < end && values[start] < min) {
|
||||
start++;
|
||||
}
|
||||
while (end > start && values[end - 1] > max) {
|
||||
end--;
|
||||
}
|
||||
|
||||
return start > 0 || end < values.length
|
||||
? values.slice(start, end)
|
||||
: values;
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ import defaults from '../core/core.defaults';
|
||||
import {isFinite, isNullOrUndef, mergeIf, valueOrDefault} from '../helpers/helpers.core';
|
||||
import {toRadians} from '../helpers/helpers.math';
|
||||
import Scale from '../core/core.scale';
|
||||
import {_lookup, _lookupByKey} from '../helpers/helpers.collection';
|
||||
import {_filterBetween, _lookup, _lookupByKey} from '../helpers/helpers.collection';
|
||||
|
||||
/**
|
||||
* @typedef { import("../core/core.adapters").Unit } Unit
|
||||
@@ -494,30 +494,6 @@ function getLabelBounds(scale) {
|
||||
return {min, max};
|
||||
}
|
||||
|
||||
/**
|
||||
* Return subset of `timestamps` between `min` and `max`.
|
||||
* Timestamps are assumend to be in sorted order.
|
||||
* @param {number[]} timestamps - array of timestamps
|
||||
* @param {number} min - min value (timestamp)
|
||||
* @param {number} max - max value (timestamp)
|
||||
*/
|
||||
function filterBetween(timestamps, min, max) {
|
||||
let start = 0;
|
||||
let end = timestamps.length - 1;
|
||||
|
||||
while (start < end && timestamps[start] < min) {
|
||||
start++;
|
||||
}
|
||||
while (end > start && timestamps[end] > max) {
|
||||
end--;
|
||||
}
|
||||
end++; // slice does not include last element
|
||||
|
||||
return start > 0 || end < timestamps.length
|
||||
? timestamps.slice(start, end)
|
||||
: timestamps;
|
||||
}
|
||||
|
||||
const defaultConfig = {
|
||||
/**
|
||||
* Data distribution along the scale:
|
||||
@@ -699,7 +675,7 @@ export default class TimeScale extends Scale {
|
||||
const min = me.min;
|
||||
const max = me.max;
|
||||
|
||||
const ticks = filterBetween(timestamps, min, max);
|
||||
const ticks = _filterBetween(timestamps, min, max);
|
||||
|
||||
// PRIVATE
|
||||
// determineUnitForFormatting relies on the number of ticks so we don't use it when
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import {_lookup, _lookupByKey, _rlookupByKey} from '../../src/helpers/helpers.collection';
|
||||
import {_filterBetween, _lookup, _lookupByKey, _rlookupByKey} from '../../src/helpers/helpers.collection';
|
||||
|
||||
describe('helpers.interpolation', function() {
|
||||
describe('helpers.collection', function() {
|
||||
it('Should do binary search', function() {
|
||||
const data = [0, 2, 6, 9];
|
||||
expect(_lookup(data, 0)).toEqual({lo: 0, hi: 1});
|
||||
@@ -27,4 +27,10 @@ describe('helpers.interpolation', function() {
|
||||
expect(_rlookupByKey(data, 'x', 8)).toEqual({lo: 0, hi: 1});
|
||||
expect(_rlookupByKey(data, 'x', 10)).toEqual({lo: 0, hi: 1});
|
||||
});
|
||||
|
||||
it('Should filter a sorted array', function() {
|
||||
expect(_filterBetween([1, 2, 3, 4, 5, 6, 7, 8, 9], 5, 8)).toEqual([5, 6, 7, 8]);
|
||||
expect(_filterBetween([1], 1, 1)).toEqual([1]);
|
||||
expect(_filterBetween([1583049600000], 1584816327553, 1585680327553)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user