From e8894afd65abb57d67d4df611b91b1915a3186ae Mon Sep 17 00:00:00 2001 From: manyyuri <327035324@qq.com> Date: Sun, 5 May 2024 17:40:42 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=88=A0=E9=99=A4=E5=A4=9A?= =?UTF-8?q?=E4=BD=99=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .eslintcache | 2 +- src/scale/time.js | 0 src/scale/utils.js | 28 ++++------------------------ 3 files changed, 5 insertions(+), 25 deletions(-) create mode 100644 src/scale/time.js diff --git a/.eslintcache b/.eslintcache index bc43ecd..21d81df 100644 --- a/.eslintcache +++ b/.eslintcache @@ -1 +1 @@ -[{"/Users/junyingli/techLearn/sparrow/.eslintrc.js":"1","/Users/junyingli/techLearn/sparrow/__tests__/index.spec.js":"2","/Users/junyingli/techLearn/sparrow/__tests__/utils.js":"3","/Users/junyingli/techLearn/sparrow/commitlint.config.js":"4","/Users/junyingli/techLearn/sparrow/jest.config.js":"5","/Users/junyingli/techLearn/sparrow/rollup.config.js":"6","/Users/junyingli/techLearn/sparrow/__tests__/renderer/renderer.spec.js":"7","/Users/junyingli/techLearn/sparrow/__tests__/renderer/shape.spec.js":"8","/Users/junyingli/techLearn/sparrow/__tests__/renderer/transform.spec.js":"9","/Users/junyingli/techLearn/sparrow/src/renderer/context.js":"10","/Users/junyingli/techLearn/sparrow/src/renderer/index.js":"11","/Users/junyingli/techLearn/sparrow/src/renderer/renderer.js":"12","/Users/junyingli/techLearn/sparrow/src/renderer/shape.js":"13","/Users/junyingli/techLearn/sparrow/src/renderer/transform.js":"14","/Users/junyingli/techLearn/sparrow/src/renderer/utils.js":"15"},{"size":532,"mtime":1714808881749,"results":"16","hashOfConfig":"17"},{"size":435,"mtime":1714801594557,"results":"18","hashOfConfig":"19"},{"size":414,"mtime":1714808881749,"results":"20","hashOfConfig":"17"},{"size":67,"mtime":1714800551268,"results":"21","hashOfConfig":"19"},{"size":241,"mtime":1714801594557,"results":"22","hashOfConfig":"19"},{"size":586,"mtime":1714801594557,"results":"23","hashOfConfig":"19"},{"size":656,"mtime":1714808881749,"results":"24","hashOfConfig":"17"},{"size":4250,"mtime":1714808881749,"results":"25","hashOfConfig":"17"},{"size":843,"mtime":1714808881749,"results":"26","hashOfConfig":"17"},{"size":533,"mtime":1714808881749,"results":"27","hashOfConfig":"17"},{"size":45,"mtime":1714808881749,"results":"28","hashOfConfig":"17"},{"size":913,"mtime":1714808881749,"results":"29","hashOfConfig":"17"},{"size":2800,"mtime":1714808881749,"results":"30","hashOfConfig":"17"},{"size":825,"mtime":1714808881750,"results":"31","hashOfConfig":"17"},{"size":1166,"mtime":1714808881750,"results":"32","hashOfConfig":"17"},{"filePath":"33","messages":"34","suppressedMessages":"35","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"1l8n0xm",{"filePath":"36","messages":"37","suppressedMessages":"38","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"1hkiy78",{"filePath":"39","messages":"40","suppressedMessages":"41","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"42","messages":"43","suppressedMessages":"44","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"45","messages":"46","suppressedMessages":"47","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"48","messages":"49","suppressedMessages":"50","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"51","messages":"52","suppressedMessages":"53","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"54","messages":"55","suppressedMessages":"56","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"57","messages":"58","suppressedMessages":"59","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"60","messages":"61","suppressedMessages":"62","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"63","messages":"64","suppressedMessages":"65","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"66","messages":"67","suppressedMessages":"68","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"69","messages":"70","suppressedMessages":"71","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"72","messages":"73","suppressedMessages":"74","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"75","messages":"76","suppressedMessages":"77","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"/Users/junyingli/techLearn/sparrow/.eslintrc.js",[],[],"/Users/junyingli/techLearn/sparrow/__tests__/index.spec.js",[],[],"/Users/junyingli/techLearn/sparrow/__tests__/utils.js",[],[],"/Users/junyingli/techLearn/sparrow/commitlint.config.js",[],[],"/Users/junyingli/techLearn/sparrow/jest.config.js",[],[],"/Users/junyingli/techLearn/sparrow/rollup.config.js",[],[],"/Users/junyingli/techLearn/sparrow/__tests__/renderer/renderer.spec.js",[],[],"/Users/junyingli/techLearn/sparrow/__tests__/renderer/shape.spec.js",[],[],"/Users/junyingli/techLearn/sparrow/__tests__/renderer/transform.spec.js",[],[],"/Users/junyingli/techLearn/sparrow/src/renderer/context.js",[],[],"/Users/junyingli/techLearn/sparrow/src/renderer/index.js",[],[],"/Users/junyingli/techLearn/sparrow/src/renderer/renderer.js",[],[],"/Users/junyingli/techLearn/sparrow/src/renderer/shape.js",[],[],"/Users/junyingli/techLearn/sparrow/src/renderer/transform.js",[],[],"/Users/junyingli/techLearn/sparrow/src/renderer/utils.js",[],[]] \ No newline at end of file +[{"/Users/junyingli/techLearn/sparrow/.eslintrc.js":"1","/Users/junyingli/techLearn/sparrow/__tests__/index.spec.js":"2","/Users/junyingli/techLearn/sparrow/__tests__/utils.js":"3","/Users/junyingli/techLearn/sparrow/commitlint.config.js":"4","/Users/junyingli/techLearn/sparrow/jest.config.js":"5","/Users/junyingli/techLearn/sparrow/rollup.config.js":"6","/Users/junyingli/techLearn/sparrow/__tests__/renderer/renderer.spec.js":"7","/Users/junyingli/techLearn/sparrow/__tests__/renderer/shape.spec.js":"8","/Users/junyingli/techLearn/sparrow/__tests__/renderer/transform.spec.js":"9","/Users/junyingli/techLearn/sparrow/src/renderer/context.js":"10","/Users/junyingli/techLearn/sparrow/src/renderer/index.js":"11","/Users/junyingli/techLearn/sparrow/src/renderer/renderer.js":"12","/Users/junyingli/techLearn/sparrow/src/renderer/shape.js":"13","/Users/junyingli/techLearn/sparrow/src/renderer/transform.js":"14","/Users/junyingli/techLearn/sparrow/src/renderer/utils.js":"15","/Users/junyingli/techLearn/sparrow/src/scale/identity.js":"16","/Users/junyingli/techLearn/sparrow/src/scale/linear.js":"17","/Users/junyingli/techLearn/sparrow/src/scale/utils.js":"18"},{"size":532,"mtime":1714808881749,"results":"19","hashOfConfig":"20"},{"size":435,"mtime":1714801594557,"results":"21","hashOfConfig":"22"},{"size":414,"mtime":1714808881749,"results":"23","hashOfConfig":"20"},{"size":67,"mtime":1714800551268,"results":"24","hashOfConfig":"22"},{"size":241,"mtime":1714801594557,"results":"25","hashOfConfig":"22"},{"size":586,"mtime":1714801594557,"results":"26","hashOfConfig":"22"},{"size":656,"mtime":1714808881749,"results":"27","hashOfConfig":"20"},{"size":4250,"mtime":1714808881749,"results":"28","hashOfConfig":"20"},{"size":843,"mtime":1714808881749,"results":"29","hashOfConfig":"20"},{"size":533,"mtime":1714808881749,"results":"30","hashOfConfig":"20"},{"size":45,"mtime":1714808881749,"results":"31","hashOfConfig":"20"},{"size":913,"mtime":1714808881749,"results":"32","hashOfConfig":"20"},{"size":2800,"mtime":1714808881749,"results":"33","hashOfConfig":"20"},{"size":825,"mtime":1714808881750,"results":"34","hashOfConfig":"20"},{"size":1166,"mtime":1714808881750,"results":"35","hashOfConfig":"20"},{"size":56,"mtime":1714809449088,"results":"36","hashOfConfig":"20"},{"size":647,"mtime":1714810093006},{"size":2345,"mtime":1714810071852,"results":"37","hashOfConfig":"20"},{"filePath":"38","messages":"39","suppressedMessages":"40","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"1l8n0xm",{"filePath":"41","messages":"42","suppressedMessages":"43","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"1hkiy78",{"filePath":"44","messages":"45","suppressedMessages":"46","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"47","messages":"48","suppressedMessages":"49","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"50","messages":"51","suppressedMessages":"52","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"53","messages":"54","suppressedMessages":"55","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"56","messages":"57","suppressedMessages":"58","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"59","messages":"60","suppressedMessages":"61","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"62","messages":"63","suppressedMessages":"64","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"65","messages":"66","suppressedMessages":"67","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"68","messages":"69","suppressedMessages":"70","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"71","messages":"72","suppressedMessages":"73","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"74","messages":"75","suppressedMessages":"76","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"77","messages":"78","suppressedMessages":"79","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"80","messages":"81","suppressedMessages":"82","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"83","messages":"84","suppressedMessages":"85","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"86","messages":"87","suppressedMessages":"88","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"/Users/junyingli/techLearn/sparrow/.eslintrc.js",[],[],"/Users/junyingli/techLearn/sparrow/__tests__/index.spec.js",[],[],"/Users/junyingli/techLearn/sparrow/__tests__/utils.js",[],[],"/Users/junyingli/techLearn/sparrow/commitlint.config.js",[],[],"/Users/junyingli/techLearn/sparrow/jest.config.js",[],[],"/Users/junyingli/techLearn/sparrow/rollup.config.js",[],[],"/Users/junyingli/techLearn/sparrow/__tests__/renderer/renderer.spec.js",[],[],"/Users/junyingli/techLearn/sparrow/__tests__/renderer/shape.spec.js",[],[],"/Users/junyingli/techLearn/sparrow/__tests__/renderer/transform.spec.js",[],[],"/Users/junyingli/techLearn/sparrow/src/renderer/context.js",[],[],"/Users/junyingli/techLearn/sparrow/src/renderer/index.js",[],[],"/Users/junyingli/techLearn/sparrow/src/renderer/renderer.js",[],[],"/Users/junyingli/techLearn/sparrow/src/renderer/shape.js",[],[],"/Users/junyingli/techLearn/sparrow/src/renderer/transform.js",[],[],"/Users/junyingli/techLearn/sparrow/src/renderer/utils.js",[],[],"/Users/junyingli/techLearn/sparrow/src/scale/identity.js",[],[],"/Users/junyingli/techLearn/sparrow/src/scale/utils.js",[],[]] \ No newline at end of file diff --git a/src/scale/time.js b/src/scale/time.js new file mode 100644 index 0000000..e69de29 diff --git a/src/scale/utils.js b/src/scale/utils.js index 964ca9c..41fa046 100644 --- a/src/scale/utils.js +++ b/src/scale/utils.js @@ -2,45 +2,25 @@ export function normalize(value, start, stop) { return (value - start) / (stop - start); } -// step0 是生成指定数量的刻度的间隔 -// step1 是最后生成的刻度的间隔 -// 我们希望 step1 满足两个条件: -// 1. step1 = 10 ^ n * b (其中 b=1,2,5) -// 2. step0 和 step1 的误差尽量的小 export function tickStep(min, max, count) { - const e10 = Math.sqrt(50); // 7.07 - const e5 = Math.sqrt(10); // 3.16 - const e2 = Math.sqrt(2); // 1.41 + const e10 = Math.sqrt(50); + const e5 = Math.sqrt(10); + const e2 = Math.sqrt(2); - // 获得目标间隔 step0,设 step0 = 10 ^ m const step0 = Math.abs(max - min) / Math.max(0, count); - // 获得 step1 的初始值 = 10 ^ n < step0,其中 n 为满足条件的最大整数 let step1 = 10 ** Math.floor(Math.log(step0) / Math.LN10); - // 计算 step1 和 step0 的误差,error = 10 ^ m / 10 ^ n = 10 ^ (m - n) const error = step0 / step1; - // 根据当前的误差改变 step1 的值,从而减少误差 - // 1. 当 m - n >= 0.85 = log(e10) 的时候,step1 * 10 - // 可以减少log(10) = 1 的误差 if (error >= e10) step1 *= 10; - // 2. 当 0.85 > m - n >= 0.5 = log(e5) 的时候,step1 * 5 - // 可以减少 log(5) = 0.7 的误差 else if (error >= e5) step1 *= 5; - // 3. 当 0.5 > m - n >= 0.15 = log(e2) 的时候,step1 * 2 - // 那么可以减少 log(2) = 0.3 的误差 else if (error >= e2) step1 *= 2; - // 4. 当 0.15 > m - n > 0 的时候,step1 * 1 return step1; } export function ticks(min, max, count) { const step = tickStep(min, max, count); - // 让 start 和 stop 都是 step 的整数倍 - // 这样生成的 ticks 都是 step 的整数倍 - // 可以让可读性更强 const start = Math.ceil(min / step); const stop = Math.floor(max / step); const n = Math.ceil(stop - start + 1); - // n 不一定等于 count,所以生成的 ticks 的数量可能和指定的不一样 const values = new Array(n); for (let i = 0; i < n; i += 1) { values[i] = round((start + i) * step); @@ -48,7 +28,7 @@ export function ticks(min, max, count) { return values; } -// 简单解决 js 的精读问题:0.1 + 0.2 !== 0.3 +// 简单解决 js 的精度问题:0.1 + 0.2 !== 0.3 export function round(n) { return Math.round(n * 1e12) / 1e12; }