|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | import helpers from '../helpers/index'; |
4 | | -import {isNumber} from '../helpers/helpers.math'; |
5 | 4 | import {_isPointInArea} from '../helpers/helpers.canvas'; |
| 5 | +import {_lookup, _rlookup} from '../helpers/helpers.collection'; |
6 | 6 |
|
7 | 7 | /** |
8 | 8 | * Helper function to get relative position for an event |
@@ -42,38 +42,58 @@ function evaluateAllVisibleItems(chart, handler) { |
42 | 42 | } |
43 | 43 |
|
44 | 44 | /** |
45 | | - * Helper function to check the items at the hovered index on the index scale |
| 45 | + * Helper function to do binary search when possible |
| 46 | + * @param {object} metaset - the dataset meta |
| 47 | + * @param {string} axis - the axis mide. x|y|xy |
| 48 | + * @param {number} value - the value to find |
| 49 | + * @param {boolean} intersect - should the element intersect |
| 50 | + * @returns {lo, hi} indices to search data array between |
| 51 | + */ |
| 52 | +function binarySearch(metaset, axis, value, intersect) { |
| 53 | + const {controller, data, _sorted} = metaset; |
| 54 | + const iScale = controller._cachedMeta.iScale; |
| 55 | + if (iScale && axis === iScale.axis && _sorted) { |
| 56 | + const lookupMethod = iScale._reversePixels ? _rlookup : _lookup; |
| 57 | + if (!intersect) { |
| 58 | + return lookupMethod(data, axis, value); |
| 59 | + } else if (controller._sharedOptions) { |
| 60 | + // _sharedOptions indicates that each element has equal options -> equal proportions |
| 61 | + // So we can do a ranged binary search based on the range of first element and |
| 62 | + // be confident to get the full range of indices that can intersect with the value. |
| 63 | + const el = data[0]; |
| 64 | + const range = typeof el.getRange === 'function' && el.getRange(axis); |
| 65 | + if (range) { |
| 66 | + const start = lookupMethod(data, axis, value - range); |
| 67 | + const end = lookupMethod(data, axis, value + range); |
| 68 | + return {lo: start.lo, hi: end.hi}; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + // Default to all elements, when binary search can not be used. |
| 73 | + return {lo: 0, hi: data.length - 1}; |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Helper function to get items using binary search, when the data is sorted. |
46 | 78 | * @param {Chart} chart - the chart |
47 | 79 | * @param {string} axis - the axis mode. x|y|xy |
48 | 80 | * @param {object} position - the point to be nearest to |
49 | 81 | * @param {function} handler - the callback to execute for each visible item |
50 | | - * @return whether all scales were of a suitable type |
| 82 | + * @param {boolean} intersect - consider intersecting items |
51 | 83 | */ |
52 | | -function evaluateItemsAtIndex(chart, axis, position, handler) { |
| 84 | +function optimizedEvaluateItems(chart, axis, position, handler, intersect) { |
53 | 85 | const metasets = chart._getSortedVisibleDatasetMetas(); |
54 | | - const indices = []; |
55 | | - for (let i = 0, ilen = metasets.length; i < ilen; ++i) { |
56 | | - const metaset = metasets[i]; |
57 | | - const iScale = metaset.controller._cachedMeta.iScale; |
58 | | - if (!iScale || axis !== iScale.axis || !iScale.getIndexForPixel) { |
59 | | - return false; |
60 | | - } |
61 | | - const index = iScale.getIndexForPixel(position[axis]); |
62 | | - if (!isNumber(index)) { |
63 | | - return false; |
64 | | - } |
65 | | - indices.push(index); |
66 | | - } |
67 | | - // do this only after checking whether all scales are of a suitable type |
| 86 | + const value = position[axis]; |
68 | 87 | for (let i = 0, ilen = metasets.length; i < ilen; ++i) { |
69 | | - const metaset = metasets[i]; |
70 | | - const index = indices[i]; |
71 | | - const element = metaset.data[index]; |
72 | | - if (!element.skip) { |
73 | | - handler(element, metaset.index, index); |
| 88 | + const {index, data} = metasets[i]; |
| 89 | + let {lo, hi} = binarySearch(metasets[i], axis, value, intersect); |
| 90 | + for (let j = lo; j <= hi; ++j) { |
| 91 | + const element = data[j]; |
| 92 | + if (!element.skip) { |
| 93 | + handler(element, index, j); |
| 94 | + } |
74 | 95 | } |
75 | 96 | } |
76 | | - return true; |
77 | 97 | } |
78 | 98 |
|
79 | 99 | /** |
@@ -112,12 +132,7 @@ function getIntersectItems(chart, position, axis) { |
112 | 132 | } |
113 | 133 | }; |
114 | 134 |
|
115 | | - const optimized = evaluateItemsAtIndex(chart, axis, position, evaluationFunc); |
116 | | - if (optimized) { |
117 | | - return items; |
118 | | - } |
119 | | - |
120 | | - evaluateAllVisibleItems(chart, evaluationFunc); |
| 135 | + optimizedEvaluateItems(chart, axis, position, evaluationFunc, true); |
121 | 136 | return items; |
122 | 137 | } |
123 | 138 |
|
@@ -154,12 +169,7 @@ function getNearestItems(chart, position, axis, intersect) { |
154 | 169 | } |
155 | 170 | }; |
156 | 171 |
|
157 | | - const optimized = evaluateItemsAtIndex(chart, axis, position, evaluationFunc); |
158 | | - if (optimized) { |
159 | | - return items; |
160 | | - } |
161 | | - |
162 | | - evaluateAllVisibleItems(chart, evaluationFunc); |
| 172 | + optimizedEvaluateItems(chart, axis, position, evaluationFunc); |
163 | 173 | return items; |
164 | 174 | } |
165 | 175 |
|
|
0 commit comments