@antv/g2 5.0.7 → 5.0.8
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/dist/g2-lite.min.js +4 -4
- package/dist/g2.min.js +3 -3
- package/esm/api/chart.js +6 -2
- package/esm/api/chart.js.map +1 -1
- package/esm/api/mark/index.d.ts +3 -1
- package/esm/api/mark/index.js +2 -1
- package/esm/api/mark/index.js.map +1 -1
- package/esm/api/mark/mark.d.ts +7 -1
- package/esm/api/mark/mark.js +9 -0
- package/esm/api/mark/mark.js.map +1 -1
- package/esm/component/axis.js +2 -2
- package/esm/component/axis.js.map +1 -1
- package/esm/composition/mark.js +2 -1
- package/esm/composition/mark.js.map +1 -1
- package/esm/data/index.d.ts +2 -0
- package/esm/data/index.js +1 -0
- package/esm/data/index.js.map +1 -1
- package/esm/data/kde.d.ts +10 -0
- package/esm/data/kde.js +30 -0
- package/esm/data/kde.js.map +1 -0
- package/esm/interaction/brushHighlight.js +6 -3
- package/esm/interaction/brushHighlight.js.map +1 -1
- package/esm/mark/density.d.ts +4 -0
- package/esm/mark/density.js +61 -0
- package/esm/mark/density.js.map +1 -0
- package/esm/mark/index.d.ts +2 -0
- package/esm/mark/index.js +1 -0
- package/esm/mark/index.js.map +1 -1
- package/esm/runtime/component.d.ts +4 -2
- package/esm/runtime/component.js +164 -12
- package/esm/runtime/component.js.map +1 -1
- package/esm/runtime/layout.d.ts +3 -3
- package/esm/runtime/layout.js +40 -21
- package/esm/runtime/layout.js.map +1 -1
- package/esm/runtime/plot.js +1 -1
- package/esm/runtime/plot.js.map +1 -1
- package/esm/shape/box/violin.d.ts +3 -0
- package/esm/shape/box/violin.js +85 -0
- package/esm/shape/box/violin.js.map +1 -0
- package/esm/shape/density/density.d.ts +8 -0
- package/esm/shape/density/density.js +50 -0
- package/esm/shape/density/density.js.map +1 -0
- package/esm/shape/index.d.ts +4 -0
- package/esm/shape/index.js +2 -0
- package/esm/shape/index.js.map +1 -1
- package/esm/spec/composition.d.ts +22 -19
- package/esm/spec/dataTransform.d.ts +34 -2
- package/esm/spec/mark.d.ts +9 -8
- package/esm/spec/transform.d.ts +1 -1
- package/esm/spec/utils.d.ts +1 -0
- package/esm/stdlib/index.js +5 -5
- package/esm/stdlib/index.js.map +1 -1
- package/esm/utils/scale.js +5 -1
- package/esm/utils/scale.js.map +1 -1
- package/esm/utils/size.js +7 -4
- package/esm/utils/size.js.map +1 -1
- package/lib/api/chart.js +6 -2
- package/lib/api/chart.js.map +1 -1
- package/lib/api/mark/index.d.ts +3 -1
- package/lib/api/mark/index.js +1 -0
- package/lib/api/mark/index.js.map +1 -1
- package/lib/api/mark/mark.d.ts +7 -1
- package/lib/api/mark/mark.js +10 -1
- package/lib/api/mark/mark.js.map +1 -1
- package/lib/component/axis.js +2 -2
- package/lib/component/axis.js.map +1 -1
- package/lib/composition/mark.js +2 -1
- package/lib/composition/mark.js.map +1 -1
- package/lib/data/index.d.ts +2 -0
- package/lib/data/index.js +3 -1
- package/lib/data/index.js.map +1 -1
- package/lib/data/kde.d.ts +10 -0
- package/lib/data/kde.js +38 -0
- package/lib/data/kde.js.map +1 -0
- package/lib/interaction/brushHighlight.js +6 -3
- package/lib/interaction/brushHighlight.js.map +1 -1
- package/lib/mark/density.d.ts +4 -0
- package/lib/mark/density.js +65 -0
- package/lib/mark/density.js.map +1 -0
- package/lib/mark/index.d.ts +2 -0
- package/lib/mark/index.js +3 -1
- package/lib/mark/index.js.map +1 -1
- package/lib/runtime/component.d.ts +4 -2
- package/lib/runtime/component.js +163 -10
- package/lib/runtime/component.js.map +1 -1
- package/lib/runtime/layout.d.ts +3 -3
- package/lib/runtime/layout.js +40 -21
- package/lib/runtime/layout.js.map +1 -1
- package/lib/runtime/plot.js +1 -1
- package/lib/runtime/plot.js.map +1 -1
- package/lib/shape/box/violin.d.ts +3 -0
- package/lib/shape/box/violin.js +89 -0
- package/lib/shape/box/violin.js.map +1 -0
- package/lib/shape/density/density.d.ts +8 -0
- package/lib/shape/density/density.js +54 -0
- package/lib/shape/density/density.js.map +1 -0
- package/lib/shape/index.d.ts +4 -0
- package/lib/shape/index.js +6 -1
- package/lib/shape/index.js.map +1 -1
- package/lib/spec/composition.d.ts +22 -19
- package/lib/spec/dataTransform.d.ts +34 -2
- package/lib/spec/mark.d.ts +9 -8
- package/lib/spec/transform.d.ts +1 -1
- package/lib/spec/utils.d.ts +1 -0
- package/lib/stdlib/index.js +2 -2
- package/lib/stdlib/index.js.map +1 -1
- package/lib/utils/scale.js +5 -1
- package/lib/utils/scale.js.map +1 -1
- package/lib/utils/size.js +7 -4
- package/lib/utils/size.js.map +1 -1
- package/package.json +3 -2
- package/src/api/chart.ts +7 -2
- package/src/api/mark/index.ts +3 -0
- package/src/api/mark/mark.ts +12 -0
- package/src/component/axis.ts +2 -0
- package/src/composition/mark.ts +2 -0
- package/src/data/index.ts +2 -0
- package/src/data/kde.ts +53 -0
- package/src/interaction/brushHighlight.ts +4 -3
- package/src/mark/density.ts +81 -0
- package/src/mark/index.ts +2 -0
- package/src/runtime/component.ts +210 -12
- package/src/runtime/layout.ts +74 -19
- package/src/runtime/plot.ts +1 -1
- package/src/shape/box/violin.ts +119 -0
- package/src/shape/density/density.ts +52 -0
- package/src/shape/index.ts +4 -0
- package/src/spec/composition.ts +22 -19
- package/src/spec/dataTransform.ts +35 -0
- package/src/spec/mark.ts +10 -6
- package/src/spec/transform.ts +1 -0
- package/src/spec/utils.ts +2 -0
- package/src/stdlib/index.ts +8 -0
- package/src/utils/scale.ts +5 -1
- package/src/utils/size.ts +10 -4
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { Band } from '@antv/scale';
|
|
2
|
+
import { MarkComponent as MC, Vector2 } from '../runtime';
|
|
3
|
+
import { DensityMark } from '../spec';
|
|
4
|
+
import {
|
|
5
|
+
baseGeometryChannels,
|
|
6
|
+
basePostInference,
|
|
7
|
+
basePreInference,
|
|
8
|
+
tooltip1d,
|
|
9
|
+
} from './utils';
|
|
10
|
+
|
|
11
|
+
export type DensityOptions = Omit<DensityMark, 'type'>;
|
|
12
|
+
|
|
13
|
+
export const Density: MC<DensityOptions> = () => {
|
|
14
|
+
return (index, scale, value, coordinate) => {
|
|
15
|
+
const { x: X, series: S } = value;
|
|
16
|
+
|
|
17
|
+
const Yn = Object.entries(value)
|
|
18
|
+
.filter(([key]) => key.startsWith('y'))
|
|
19
|
+
.map(([, value]) => value);
|
|
20
|
+
|
|
21
|
+
const SZn = Object.entries(value)
|
|
22
|
+
.filter(([key]) => key.startsWith('size'))
|
|
23
|
+
.map(([, value]) => value);
|
|
24
|
+
|
|
25
|
+
// Because x and y channel is not strictly required in Line.props,
|
|
26
|
+
// it should throw error with empty x or y channels.
|
|
27
|
+
if (X === undefined || Yn === undefined || SZn === undefined) {
|
|
28
|
+
throw new Error('Missing encode for x or y or size channel.');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Calc width for each box.
|
|
32
|
+
// The scales for x and series channels must be band scale.
|
|
33
|
+
const xScale = scale.x as Band;
|
|
34
|
+
const series = scale.series as Band;
|
|
35
|
+
|
|
36
|
+
const P = Array.from(index, (i) => {
|
|
37
|
+
const groupWidth = xScale.getBandWidth(xScale.invert(+X[i]));
|
|
38
|
+
const ratio = series ? series.getBandWidth(series.invert(+S?.[i])) : 1;
|
|
39
|
+
const width = groupWidth * ratio;
|
|
40
|
+
const offset = (+S?.[i] || 0) * groupWidth;
|
|
41
|
+
|
|
42
|
+
const x = +X[i] + offset + width / 2;
|
|
43
|
+
|
|
44
|
+
const PN = [
|
|
45
|
+
...Yn.map((_, idx) => [x + +SZn[idx][i] / index.length, +Yn[idx][i]]), // right
|
|
46
|
+
...Yn.map((_, idx) => [
|
|
47
|
+
x - +SZn[idx][i] / index.length,
|
|
48
|
+
+Yn[idx][i],
|
|
49
|
+
]).reverse(), // left
|
|
50
|
+
];
|
|
51
|
+
|
|
52
|
+
return PN.map((p) => coordinate.map(p)) as Vector2[];
|
|
53
|
+
});
|
|
54
|
+
return [index, P];
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const shapes = ['density'];
|
|
59
|
+
|
|
60
|
+
Density.props = {
|
|
61
|
+
defaultShape: 'density',
|
|
62
|
+
defaultLabelShape: 'label',
|
|
63
|
+
composite: false,
|
|
64
|
+
channels: [
|
|
65
|
+
...baseGeometryChannels({ shapes }),
|
|
66
|
+
{ name: 'x', scale: 'band', required: true },
|
|
67
|
+
{ name: 'y', required: true },
|
|
68
|
+
{ name: 'size', required: true },
|
|
69
|
+
{ name: 'series', scale: 'band' },
|
|
70
|
+
{ name: 'size', required: true, scale: 'identity' },
|
|
71
|
+
],
|
|
72
|
+
preInference: [
|
|
73
|
+
...basePreInference(),
|
|
74
|
+
{ type: 'maybeZeroY1' },
|
|
75
|
+
{ type: 'maybeZeroX' },
|
|
76
|
+
],
|
|
77
|
+
postInference: [...basePostInference(), ...tooltip1d()],
|
|
78
|
+
interaction: {
|
|
79
|
+
shareTooltip: true,
|
|
80
|
+
},
|
|
81
|
+
};
|
package/src/mark/index.ts
CHANGED
|
@@ -26,6 +26,7 @@ export { ForceGraph } from './forceGraph';
|
|
|
26
26
|
export { Tree } from './tree';
|
|
27
27
|
export { WordCloud } from './wordCloud';
|
|
28
28
|
export { Gauge } from './gauge';
|
|
29
|
+
export { Density } from './density';
|
|
29
30
|
|
|
30
31
|
export type { IntervalOptions } from './interval';
|
|
31
32
|
export type { RectOptions } from './rect';
|
|
@@ -53,3 +54,4 @@ export type { ForceGraphOptions } from './forceGraph';
|
|
|
53
54
|
export type { TreeOptions } from './tree';
|
|
54
55
|
export type { WordCloudOptions } from './wordCloud';
|
|
55
56
|
export type { GaugeOptions } from './gauge';
|
|
57
|
+
export type { DensityOptions } from './density';
|
package/src/runtime/component.ts
CHANGED
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
* @see https://github.com/antvis/G2/discussions/4557
|
|
3
3
|
*/
|
|
4
4
|
import { Coordinate } from '@antv/coord';
|
|
5
|
-
import { isEqual } from '@antv/util';
|
|
6
|
-
import { group } from 'd3-array';
|
|
5
|
+
import { deepMix, isEqual } from '@antv/util';
|
|
6
|
+
import { group, max, sum } from 'd3-array';
|
|
7
|
+
import { format } from 'd3-format';
|
|
8
|
+
import { DisplayObject, Text } from '@antv/g';
|
|
7
9
|
import {
|
|
8
10
|
getPolarOptions,
|
|
9
11
|
getRadialOptions,
|
|
@@ -11,7 +13,7 @@ import {
|
|
|
11
13
|
type RadialOptions,
|
|
12
14
|
} from '../coordinate';
|
|
13
15
|
import { combine } from '../utils/array';
|
|
14
|
-
import { defined } from '../utils/helper';
|
|
16
|
+
import { capitalizeFirst, defined, subObject } from '../utils/helper';
|
|
15
17
|
import { LEGEND_INFER_STRATEGIES } from '../component/constant';
|
|
16
18
|
import {
|
|
17
19
|
coordOf,
|
|
@@ -36,6 +38,8 @@ import {
|
|
|
36
38
|
import {
|
|
37
39
|
GuideComponent,
|
|
38
40
|
GuideComponentComponent as GCC,
|
|
41
|
+
ScaleComponent,
|
|
42
|
+
Scale,
|
|
39
43
|
} from './types/component';
|
|
40
44
|
import {
|
|
41
45
|
G2CoordinateOptions,
|
|
@@ -90,13 +94,13 @@ export function inferComponent(
|
|
|
90
94
|
});
|
|
91
95
|
}
|
|
92
96
|
|
|
93
|
-
const
|
|
97
|
+
const inferredComponents = inferComponentsType(displayedScales, coordinates);
|
|
94
98
|
|
|
95
|
-
|
|
99
|
+
inferredComponents.forEach(([type, relativeScales]) => {
|
|
96
100
|
const { props } = createGuideComponent(type);
|
|
97
101
|
const { defaultPosition, defaultOrientation, defaultSize, defaultOrder } =
|
|
98
102
|
props;
|
|
99
|
-
// @todo to be
|
|
103
|
+
// @todo to be confirm if the scale can be merged.
|
|
100
104
|
const scale: G2ScaleOptions = Object.assign({}, ...relativeScales);
|
|
101
105
|
const { guide: guideOptions, field } = scale;
|
|
102
106
|
// A scale may have multiple guides.
|
|
@@ -263,10 +267,10 @@ function inferComponentsType(
|
|
|
263
267
|
scales: G2ScaleOptions[],
|
|
264
268
|
coordinates: G2CoordinateOptions[],
|
|
265
269
|
): [string | GCC, G2ScaleOptions[]][] {
|
|
266
|
-
const
|
|
270
|
+
const availableScales = scales.filter((scale) => isValidScale(scale));
|
|
267
271
|
return [
|
|
268
|
-
...inferLegendComponentType(
|
|
269
|
-
...inferAxisComponentType(
|
|
272
|
+
...inferLegendComponentType(availableScales, coordinates),
|
|
273
|
+
...inferAxisComponentType(availableScales, coordinates),
|
|
270
274
|
];
|
|
271
275
|
}
|
|
272
276
|
|
|
@@ -307,7 +311,7 @@ function inferAxisPositionAndOrientation(
|
|
|
307
311
|
// todo, in current resolution, the radar chart is implement by parallel + polar coordinate.
|
|
308
312
|
// implementation plan to be confirmed.
|
|
309
313
|
// in current implementation, it must to add the first position encode to it's last.
|
|
310
|
-
// so we won't render the last axis
|
|
314
|
+
// so we won't render the last axis repeatably.
|
|
311
315
|
if (type === 'axisRadar') {
|
|
312
316
|
const positions = scales.filter((scale) =>
|
|
313
317
|
scale.name.startsWith('position'),
|
|
@@ -396,7 +400,7 @@ function inferComponentPositionAndOrientation(
|
|
|
396
400
|
return ordinalPositionAndOrientation;
|
|
397
401
|
}
|
|
398
402
|
|
|
399
|
-
function
|
|
403
|
+
function inferScrollableType(name: string, type: string, coordinates = []) {
|
|
400
404
|
if (name === 'x') return isTranspose(coordinates) ? `${type}Y` : `${type}X`;
|
|
401
405
|
if (name === 'y') return isTranspose(coordinates) ? `${type}X` : `${type}Y`;
|
|
402
406
|
return null;
|
|
@@ -424,7 +428,7 @@ function inferScrollableComponents(
|
|
|
424
428
|
scale: G2ScaleOptions,
|
|
425
429
|
options: Record<string, any>,
|
|
426
430
|
) {
|
|
427
|
-
const componentType =
|
|
431
|
+
const componentType = inferScrollableType(channelName, type, coordinates);
|
|
428
432
|
if (!options || !componentType) return;
|
|
429
433
|
|
|
430
434
|
const { props } = createGuideComponent(componentType);
|
|
@@ -450,3 +454,197 @@ function inferScrollableComponents(
|
|
|
450
454
|
})
|
|
451
455
|
.filter((d) => !!d);
|
|
452
456
|
}
|
|
457
|
+
|
|
458
|
+
// !!! Note Mutate component.size and component.style.
|
|
459
|
+
export function computeComponentSize(
|
|
460
|
+
component: G2GuideComponentOptions,
|
|
461
|
+
crossSize: number,
|
|
462
|
+
crossPadding: [number, number],
|
|
463
|
+
position: GCP,
|
|
464
|
+
theme: G2Theme,
|
|
465
|
+
library: G2Library,
|
|
466
|
+
): G2GuideComponentOptions {
|
|
467
|
+
const [useScale] = useLibrary<G2ScaleOptions, ScaleComponent, Scale>(
|
|
468
|
+
'scale',
|
|
469
|
+
library,
|
|
470
|
+
);
|
|
471
|
+
|
|
472
|
+
// Only compute and update size of axis component in padding area.
|
|
473
|
+
// @todo Legend, slider.
|
|
474
|
+
const { type } = component;
|
|
475
|
+
const paddingAreas = ['left', 'right', 'bottom', 'top'];
|
|
476
|
+
if (typeof type !== 'string' || !type.startsWith('axis')) return;
|
|
477
|
+
if (!paddingAreas.includes(position)) return;
|
|
478
|
+
|
|
479
|
+
// If padding is auto, use hide as the labelTransform by default
|
|
480
|
+
// to avoid overlap between labels.
|
|
481
|
+
component.transform = component.transform || [{ type: 'hide' }];
|
|
482
|
+
|
|
483
|
+
const { labelFormatter, scales, title, tickCount, tickMethod, tickFilter } =
|
|
484
|
+
component;
|
|
485
|
+
const isVertical = position === 'left' || position === 'right';
|
|
486
|
+
|
|
487
|
+
// Get styles to be applied.
|
|
488
|
+
const style = styleOf(component, position, theme);
|
|
489
|
+
const { tickLength = 0, labelSpacing = 0, titleSpacing = 0, ...rest } = style;
|
|
490
|
+
|
|
491
|
+
// Init scale, the tickCount of axis has higher priority than scale.
|
|
492
|
+
const [scaleOptions] = scales;
|
|
493
|
+
if (tickCount !== undefined) scaleOptions.tickCount = tickCount;
|
|
494
|
+
if (tickMethod !== undefined) scaleOptions.tickMethod = tickMethod;
|
|
495
|
+
const scale = useScale(scaleOptions);
|
|
496
|
+
|
|
497
|
+
// Get labels to be rendered.
|
|
498
|
+
const labels = labelsOf(scale, labelFormatter, tickFilter);
|
|
499
|
+
const labeStyle = subObject(rest, 'label');
|
|
500
|
+
const labelBBoxes = labels.map((d, i) => {
|
|
501
|
+
const normalizeStyle = Object.fromEntries(
|
|
502
|
+
Object.entries(labeStyle).map(([key, value]) => [
|
|
503
|
+
key,
|
|
504
|
+
typeof value === 'function' ? value(d, i) : value,
|
|
505
|
+
]),
|
|
506
|
+
);
|
|
507
|
+
// Auto padding should ignore transform for horizontal axis.
|
|
508
|
+
if (!isVertical) normalizeStyle.transform = 'none';
|
|
509
|
+
return computeLabelSize(d, normalizeStyle);
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
const maxLabelWidth = max(labelBBoxes, (d) => d.width);
|
|
513
|
+
const paddingTick = tickLength + labelSpacing;
|
|
514
|
+
|
|
515
|
+
if (isVertical) {
|
|
516
|
+
component.size = maxLabelWidth + paddingTick;
|
|
517
|
+
} else {
|
|
518
|
+
// If the labels can't be placed horizontally,
|
|
519
|
+
// rotate 90 deg to display them.
|
|
520
|
+
if (overflowX(scale, labelBBoxes, crossSize, crossPadding, tickFilter)) {
|
|
521
|
+
component.size = maxLabelWidth + paddingTick;
|
|
522
|
+
component.style = {
|
|
523
|
+
...component.style,
|
|
524
|
+
labelTransform: 'rotate(90)',
|
|
525
|
+
};
|
|
526
|
+
} else {
|
|
527
|
+
const maxLabelHeight = max(labelBBoxes, (d) => d.height);
|
|
528
|
+
component.size = maxLabelHeight + paddingTick;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
// Cache boxes to avoid computed twice.
|
|
533
|
+
const I = labels.map((_, i) => i);
|
|
534
|
+
component.indexBBox = new Map(I.map((i) => [i, [labels[i], labelBBoxes[i]]]));
|
|
535
|
+
|
|
536
|
+
if (title === false || title === null || title === undefined) return;
|
|
537
|
+
|
|
538
|
+
// Get title to be rendered.
|
|
539
|
+
const titleStyle = subObject(rest, 'title');
|
|
540
|
+
const titleText = Array.isArray(title) ? title.join(',') : title;
|
|
541
|
+
const titleBBox = computeLabelSize(titleText, titleStyle);
|
|
542
|
+
|
|
543
|
+
if (isVertical) {
|
|
544
|
+
component.size += titleSpacing + titleBBox.width;
|
|
545
|
+
} else {
|
|
546
|
+
component.size += titleSpacing + titleBBox.height;
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
function styleOf(
|
|
551
|
+
axis: G2GuideComponentOptions,
|
|
552
|
+
position: GCP,
|
|
553
|
+
theme: G2Theme,
|
|
554
|
+
): Record<string, any> {
|
|
555
|
+
const {
|
|
556
|
+
axis: baseStyle,
|
|
557
|
+
// @ts-ignore
|
|
558
|
+
[`axis${capitalizeFirst(position)}`]: positionStyle,
|
|
559
|
+
} = theme;
|
|
560
|
+
return deepMix({}, baseStyle, positionStyle, axis.style);
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
function ticksOf(scale: Scale, tickFilter: (d: any) => boolean): any[] {
|
|
564
|
+
const ticks = scale.getTicks ? scale.getTicks() : scale.getOptions().domain;
|
|
565
|
+
if (!tickFilter) return ticks;
|
|
566
|
+
return ticks.filter(tickFilter);
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
function labelsOf(
|
|
570
|
+
scale: Scale,
|
|
571
|
+
labelFormatter: (d: any) => string | DisplayObject,
|
|
572
|
+
tickFilter,
|
|
573
|
+
): (string | DisplayObject)[] {
|
|
574
|
+
const T = ticksOf(scale, tickFilter);
|
|
575
|
+
const ticks = T.map((d) => (typeof d === 'number' ? prettyNumber(d) : d));
|
|
576
|
+
const formatter = labelFormatter
|
|
577
|
+
? typeof labelFormatter === 'string'
|
|
578
|
+
? format(labelFormatter)
|
|
579
|
+
: labelFormatter
|
|
580
|
+
: scale.getFormatter
|
|
581
|
+
? scale.getFormatter()
|
|
582
|
+
: (d) => `${d}`;
|
|
583
|
+
return ticks.map(formatter);
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
function offsetOf(scale: Scale, d: any): number {
|
|
587
|
+
if (!scale.getBandWidth) return 0;
|
|
588
|
+
const offset = scale.getBandWidth(d) / 2;
|
|
589
|
+
return offset;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
function overflowX(
|
|
593
|
+
scale: Scale,
|
|
594
|
+
labelBBoxes: DOMRect[],
|
|
595
|
+
crossSize: number,
|
|
596
|
+
crossPadding: [number, number],
|
|
597
|
+
tickFilter: (d: any) => boolean,
|
|
598
|
+
): boolean {
|
|
599
|
+
// If actual size bigger than container size, overflow.
|
|
600
|
+
const totalSize = sum(labelBBoxes, (d) => d.width);
|
|
601
|
+
if (totalSize > crossSize) return true;
|
|
602
|
+
|
|
603
|
+
// Clone scale to get visual position for labels.
|
|
604
|
+
const scaleX = scale.clone();
|
|
605
|
+
scaleX.update({ range: [0, crossSize] });
|
|
606
|
+
const ticks = ticksOf(scale, tickFilter);
|
|
607
|
+
const X = ticks.map((d) => scaleX.map(d) + offsetOf(scaleX, d));
|
|
608
|
+
|
|
609
|
+
const I = ticks.map((_, i) => i);
|
|
610
|
+
const startX = -crossPadding[0];
|
|
611
|
+
const endX = crossSize + crossPadding[1];
|
|
612
|
+
const extent = (x, bbox) => {
|
|
613
|
+
const { width } = bbox;
|
|
614
|
+
return [x - width / 2, x + width / 2];
|
|
615
|
+
};
|
|
616
|
+
|
|
617
|
+
// Collision detection.
|
|
618
|
+
for (let i = 0; i < I.length; i++) {
|
|
619
|
+
const x = X[i];
|
|
620
|
+
const [x0, x1] = extent(x, labelBBoxes[i]);
|
|
621
|
+
// If a label is out of plot area, overflow.
|
|
622
|
+
if (x0 < startX || x1 > endX) return true;
|
|
623
|
+
const y = X[i + 1];
|
|
624
|
+
if (y) {
|
|
625
|
+
// If two labels intersect, overflow.
|
|
626
|
+
const [y0] = extent(y, labelBBoxes[i + 1]);
|
|
627
|
+
if (x1 > y0) return true;
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
return false;
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
function computeLabelSize(
|
|
634
|
+
d: string | DisplayObject,
|
|
635
|
+
style: Record<string, any>,
|
|
636
|
+
): DOMRect {
|
|
637
|
+
const shape = normalizeLabel(d);
|
|
638
|
+
shape.attr({ ...style, visibility: 'none' });
|
|
639
|
+
const bbox = shape.getBBox();
|
|
640
|
+
return bbox;
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
function normalizeLabel(d: string | DisplayObject): DisplayObject {
|
|
644
|
+
if (d instanceof DisplayObject) return d;
|
|
645
|
+
return new Text({ style: { text: `${d}` } });
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
function prettyNumber(n: number) {
|
|
649
|
+
return Math.abs(n) < 1e-15 ? n : parseFloat(n.toFixed(15));
|
|
650
|
+
}
|
package/src/runtime/layout.ts
CHANGED
|
@@ -3,21 +3,24 @@ import { ascending, group } from 'd3-array';
|
|
|
3
3
|
import { isParallel, isPolar, isRadar, radiusOf } from '../utils/coordinate';
|
|
4
4
|
import { capitalizeFirst } from '../utils/helper';
|
|
5
5
|
import { divide } from '../utils/array';
|
|
6
|
+
import { camelCase } from '../utils/string';
|
|
6
7
|
import {
|
|
7
8
|
GuideComponentPosition as GCP,
|
|
8
9
|
GuideComponentOrientation as GCO,
|
|
9
10
|
Layout,
|
|
10
11
|
Section,
|
|
11
12
|
SectionArea,
|
|
13
|
+
G2Theme,
|
|
12
14
|
} from './types/common';
|
|
13
|
-
import {
|
|
15
|
+
import { computeComponentSize } from './component';
|
|
16
|
+
import { G2GuideComponentOptions, G2Library, G2View } from './types/options';
|
|
14
17
|
|
|
15
18
|
export function computeLayout(
|
|
16
19
|
components: G2GuideComponentOptions[],
|
|
17
20
|
options: G2View,
|
|
21
|
+
theme: G2Theme,
|
|
22
|
+
library: G2Library,
|
|
18
23
|
): Layout {
|
|
19
|
-
const padding = computePadding(components, options);
|
|
20
|
-
const { paddingLeft, paddingRight, paddingTop, paddingBottom } = padding;
|
|
21
24
|
const {
|
|
22
25
|
width,
|
|
23
26
|
height,
|
|
@@ -33,18 +36,62 @@ export function computeLayout(
|
|
|
33
36
|
marginBottom = margin,
|
|
34
37
|
marginTop = margin,
|
|
35
38
|
marginRight = margin,
|
|
39
|
+
padding,
|
|
40
|
+
paddingBottom = padding,
|
|
41
|
+
paddingLeft = padding,
|
|
42
|
+
paddingRight = padding,
|
|
43
|
+
paddingTop = padding,
|
|
36
44
|
} = options;
|
|
37
45
|
|
|
46
|
+
const MAX_PADDING_RATIO = 1 / 3;
|
|
47
|
+
const clamp = (origin, computed, max) =>
|
|
48
|
+
origin === 'auto' ? Math.min(max, computed) : computed;
|
|
49
|
+
|
|
50
|
+
// Compute paddingLeft and paddingRight first to get innerWidth.
|
|
51
|
+
const horizontalPadding = computePadding(
|
|
52
|
+
components,
|
|
53
|
+
height,
|
|
54
|
+
[0, 0],
|
|
55
|
+
['left', 'right'],
|
|
56
|
+
options,
|
|
57
|
+
theme,
|
|
58
|
+
library,
|
|
59
|
+
);
|
|
60
|
+
const { paddingLeft: pl0, paddingRight: pr0 } = horizontalPadding;
|
|
61
|
+
const viewWidth = width - marginLeft - marginRight;
|
|
62
|
+
const pl = clamp(paddingLeft, pl0, viewWidth * MAX_PADDING_RATIO);
|
|
63
|
+
const pr = clamp(paddingRight, pr0, viewWidth * MAX_PADDING_RATIO);
|
|
64
|
+
const iw = viewWidth - pl - pr;
|
|
65
|
+
|
|
66
|
+
// Compute paddingBottom and paddingTop based on innerWidth.
|
|
67
|
+
const verticalPadding = computePadding(
|
|
68
|
+
components,
|
|
69
|
+
iw,
|
|
70
|
+
[pl, pr],
|
|
71
|
+
['bottom', 'top'],
|
|
72
|
+
options,
|
|
73
|
+
theme,
|
|
74
|
+
library,
|
|
75
|
+
);
|
|
76
|
+
const { paddingTop: pt0, paddingBottom: pb0 } = verticalPadding;
|
|
77
|
+
const viewHeight = height - marginBottom - marginTop;
|
|
78
|
+
const pb = clamp(paddingBottom, pb0, viewHeight * MAX_PADDING_RATIO);
|
|
79
|
+
const pt = clamp(paddingTop, pt0, viewHeight * MAX_PADDING_RATIO);
|
|
80
|
+
const ih = viewHeight - pb - pt;
|
|
81
|
+
|
|
38
82
|
return {
|
|
39
|
-
...padding,
|
|
40
83
|
width,
|
|
41
84
|
height,
|
|
42
85
|
insetLeft,
|
|
43
86
|
insetTop,
|
|
44
87
|
insetBottom,
|
|
45
88
|
insetRight,
|
|
46
|
-
innerWidth:
|
|
47
|
-
innerHeight:
|
|
89
|
+
innerWidth: iw,
|
|
90
|
+
innerHeight: ih,
|
|
91
|
+
paddingLeft: pl,
|
|
92
|
+
paddingRight: pr,
|
|
93
|
+
paddingTop: pt,
|
|
94
|
+
paddingBottom: pb,
|
|
48
95
|
marginLeft,
|
|
49
96
|
marginBottom,
|
|
50
97
|
marginTop,
|
|
@@ -59,18 +106,13 @@ export function computeLayout(
|
|
|
59
106
|
*/
|
|
60
107
|
function computePadding(
|
|
61
108
|
components: G2GuideComponentOptions[],
|
|
109
|
+
crossSize: number,
|
|
110
|
+
crossPadding: [number, number],
|
|
111
|
+
positions: GCP[],
|
|
62
112
|
options: G2View,
|
|
113
|
+
theme: G2Theme,
|
|
114
|
+
library: G2Library,
|
|
63
115
|
) {
|
|
64
|
-
const positions: GCP[] = [
|
|
65
|
-
'left',
|
|
66
|
-
'right',
|
|
67
|
-
'top',
|
|
68
|
-
'bottom',
|
|
69
|
-
'top-left',
|
|
70
|
-
'top-right',
|
|
71
|
-
'bottom-left',
|
|
72
|
-
'bottom-right',
|
|
73
|
-
];
|
|
74
116
|
const positionComponents = group(components, (d) => d.position);
|
|
75
117
|
const {
|
|
76
118
|
padding,
|
|
@@ -86,12 +128,25 @@ function computePadding(
|
|
|
86
128
|
paddingRight,
|
|
87
129
|
};
|
|
88
130
|
for (const position of positions) {
|
|
89
|
-
const key = `padding${capitalizeFirst(position)}`;
|
|
90
|
-
|
|
131
|
+
const key = `padding${capitalizeFirst(camelCase(position))}`;
|
|
132
|
+
const value = layout[key];
|
|
133
|
+
if (value === undefined || value === 'auto') {
|
|
91
134
|
if (!positionComponents.has(position)) {
|
|
92
135
|
layout[key] = 30;
|
|
93
136
|
} else {
|
|
94
137
|
const components = positionComponents.get(position);
|
|
138
|
+
if (value === 'auto') {
|
|
139
|
+
components.forEach((component) =>
|
|
140
|
+
computeComponentSize(
|
|
141
|
+
component,
|
|
142
|
+
crossSize,
|
|
143
|
+
crossPadding,
|
|
144
|
+
position,
|
|
145
|
+
theme,
|
|
146
|
+
library,
|
|
147
|
+
),
|
|
148
|
+
);
|
|
149
|
+
}
|
|
95
150
|
const totalSize = components.reduce((sum, { size }) => sum + size, 0);
|
|
96
151
|
layout[key] = totalSize;
|
|
97
152
|
}
|
|
@@ -161,7 +216,7 @@ export function placeComponents(
|
|
|
161
216
|
/**
|
|
162
217
|
* @description non-entity components: axis in the center, inner, outer, component in the center
|
|
163
218
|
* @description entity components: other components
|
|
164
|
-
* @description no
|
|
219
|
+
* @description no volume components take up no extra space
|
|
165
220
|
*/
|
|
166
221
|
|
|
167
222
|
const [nonEntityComponents, entityComponents] = divide(
|
package/src/runtime/plot.ts
CHANGED
|
@@ -537,7 +537,7 @@ function initializeState(
|
|
|
537
537
|
options,
|
|
538
538
|
library,
|
|
539
539
|
);
|
|
540
|
-
const layout = computeLayout(components, options);
|
|
540
|
+
const layout = computeLayout(components, options, theme, library);
|
|
541
541
|
const coordinate = createCoordinate(layout, options, library);
|
|
542
542
|
const framedStyle = frame
|
|
543
543
|
? deepMix({ mainLineWidth: 1, mainStroke: '#000' }, style)
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { Path as GPath } from '@antv/g';
|
|
2
|
+
import { path as d3path } from 'd3-path';
|
|
3
|
+
import { Coordinate } from '@antv/coord';
|
|
4
|
+
import { applyStyle, getShapeTheme } from '../utils';
|
|
5
|
+
import { select } from '../../utils/selection';
|
|
6
|
+
import { ShapeComponent as SC, Vector2 } from '../../runtime';
|
|
7
|
+
import { isPolar } from '../../utils/coordinate';
|
|
8
|
+
import { angle, sub, dist } from '../../utils/vector';
|
|
9
|
+
|
|
10
|
+
export type ViolinOptions = Record<string, any>;
|
|
11
|
+
|
|
12
|
+
function getPath(p: Vector2[], coordinate: Coordinate, size = 4) {
|
|
13
|
+
const path = d3path();
|
|
14
|
+
|
|
15
|
+
if (!isPolar(coordinate)) {
|
|
16
|
+
path.moveTo(...p[2]);
|
|
17
|
+
path.lineTo(...p[3]);
|
|
18
|
+
|
|
19
|
+
path.lineTo(p[3][0] - size, p[3][1]);
|
|
20
|
+
path.lineTo(p[10][0] - size, p[10][1]);
|
|
21
|
+
path.lineTo(p[10][0] + size, p[10][1]);
|
|
22
|
+
path.lineTo(p[3][0] + size, p[3][1]);
|
|
23
|
+
path.lineTo(...p[3]);
|
|
24
|
+
path.closePath();
|
|
25
|
+
|
|
26
|
+
path.moveTo(...p[10]);
|
|
27
|
+
path.lineTo(...p[11]);
|
|
28
|
+
|
|
29
|
+
path.moveTo(p[3][0] + size / 2, p[8][1]);
|
|
30
|
+
path.arc(p[3][0], p[8][1], size / 2, 0, Math.PI * 2);
|
|
31
|
+
path.closePath();
|
|
32
|
+
|
|
33
|
+
return path;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const center = coordinate.getCenter();
|
|
37
|
+
const [x, y] = center;
|
|
38
|
+
|
|
39
|
+
const radiusQ3 = dist(center, p[3]);
|
|
40
|
+
const radiusMedian = dist(center, p[8]);
|
|
41
|
+
const radiusQ1 = dist(center, p[10]);
|
|
42
|
+
|
|
43
|
+
const middleAngle = angle(sub(p[2], center));
|
|
44
|
+
const rectAngle = Math.asin(size / radiusMedian);
|
|
45
|
+
const startAngle = middleAngle - rectAngle;
|
|
46
|
+
const endAngle = middleAngle + rectAngle;
|
|
47
|
+
|
|
48
|
+
path.moveTo(...p[2]);
|
|
49
|
+
path.lineTo(...p[3]);
|
|
50
|
+
|
|
51
|
+
path.moveTo(
|
|
52
|
+
Math.cos(startAngle) * radiusQ3 + x,
|
|
53
|
+
Math.sin(startAngle) * radiusQ3 + y,
|
|
54
|
+
);
|
|
55
|
+
path.arc(x, y, radiusQ3, startAngle, endAngle);
|
|
56
|
+
path.lineTo(
|
|
57
|
+
Math.cos(endAngle) * radiusQ1 + x,
|
|
58
|
+
Math.sin(endAngle) * radiusQ1 + y,
|
|
59
|
+
);
|
|
60
|
+
path.arc(x, y, radiusQ1, endAngle, startAngle, true);
|
|
61
|
+
path.lineTo(
|
|
62
|
+
Math.cos(startAngle) * radiusQ3 + x,
|
|
63
|
+
Math.sin(startAngle) * radiusQ3 + y,
|
|
64
|
+
);
|
|
65
|
+
path.closePath();
|
|
66
|
+
|
|
67
|
+
path.moveTo(...p[10]);
|
|
68
|
+
path.lineTo(...p[11]);
|
|
69
|
+
|
|
70
|
+
const a = (startAngle + endAngle) / 2;
|
|
71
|
+
|
|
72
|
+
path.moveTo(
|
|
73
|
+
Math.cos(a) * (radiusMedian + size / 2) + x,
|
|
74
|
+
Math.sin(a) * (radiusMedian + size / 2) + y,
|
|
75
|
+
);
|
|
76
|
+
path.arc(
|
|
77
|
+
Math.cos(a) * radiusMedian + x,
|
|
78
|
+
Math.sin(a) * radiusMedian + y,
|
|
79
|
+
size / 2,
|
|
80
|
+
a,
|
|
81
|
+
Math.PI * 2 + a,
|
|
82
|
+
);
|
|
83
|
+
path.closePath();
|
|
84
|
+
|
|
85
|
+
return path;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export const Violin: SC<ViolinOptions> = (options) => {
|
|
89
|
+
const { ...style } = options;
|
|
90
|
+
return (points, value, coordinate, theme) => {
|
|
91
|
+
const { mark, shape, defaultShape, color, transform } = value;
|
|
92
|
+
// TODO: how to setting it by size channel.
|
|
93
|
+
const size = 4;
|
|
94
|
+
const {
|
|
95
|
+
defaultColor,
|
|
96
|
+
fill = defaultColor,
|
|
97
|
+
stroke = defaultColor,
|
|
98
|
+
...shapeTheme
|
|
99
|
+
} = getShapeTheme(theme, mark, shape, defaultShape);
|
|
100
|
+
|
|
101
|
+
const path = getPath(points, coordinate, size);
|
|
102
|
+
|
|
103
|
+
return select(new GPath())
|
|
104
|
+
.call(applyStyle, shapeTheme)
|
|
105
|
+
.style('d', path.toString())
|
|
106
|
+
.style('stroke', stroke)
|
|
107
|
+
.style('fill', color || fill)
|
|
108
|
+
.style('transform', transform)
|
|
109
|
+
.call(applyStyle, style)
|
|
110
|
+
.node();
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
Violin.props = {
|
|
115
|
+
defaultMarker: 'point',
|
|
116
|
+
defaultEnterAnimation: 'fadeIn',
|
|
117
|
+
defaultUpdateAnimation: 'morphing',
|
|
118
|
+
defaultExitAnimation: 'fadeOut',
|
|
119
|
+
};
|