MCPcopy
hub / github.com/chartjs/Chart.js / buildLookupTable

Method buildLookupTable

src/scales/scale.timeseries.js:79–111  ·  view source on GitHub ↗

* Returns an array of {time, pos} objects used to interpolate a specific `time` or position * (`pos`) on the scale, by searching entries before and after the requested value. `pos` is * a decimal between 0 and 1: 0 being the start of the scale (left or top) and 1 the other * extremity (left +

(timestamps)

Source from the content-addressed store, hash-verified

77 * @protected
78 */
79 buildLookupTable(timestamps) {
80 const {min, max} = this;
81 const items = [];
82 const table = [];
83 let i, ilen, prev, curr, next;
84
85 for (i = 0, ilen = timestamps.length; i < ilen; ++i) {
86 curr = timestamps[i];
87 if (curr >= min && curr <= max) {
88 items.push(curr);
89 }
90 }
91
92 if (items.length < 2) {
93 // In case there is less that 2 timestamps between min and max, the scale is defined by min and max
94 return [
95 {time: min, pos: 0},
96 {time: max, pos: 1}
97 ];
98 }
99
100 for (i = 0, ilen = items.length; i < ilen; ++i) {
101 next = items[i + 1];
102 prev = items[i - 1];
103 curr = items[i];
104
105 // only add points that breaks the scale linearity
106 if (Math.round((next + prev) / 2) !== curr) {
107 table.push({time: curr, pos: i / (ilen - 1)});
108 }
109 }
110 return table;
111 }
112
113 /**
114 * Generates all timestamps defined in the data.

Callers 1

initOffsetsMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected