import isNumber from 'lodash/isNumber.js' import isPlainObject from 'lodash/isPlainObject.js' import isString from 'lodash/isString.js' import get from 'lodash/get.js' import { scaleLinear, ScaleOrdinal, scaleOrdinal } from 'd3-scale' import { forceSimulation, forceX, forceY, forceCollide, ForceX, ForceY } from 'd3-force' import { computeScale, createDateNormalizer, generateSeriesAxis, ScaleLinear, ScaleLinearSpec, ScaleTime, ScaleTimeSpec, } from '@nivo/scales' import { ComputedDatum, PreSimulationDatum, SizeSpec, SimulationForces } from './types' const getParsedValue = (scaleSpec: ScaleLinearSpec | ScaleTimeSpec) => { if (scaleSpec.type === 'time' && scaleSpec.format !== 'native') { return createDateNormalizer(scaleSpec) as (value: T) => T } return (value: T) => value } export const computeOrdinalScale = ({ width, height, axis, groups, gap, }: { width: number height: number axis: 'x' | 'y' groups: string[] gap: number }) => { if (!Array.isArray(groups) || groups.length === 0) { throw new Error(`'groups' should be an array containing at least one item`) } const groupCount = groups.length let groupSize: number if (axis === 'x') { groupSize = (height - gap * (groupCount - 1)) / groupCount } else if (axis === 'y') { groupSize = (width - gap * (groupCount - 1)) / groupCount } const range = groups.map((_, i) => i * (groupSize + gap) + groupSize / 2) return scaleOrdinal(range).domain(groups) } export const computeValueScale = ({ width, height, axis, getValue, scale, data, }: { width: number height: number axis: 'x' | 'y' getValue: (datum: RawDatum) => number | Date scale: ScaleLinearSpec | ScaleTimeSpec data: RawDatum[] }) => { const values = data.map(getValue) if (scale.type === 'time') { const series = [ { data: values.map(value => ({ data: { x: null, y: null, [axis]: value } })) }, ] const axes = generateSeriesAxis(series, axis, scale) return computeScale(scale, axes, axis === 'x' ? width : height, axis) as ScaleTime< Date | string > } const min = Math.min(...(values as number[])) const max = Math.max(...(values as number[])) return computeScale( scale, { all: values, min, max }, axis === 'x' ? width : height, axis ) as ScaleLinear } export const getSizeGenerator = (size: SizeSpec) => { // user defined size function if (typeof size === 'function') { return size } // static size if (isNumber(size)) { return () => size } // dynamic size based on config if (isPlainObject(size)) { if (!isString(size.key)) { throw new Error( 'Size is invalid, key should be a string pointing to the property to use to determine node size' ) } if (!Array.isArray(size.values) || size.values.length !== 2) { throw new Error( 'Size is invalid, values spec should be an array containing two values, min and max' ) } if (!Array.isArray(size.sizes) || size.sizes.length !== 2) { throw new Error( 'Size is invalid, sizes spec should be an array containing two values, min and max' ) } const sizeScale = scaleLinear() .domain([size.values[0], size.values[1]]) .range([size.sizes[0], size.sizes[1]]) return (d: RawDatum) => sizeScale(get(d, size.key)) } throw new Error('Size is invalid, it should be either a function, a number or an object') } export const computeForces = ({ axis, valueScale, ordinalScale, spacing, forceStrength, }: { axis: 'x' | 'y' valueScale: ScaleLinear | ScaleTime ordinalScale: ScaleOrdinal spacing: number forceStrength: number }): SimulationForces => { const collisionForce = forceCollide>(d => d.size / 2 + spacing / 2) let xForce: ForceX> let yForce: ForceY> if (axis === 'x') { xForce = forceX>(d => valueScale(d.value)).strength( forceStrength ) yForce = forceY>(d => ordinalScale(d.group)) } else if (axis === 'y') { xForce = forceX>(d => ordinalScale(d.group)) yForce = forceY>(d => valueScale(d.value)).strength( forceStrength ) } else { throw new Error(`Invalid axis provided: ${axis}`) } return { x: xForce, y: yForce, collision: collisionForce } } export const computeNodes = ({ data, getId, layout, getValue, valueScale, getGroup, ordinalScale, getSize, forces, simulationIterations, valueScaleConfig, }: { data: RawDatum[] getId: (datum: RawDatum) => string layout: 'vertical' | 'horizontal' getValue: (datum: RawDatum) => number | Date valueScale: ScaleLinear | ScaleTime getGroup: (datum: RawDatum) => string ordinalScale: ScaleOrdinal getSize: (datum: RawDatum) => number forces: SimulationForces simulationIterations: number valueScaleConfig: ScaleLinearSpec | ScaleTimeSpec }) => { const config = { horizontal: ['x', 'y'], vertical: ['y', 'x'], } const parseValue = getParsedValue(valueScaleConfig) const simulatedNodes: PreSimulationDatum[] = data.map(d => ({ id: getId(d), group: getGroup(d), value: parseValue(getValue(d)), size: getSize(d), data: { ...d }, })) const simulation = forceSimulation>(simulatedNodes) .force('x', forces.x) .force('y', forces.y) .force('collide', forces.collision) .stop() simulation.tick(simulationIterations) return { [`${config[layout][0]}Scale`]: valueScale, [`${config[layout][1]}Scale`]: ordinalScale, nodes: simulation.nodes() as ComputedDatum[], } }