@antv/g2
Version:
the Grammar of Graphics in Javascript
65 lines • 2.55 kB
JavaScript
import { group } from 'd3-array';
import { Line3DShape } from '../shape';
import { MaybeSeries } from '../transform';
import { baseGeometryChannels, basePostInference, basePreInference, tooltip3d, } from './utils';
const shape = {
line: Line3DShape,
};
const line = (index, scale, value, coordinate) => {
var _a, _b, _c;
const { series: S, x: X, y: Y, z: Z } = value;
const { x, y, z } = scale;
// Because x and y channel is not strictly required in Line.props,
// it should throw error with empty x, y or z channels.
if (X === undefined || Y === undefined || Z === undefined) {
throw new Error('Missing encode for x, y or z channel.');
}
// Group data into series.
// There is only one series without specified series encode.
const series = S ? Array.from(group(index, (i) => S[i]).values()) : [index];
const I = series.map((group) => group[0]).filter((i) => i !== undefined);
// A group of data corresponds to one line.
const xoffset = (((_a = x === null || x === void 0 ? void 0 : x.getBandWidth) === null || _a === void 0 ? void 0 : _a.call(x)) || 0) / 2;
const yoffset = (((_b = y === null || y === void 0 ? void 0 : y.getBandWidth) === null || _b === void 0 ? void 0 : _b.call(y)) || 0) / 2;
const zoffset = (((_c = z === null || z === void 0 ? void 0 : z.getBandWidth) === null || _c === void 0 ? void 0 : _c.call(z)) || 0) / 2;
const P = Array.from(series, (I) => {
return I.map((i) => coordinate.map([
(+X[i] || 0) + xoffset,
(+Y[i] || 0) + yoffset,
(+Z[i] || 0) + zoffset,
]));
});
return [I, P, series];
};
/**
* Convert value for each channel to line shapes.
*/
export const Line3D = () => {
return (index, scale, value, coordinate) => {
const mark = line;
return mark(index, scale, value, coordinate);
};
};
Line3D.props = {
defaultShape: 'line',
defaultLabelShape: 'label',
composite: false,
shape,
channels: [
...baseGeometryChannels({ shapes: Object.keys(shape) }),
{ name: 'x' },
{ name: 'y' },
{ name: 'z' },
{ name: 'position', independent: true },
{ name: 'size' },
{ name: 'series', scale: 'identity' },
],
preInference: [...basePreInference(), { type: MaybeSeries }],
postInference: [...basePostInference(), ...tooltip3d()],
interaction: {
shareTooltip: false,
seriesTooltip: false,
crosshairs: false,
},
};
//# sourceMappingURL=line3D.js.map