You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@echarts.apache.org by su...@apache.org on 2019/11/15 11:50:02 UTC

[incubator-echarts] 03/03: fix: add unit test and comments.

This is an automated email from the ASF dual-hosted git repository.

sushuang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-echarts.git

commit 1e55f19b72d105db460dc73f7c12e11fdfdf398c
Author: SHUANG SU <su...@gmail.com>
AuthorDate: Fri Nov 15 18:12:05 2019 +0800

    fix: add unit test and comments.
---
 src/data/List.js          |  7 ++++++-
 test/ut/spec/data/List.js | 20 ++++++++++++++++++++
 2 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/src/data/List.js b/src/data/List.js
index 86d31ec..d98c2b6 100644
--- a/src/data/List.js
+++ b/src/data/List.js
@@ -1144,7 +1144,8 @@ listProto.indexOfRawIndex = function (rawIndex) {
  * @param {string} dim
  * @param {number} value
  * @param {number} [maxDistance=Infinity]
- * @return {Array.<number>} Considere multiple points has the same value.
+ * @return {Array.<number>} If and only if multiple indices has
+ *        the same value, they are put to the result.
  */
 listProto.indicesOfNearest = function (dim, value, maxDistance) {
     var storage = this._storage;
@@ -1162,6 +1163,8 @@ listProto.indicesOfNearest = function (dim, value, maxDistance) {
     var minDist = Infinity;
     var minDiff = -1;
     var nearestIndicesLen = 0;
+
+    // Check the test case of `test/ut/spec/data/List.js`.
     for (var i = 0, len = this.count(); i < len; i++) {
         var diff = value - this.get(dim, i);
         var dist = Math.abs(diff);
@@ -1170,6 +1173,8 @@ listProto.indicesOfNearest = function (dim, value, maxDistance) {
             // we'd better not push both of them to `nearestIndices`, otherwise it is easy to
             // get more than one item in `nearestIndices` (more specifically, in `tooltip`).
             // So we chose the one that `diff >= 0` in this csae.
+            // But if `this.get(dim, i)` and `this.get(dim, j)` get the same value, both of them
+            // should be push to `nearestIndices`.
             if (dist < minDist
                 || (dist === minDist && diff >= 0 && minDiff < 0)
             ) {
diff --git a/test/ut/spec/data/List.js b/test/ut/spec/data/List.js
index 878cc7f..9d872f2 100644
--- a/test/ut/spec/data/List.js
+++ b/test/ut/spec/data/List.js
@@ -180,4 +180,24 @@ describe('List', function () {
             expect(list.getItemModel(0).option).toEqual([10, 10]);
         });
     });
+
+    describe('Data read', function () {
+        testCase('indicesOfNearest', function (List) {
+            var list = new List(['value']);
+            // ---- index: 0   1   2   3   4   5   6   7
+            list.initData([10, 20, 30, 35, 40, 40, 35, 50]);
+
+            expect(list.indicesOfNearest('value', 24.5)).toEqual([1]);
+            expect(list.indicesOfNearest('value', 25)).toEqual([1]);
+            expect(list.indicesOfNearest('value', 25.5)).toEqual([2]);
+            expect(list.indicesOfNearest('value', 25.5)).toEqual([2]);
+            expect(list.indicesOfNearest('value', 41)).toEqual([4, 5]);
+            expect(list.indicesOfNearest('value', 39)).toEqual([4, 5]);
+            expect(list.indicesOfNearest('value', 41)).toEqual([4, 5]);
+            expect(list.indicesOfNearest('value', 36)).toEqual([3, 6]);
+
+            expect(list.indicesOfNearest('value', 50.6, 0.5)).toEqual([]);
+            expect(list.indicesOfNearest('value', 50.5, 0.5)).toEqual([7]);
+        });
+    });
 });
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@echarts.apache.org
For additional commands, e-mail: commits-help@echarts.apache.org