* Linearly interpolates the given source `val` using the table. If value is out of bounds, values * at edges are used for the interpolation. * @param {object} table * @param {number} val * @param {boolean} [reverse] lookup time based on position instead of vice versa * @return {object}
(table, val, reverse)
| 10 | * @return {object} |
| 11 | */ |
| 12 | function interpolate(table, val, reverse) { |
| 13 | let lo = 0; |
| 14 | let hi = table.length - 1; |
| 15 | let prevSource, nextSource, prevTarget, nextTarget; |
| 16 | if (reverse) { |
| 17 | if (val >= table[lo].pos && val <= table[hi].pos) { |
| 18 | ({lo, hi} = _lookupByKey(table, 'pos', val)); |
| 19 | } |
| 20 | ({pos: prevSource, time: prevTarget} = table[lo]); |
| 21 | ({pos: nextSource, time: nextTarget} = table[hi]); |
| 22 | } else { |
| 23 | if (val >= table[lo].time && val <= table[hi].time) { |
| 24 | ({lo, hi} = _lookupByKey(table, 'time', val)); |
| 25 | } |
| 26 | ({time: prevSource, pos: prevTarget} = table[lo]); |
| 27 | ({time: nextSource, pos: nextTarget} = table[hi]); |
| 28 | } |
| 29 | |
| 30 | const span = nextSource - prevSource; |
| 31 | return span ? prevTarget + (nextTarget - prevTarget) * (val - prevSource) / span : prevTarget; |
| 32 | } |
| 33 | |
| 34 | class TimeSeriesScale extends TimeScale { |
| 35 |
no test coverage detected