File size: 6,460 Bytes
1e92f2d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | 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 <T>(value: T) => T
}
return <T>(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 = <RawDatum>({
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<number>
}
export const getSizeGenerator = <RawDatum>(size: SizeSpec<RawDatum>) => {
// 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 = <RawDatum>({
axis,
valueScale,
ordinalScale,
spacing,
forceStrength,
}: {
axis: 'x' | 'y'
valueScale: ScaleLinear<number> | ScaleTime<string | Date>
ordinalScale: ScaleOrdinal<string, number>
spacing: number
forceStrength: number
}): SimulationForces<RawDatum> => {
const collisionForce = forceCollide<PreSimulationDatum<RawDatum>>(d => d.size / 2 + spacing / 2)
let xForce: ForceX<PreSimulationDatum<RawDatum>>
let yForce: ForceY<PreSimulationDatum<RawDatum>>
if (axis === 'x') {
xForce = forceX<PreSimulationDatum<RawDatum>>(d => valueScale(d.value)).strength(
forceStrength
)
yForce = forceY<PreSimulationDatum<RawDatum>>(d => ordinalScale(d.group))
} else if (axis === 'y') {
xForce = forceX<PreSimulationDatum<RawDatum>>(d => ordinalScale(d.group))
yForce = forceY<PreSimulationDatum<RawDatum>>(d => valueScale(d.value)).strength(
forceStrength
)
} else {
throw new Error(`Invalid axis provided: ${axis}`)
}
return { x: xForce, y: yForce, collision: collisionForce }
}
export const computeNodes = <RawDatum>({
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<number> | ScaleTime<string | Date>
getGroup: (datum: RawDatum) => string
ordinalScale: ScaleOrdinal<string, number>
getSize: (datum: RawDatum) => number
forces: SimulationForces<RawDatum>
simulationIterations: number
valueScaleConfig: ScaleLinearSpec | ScaleTimeSpec
}) => {
const config = {
horizontal: ['x', 'y'],
vertical: ['y', 'x'],
}
const parseValue = getParsedValue(valueScaleConfig)
const simulatedNodes: PreSimulationDatum<RawDatum>[] = data.map(d => ({
id: getId(d),
group: getGroup(d),
value: parseValue(getValue(d)),
size: getSize(d),
data: { ...d },
}))
const simulation = forceSimulation<PreSimulationDatum<RawDatum>>(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<RawDatum>[],
}
}
|