| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- import { stratify, hierarchy } from 'd3-hierarchy';
- export function baseChannels(options = {}) {
- const { shapes } = options;
- return [
- { name: 'color' },
- { name: 'opacity' },
- { name: 'shape', range: shapes },
- { name: 'enterType' },
- { name: 'enterDelay', scaleKey: 'enter' },
- { name: 'enterDuration', scaleKey: 'enter' },
- { name: 'enterEasing' },
- { name: 'key', scale: 'identity' },
- { name: 'groupKey', scale: 'identity' },
- { name: 'label', scale: 'identity' },
- ];
- }
- export function baseGeometryChannels(options = {}) {
- return [...baseChannels(options), { name: 'title', scale: 'identity' }];
- }
- export function tooltip2d() {
- return [
- { type: 'maybeTitle', channel: 'color' },
- { type: 'maybeTooltip', channel: ['x', 'y'] },
- ];
- }
- export function tooltip1d() {
- return [
- { type: 'maybeTitle', channel: 'x' },
- { type: 'maybeTooltip', channel: ['y'] },
- ];
- }
- export function tooltipXd() {
- return [
- { type: 'maybeTitle', channel: 'color' },
- { type: 'maybeTooltip', channel: ['position'] },
- ];
- }
- export function baseAnnotationChannels(options = {}) {
- return baseChannels(options);
- }
- export function basePreInference() {
- return [{ type: 'maybeKey' }];
- }
- export function basePostInference() {
- return [];
- }
- export function bandWidth(scale, x) {
- return scale.getBandWidth(scale.invert(x));
- }
- export function createBandOffset(scale, value, options = {}) {
- const { x: X, y: Y, series: S } = value;
- const { x, y, series } = scale;
- const { style: { bandOffset = series ? 0 : 0.5, bandOffsetX = bandOffset, bandOffsetY = bandOffset, } = {}, } = options;
- const isBandX = !!(x === null || x === void 0 ? void 0 : x.getBandWidth);
- const isBandY = !!(y === null || y === void 0 ? void 0 : y.getBandWidth);
- const isSeries = !!(series === null || series === void 0 ? void 0 : series.getBandWidth);
- if (!isBandX && !isBandY)
- return (d) => d;
- return (d, i) => {
- const widthX = isBandX ? bandWidth(x, X[i]) : 0;
- const widthY = isBandY ? bandWidth(y, Y[i]) : 0;
- const f = () => (bandWidth(series, S[i]) / 2 + +S[i]) * widthX;
- const offset = isSeries && S ? f() : 0;
- const [x0, y0] = d;
- return [x0 + bandOffsetX * widthX + offset, y0 + bandOffsetY * widthY];
- };
- }
- export function p(d) {
- return parseFloat(d) / 100;
- }
- export function visualMark(index, scale, value, coordinate) {
- const { x: X, y: Y } = value;
- const { width, height } = coordinate.getOptions();
- const P = Array.from(index, (i) => {
- const x0 = X[i];
- const y0 = Y[i];
- const x = typeof x0 === 'string' ? p(x0) * width : +x0;
- const y = typeof y0 === 'string' ? p(y0) * height : +y0;
- return [[x, y]];
- });
- return [index, P];
- }
- export function field(encode) {
- return typeof encode === 'function' ? encode : (d) => d[encode];
- }
- export function valueof(data, encode) {
- return Array.from(data, field(encode));
- }
- export function initializeData(data, encode) {
- const { source = (d) => d.source, target = (d) => d.target, value = (d) => d.value, } = encode;
- const { links, nodes } = data;
- const LS = valueof(links, source);
- const LT = valueof(links, target);
- const LV = valueof(links, value);
- return {
- links: links.map((_, i) => ({
- target: LT[i],
- source: LS[i],
- value: LV[i],
- })),
- nodes: nodes || Array.from(new Set([...LS, ...LT]), (key) => ({ key })),
- };
- }
- /**
- * @description Path need when the data is a flat json structure,
- * and the tree object structure do not need.
- */
- export function generateHierarchyRoot(data, path) {
- if (Array.isArray(data)) {
- return typeof path === 'function'
- ? stratify().path(path)(data)
- : stratify()(data);
- }
- return hierarchy(data);
- }
- //# sourceMappingURL=utils.js.map
|