Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@antv/scale

Package Overview
Dependencies
Maintainers
36
Versions
71
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@antv/scale - npm Package Compare versions

Comparing version 0.3.9 to 0.3.10

4

esm/base.d.ts

@@ -55,4 +55,4 @@ import { ScaleConfig, Tick } from './types';

protected calculateTicks(): any[];
protected rangeMin(): any;
protected rangeMax(): any;
protected rangeMin(): number;
protected rangeMax(): number;
/** 定义域转 0~1 */

@@ -59,0 +59,0 @@ protected calcPercent(value: any, min: number, max: number): number;

@@ -1,2 +0,2 @@

import { assign, head, isEmpty, isFunction, isNil, isNumber, isObject, isString, last, map } from '@antv/util';
import { assign, isEmpty, isFunction, isNil, isNumber, isObject, isString, map } from '@antv/util';
import { getTickMethod } from './tick-method/register';

@@ -101,7 +101,7 @@ var Scale = /** @class */ (function () {

Scale.prototype.rangeMin = function () {
return head(this.range);
return this.range[0];
};
// range 的最大值
Scale.prototype.rangeMax = function () {
return last(this.range);
return this.range[1];
};

@@ -108,0 +108,0 @@ /** 定义域转 0~1 */

@@ -6,4 +6,4 @@ export declare const DEFAULT_Q: number[];

* https://www.yuque.com/preview/yuque/0/2019/pdf/185317/1546999150858-45c3b9c2-4e86-4223-bf1a-8a732e8195ed.pdf
* @param dmin 最小值
* @param dmax 最大值
* @param dMin 最小值
* @param dMax 最大值
* @param m tick个数

@@ -14,3 +14,3 @@ * @param onlyLoose 是否允许扩展min、max,不绝对强制,例如[3, 97]

*/
export default function extended(dmin: number, dmax: number, m?: number, onlyLoose?: boolean, Q?: number[], w?: [number, number, number, number]): {
export default function extended(dMin: number, dMax: number, m?: number, onlyLoose?: boolean, Q?: number[], w?: [number, number, number, number]): {
min: number;

@@ -17,0 +17,0 @@ max: number;

@@ -1,6 +0,6 @@

import { head, indexOf, last, map, size } from '@antv/util';
import { head, indexOf, size, last } from '@antv/util';
import { precisionAdd } from './math';
export var DEFAULT_Q = [1, 5, 2, 2.5, 4, 3];
export var ALL_Q = [1, 5, 2, 2.5, 4, 3, 1.5, 7, 6, 8, 9];
var eps = Number.EPSILON * 100;
// https://stackoverflow.com/questions/4467539/javascript-modulo-gives-a-negative-result-for-negative-numbers
function mod(n, m) {

@@ -25,5 +25,5 @@ return ((n % m) + m) % m;

}
function density(k, m, dmin, dmax, lmin, lmax) {
var r = (k - 1) / (lmax - lmin);
var rt = (m - 1) / (Math.max(lmax, dmax) - Math.min(dmin, lmin));
function density(k, m, dMin, dMax, lMin, lMax) {
var r = (k - 1) / (lMax - lMin);
var rt = (m - 1) / (Math.max(lMax, dMax) - Math.min(dMin, lMin));
return 2 - Math.max(r / rt, rt / r);

@@ -37,11 +37,11 @@ }

}
function coverage(dmin, dmax, lmin, lmax) {
var range = dmax - dmin;
return 1 - (0.5 * (Math.pow(dmax - lmax, 2) + Math.pow(dmin - lmin, 2))) / Math.pow(0.1 * range, 2);
function coverage(dMin, dMax, lMin, lMax) {
var range = dMax - dMin;
return 1 - (0.5 * (Math.pow((dMax - lMax), 2) + Math.pow((dMin - lMin), 2))) / Math.pow((0.1 * range), 2);
}
function coverageMax(dmin, dmax, span) {
var range = dmax - dmin;
function coverageMax(dMin, dMax, span) {
var range = dMax - dMin;
if (span > range) {
var half = (span - range) / 2;
return 1 - Math.pow(half, 2) / Math.pow(0.1 * range, 2);
return 1 - Math.pow(half, 2) / Math.pow((0.1 * range), 2);
}

@@ -56,4 +56,4 @@ return 1;

* https://www.yuque.com/preview/yuque/0/2019/pdf/185317/1546999150858-45c3b9c2-4e86-4223-bf1a-8a732e8195ed.pdf
* @param dmin 最小值
* @param dmax 最大值
* @param dMin 最小值
* @param dMax 最大值
* @param m tick个数

@@ -64,3 +64,3 @@ * @param onlyLoose 是否允许扩展min、max,不绝对强制,例如[3, 97]

*/
export default function extended(dmin, dmax, m, onlyLoose, Q, w) {
export default function extended(dMin, dMax, m, onlyLoose, Q, w) {
if (m === void 0) { m = 5; }

@@ -70,4 +70,4 @@ if (onlyLoose === void 0) { onlyLoose = true; }

if (w === void 0) { w = [0.25, 0.2, 0.5, 0.05]; }
// 异常数据情况下,直接返回,防止 oom
if (typeof dmin !== 'number' || typeof dmax !== 'number' || !m) {
// nan 也会导致异常
if (Number.isNaN(dMin) || Number.isNaN(dMax) || typeof dMin !== 'number' || typeof dMax !== 'number' || !m) {
return {

@@ -80,7 +80,7 @@ min: 0,

// js 极大值极小值问题,差值小于 1e-15 会导致计算出错
if (dmax - dmin < 1e-15 || m === 1) {
if (dMax - dMin < 1e-15 || m === 1) {
return {
min: dmin,
max: dmax,
ticks: [dmin],
min: dMin,
max: dMax,
ticks: [dMin],
};

@@ -96,8 +96,5 @@ }

while (j < Infinity) {
for (var _i = 0, Q_1 = Q; _i < Q_1.length; _i++) {
var q = Q_1[_i];
for (var i_1 = 0; i_1 < Q.length; i_1 += 1) {
var q = Q[i_1];
var sm = simplicityMax(q, Q, j);
if (Number.isNaN(sm)) {
throw new Error('NaN');
}
if (w[0] * sm + w[1] + w[2] + w[3] < best.score) {

@@ -113,52 +110,54 @@ j = Infinity;

}
var delta = (dmax - dmin) / (k + 1) / j / q;
var delta = (dMax - dMin) / (k + 1) / j / q;
var z = Math.ceil(Math.log10(delta));
while (z < Infinity) {
var step = j * q * Math.pow(10, z);
var cm = coverageMax(dmin, dmax, step * (k - 1));
var cm = coverageMax(dMin, dMax, step * (k - 1));
if (w[0] * sm + w[1] * cm + w[2] * dm + w[3] < best.score) {
break;
}
var minStart = Math.floor(dmax / step) * j - (k - 1) * j;
var maxStart = Math.ceil(dmin / step) * j;
var minStart = Math.floor(dMax / step) * j - (k - 1) * j;
var maxStart = Math.ceil(dMin / step) * j;
if (minStart > maxStart) {
z = z + 1;
z += 1;
// eslint-disable-next-line no-continue
continue;
}
for (var start = minStart; start <= maxStart; start = start + 1) {
var lmin = start * (step / j);
var lmax = lmin + step * (k - 1);
var lstep = step;
var s = simplicity(q, Q, j, lmin, lmax, lstep);
var c = coverage(dmin, dmax, lmin, lmax);
var g = density(k, m, dmin, dmax, lmin, lmax);
for (var start = minStart; start <= maxStart; start += 1) {
var lMin = start * (step / j);
var lMax = lMin + step * (k - 1);
var lStep = step;
var s = simplicity(q, Q, j, lMin, lMax, lStep);
var c = coverage(dMin, dMax, lMin, lMax);
var g = density(k, m, dMin, dMax, lMin, lMax);
var l = legibility();
var score = w[0] * s + w[1] * c + w[2] * g + w[3] * l;
if (score > best.score && (!onlyLoose || (lmin <= dmin && lmax >= dmax))) {
best.lmin = lmin;
best.lmax = lmax;
best.lstep = lstep;
if (score > best.score && (!onlyLoose || (lMin <= dMin && lMax >= dMax))) {
best.lmin = lMin;
best.lmax = lMax;
best.lstep = lStep;
best.score = score;
}
}
z = z + 1;
z += 1;
}
k = k + 1;
k += 1;
}
}
j = j + 1;
j += 1;
}
var i = 0;
// 步长为浮点数时处理精度
var toFixed = Number.isInteger(best.lstep) ? 0 : Math.ceil(Math.abs(Math.log10(best.lstep)));
var range = [];
for (var tick = best.lmin; tick <= best.lmax; tick += best.lstep) {
range.push(tick);
var range = new Array(Math.floor((best.lmax - best.lmin) / best.lstep));
for (var tick = best.lmin; tick <= best.lmax; tick = precisionAdd(tick, best.lstep)) {
range[i] = tick;
i += 1;
}
var ticks = toFixed ? map(range, function (x) { return Number.parseFloat(x.toFixed(toFixed)); }) : range;
return {
min: Math.min(dmin, head(ticks)),
max: Math.max(dmax, last(ticks)),
ticks: ticks,
min: Math.min(dMin, head(range)),
max: Math.max(dMax, last(range)),
ticks: range,
};
}
;
//# sourceMappingURL=extended.js.map
export declare function calBase(a: number, b: number): any;
export declare function log(a: number, b: number): number;
export declare function getLogPositiveMin(values: any, base: any, max?: number): number;
/**
* 高精度加法,解决 0.1 + 0.2 !== 0.3 的经典问题
*
* @param num1 加数
* @param num2 被加数
* @return {number} 返回值
*/
export declare function precisionAdd(num1: number, num2: number): number;

@@ -39,2 +39,21 @@ import { each, isNil } from '@antv/util';

}
function digitLength(num) {
// Get digit length of e
var eSplit = num.toString().split(/[eE]/);
var len = (eSplit[0].split('.')[1] || '').length - +(eSplit[1] || 0);
return len > 0 ? len : 0;
}
/**
* 高精度加法,解决 0.1 + 0.2 !== 0.3 的经典问题
*
* @param num1 加数
* @param num2 被加数
* @return {number} 返回值
*/
export function precisionAdd(num1, num2) {
var num1Digits = digitLength(num1);
var num2Digits = digitLength(num2);
var baseNum = Math.pow(10, Math.max(num1Digits, num2Digits));
return (num1 * baseNum + num2 * baseNum) / baseNum;
}
//# sourceMappingURL=math.js.map

@@ -55,4 +55,4 @@ import { ScaleConfig, Tick } from './types';

protected calculateTicks(): any[];
protected rangeMin(): any;
protected rangeMax(): any;
protected rangeMin(): number;
protected rangeMax(): number;
/** 定义域转 0~1 */

@@ -59,0 +59,0 @@ protected calcPercent(value: any, min: number, max: number): number;

@@ -103,7 +103,7 @@ "use strict";

Scale.prototype.rangeMin = function () {
return util_1.head(this.range);
return this.range[0];
};
// range 的最大值
Scale.prototype.rangeMax = function () {
return util_1.last(this.range);
return this.range[1];
};

@@ -110,0 +110,0 @@ /** 定义域转 0~1 */

@@ -6,4 +6,4 @@ export declare const DEFAULT_Q: number[];

* https://www.yuque.com/preview/yuque/0/2019/pdf/185317/1546999150858-45c3b9c2-4e86-4223-bf1a-8a732e8195ed.pdf
* @param dmin 最小值
* @param dmax 最大值
* @param dMin 最小值
* @param dMax 最大值
* @param m tick个数

@@ -14,3 +14,3 @@ * @param onlyLoose 是否允许扩展min、max,不绝对强制,例如[3, 97]

*/
export default function extended(dmin: number, dmax: number, m?: number, onlyLoose?: boolean, Q?: number[], w?: [number, number, number, number]): {
export default function extended(dMin: number, dMax: number, m?: number, onlyLoose?: boolean, Q?: number[], w?: [number, number, number, number]): {
min: number;

@@ -17,0 +17,0 @@ max: number;

@@ -5,6 +5,6 @@ "use strict";

var util_1 = require("@antv/util");
var math_1 = require("./math");
exports.DEFAULT_Q = [1, 5, 2, 2.5, 4, 3];
exports.ALL_Q = [1, 5, 2, 2.5, 4, 3, 1.5, 7, 6, 8, 9];
var eps = Number.EPSILON * 100;
// https://stackoverflow.com/questions/4467539/javascript-modulo-gives-a-negative-result-for-negative-numbers
function mod(n, m) {

@@ -29,5 +29,5 @@ return ((n % m) + m) % m;

}
function density(k, m, dmin, dmax, lmin, lmax) {
var r = (k - 1) / (lmax - lmin);
var rt = (m - 1) / (Math.max(lmax, dmax) - Math.min(dmin, lmin));
function density(k, m, dMin, dMax, lMin, lMax) {
var r = (k - 1) / (lMax - lMin);
var rt = (m - 1) / (Math.max(lMax, dMax) - Math.min(dMin, lMin));
return 2 - Math.max(r / rt, rt / r);

@@ -41,11 +41,11 @@ }

}
function coverage(dmin, dmax, lmin, lmax) {
var range = dmax - dmin;
return 1 - (0.5 * (Math.pow(dmax - lmax, 2) + Math.pow(dmin - lmin, 2))) / Math.pow(0.1 * range, 2);
function coverage(dMin, dMax, lMin, lMax) {
var range = dMax - dMin;
return 1 - (0.5 * (Math.pow((dMax - lMax), 2) + Math.pow((dMin - lMin), 2))) / Math.pow((0.1 * range), 2);
}
function coverageMax(dmin, dmax, span) {
var range = dmax - dmin;
function coverageMax(dMin, dMax, span) {
var range = dMax - dMin;
if (span > range) {
var half = (span - range) / 2;
return 1 - Math.pow(half, 2) / Math.pow(0.1 * range, 2);
return 1 - Math.pow(half, 2) / Math.pow((0.1 * range), 2);
}

@@ -60,4 +60,4 @@ return 1;

* https://www.yuque.com/preview/yuque/0/2019/pdf/185317/1546999150858-45c3b9c2-4e86-4223-bf1a-8a732e8195ed.pdf
* @param dmin 最小值
* @param dmax 最大值
* @param dMin 最小值
* @param dMax 最大值
* @param m tick个数

@@ -68,3 +68,3 @@ * @param onlyLoose 是否允许扩展min、max,不绝对强制,例如[3, 97]

*/
function extended(dmin, dmax, m, onlyLoose, Q, w) {
function extended(dMin, dMax, m, onlyLoose, Q, w) {
if (m === void 0) { m = 5; }

@@ -74,4 +74,4 @@ if (onlyLoose === void 0) { onlyLoose = true; }

if (w === void 0) { w = [0.25, 0.2, 0.5, 0.05]; }
// 异常数据情况下,直接返回,防止 oom
if (typeof dmin !== 'number' || typeof dmax !== 'number' || !m) {
// nan 也会导致异常
if (Number.isNaN(dMin) || Number.isNaN(dMax) || typeof dMin !== 'number' || typeof dMax !== 'number' || !m) {
return {

@@ -84,7 +84,7 @@ min: 0,

// js 极大值极小值问题,差值小于 1e-15 会导致计算出错
if (dmax - dmin < 1e-15 || m === 1) {
if (dMax - dMin < 1e-15 || m === 1) {
return {
min: dmin,
max: dmax,
ticks: [dmin],
min: dMin,
max: dMax,
ticks: [dMin],
};

@@ -100,8 +100,5 @@ }

while (j < Infinity) {
for (var _i = 0, Q_1 = Q; _i < Q_1.length; _i++) {
var q = Q_1[_i];
for (var i_1 = 0; i_1 < Q.length; i_1 += 1) {
var q = Q[i_1];
var sm = simplicityMax(q, Q, j);
if (Number.isNaN(sm)) {
throw new Error('NaN');
}
if (w[0] * sm + w[1] + w[2] + w[3] < best.score) {

@@ -117,53 +114,55 @@ j = Infinity;

}
var delta = (dmax - dmin) / (k + 1) / j / q;
var delta = (dMax - dMin) / (k + 1) / j / q;
var z = Math.ceil(Math.log10(delta));
while (z < Infinity) {
var step = j * q * Math.pow(10, z);
var cm = coverageMax(dmin, dmax, step * (k - 1));
var cm = coverageMax(dMin, dMax, step * (k - 1));
if (w[0] * sm + w[1] * cm + w[2] * dm + w[3] < best.score) {
break;
}
var minStart = Math.floor(dmax / step) * j - (k - 1) * j;
var maxStart = Math.ceil(dmin / step) * j;
var minStart = Math.floor(dMax / step) * j - (k - 1) * j;
var maxStart = Math.ceil(dMin / step) * j;
if (minStart > maxStart) {
z = z + 1;
z += 1;
// eslint-disable-next-line no-continue
continue;
}
for (var start = minStart; start <= maxStart; start = start + 1) {
var lmin = start * (step / j);
var lmax = lmin + step * (k - 1);
var lstep = step;
var s = simplicity(q, Q, j, lmin, lmax, lstep);
var c = coverage(dmin, dmax, lmin, lmax);
var g = density(k, m, dmin, dmax, lmin, lmax);
for (var start = minStart; start <= maxStart; start += 1) {
var lMin = start * (step / j);
var lMax = lMin + step * (k - 1);
var lStep = step;
var s = simplicity(q, Q, j, lMin, lMax, lStep);
var c = coverage(dMin, dMax, lMin, lMax);
var g = density(k, m, dMin, dMax, lMin, lMax);
var l = legibility();
var score = w[0] * s + w[1] * c + w[2] * g + w[3] * l;
if (score > best.score && (!onlyLoose || (lmin <= dmin && lmax >= dmax))) {
best.lmin = lmin;
best.lmax = lmax;
best.lstep = lstep;
if (score > best.score && (!onlyLoose || (lMin <= dMin && lMax >= dMax))) {
best.lmin = lMin;
best.lmax = lMax;
best.lstep = lStep;
best.score = score;
}
}
z = z + 1;
z += 1;
}
k = k + 1;
k += 1;
}
}
j = j + 1;
j += 1;
}
var i = 0;
// 步长为浮点数时处理精度
var toFixed = Number.isInteger(best.lstep) ? 0 : Math.ceil(Math.abs(Math.log10(best.lstep)));
var range = [];
for (var tick = best.lmin; tick <= best.lmax; tick += best.lstep) {
range.push(tick);
var range = new Array(Math.floor((best.lmax - best.lmin) / best.lstep));
for (var tick = best.lmin; tick <= best.lmax; tick = math_1.precisionAdd(tick, best.lstep)) {
range[i] = tick;
i += 1;
}
var ticks = toFixed ? util_1.map(range, function (x) { return Number.parseFloat(x.toFixed(toFixed)); }) : range;
return {
min: Math.min(dmin, util_1.head(ticks)),
max: Math.max(dmax, util_1.last(ticks)),
ticks: ticks,
min: Math.min(dMin, util_1.head(range)),
max: Math.max(dMax, util_1.last(range)),
ticks: range,
};
}
exports.default = extended;
;
//# sourceMappingURL=extended.js.map
export declare function calBase(a: number, b: number): any;
export declare function log(a: number, b: number): number;
export declare function getLogPositiveMin(values: any, base: any, max?: number): number;
/**
* 高精度加法,解决 0.1 + 0.2 !== 0.3 的经典问题
*
* @param num1 加数
* @param num2 被加数
* @return {number} 返回值
*/
export declare function precisionAdd(num1: number, num2: number): number;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLogPositiveMin = exports.log = exports.calBase = void 0;
exports.precisionAdd = exports.getLogPositiveMin = exports.log = exports.calBase = void 0;
var util_1 = require("@antv/util");

@@ -45,2 +45,22 @@ // 求以a为次幂,结果为b的基数,如 x^^a = b;求x

exports.getLogPositiveMin = getLogPositiveMin;
function digitLength(num) {
// Get digit length of e
var eSplit = num.toString().split(/[eE]/);
var len = (eSplit[0].split('.')[1] || '').length - +(eSplit[1] || 0);
return len > 0 ? len : 0;
}
/**
* 高精度加法,解决 0.1 + 0.2 !== 0.3 的经典问题
*
* @param num1 加数
* @param num2 被加数
* @return {number} 返回值
*/
function precisionAdd(num1, num2) {
var num1Digits = digitLength(num1);
var num2Digits = digitLength(num2);
var baseNum = Math.pow(10, Math.max(num1Digits, num2Digits));
return (num1 * baseNum + num2 * baseNum) / baseNum;
}
exports.precisionAdd = precisionAdd;
//# sourceMappingURL=math.js.map
{
"name": "@antv/scale",
"version": "0.3.9",
"version": "0.3.10",
"description": "The scale module for G2",

@@ -5,0 +5,0 @@ "author": "https://github.com/orgs/antvis/people",

@@ -1,2 +0,2 @@

import { assign, head, isEmpty, isFunction, isNil, isNumber, isObject, isString, last, map } from '@antv/util';
import { assign, isEmpty, isFunction, isNil, isNumber, isObject, isString, map } from '@antv/util';
import { getTickMethod } from './tick-method/register';

@@ -130,3 +130,3 @@ import { ScaleConfig, Tick } from './types';

protected rangeMin() {
return head(this.range);
return this.range[0];
}

@@ -136,3 +136,3 @@

protected rangeMax() {
return last(this.range);
return this.range[1];
}

@@ -139,0 +139,0 @@

@@ -1,2 +0,3 @@

import { head, indexOf, last, map, size } from '@antv/util';
import { head, indexOf, size, last } from '@antv/util';
import { precisionAdd } from './math';

@@ -9,3 +10,2 @@ export const DEFAULT_Q = [1, 5, 2, 2.5, 4, 3];

// https://stackoverflow.com/questions/4467539/javascript-modulo-gives-a-negative-result-for-negative-numbers
function mod(n: number, m: number) {

@@ -33,5 +33,5 @@ return ((n % m) + m) % m;

function density(k: number, m: number, dmin: number, dmax: number, lmin: number, lmax: number) {
const r = (k - 1) / (lmax - lmin);
const rt = (m - 1) / (Math.max(lmax, dmax) - Math.min(dmin, lmin));
function density(k: number, m: number, dMin: number, dMax: number, lMin: number, lMax: number) {
const r = (k - 1) / (lMax - lMin);
const rt = (m - 1) / (Math.max(lMax, dMax) - Math.min(dMin, lMin));
return 2 - Math.max(r / rt, rt / r);

@@ -47,12 +47,12 @@ }

function coverage(dmin: number, dmax: number, lmin: number, lmax: number) {
const range = dmax - dmin;
return 1 - (0.5 * (Math.pow(dmax - lmax, 2) + Math.pow(dmin - lmin, 2))) / Math.pow(0.1 * range, 2);
function coverage(dMin: number, dMax: number, lMin: number, lMax: number) {
const range = dMax - dMin;
return 1 - (0.5 * ((dMax - lMax) ** 2 + (dMin - lMin) ** 2)) / (0.1 * range) ** 2;
}
function coverageMax(dmin: number, dmax: number, span: number) {
const range = dmax - dmin;
function coverageMax(dMin: number, dMax: number, span: number) {
const range = dMax - dMin;
if (span > range) {
const half = (span - range) / 2;
return 1 - Math.pow(half, 2) / Math.pow(0.1 * range, 2);
return 1 - half ** 2 / (0.1 * range) ** 2;
}

@@ -69,4 +69,4 @@ return 1;

* https://www.yuque.com/preview/yuque/0/2019/pdf/185317/1546999150858-45c3b9c2-4e86-4223-bf1a-8a732e8195ed.pdf
* @param dmin 最小值
* @param dmax 最大值
* @param dMin 最小值
* @param dMax 最大值
* @param m tick个数

@@ -78,11 +78,11 @@ * @param onlyLoose 是否允许扩展min、max,不绝对强制,例如[3, 97]

export default function extended(
dmin: number,
dmax: number,
dMin: number,
dMax: number,
m: number = 5,
onlyLoose: boolean = true,
Q: number[] = DEFAULT_Q,
w: [number, number, number, number] = [0.25, 0.2, 0.5, 0.05]
w: [number, number, number, number] = [0.25, 0.2, 0.5, 0.05],
): { min: number; max: number; ticks: number[] } {
// 异常数据情况下,直接返回,防止 oom
if (typeof dmin !== 'number' || typeof dmax !== 'number' || !m) {
// nan 也会导致异常
if (Number.isNaN(dMin) || Number.isNaN(dMax) || typeof dMin !== 'number' || typeof dMax !== 'number' || !m) {
return {

@@ -94,9 +94,9 @@ min: 0,

}
// js 极大值极小值问题,差值小于 1e-15 会导致计算出错
if (dmax - dmin < 1e-15 || m === 1) {
if (dMax - dMin < 1e-15 || m === 1) {
return {
min: dmin,
max: dmax,
ticks: [dmin],
min: dMin,
max: dMax,
ticks: [dMin],
};

@@ -111,9 +111,8 @@ }

};
let j = 1;
while (j < Infinity) {
for (const q of Q) {
for (let i = 0; i < Q.length; i += 1) {
const q = Q[i];
const sm = simplicityMax(q, Q, j);
if (Number.isNaN(sm)) {
throw new Error('NaN');
}
if (w[0] * sm + w[1] + w[2] + w[3] < best.score) {

@@ -130,8 +129,8 @@ j = Infinity;

const delta = (dmax - dmin) / (k + 1) / j / q;
const delta = (dMax - dMin) / (k + 1) / j / q;
let z = Math.ceil(Math.log10(delta));
while (z < Infinity) {
const step = j * q * Math.pow(10, z);
const cm = coverageMax(dmin, dmax, step * (k - 1));
const step = j * q * 10 ** z;
const cm = coverageMax(dMin, dMax, step * (k - 1));

@@ -142,47 +141,50 @@ if (w[0] * sm + w[1] * cm + w[2] * dm + w[3] < best.score) {

const minStart = Math.floor(dmax / step) * j - (k - 1) * j;
const maxStart = Math.ceil(dmin / step) * j;
const minStart = Math.floor(dMax / step) * j - (k - 1) * j;
const maxStart = Math.ceil(dMin / step) * j;
if (minStart > maxStart) {
z = z + 1;
z += 1;
// eslint-disable-next-line no-continue
continue;
}
for (let start = minStart; start <= maxStart; start = start + 1) {
const lmin = start * (step / j);
const lmax = lmin + step * (k - 1);
const lstep = step;
for (let start = minStart; start <= maxStart; start += 1) {
const lMin = start * (step / j);
const lMax = lMin + step * (k - 1);
const lStep = step;
const s = simplicity(q, Q, j, lmin, lmax, lstep);
const c = coverage(dmin, dmax, lmin, lmax);
const g = density(k, m, dmin, dmax, lmin, lmax);
const s = simplicity(q, Q, j, lMin, lMax, lStep);
const c = coverage(dMin, dMax, lMin, lMax);
const g = density(k, m, dMin, dMax, lMin, lMax);
const l = legibility();
const score = w[0] * s + w[1] * c + w[2] * g + w[3] * l;
if (score > best.score && (!onlyLoose || (lmin <= dmin && lmax >= dmax))) {
best.lmin = lmin;
best.lmax = lmax;
best.lstep = lstep;
if (score > best.score && (!onlyLoose || (lMin <= dMin && lMax >= dMax))) {
best.lmin = lMin;
best.lmax = lMax;
best.lstep = lStep;
best.score = score;
}
}
z = z + 1;
z += 1;
}
k = k + 1;
k += 1;
}
}
j = j + 1;
j += 1;
}
let i = 0;
// 步长为浮点数时处理精度
const toFixed = Number.isInteger(best.lstep) ? 0 : Math.ceil(Math.abs(Math.log10(best.lstep)));
const range = [];
for (let tick = best.lmin; tick <= best.lmax; tick += best.lstep) {
range.push(tick);
const range = new Array(Math.floor((best.lmax - best.lmin) / best.lstep));
for (let tick = best.lmin; tick <= best.lmax; tick = precisionAdd(tick, best.lstep)) {
range[i] = tick;
i += 1;
}
const ticks = toFixed ? map(range, (x: number) => Number.parseFloat(x.toFixed(toFixed))) : range;
return {
min: Math.min(dmin, head(ticks)),
max: Math.max(dmax, last(ticks)),
ticks,
min: Math.min(dMin, head(range)),
max: Math.max(dMax, last(range)),
ticks: range,
};
}
};
import { each, isNil } from '@antv/util';
// 求以a为次幂,结果为b的基数,如 x^^a = b;求x

@@ -40,1 +41,22 @@ // 虽然数学上 b 不支持负数,但是这里需要支持 负数

}
function digitLength(num: number) {
// Get digit length of e
const eSplit = num.toString().split(/[eE]/);
const len = (eSplit[0].split('.')[1] || '').length - +(eSplit[1] || 0);
return len > 0 ? len : 0;
}
/**
* 高精度加法,解决 0.1 + 0.2 !== 0.3 的经典问题
*
* @param num1 加数
* @param num2 被加数
* @return {number} 返回值
*/
export function precisionAdd(num1: number, num2: number) {
const num1Digits = digitLength(num1);
const num2Digits = digitLength(num2);
const baseNum = 10 ** Math.max(num1Digits, num2Digits);
return (num1 * baseNum + num2 * baseNum) / baseNum;
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc