@easyv/charts 1.10.23 → 1.10.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/components/Axis.js +68 -79
- package/lib/components/PieChart.js +2 -2
- package/package.json +2 -2
- package/src/components/Axis.tsx +83 -108
- package/src/components/PieChart.js +1629 -1629
package/lib/components/Axis.js
CHANGED
|
@@ -7,9 +7,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
7
7
|
});
|
|
8
8
|
exports["default"] = exports.calculateTextWidth = void 0;
|
|
9
9
|
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
10
|
-
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
11
|
-
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
|
|
12
10
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
+
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
|
|
13
12
|
var _react = _interopRequireWildcard(require("react"));
|
|
14
13
|
var _utils = require("../utils");
|
|
15
14
|
var _context = require("../context");
|
|
@@ -193,20 +192,71 @@ var Unit = function Unit(_ref4) {
|
|
|
193
192
|
textAnchor: textAnchor
|
|
194
193
|
}, dataUnit || text);
|
|
195
194
|
};
|
|
195
|
+
function getLabelFormatterContext(config) {
|
|
196
|
+
var format = config.format,
|
|
197
|
+
showType = config.showType;
|
|
198
|
+
if (format && (0, _typeof2["default"])(format) === "object" && format.type != null) {
|
|
199
|
+
var _format$showType;
|
|
200
|
+
return _objectSpread(_objectSpread({}, config), {}, {
|
|
201
|
+
format: {
|
|
202
|
+
type: format.type,
|
|
203
|
+
showType: (_format$showType = format.showType) !== null && _format$showType !== void 0 ? _format$showType : showType
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
return _objectSpread(_objectSpread({}, config), {}, {
|
|
208
|
+
format: {
|
|
209
|
+
type: format,
|
|
210
|
+
showType: showType
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
function formatAxisLabel(label, config, formatter) {
|
|
215
|
+
return formatter(label, getLabelFormatterContext(config));
|
|
216
|
+
}
|
|
217
|
+
function buildUniformStepIndices(n, step) {
|
|
218
|
+
var indices = [];
|
|
219
|
+
for (var i = 0; i < n; i += step) {
|
|
220
|
+
indices.push(i);
|
|
221
|
+
}
|
|
222
|
+
return indices;
|
|
223
|
+
}
|
|
224
|
+
function satisfiesIndexSpacing(indices, xAt, minSpacing) {
|
|
225
|
+
for (var j = 1; j < indices.length; j++) {
|
|
226
|
+
if (xAt(indices[j]) - xAt(indices[j - 1]) < minSpacing) return false;
|
|
227
|
+
}
|
|
228
|
+
return true;
|
|
229
|
+
}
|
|
230
|
+
function getAdaptiveOrdinalTicks(allTicks, scaler, labelWidth, minGap) {
|
|
231
|
+
var n = allTicks.length;
|
|
232
|
+
if (!n) return [];
|
|
233
|
+
if (n === 1) return [allTicks[0]];
|
|
234
|
+
var minSpacing = labelWidth + minGap;
|
|
235
|
+
var xAt = function xAt(i) {
|
|
236
|
+
return scaler(allTicks[i]);
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
// 仅用相邻刻度像素间距约束,不用 floor(available/step) 限制数量:
|
|
240
|
+
// 极长标签时后者会得到 maxByWidth=1,错误地丢弃所有多刻度方案。
|
|
241
|
+
for (var step = 1; step < n; step++) {
|
|
242
|
+
var indices = buildUniformStepIndices(n, step);
|
|
243
|
+
if (satisfiesIndexSpacing(indices, xAt, minSpacing)) {
|
|
244
|
+
return indices.map(function (i) {
|
|
245
|
+
return allTicks[i];
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
if (n >= 2 && xAt(n - 1) - xAt(0) >= minSpacing) {
|
|
250
|
+
return [allTicks[0], allTicks[n - 1]];
|
|
251
|
+
}
|
|
252
|
+
return [allTicks[0]];
|
|
253
|
+
}
|
|
196
254
|
function maxLabelFT(data, config, formatter, font, isVertical) {
|
|
197
255
|
var max = 0;
|
|
198
256
|
data.length ? data.forEach(function (item) {
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
showType: config.showType
|
|
203
|
-
}
|
|
204
|
-
})), font, isVertical) > max) max = calculateTextWidth(formatter(item, _objectSpread(_objectSpread({}, config), {}, {
|
|
205
|
-
format: {
|
|
206
|
-
type: config.format,
|
|
207
|
-
showType: config.showType
|
|
208
|
-
}
|
|
209
|
-
})), font, isVertical);
|
|
257
|
+
var formatted = formatAxisLabel(item, config, formatter);
|
|
258
|
+
var width = calculateTextWidth(formatted, font, isVertical);
|
|
259
|
+
if (width > max) max = width;
|
|
210
260
|
}) : "";
|
|
211
261
|
return max;
|
|
212
262
|
}
|
|
@@ -245,12 +295,7 @@ var Label = function Label(_ref5) {
|
|
|
245
295
|
textOverflow = _ref5$config$appearan2.textOverflow,
|
|
246
296
|
LabelWidth = _ref5.LabelWidth;
|
|
247
297
|
if (!show) return null;
|
|
248
|
-
var _label =
|
|
249
|
-
format: {
|
|
250
|
-
type: config.format,
|
|
251
|
-
showType: config.showType
|
|
252
|
-
}
|
|
253
|
-
}));
|
|
298
|
+
var _label = formatAxisLabel(label, config, formatter);
|
|
254
299
|
var _getLayout = getLayout(orientation, adaptive ? 0 : rotate),
|
|
255
300
|
transform = _getLayout.transform,
|
|
256
301
|
directionX = _getLayout.directionX,
|
|
@@ -351,68 +396,12 @@ var _default = exports["default"] = /*#__PURE__*/(0, _react.memo)(/*#__PURE__*/(
|
|
|
351
396
|
var LabelWidth = 1;
|
|
352
397
|
if (!label.adaptive && label.appearance) {
|
|
353
398
|
LabelWidth = label.appearance.width;
|
|
354
|
-
} else {
|
|
355
|
-
|
|
356
|
-
LabelWidth = maxLabelFT(allTicks, label, formatter, label.font, isVertical);
|
|
357
|
-
}
|
|
399
|
+
} else if (allTicks.length) {
|
|
400
|
+
LabelWidth = Math.max(maxLabelFT(allTicks, label, formatter, label.font, isVertical), 1);
|
|
358
401
|
}
|
|
359
|
-
var
|
|
360
|
-
var ticks = !label.adaptive ? tickss :
|
|
402
|
+
var LABEL_MIN_GAP = 12;
|
|
403
|
+
var ticks = !label.adaptive ? tickss : getAdaptiveOrdinalTicks(allTicks, scaler, LabelWidth, LABEL_MIN_GAP);
|
|
361
404
|
if (!(on && ticks.length > 0)) return null;
|
|
362
|
-
|
|
363
|
-
//数据抽取逻辑
|
|
364
|
-
function getEvenlySpacedElements(arr, expectCount) {
|
|
365
|
-
var acc = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
366
|
-
if (!arr.length || expectCount <= 0) return [];
|
|
367
|
-
if (expectCount >= arr.length) return (0, _toConsumableArray2["default"])(arr);
|
|
368
|
-
if (expectCount === 1) return [arr[0]];
|
|
369
|
-
if (acc) {
|
|
370
|
-
var totalLength = arr.length;
|
|
371
|
-
var result = [];
|
|
372
|
-
var bestCount = 2;
|
|
373
|
-
for (var k = expectCount; k >= 2; k--) {
|
|
374
|
-
var denominator = k - 1;
|
|
375
|
-
var numerator = totalLength - 1;
|
|
376
|
-
if (denominator > 0 && numerator % denominator === 0) {
|
|
377
|
-
bestCount = k;
|
|
378
|
-
break;
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
var step = (totalLength - 1) / (bestCount - 1);
|
|
382
|
-
for (var i = 0; i < bestCount; i++) {
|
|
383
|
-
var rawIndex = i * step;
|
|
384
|
-
var index = Math.ceil(rawIndex);
|
|
385
|
-
var safeIndex = Math.max(0, Math.min(totalLength - 1, index));
|
|
386
|
-
result.push(arr[safeIndex]);
|
|
387
|
-
}
|
|
388
|
-
if (result.length > 0) result[0] = arr[0];
|
|
389
|
-
if (result.length >= 2) result[result.length - 1] = arr[totalLength - 1];
|
|
390
|
-
return result;
|
|
391
|
-
} else {
|
|
392
|
-
// 重构acc=false逻辑:优先均匀分布,不强制首尾
|
|
393
|
-
var _result = [];
|
|
394
|
-
var arrLen = arr.length;
|
|
395
|
-
if (expectCount === 2) {
|
|
396
|
-
_result.push(arr[0]);
|
|
397
|
-
_result.push(arr[arrLen - 1]);
|
|
398
|
-
return _result;
|
|
399
|
-
}
|
|
400
|
-
var idealStep = (arrLen - 1) / (expectCount - 1);
|
|
401
|
-
var isIdealStepInteger = Math.abs(idealStep - Math.round(idealStep)) < 1e-10;
|
|
402
|
-
if (isIdealStepInteger) {
|
|
403
|
-
for (var _i = 0; _i < expectCount; _i++) {
|
|
404
|
-
var _index = _i * idealStep;
|
|
405
|
-
_result.push(arr[_index]);
|
|
406
|
-
}
|
|
407
|
-
} else {
|
|
408
|
-
var _step = Math.max(1, Math.floor(arrLen / expectCount));
|
|
409
|
-
for (var _i2 = 0; _i2 < arrLen && _result.length < expectCount; _i2 += _step) {
|
|
410
|
-
_result.push(arr[_i2]);
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
return _result;
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
405
|
function drawAxisTickLine() {
|
|
417
406
|
var draw = function draw(ticks, scaler) {
|
|
418
407
|
return ticks.map(function (tick, index) {
|
|
@@ -27,8 +27,8 @@ var _excluded = ["startAngle", "endAngle", "antiClockwise"],
|
|
|
27
27
|
_excluded2 = ["padAngle", "innerRadius", "outerRadius", "cornerRadius", "startAngle", "endAngle"],
|
|
28
28
|
_excluded3 = ["formatter"],
|
|
29
29
|
_excluded4 = ["startAngle", "endAngle"];
|
|
30
|
-
/**
|
|
31
|
-
* 饼环图
|
|
30
|
+
/**
|
|
31
|
+
* 饼环图
|
|
32
32
|
*/
|
|
33
33
|
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
|
|
34
34
|
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
|
package/package.json
CHANGED
package/src/components/Axis.tsx
CHANGED
|
@@ -248,6 +248,74 @@ type LabelType = {
|
|
|
248
248
|
LabelWidth: any;
|
|
249
249
|
};
|
|
250
250
|
|
|
251
|
+
function getLabelFormatterContext(config: any) {
|
|
252
|
+
const { format, showType } = config;
|
|
253
|
+
if (format && typeof format === "object" && format.type != null) {
|
|
254
|
+
return {
|
|
255
|
+
...config,
|
|
256
|
+
format: {
|
|
257
|
+
type: format.type,
|
|
258
|
+
showType: format.showType ?? showType,
|
|
259
|
+
},
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
return {
|
|
263
|
+
...config,
|
|
264
|
+
format: { type: format, showType },
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function formatAxisLabel(label: any, config: any, formatter: any) {
|
|
269
|
+
return formatter(label, getLabelFormatterContext(config));
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function buildUniformStepIndices(n: number, step: number): number[] {
|
|
273
|
+
const indices: number[] = [];
|
|
274
|
+
for (let i = 0; i < n; i += step) {
|
|
275
|
+
indices.push(i);
|
|
276
|
+
}
|
|
277
|
+
return indices;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function satisfiesIndexSpacing(
|
|
281
|
+
indices: number[],
|
|
282
|
+
xAt: (i: number) => number,
|
|
283
|
+
minSpacing: number,
|
|
284
|
+
) {
|
|
285
|
+
for (let j = 1; j < indices.length; j++) {
|
|
286
|
+
if (xAt(indices[j]) - xAt(indices[j - 1]) < minSpacing) return false;
|
|
287
|
+
}
|
|
288
|
+
return true;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function getAdaptiveOrdinalTicks(
|
|
292
|
+
allTicks: any[],
|
|
293
|
+
scaler: any,
|
|
294
|
+
labelWidth: number,
|
|
295
|
+
minGap: number,
|
|
296
|
+
) {
|
|
297
|
+
const n = allTicks.length;
|
|
298
|
+
if (!n) return [];
|
|
299
|
+
if (n === 1) return [allTicks[0]];
|
|
300
|
+
|
|
301
|
+
const minSpacing = labelWidth + minGap;
|
|
302
|
+
const xAt = (i: number) => scaler(allTicks[i]);
|
|
303
|
+
|
|
304
|
+
// 仅用相邻刻度像素间距约束,不用 floor(available/step) 限制数量:
|
|
305
|
+
// 极长标签时后者会得到 maxByWidth=1,错误地丢弃所有多刻度方案。
|
|
306
|
+
for (let step = 1; step < n; step++) {
|
|
307
|
+
const indices = buildUniformStepIndices(n, step);
|
|
308
|
+
if (satisfiesIndexSpacing(indices, xAt, minSpacing)) {
|
|
309
|
+
return indices.map((i) => allTicks[i]);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
if (n >= 2 && xAt(n - 1) - xAt(0) >= minSpacing) {
|
|
314
|
+
return [allTicks[0], allTicks[n - 1]];
|
|
315
|
+
}
|
|
316
|
+
return [allTicks[0]];
|
|
317
|
+
}
|
|
318
|
+
|
|
251
319
|
function maxLabelFT(
|
|
252
320
|
data: any,
|
|
253
321
|
config: any,
|
|
@@ -258,24 +326,9 @@ function maxLabelFT(
|
|
|
258
326
|
let max = 0;
|
|
259
327
|
data.length
|
|
260
328
|
? data.forEach((item: any) => {
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
...config,
|
|
265
|
-
format: { type: config.format, showType: config.showType },
|
|
266
|
-
}),
|
|
267
|
-
font,
|
|
268
|
-
isVertical,
|
|
269
|
-
) > max
|
|
270
|
-
)
|
|
271
|
-
max = calculateTextWidth(
|
|
272
|
-
formatter(item, {
|
|
273
|
-
...config,
|
|
274
|
-
format: { type: config.format, showType: config.showType },
|
|
275
|
-
}),
|
|
276
|
-
font,
|
|
277
|
-
isVertical,
|
|
278
|
-
);
|
|
329
|
+
const formatted = formatAxisLabel(item, config, formatter);
|
|
330
|
+
const width = calculateTextWidth(formatted, font, isVertical);
|
|
331
|
+
if (width > max) max = width;
|
|
279
332
|
})
|
|
280
333
|
: "";
|
|
281
334
|
return max;
|
|
@@ -305,10 +358,7 @@ const Label: (
|
|
|
305
358
|
LabelWidth,
|
|
306
359
|
}) => {
|
|
307
360
|
if (!show) return null;
|
|
308
|
-
const _label =
|
|
309
|
-
...config,
|
|
310
|
-
format: { type: config.format, showType: config.showType },
|
|
311
|
-
});
|
|
361
|
+
const _label = formatAxisLabel(label, config, formatter);
|
|
312
362
|
const { transform, directionX, directionY } = getLayout(
|
|
313
363
|
orientation,
|
|
314
364
|
adaptive ? 0 : rotate,
|
|
@@ -420,104 +470,29 @@ export default memo(
|
|
|
420
470
|
let LabelWidth = 1;
|
|
421
471
|
if (!label.adaptive && label.appearance) {
|
|
422
472
|
LabelWidth = label.appearance.width;
|
|
423
|
-
} else {
|
|
424
|
-
|
|
425
|
-
|
|
473
|
+
} else if (allTicks.length) {
|
|
474
|
+
LabelWidth = Math.max(
|
|
475
|
+
maxLabelFT(
|
|
426
476
|
allTicks,
|
|
427
477
|
label,
|
|
428
478
|
formatter,
|
|
429
479
|
label.font,
|
|
430
480
|
isVertical,
|
|
431
|
-
)
|
|
432
|
-
|
|
481
|
+
),
|
|
482
|
+
1,
|
|
483
|
+
);
|
|
433
484
|
}
|
|
434
|
-
const
|
|
435
|
-
? Math.floor(
|
|
436
|
-
((width / (isC ? cPercent : 1)) * (1 - paddingOuter)) / LabelWidth,
|
|
437
|
-
)
|
|
438
|
-
: Math.floor(height / LabelWidth);
|
|
439
|
-
|
|
485
|
+
const LABEL_MIN_GAP = 12;
|
|
440
486
|
const ticks: any = !label.adaptive
|
|
441
487
|
? tickss
|
|
442
|
-
:
|
|
488
|
+
: getAdaptiveOrdinalTicks(
|
|
443
489
|
allTicks,
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
: LabelNum
|
|
448
|
-
: allTicks.length,
|
|
449
|
-
label.showLast,
|
|
490
|
+
scaler,
|
|
491
|
+
LabelWidth,
|
|
492
|
+
LABEL_MIN_GAP,
|
|
450
493
|
);
|
|
451
494
|
if (!(on && ticks.length > 0)) return null;
|
|
452
495
|
|
|
453
|
-
//数据抽取逻辑
|
|
454
|
-
function getEvenlySpacedElements(
|
|
455
|
-
arr: any[],
|
|
456
|
-
expectCount: number,
|
|
457
|
-
acc: boolean = false,
|
|
458
|
-
): any[] {
|
|
459
|
-
if (!arr.length || expectCount <= 0) return [];
|
|
460
|
-
if (expectCount >= arr.length) return [...arr];
|
|
461
|
-
if (expectCount === 1) return [arr[0]];
|
|
462
|
-
|
|
463
|
-
if (acc) {
|
|
464
|
-
const totalLength = arr.length;
|
|
465
|
-
const result: any[] = [];
|
|
466
|
-
|
|
467
|
-
let bestCount = 2;
|
|
468
|
-
for (let k = expectCount; k >= 2; k--) {
|
|
469
|
-
const denominator = k - 1;
|
|
470
|
-
const numerator = totalLength - 1;
|
|
471
|
-
if (denominator > 0 && numerator % denominator === 0) {
|
|
472
|
-
bestCount = k;
|
|
473
|
-
break;
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
const step = (totalLength - 1) / (bestCount - 1);
|
|
478
|
-
for (let i = 0; i < bestCount; i++) {
|
|
479
|
-
const rawIndex = i * step;
|
|
480
|
-
const index = Math.ceil(rawIndex);
|
|
481
|
-
const safeIndex = Math.max(0, Math.min(totalLength - 1, index));
|
|
482
|
-
result.push(arr[safeIndex]);
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
if (result.length > 0) result[0] = arr[0];
|
|
486
|
-
if (result.length >= 2)
|
|
487
|
-
result[result.length - 1] = arr[totalLength - 1];
|
|
488
|
-
|
|
489
|
-
return result;
|
|
490
|
-
} else {
|
|
491
|
-
// 重构acc=false逻辑:优先均匀分布,不强制首尾
|
|
492
|
-
const result: any[] = [];
|
|
493
|
-
const arrLen = arr.length;
|
|
494
|
-
if (expectCount === 2) {
|
|
495
|
-
result.push(arr[0]);
|
|
496
|
-
result.push(arr[arrLen - 1]);
|
|
497
|
-
return result;
|
|
498
|
-
}
|
|
499
|
-
const idealStep = (arrLen - 1) / (expectCount - 1);
|
|
500
|
-
const isIdealStepInteger =
|
|
501
|
-
Math.abs(idealStep - Math.round(idealStep)) < 1e-10;
|
|
502
|
-
|
|
503
|
-
if (isIdealStepInteger) {
|
|
504
|
-
for (let i = 0; i < expectCount; i++) {
|
|
505
|
-
const index = i * idealStep;
|
|
506
|
-
result.push(arr[index]);
|
|
507
|
-
}
|
|
508
|
-
} else {
|
|
509
|
-
const step = Math.max(1, Math.floor(arrLen / expectCount));
|
|
510
|
-
for (
|
|
511
|
-
let i = 0;
|
|
512
|
-
i < arrLen && result.length < expectCount;
|
|
513
|
-
i += step
|
|
514
|
-
) {
|
|
515
|
-
result.push(arr[i]);
|
|
516
|
-
}
|
|
517
|
-
}
|
|
518
|
-
return result;
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
496
|
function drawAxisTickLine() {
|
|
522
497
|
const draw = (ticks: any, scaler: any) => {
|
|
523
498
|
return ticks.map((tick: string, index: number) => {
|