@cdc/chart 4.26.6 → 4.26.10
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/CONFIG.md +504 -368
- package/dist/cdcchart-CYc-Kayd.es.js +22488 -0
- package/dist/cdcchart-DCKn8NmI.es.js +20695 -0
- package/dist/cdcchart-Dyui9nk_.es.js +45828 -0
- package/dist/cdcchart.js +4 -96498
- package/examples/__data__/vega-remote-data.json +6 -0
- package/examples/dendrogram.json +18 -0
- package/examples/feature/bar/bar-chart-race-annual-change.json +68 -0
- package/examples/feature/heatmap/average-age-categorical.json +2 -2
- package/examples/feature/heatmap/calendar-heatmap.json +2 -2
- package/examples/feature/heatmap/cell-values-bottom-axis.json +2 -2
- package/examples/feature/heatmap/sparse-aggregation.json +2 -2
- package/examples/feature/line/line-chart-race.json +50 -0
- package/examples/feature/sankey/sankey-example-data.json +40 -1441
- package/examples/feature/sankey/sankey_chart_data.csv +9 -20
- package/examples/network.json +31 -0
- package/examples/private/box-plot.json +573 -0
- package/examples/private/custom-color-issue.json +625 -0
- package/examples/private/custom-colors.json +522 -0
- package/examples/private/cutoff.json +10750 -0
- package/examples/private/flueview.json +22051 -0
- package/examples/private/overlay-legend.json +485 -0
- package/examples/private/pie-test.json +469 -0
- package/examples/vega-remote.json +22 -0
- package/package.json +11 -13
- package/src/CdcChart.tsx +2 -2
- package/src/CdcChartComponent.tsx +444 -217
- package/src/_stories/Chart.BarRace.stories.tsx +443 -0
- package/src/_stories/Chart.BoxPlotNumericSort.stories.tsx +189 -0
- package/src/_stories/Chart.BoxPlotStatsParity.stories.tsx +207 -0
- package/src/_stories/Chart.ColorDistribution.stories.tsx +299 -0
- package/src/_stories/Chart.DataCutoff.stories.tsx +147 -0
- package/src/_stories/Chart.DataTableDownloadFilename.stories.tsx +60 -0
- package/src/_stories/Chart.Dendrogram.stories.tsx +373 -0
- package/src/_stories/Chart.Forecast.CILegend.stories.tsx +236 -0
- package/src/_stories/Chart.HeatMap.stories.tsx +170 -4
- package/src/_stories/Chart.Legend.AxisAlign.stories.tsx +119 -0
- package/src/_stories/Chart.LineRace.stories.tsx +149 -0
- package/src/_stories/Chart.Network.stories.tsx +432 -0
- package/src/_stories/Chart.PaletteConversion.Editor.stories.tsx +208 -0
- package/src/_stories/Chart.Patterns.smoke.stories.tsx +31 -0
- package/src/_stories/Chart.PercentageValue.stories.tsx +311 -0
- package/src/_stories/Chart.PieLabels.stories.tsx +49 -0
- package/src/_stories/Chart.RankByValueMutation.stories.tsx +206 -0
- package/src/_stories/Chart.RequiredFields.stories.tsx +681 -0
- package/src/_stories/Chart.Sankey.stories.tsx +417 -0
- package/src/_stories/Chart.SeriesColorAssignments.stories.tsx +47 -0
- package/src/_stories/Chart.SmallMultiplesNumericTileOrder.stories.tsx +137 -0
- package/src/_stories/Chart.TooltipSeriesLabels.stories.tsx +43 -0
- package/src/_stories/Chart.VegaRemoteData.stories.tsx +31 -0
- package/src/_stories/Chart.smoke.stories.tsx +14 -1
- package/src/_stories/ChartAnnotation.Editor.stories.tsx +78 -0
- package/src/_stories/ChartAxisTitles.smoke.stories.tsx +48 -0
- package/src/_stories/ChartBar.Editor.stories.tsx +499 -57
- package/src/_stories/ChartBrush.Editor.stories.tsx +46 -67
- package/src/_stories/ChartBrush.Regression.stories.tsx +135 -0
- package/src/_stories/ChartEditor.Editor.stories.tsx +33 -3
- package/src/_stories/ChartLine.EffectCompatibility.stories.tsx +120 -0
- package/src/_stories/ChartPrefixSuffix.smoke.stories.tsx +28 -1
- package/src/_stories/_mock/annotation_event-line_mock.json +2 -2
- package/src/_stories/_mock/editor-test-configs.json +20 -10
- package/src/_stories/_mock/editor-test-datasets.json +38 -42
- package/src/_stories/_mock/heatmap-average-age-categorical.json +2 -2
- package/src/_stories/_mock/heatmap.json +2 -2
- package/src/_stories/_mock/legend-axis-align-stacked-bar.json +125 -0
- package/src/_stories/_mock/sankey_mock.json +23 -27
- package/src/components/Annotations/components/AnnotationDraggable.styles.css +52 -0
- package/src/components/Annotations/components/AnnotationDraggable.tsx +639 -139
- package/src/components/Annotations/components/AnnotationDropdown.styles.css +44 -4
- package/src/components/Annotations/components/AnnotationDropdown.tsx +10 -32
- package/src/components/Annotations/helpers/resolveAnnotationLayout.ts +212 -0
- package/src/components/Annotations/tests/AnnotationDraggable.test.tsx +612 -19
- package/src/components/Annotations/tests/AnnotationDropdown.test.tsx +50 -0
- package/src/components/Annotations/tests/resolveAnnotationLayout.test.ts +203 -0
- package/src/components/Axis/BottomAxis.tsx +1 -1
- package/src/components/Axis/Categorical.Axis.tsx +3 -3
- package/src/components/Axis/RightAxis.tsx +21 -13
- package/src/components/BarChart/components/BarChart.Horizontal.tsx +39 -8
- package/src/components/BarChart/components/BarChart.StackedHorizontal.tsx +13 -4
- package/src/components/BarChart/components/BarChart.StackedVertical.test.ts +28 -0
- package/src/components/BarChart/components/BarChart.StackedVertical.tsx +30 -26
- package/src/components/BarChart/components/BarChart.Vertical.tsx +38 -28
- package/src/components/BarChart/components/PortionPatternOverlay.tsx +61 -0
- package/src/components/BarChart/components/context.tsx +6 -0
- package/src/components/BarChart/helpers/getBarData.ts +28 -28
- package/src/components/BarChart/helpers/getBarHeights.ts +2 -2
- package/src/components/BarChart/helpers/getPatternUrl.ts +2 -5
- package/src/components/BarChart/helpers/index.ts +8 -0
- package/src/components/BarChart/helpers/portionPattern.ts +243 -0
- package/src/components/BarChart/helpers/tests/addMinimumBarHeights.test.ts +42 -0
- package/src/components/BarChart/helpers/tests/getPatternUrl.test.ts +13 -0
- package/src/components/BarChart/helpers/tests/portionPattern.test.ts +280 -0
- package/src/components/BarChart/helpers/tests/useBarChart.test.tsx +202 -0
- package/src/components/BarChart/helpers/useBarChart.ts +60 -60
- package/src/components/BarChartRace/BarChartRace.test.tsx +199 -0
- package/src/components/BarChartRace/BarChartRace.tsx +230 -0
- package/src/components/BarChartRace/BarChartRaceFallback.test.tsx +61 -0
- package/src/components/BarChartRace/BarChartRaceFallback.tsx +48 -0
- package/src/components/BarChartRace/bar-chart-race.css +160 -0
- package/src/components/BarChartRace/helpers.test.ts +114 -0
- package/src/components/BarChartRace/helpers.ts +185 -0
- package/src/components/BarChartRace/index.ts +3 -0
- package/src/components/BoxPlot/BoxPlot.Horizontal.tsx +7 -6
- package/src/components/BoxPlot/BoxPlot.Vertical.tsx +9 -7
- package/src/components/BoxPlot/helpers/index.ts +13 -83
- package/src/components/Brush/BrushSelector.tsx +10 -24
- package/src/components/Brush/MiniChartPreview.tsx +33 -1
- package/src/components/Dendrogram/components/Dendrogram.tsx +374 -0
- package/src/components/Dendrogram/dendrogram.css +113 -0
- package/src/components/Dendrogram/helpers/layoutDendrogram.test.ts +95 -0
- package/src/components/Dendrogram/helpers/layoutDendrogram.ts +56 -0
- package/src/components/Dendrogram/helpers/prepareDendrogramData.test.ts +135 -0
- package/src/components/Dendrogram/helpers/prepareDendrogramData.ts +166 -0
- package/src/components/Dendrogram/helpers/resolveDendrogramLabels.test.ts +196 -0
- package/src/components/Dendrogram/helpers/resolveDendrogramLabels.ts +418 -0
- package/src/components/Dendrogram/index.tsx +1 -0
- package/src/components/Dendrogram/types/index.ts +66 -0
- package/src/components/EditorPanel/EditorPanel.tsx +1111 -805
- package/src/components/EditorPanel/components/Panels/Panel.Annotate.tsx +50 -5
- package/src/components/EditorPanel/components/Panels/Panel.Dendrogram.css +10 -0
- package/src/components/EditorPanel/components/Panels/Panel.Dendrogram.tsx +211 -0
- package/src/components/EditorPanel/components/Panels/Panel.ForestPlotSettings.tsx +19 -8
- package/src/components/EditorPanel/components/Panels/Panel.General.tsx +104 -61
- package/src/components/EditorPanel/components/Panels/Panel.Network.css +9 -0
- package/src/components/EditorPanel/components/Panels/Panel.Network.tsx +202 -0
- package/src/components/EditorPanel/components/Panels/Panel.PatternSettings.tsx +236 -36
- package/src/components/EditorPanel/components/Panels/Panel.Radar.tsx +6 -1
- package/src/components/EditorPanel/components/Panels/Panel.Sankey.tsx +89 -105
- package/src/components/EditorPanel/components/Panels/Panel.Series.tsx +21 -6
- package/src/components/EditorPanel/components/Panels/Panel.SmallMultiples.tsx +1 -1
- package/src/components/EditorPanel/components/Panels/Panel.Visual.tsx +8 -66
- package/src/components/EditorPanel/components/Panels/RacingControls.test.tsx +131 -0
- package/src/components/EditorPanel/components/Panels/RacingControls.tsx +185 -0
- package/src/components/EditorPanel/components/Panels/SeriesColorAssignments.tsx +2 -2
- package/src/components/EditorPanel/components/Panels/index.tsx +4 -0
- package/src/components/EditorPanel/helpers/getVisualizationTypeConfigUpdate.ts +20 -7
- package/src/components/EditorPanel/helpers/tests/getVisualizationTypeConfigUpdate.test.ts +25 -2
- package/src/components/EditorPanel/helpers/updateFieldRankByValue.ts +5 -4
- package/src/components/EditorPanel/useEditorPermissions.test.tsx +94 -0
- package/src/components/EditorPanel/useEditorPermissions.ts +75 -25
- package/src/components/Forecasting/Forecasting.tsx +16 -7
- package/src/components/HeatMap/components/HeatMap.tsx +511 -178
- package/src/components/HeatMap/heatmap.constants.ts +7 -3
- package/src/components/HeatMap/heatmap.css +101 -2
- package/src/components/HeatMap/helpers/layout.ts +80 -0
- package/src/components/HeatMap/tests/HeatMap.test.tsx +339 -16
- package/src/components/HeatMap/tests/helpers.test.ts +79 -0
- package/src/components/Legend/Legend.Component.tsx +163 -148
- package/src/components/Legend/Legend.Suppression.tsx +3 -3
- package/src/components/Legend/LegendGroup/LegendGroup.styles.css +7 -0
- package/src/components/Legend/LegendGroup/LegendGroup.tsx +47 -35
- package/src/components/Legend/helpers/createFormatLabels.test.ts +226 -0
- package/src/components/Legend/helpers/createFormatLabels.tsx +45 -51
- package/src/components/LineChart/components/LineChart.Circle.tsx +3 -3
- package/src/components/LineChart/helpers.test.ts +126 -38
- package/src/components/LineChart/helpers.ts +9 -6
- package/src/components/LineChart/index.tsx +71 -27
- package/src/components/LineChart/tests/LineChart.test.tsx +630 -0
- package/src/components/LineChartRace/LineChartRace.test.tsx +275 -0
- package/src/components/LineChartRace/LineChartRace.tsx +134 -0
- package/src/components/LineChartRace/LineChartRaceFallback.tsx +28 -0
- package/src/components/LineChartRace/helpers.test.ts +88 -0
- package/src/components/LineChartRace/helpers.ts +157 -0
- package/src/components/LineChartRace/index.ts +3 -0
- package/src/components/LineChartRace/line-chart-race.css +16 -0
- package/src/components/LinearChart/ValueAxisAnchors.tsx +58 -0
- package/src/components/LinearChart/VisualizationRenderer.tsx +18 -23
- package/src/components/LinearChart/tests/LinearChart.test.tsx +412 -13
- package/src/components/LinearChart/tests/VisualizationRenderer.test.tsx +141 -0
- package/src/components/LinearChart/tests/mockConfigContext.ts +2 -1
- package/src/components/LinearChart.tsx +85 -54
- package/src/components/Network/components/Network.tsx +372 -0
- package/src/components/Network/helpers/layoutNetwork.test.ts +68 -0
- package/src/components/Network/helpers/layoutNetwork.ts +87 -0
- package/src/components/Network/helpers/prepareNetworkData.test.ts +116 -0
- package/src/components/Network/helpers/prepareNetworkData.ts +101 -0
- package/src/components/Network/helpers/resolveNetworkLabels.test.ts +112 -0
- package/src/components/Network/helpers/resolveNetworkLabels.ts +206 -0
- package/src/components/Network/index.tsx +3 -0
- package/src/components/Network/network.css +107 -0
- package/src/components/Network/types/index.ts +56 -0
- package/src/components/PairedBarChart.jsx +5 -10
- package/src/components/PairedBarChart.test.tsx +71 -0
- package/src/components/PieChart/PieChart.tsx +121 -13
- package/src/components/PieChart/helpers/labelPlacement.test.ts +88 -0
- package/src/components/PieChart/helpers/labelPlacement.ts +95 -0
- package/src/components/RacingChartRenderer.tsx +44 -0
- package/src/components/Regions/components/Regions.test.tsx +179 -0
- package/src/components/Regions/components/Regions.tsx +104 -69
- package/src/components/Regions/index.tsx +2 -3
- package/src/components/Sankey/components/Sankey.tsx +264 -437
- package/src/components/Sankey/helpers/getSankeyTooltip.ts +12 -0
- package/src/components/Sankey/helpers/prepareSankeyData.test.ts +71 -0
- package/src/components/Sankey/helpers/prepareSankeyData.ts +90 -0
- package/src/components/Sankey/sankey.scss +57 -116
- package/src/components/Sankey/types/index.ts +41 -4
- package/src/components/ScatterPlot/ScatterPlot.jsx +28 -12
- package/src/components/ScatterPlot/ScatterPlot.test.tsx +60 -0
- package/src/components/SmallMultiples/SmallMultiples.tsx +12 -12
- package/src/components/SmallMultiples/tests/SmallMultipleTile.test.tsx +7 -5
- package/src/components/raceTiming.ts +18 -0
- package/src/data/initial-state.js +79 -31
- package/src/helpers/applyChartTypeContract.ts +71 -0
- package/src/helpers/axisLabelFontSize.ts +2 -2
- package/src/helpers/boxPlotStats.ts +60 -0
- package/src/helpers/bumpChartTooltip.ts +2 -1
- package/src/helpers/calculateHorizontalBarCategoryLabelWidth.test.ts +37 -0
- package/src/helpers/calculateHorizontalBarCategoryLabelWidth.ts +3 -2
- package/src/helpers/calculateRightYAxisWidth.ts +60 -0
- package/src/helpers/categoryOrder.ts +60 -0
- package/src/helpers/colorAssignmentHelpers.ts +2 -2
- package/src/helpers/createNewChartConfig.ts +56 -0
- package/src/helpers/dataDrivenYAxisCategories.ts +18 -0
- package/src/helpers/forecastLegend.ts +88 -0
- package/src/helpers/getBoxPlotConfig.ts +26 -38
- package/src/helpers/getChartTypeDefaultPalette.ts +29 -0
- package/src/helpers/getColorCodeCategoryColorMap.ts +26 -0
- package/src/helpers/getColorScale.ts +31 -22
- package/src/helpers/getComboChartConfig.ts +0 -1
- package/src/helpers/getDynamicYAxisCategories.ts +69 -0
- package/src/helpers/getExcludedData.ts +0 -1
- package/src/helpers/getHasBoundarySuppression.ts +4 -3
- package/src/helpers/getMinMax.ts +11 -8
- package/src/helpers/getNewRuntime.ts +4 -3
- package/src/helpers/getPiePercent.ts +3 -3
- package/src/helpers/getStackedSeriesMax.ts +51 -0
- package/src/helpers/getV21ChartDistributionColors.ts +57 -0
- package/src/helpers/handleRankByValue.ts +15 -15
- package/src/helpers/isConvertLineToBarGraph.ts +2 -2
- package/src/helpers/seriesColumnSettings.ts +31 -3
- package/src/helpers/sizeHelpers.ts +4 -5
- package/src/helpers/smallMultiplesHelpers.ts +10 -13
- package/src/helpers/tests/applyChartTypeContract.test.ts +37 -0
- package/src/helpers/tests/boxPlotStats.test.ts +31 -0
- package/src/helpers/tests/bumpChartTooltip.test.ts +29 -0
- package/src/helpers/tests/calculateRightYAxisWidth.test.ts +105 -0
- package/src/helpers/tests/categoryOrder.test.ts +57 -0
- package/src/helpers/tests/countNumOfTicks.test.ts +11 -0
- package/src/helpers/tests/createNewChartConfig.test.ts +182 -0
- package/src/helpers/tests/getBoxPlotConfig.test.ts +70 -0
- package/src/helpers/tests/getColorCodeCategoryColorMap.test.ts +113 -0
- package/src/helpers/tests/getColorScale.test.ts +226 -1
- package/src/helpers/tests/getDynamicYAxisCategories.test.ts +61 -0
- package/src/helpers/tests/getMinMax.test.ts +41 -0
- package/src/helpers/tests/getStackedSeriesMax.test.ts +26 -0
- package/src/helpers/tests/handleRankByValue.test.ts +55 -37
- package/src/helpers/tests/seriesColumnSettings.test.ts +83 -0
- package/src/helpers/tests/smallMultiplesHelpers.test.ts +37 -1
- package/src/helpers/tests/tooltipHelpers.test.ts +80 -0
- package/src/helpers/tooltipHelpers.ts +16 -0
- package/src/hooks/tests/useRightAxis.test.tsx +143 -2
- package/src/hooks/tests/useScales.test.tsx +3 -6
- package/src/hooks/tests/useTooltip.test.tsx +484 -0
- package/src/hooks/usePrefersReducedMotion.ts +22 -0
- package/src/hooks/useReduceData.ts +4 -36
- package/src/hooks/useRightAxis.ts +19 -26
- package/src/hooks/useScales.ts +15 -0
- package/src/hooks/useTooltip.tsx +106 -71
- package/src/scss/main.scss +74 -2
- package/src/store/chart.reducer.ts +4 -4
- package/src/test/CdcChart.dataTable.test.tsx +582 -14
- package/src/test/CdcChart.dendrogram.test.tsx +277 -0
- package/src/test/CdcChart.finalization.test.tsx +143 -0
- package/src/test/CdcChart.network.test.tsx +271 -0
- package/src/types/ChartConfig.ts +91 -54
- package/src/types/ChartContext.ts +5 -0
- package/src/types/Horizon.ts +1 -1
- package/LICENSE +0 -201
- package/dist/cdcchart-BWVIUcAF.es.js +0 -4802
- package/src/_stories/Chart.Sankey.smoke.stories.tsx +0 -62
- package/src/components/Sankey/components/ColumnList.tsx +0 -19
- package/src/components/Sankey/helpers/getSankeyTooltip.tsx +0 -34
- package/src/components/Sankey/useSankeyAlert.tsx +0 -60
- package/src/data/legacy-defaults.ts +0 -18
- package/src/helpers/ensureSpecialChartAxisTypes.ts +0 -11
- package/src/helpers/tests/ensureSpecialChartAxisTypes.test.ts +0 -30
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
import { useCallback, useContext, useEffect, useId, useMemo, useRef, useState } from 'react'
|
|
2
|
+
import { Tooltip as ReactTooltip } from 'react-tooltip'
|
|
3
|
+
import 'react-tooltip/dist/react-tooltip.css'
|
|
4
|
+
|
|
5
|
+
import Button from '@cdc/core/components/elements/Button'
|
|
6
|
+
import { sanitizePaletteColor } from '@cdc/core/helpers/palettes/colorValidation'
|
|
7
|
+
|
|
8
|
+
import ConfigContext from '../../../ConfigContext'
|
|
9
|
+
import type { ChartContext } from '../../../types/ChartContext'
|
|
10
|
+
import { layoutDendrogram } from '../helpers/layoutDendrogram'
|
|
11
|
+
import { prepareDendrogramData } from '../helpers/prepareDendrogramData'
|
|
12
|
+
import { resolveDendrogramLabels } from '../helpers/resolveDendrogramLabels'
|
|
13
|
+
import type { DendrogramLayoutLink, DendrogramLayoutNode, DendrogramProps } from '../types'
|
|
14
|
+
import '../dendrogram.css'
|
|
15
|
+
|
|
16
|
+
type DendrogramView = {
|
|
17
|
+
scale: number
|
|
18
|
+
offsetX: number
|
|
19
|
+
offsetY: number
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const DEFAULT_VIEW: DendrogramView = { scale: 1, offsetX: 0, offsetY: 0 }
|
|
23
|
+
const LINK_KEY_HEIGHT = 32
|
|
24
|
+
const DEFAULT_LINK_STYLE_LABELS = { solid: 'Solid', dashed: 'Dashed' } as const
|
|
25
|
+
|
|
26
|
+
const Dendrogram = ({ data, width, height, runtime }: DendrogramProps) => {
|
|
27
|
+
const { config, handleChartAriaLabels } = useContext<ChartContext>(ConfigContext)
|
|
28
|
+
const dendrogram = config.dendrogram
|
|
29
|
+
const reactId = useId().replace(/:/g, '')
|
|
30
|
+
const parsedWidth = Number(width)
|
|
31
|
+
const parsedHeight = Number(dendrogram?.height)
|
|
32
|
+
const parsedParentHeight = Number(height)
|
|
33
|
+
const chartWidth = Math.max(Number.isFinite(parsedWidth) ? parsedWidth : 640, 160)
|
|
34
|
+
const chartHeight = Math.max(
|
|
35
|
+
Number.isFinite(parsedHeight) ? parsedHeight : Number.isFinite(parsedParentHeight) ? parsedParentHeight : 500,
|
|
36
|
+
160
|
|
37
|
+
)
|
|
38
|
+
const orientation = dendrogram?.orientation === 'vertical' ? 'vertical' : 'horizontal'
|
|
39
|
+
const connectionType = ['line', 'elbow'].includes(dendrogram?.connectionType || '')
|
|
40
|
+
? dendrogram!.connectionType
|
|
41
|
+
: 'curve'
|
|
42
|
+
const nodeRadius = Math.max(2, Number.isFinite(Number(dendrogram?.nodeRadius)) ? Number(dendrogram.nodeRadius) : 6)
|
|
43
|
+
const prepared = useMemo(() => prepareDendrogramData(data, dendrogram?.columns), [data, dendrogram?.columns])
|
|
44
|
+
const layout = useMemo(
|
|
45
|
+
() =>
|
|
46
|
+
layoutDendrogram(prepared, {
|
|
47
|
+
orientation,
|
|
48
|
+
leafSpacing: dendrogram?.leafSpacing ?? 40,
|
|
49
|
+
depthSpacing: dendrogram?.depthSpacing ?? 140,
|
|
50
|
+
nodeRadius
|
|
51
|
+
}),
|
|
52
|
+
[dendrogram?.depthSpacing, dendrogram?.leafSpacing, nodeRadius, orientation, prepared]
|
|
53
|
+
)
|
|
54
|
+
const linkStyleColumn = dendrogram?.columns?.style
|
|
55
|
+
const hasMappedLinkStyle = Boolean(
|
|
56
|
+
linkStyleColumn &&
|
|
57
|
+
Array.isArray(data) &&
|
|
58
|
+
data.some(row => row && typeof row === 'object' && !Array.isArray(row) && linkStyleColumn in row)
|
|
59
|
+
)
|
|
60
|
+
const renderedLinkStyles = useMemo(
|
|
61
|
+
() =>
|
|
62
|
+
hasMappedLinkStyle && layout.links.length
|
|
63
|
+
? (['solid', 'dashed'] as const).filter(style => layout.links.some(link => link.style === style))
|
|
64
|
+
: [],
|
|
65
|
+
[hasMappedLinkStyle, layout.links]
|
|
66
|
+
)
|
|
67
|
+
const plotHeight = chartHeight - (renderedLinkStyles.length ? LINK_KEY_HEIGHT : 0)
|
|
68
|
+
const resolvedLabels = useMemo(
|
|
69
|
+
() => resolveDendrogramLabels(layout.nodes, layout.links, { orientation, connectionType, nodeRadius }),
|
|
70
|
+
[connectionType, layout.links, layout.nodes, nodeRadius, orientation]
|
|
71
|
+
)
|
|
72
|
+
const labelsByNodeId = useMemo(
|
|
73
|
+
() => new Map(resolvedLabels.map(placement => [placement.nodeId, placement])),
|
|
74
|
+
[resolvedLabels]
|
|
75
|
+
)
|
|
76
|
+
const contentBounds = useMemo(() => {
|
|
77
|
+
if (!layout.nodes.length) return null
|
|
78
|
+
const extents = layout.nodes.reduce(
|
|
79
|
+
(result, node) => ({
|
|
80
|
+
minX: Math.min(result.minX, node.x - nodeRadius),
|
|
81
|
+
maxX: Math.max(result.maxX, node.x + nodeRadius),
|
|
82
|
+
minY: Math.min(result.minY, node.y - nodeRadius),
|
|
83
|
+
maxY: Math.max(result.maxY, node.y + nodeRadius)
|
|
84
|
+
}),
|
|
85
|
+
{ minX: Infinity, maxX: -Infinity, minY: Infinity, maxY: -Infinity }
|
|
86
|
+
)
|
|
87
|
+
resolvedLabels.forEach(({ bounds }) => {
|
|
88
|
+
extents.minX = Math.min(extents.minX, bounds.x)
|
|
89
|
+
extents.maxX = Math.max(extents.maxX, bounds.x + bounds.width)
|
|
90
|
+
extents.minY = Math.min(extents.minY, bounds.y)
|
|
91
|
+
extents.maxY = Math.max(extents.maxY, bounds.y + bounds.height)
|
|
92
|
+
})
|
|
93
|
+
const padding = 4
|
|
94
|
+
return {
|
|
95
|
+
minX: extents.minX - padding,
|
|
96
|
+
maxX: extents.maxX + padding,
|
|
97
|
+
minY: extents.minY - padding,
|
|
98
|
+
maxY: extents.maxY + padding
|
|
99
|
+
}
|
|
100
|
+
}, [layout.nodes, nodeRadius, resolvedLabels])
|
|
101
|
+
|
|
102
|
+
const constrainView = useCallback(
|
|
103
|
+
(nextView: DendrogramView): DendrogramView => {
|
|
104
|
+
if (!contentBounds) return DEFAULT_VIEW
|
|
105
|
+
const centerX = chartWidth / 2
|
|
106
|
+
const centerY = plotHeight / 2
|
|
107
|
+
const minX = centerX + (contentBounds.minX - centerX) * nextView.scale
|
|
108
|
+
const maxX = centerX + (contentBounds.maxX - centerX) * nextView.scale
|
|
109
|
+
const minY = centerY + (contentBounds.minY - centerY) * nextView.scale
|
|
110
|
+
const maxY = centerY + (contentBounds.maxY - centerY) * nextView.scale
|
|
111
|
+
const horizontalLimits = [chartWidth - maxX, -minX].sort((a, b) => a - b)
|
|
112
|
+
const verticalLimits = [plotHeight - maxY, -minY].sort((a, b) => a - b)
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
...nextView,
|
|
116
|
+
offsetX: Math.min(horizontalLimits[1], Math.max(horizontalLimits[0], nextView.offsetX)),
|
|
117
|
+
offsetY: Math.min(verticalLimits[1], Math.max(verticalLimits[0], nextView.offsetY))
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
[chartWidth, contentBounds, plotHeight]
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
const getAlignedView = useCallback((): DendrogramView => {
|
|
124
|
+
if (!contentBounds) return DEFAULT_VIEW
|
|
125
|
+
const offsetX = chartWidth / 2 - (contentBounds.minX + contentBounds.maxX) / 2
|
|
126
|
+
const offsetY = plotHeight / 2 - (contentBounds.minY + contentBounds.maxY) / 2
|
|
127
|
+
return constrainView({ ...DEFAULT_VIEW, offsetX, offsetY })
|
|
128
|
+
}, [chartWidth, constrainView, contentBounds, plotHeight])
|
|
129
|
+
|
|
130
|
+
const alignedView = useMemo(getAlignedView, [getAlignedView])
|
|
131
|
+
const [view, setView] = useState<DendrogramView>(alignedView)
|
|
132
|
+
const previousAlignedView = useRef(alignedView)
|
|
133
|
+
const dragStart = useRef<{ x: number; y: number; offsetX: number; offsetY: number } | null>(null)
|
|
134
|
+
|
|
135
|
+
useEffect(() => {
|
|
136
|
+
const previous = previousAlignedView.current
|
|
137
|
+
if (
|
|
138
|
+
previous.scale === alignedView.scale &&
|
|
139
|
+
previous.offsetX === alignedView.offsetX &&
|
|
140
|
+
previous.offsetY === alignedView.offsetY
|
|
141
|
+
)
|
|
142
|
+
return
|
|
143
|
+
|
|
144
|
+
previousAlignedView.current = alignedView
|
|
145
|
+
setView(alignedView)
|
|
146
|
+
}, [alignedView])
|
|
147
|
+
|
|
148
|
+
if (config.visualizationType !== 'Dendrogram') return null
|
|
149
|
+
|
|
150
|
+
if (!dendrogram?.columns?.node || !dendrogram?.columns?.parent) {
|
|
151
|
+
return (
|
|
152
|
+
<div className='dendrogram-chart__message'>Select Node ID and Parent ID columns to display the dendrogram.</div>
|
|
153
|
+
)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const selectedColumnsExist = [dendrogram.columns.node, dendrogram.columns.parent].every(column =>
|
|
157
|
+
Array.isArray(data)
|
|
158
|
+
? data.some(row => row && typeof row === 'object' && !Array.isArray(row) && column in row)
|
|
159
|
+
: false
|
|
160
|
+
)
|
|
161
|
+
if (!selectedColumnsExist) {
|
|
162
|
+
return (
|
|
163
|
+
<div className='dendrogram-chart__message'>
|
|
164
|
+
Select Node ID and Parent ID columns that exist in the imported data to display the dendrogram.
|
|
165
|
+
</div>
|
|
166
|
+
)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (!prepared.nodes.length) {
|
|
170
|
+
return (
|
|
171
|
+
<div className='dendrogram-chart__message'>
|
|
172
|
+
No valid hierarchy rows are available. Check the selected Node ID and Parent ID columns.
|
|
173
|
+
{prepared.rejectedRowCount > 0 && ` ${prepared.rejectedRowCount} row(s) were rejected.`}
|
|
174
|
+
</div>
|
|
175
|
+
)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (prepared.error) {
|
|
179
|
+
return (
|
|
180
|
+
<div className='dendrogram-chart__message' role='alert'>
|
|
181
|
+
{prepared.error.message}
|
|
182
|
+
{prepared.rejectedRowCount > 0 && ` ${prepared.rejectedRowCount} row(s) were rejected.`}
|
|
183
|
+
</div>
|
|
184
|
+
)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const instanceId = `${runtime?.uniqueId || 'chart'}-${reactId}`
|
|
188
|
+
const tooltipId = `cdc-open-viz-tooltip-${instanceId}-dendrogram`
|
|
189
|
+
const nodeColor = sanitizePaletteColor(dendrogram?.nodeColor) || '#005eaa'
|
|
190
|
+
const linkColor = sanitizePaletteColor(dendrogram?.linkColor) || '#333333'
|
|
191
|
+
const getLinkStyleLabel = (style: (typeof renderedLinkStyles)[number]) => {
|
|
192
|
+
const authoredLabel: unknown = dendrogram.linkStyleLabels?.[style]
|
|
193
|
+
return typeof authoredLabel === 'string' && authoredLabel.trim()
|
|
194
|
+
? authoredLabel.trim()
|
|
195
|
+
: DEFAULT_LINK_STYLE_LABELS[style]
|
|
196
|
+
}
|
|
197
|
+
const viewTransform = `translate(${chartWidth / 2 + view.offsetX} ${plotHeight / 2 + view.offsetY}) scale(${
|
|
198
|
+
view.scale
|
|
199
|
+
}) translate(${-chartWidth / 2} ${-plotHeight / 2})`
|
|
200
|
+
const childCounts = new Map<string, number>()
|
|
201
|
+
layout.nodes.forEach(node => {
|
|
202
|
+
if (node.parentId !== null) childCounts.set(node.parentId, (childCounts.get(node.parentId) || 0) + 1)
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
const zoomBy = (factor: number) =>
|
|
206
|
+
setView(current => constrainView({ ...current, scale: Math.min(4, Math.max(0.25, current.scale * factor)) }))
|
|
207
|
+
const resetView = () => setView(alignedView)
|
|
208
|
+
const handlePointerDown = (event: React.PointerEvent<SVGSVGElement>) => {
|
|
209
|
+
if (event.button !== 0) return
|
|
210
|
+
event.currentTarget.setPointerCapture(event.pointerId)
|
|
211
|
+
dragStart.current = { x: event.clientX, y: event.clientY, offsetX: view.offsetX, offsetY: view.offsetY }
|
|
212
|
+
}
|
|
213
|
+
const handlePointerMove = (event: React.PointerEvent<SVGSVGElement>) => {
|
|
214
|
+
if (!dragStart.current) return
|
|
215
|
+
const bounds = event.currentTarget.getBoundingClientRect()
|
|
216
|
+
const scaleX = bounds.width ? chartWidth / bounds.width : 1
|
|
217
|
+
const scaleY = bounds.height ? plotHeight / bounds.height : 1
|
|
218
|
+
setView(current =>
|
|
219
|
+
constrainView({
|
|
220
|
+
...current,
|
|
221
|
+
offsetX: dragStart.current!.offsetX + (event.clientX - dragStart.current!.x) * scaleX,
|
|
222
|
+
offsetY: dragStart.current!.offsetY + (event.clientY - dragStart.current!.y) * scaleY
|
|
223
|
+
})
|
|
224
|
+
)
|
|
225
|
+
}
|
|
226
|
+
const handlePointerUp = (event: React.PointerEvent<SVGSVGElement>) => {
|
|
227
|
+
dragStart.current = null
|
|
228
|
+
if (event.currentTarget.hasPointerCapture(event.pointerId))
|
|
229
|
+
event.currentTarget.releasePointerCapture(event.pointerId)
|
|
230
|
+
}
|
|
231
|
+
const getLinkPath = (link: DendrogramLayoutLink) => {
|
|
232
|
+
if (connectionType === 'line') {
|
|
233
|
+
return `M ${link.source.x} ${link.source.y} L ${link.target.x} ${link.target.y}`
|
|
234
|
+
}
|
|
235
|
+
if (orientation === 'vertical') {
|
|
236
|
+
const midpoint = (link.source.y + link.target.y) / 2
|
|
237
|
+
if (connectionType === 'elbow') {
|
|
238
|
+
return `M ${link.source.x} ${link.source.y} L ${link.source.x} ${midpoint} L ${link.target.x} ${midpoint} L ${link.target.x} ${link.target.y}`
|
|
239
|
+
}
|
|
240
|
+
return `M ${link.source.x} ${link.source.y} C ${link.source.x} ${midpoint}, ${link.target.x} ${midpoint}, ${link.target.x} ${link.target.y}`
|
|
241
|
+
}
|
|
242
|
+
const midpoint = (link.source.x + link.target.x) / 2
|
|
243
|
+
if (connectionType === 'elbow') {
|
|
244
|
+
return `M ${link.source.x} ${link.source.y} L ${midpoint} ${link.source.y} L ${midpoint} ${link.target.y} L ${link.target.x} ${link.target.y}`
|
|
245
|
+
}
|
|
246
|
+
return `M ${link.source.x} ${link.source.y} C ${midpoint} ${link.source.y}, ${midpoint} ${link.target.y}, ${link.target.x} ${link.target.y}`
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const renderLink = (link: DendrogramLayoutLink) => {
|
|
250
|
+
const label = `${link.source.label} to ${link.target.label}, ${link.style} link`
|
|
251
|
+
const path = getLinkPath(link)
|
|
252
|
+
return (
|
|
253
|
+
<g
|
|
254
|
+
key={link.id}
|
|
255
|
+
className='dendrogram-chart__link-group'
|
|
256
|
+
tabIndex={0}
|
|
257
|
+
role='img'
|
|
258
|
+
aria-label={label}
|
|
259
|
+
data-tooltip-id={config.enableTooltips ? tooltipId : undefined}
|
|
260
|
+
data-tooltip-content={config.enableTooltips ? label : undefined}
|
|
261
|
+
>
|
|
262
|
+
<path className='dendrogram-chart__link-hitbox' d={path} stroke='transparent' strokeWidth={12} />
|
|
263
|
+
<path
|
|
264
|
+
aria-hidden='true'
|
|
265
|
+
className={`dendrogram-chart__link${link.style === 'dashed' ? ' dendrogram-chart__link--dashed' : ''}`}
|
|
266
|
+
d={path}
|
|
267
|
+
stroke={linkColor}
|
|
268
|
+
/>
|
|
269
|
+
</g>
|
|
270
|
+
)
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const renderNode = (node: DendrogramLayoutNode) => {
|
|
274
|
+
const placement = labelsByNodeId.get(node.id)
|
|
275
|
+
const children = childCounts.get(node.id) || 0
|
|
276
|
+
const accessibleName = node.label === node.id ? node.label : `${node.label} (${node.id})`
|
|
277
|
+
const label = `${accessibleName}, depth ${node.depth}, ${children} ${children === 1 ? 'child' : 'children'}`
|
|
278
|
+
return (
|
|
279
|
+
<g
|
|
280
|
+
key={node.id}
|
|
281
|
+
className='dendrogram-chart__node'
|
|
282
|
+
transform={`translate(${node.x} ${node.y})`}
|
|
283
|
+
tabIndex={0}
|
|
284
|
+
role='img'
|
|
285
|
+
aria-label={label}
|
|
286
|
+
data-tooltip-id={config.enableTooltips ? tooltipId : undefined}
|
|
287
|
+
data-tooltip-content={config.enableTooltips ? label : undefined}
|
|
288
|
+
>
|
|
289
|
+
<circle r={nodeRadius} fill={node.color || nodeColor} />
|
|
290
|
+
{placement && (
|
|
291
|
+
<text
|
|
292
|
+
className={`dendrogram-chart__label dendrogram-chart__label--${placement.anchor}`}
|
|
293
|
+
x={placement.x}
|
|
294
|
+
y={placement.y}
|
|
295
|
+
textAnchor={placement.textAnchor}
|
|
296
|
+
dominantBaseline={placement.dominantBaseline}
|
|
297
|
+
>
|
|
298
|
+
{node.label}
|
|
299
|
+
</text>
|
|
300
|
+
)}
|
|
301
|
+
</g>
|
|
302
|
+
)
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
return (
|
|
306
|
+
<div className='dendrogram-chart__container' style={{ height: chartHeight }}>
|
|
307
|
+
{renderedLinkStyles.length > 0 && (
|
|
308
|
+
<div className='dendrogram-chart__link-key' role='group' aria-label='Link styles'>
|
|
309
|
+
{renderedLinkStyles.map(style => (
|
|
310
|
+
<div className='dendrogram-chart__link-key-item' key={style}>
|
|
311
|
+
<svg aria-hidden='true' width='36' height='12' viewBox='0 0 36 12'>
|
|
312
|
+
<line
|
|
313
|
+
className={`dendrogram-chart__link${style === 'dashed' ? ' dendrogram-chart__link--dashed' : ''}`}
|
|
314
|
+
x1='1'
|
|
315
|
+
x2='35'
|
|
316
|
+
y1='6'
|
|
317
|
+
y2='6'
|
|
318
|
+
stroke={linkColor}
|
|
319
|
+
/>
|
|
320
|
+
</svg>
|
|
321
|
+
<span>{getLinkStyleLabel(style)}</span>
|
|
322
|
+
</div>
|
|
323
|
+
))}
|
|
324
|
+
</div>
|
|
325
|
+
)}
|
|
326
|
+
<div className='dendrogram-chart__plot'>
|
|
327
|
+
{prepared.rejectedRowCount > 0 && (
|
|
328
|
+
<div className='dendrogram-chart__status' role='status'>
|
|
329
|
+
{prepared.rejectedRowCount} invalid row(s) excluded.
|
|
330
|
+
</div>
|
|
331
|
+
)}
|
|
332
|
+
<svg
|
|
333
|
+
className='dendrogram-chart'
|
|
334
|
+
width='100%'
|
|
335
|
+
height='100%'
|
|
336
|
+
viewBox={`0 0 ${chartWidth} ${plotHeight}`}
|
|
337
|
+
role='group'
|
|
338
|
+
aria-label={handleChartAriaLabels(config)}
|
|
339
|
+
onPointerDown={handlePointerDown}
|
|
340
|
+
onPointerMove={handlePointerMove}
|
|
341
|
+
onPointerUp={handlePointerUp}
|
|
342
|
+
onPointerCancel={handlePointerUp}
|
|
343
|
+
onLostPointerCapture={() => {
|
|
344
|
+
dragStart.current = null
|
|
345
|
+
}}
|
|
346
|
+
>
|
|
347
|
+
<g className='dendrogram-chart__viewport' transform={viewTransform}>
|
|
348
|
+
{layout.links.map(renderLink)}
|
|
349
|
+
{layout.nodes.map(renderNode)}
|
|
350
|
+
</g>
|
|
351
|
+
</svg>
|
|
352
|
+
<div
|
|
353
|
+
className='dendrogram-chart__controls'
|
|
354
|
+
role='group'
|
|
355
|
+
aria-label='Dendrogram view controls'
|
|
356
|
+
data-html2canvas-ignore='true'
|
|
357
|
+
>
|
|
358
|
+
<Button type='button' variant='light' size='sm' onClick={() => zoomBy(1.2)} aria-label='Zoom in'>
|
|
359
|
+
+
|
|
360
|
+
</Button>
|
|
361
|
+
<Button type='button' variant='light' size='sm' onClick={() => zoomBy(1 / 1.2)} aria-label='Zoom out'>
|
|
362
|
+
−
|
|
363
|
+
</Button>
|
|
364
|
+
<Button type='button' variant='light' size='sm' onClick={resetView} aria-label='Reset view'>
|
|
365
|
+
Reset
|
|
366
|
+
</Button>
|
|
367
|
+
</div>
|
|
368
|
+
{config.enableTooltips && <ReactTooltip id={tooltipId} variant='light' className='dendrogram-chart__tooltip' />}
|
|
369
|
+
</div>
|
|
370
|
+
</div>
|
|
371
|
+
)
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
export default Dendrogram
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
.dendrogram-chart__container {
|
|
2
|
+
display: flex;
|
|
3
|
+
flex-direction: column;
|
|
4
|
+
position: relative;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
.dendrogram-chart__plot {
|
|
8
|
+
flex: 1 1 auto;
|
|
9
|
+
min-height: 0;
|
|
10
|
+
position: relative;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
.dendrogram-chart {
|
|
14
|
+
cursor: grab;
|
|
15
|
+
display: block;
|
|
16
|
+
overflow: hidden;
|
|
17
|
+
touch-action: none;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
.dendrogram-chart:active {
|
|
21
|
+
cursor: grabbing;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
.dendrogram-chart__controls {
|
|
25
|
+
align-items: center;
|
|
26
|
+
bottom: 2em;
|
|
27
|
+
display: flex;
|
|
28
|
+
gap: 0.25rem;
|
|
29
|
+
left: 1em;
|
|
30
|
+
position: absolute;
|
|
31
|
+
z-index: 4;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
.dendrogram-chart__controls .cove-button {
|
|
35
|
+
min-height: 2rem;
|
|
36
|
+
min-width: 2rem;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
.dendrogram-chart__status {
|
|
40
|
+
background: rgb(255 255 255 / 90%);
|
|
41
|
+
color: #666;
|
|
42
|
+
left: 0;
|
|
43
|
+
padding: 0.25rem 0.5rem;
|
|
44
|
+
position: absolute;
|
|
45
|
+
top: 0;
|
|
46
|
+
z-index: 1;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
.dendrogram-chart__link {
|
|
50
|
+
fill: none;
|
|
51
|
+
stroke-width: 2;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
.dendrogram-chart__link-hitbox {
|
|
55
|
+
fill: none;
|
|
56
|
+
pointer-events: stroke;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
.dendrogram-chart__link-group,
|
|
60
|
+
.dendrogram-chart__node {
|
|
61
|
+
outline: none;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
.dendrogram-chart__link-group:focus-visible,
|
|
65
|
+
.dendrogram-chart__node:focus-visible {
|
|
66
|
+
filter: drop-shadow(0 0 2px #fff) drop-shadow(0 0 4px #005ea8);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
.dendrogram-chart__link--dashed {
|
|
70
|
+
stroke-dasharray: 12 8;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
.dendrogram-chart__label {
|
|
74
|
+
fill: currentcolor;
|
|
75
|
+
font-size: 16px;
|
|
76
|
+
pointer-events: none;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
.dendrogram-chart__link-key {
|
|
80
|
+
align-items: center;
|
|
81
|
+
display: flex;
|
|
82
|
+
flex: 0 0 32px;
|
|
83
|
+
font-size: 12px;
|
|
84
|
+
gap: 1rem;
|
|
85
|
+
justify-content: flex-end;
|
|
86
|
+
min-width: 0;
|
|
87
|
+
overflow-x: auto;
|
|
88
|
+
overflow-y: hidden;
|
|
89
|
+
padding: 0 0.5rem;
|
|
90
|
+
white-space: nowrap;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
.dendrogram-chart__link-key-item {
|
|
94
|
+
align-items: center;
|
|
95
|
+
display: flex;
|
|
96
|
+
flex: 0 0 auto;
|
|
97
|
+
gap: 0.35rem;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
.dendrogram-chart__message {
|
|
101
|
+
color: #666;
|
|
102
|
+
padding: 2rem;
|
|
103
|
+
text-align: center;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
.dendrogram-chart__tooltip {
|
|
107
|
+
--rt-opacity: 1;
|
|
108
|
+
|
|
109
|
+
background: #fff;
|
|
110
|
+
border: 1px solid #bdbdbd;
|
|
111
|
+
box-shadow: 0 3px 10px rgb(0 0 0 / 20%);
|
|
112
|
+
color: var(--cool-gray-90, #1b1b1b);
|
|
113
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { layoutDendrogram } from './layoutDendrogram'
|
|
2
|
+
import { prepareDendrogramData } from './prepareDendrogramData'
|
|
3
|
+
|
|
4
|
+
describe('layoutDendrogram', () => {
|
|
5
|
+
const prepared = prepareDendrogramData(
|
|
6
|
+
[
|
|
7
|
+
{ id: 'root', parentId: '' },
|
|
8
|
+
{ id: 'branch-b', parentId: 'root' },
|
|
9
|
+
{ id: 'branch-a', parentId: 'root' },
|
|
10
|
+
{ id: 'leaf', parentId: 'branch-a', linkStyle: 'dashed' }
|
|
11
|
+
],
|
|
12
|
+
{ node: 'id', parent: 'parentId', style: 'linkStyle' }
|
|
13
|
+
)
|
|
14
|
+
const options = { orientation: 'horizontal' as const, leafSpacing: 50, depthSpacing: 100, nodeRadius: 6 }
|
|
15
|
+
|
|
16
|
+
it('produces stable deterministic coordinates and one link per non-root node', () => {
|
|
17
|
+
const first = layoutDendrogram(prepared, options)
|
|
18
|
+
const second = layoutDendrogram(prepared, options)
|
|
19
|
+
|
|
20
|
+
expect(second).toEqual(first)
|
|
21
|
+
expect(first.nodes).toHaveLength(4)
|
|
22
|
+
expect(first.links).toHaveLength(3)
|
|
23
|
+
expect(first.nodes.find(node => node.id === 'root')?.depth).toBe(0)
|
|
24
|
+
expect(first.nodes.find(node => node.id === 'leaf')?.depth).toBe(2)
|
|
25
|
+
expect(first.links.find(link => link.target.id === 'leaf')?.style).toBe('dashed')
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
it('uses depth on the x-axis horizontally and on the y-axis vertically', () => {
|
|
29
|
+
const horizontal = layoutDendrogram(prepared, options)
|
|
30
|
+
const vertical = layoutDendrogram(prepared, { ...options, orientation: 'vertical' })
|
|
31
|
+
const horizontalRoot = horizontal.nodes.find(node => node.id === 'root')!
|
|
32
|
+
const horizontalLeaf = horizontal.nodes.find(node => node.id === 'leaf')!
|
|
33
|
+
const verticalRoot = vertical.nodes.find(node => node.id === 'root')!
|
|
34
|
+
const verticalLeaf = vertical.nodes.find(node => node.id === 'leaf')!
|
|
35
|
+
|
|
36
|
+
expect(horizontalLeaf.x - horizontalRoot.x).toBe(200)
|
|
37
|
+
expect(verticalLeaf.y - verticalRoot.y).toBe(200)
|
|
38
|
+
expect(verticalLeaf.x).toBe(horizontalLeaf.y)
|
|
39
|
+
expect(verticalLeaf.y).toBe(horizontalLeaf.x)
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
it('honors leaf and depth spacing while clamping unsafe values', () => {
|
|
43
|
+
const regular = layoutDendrogram(prepared, options)
|
|
44
|
+
const expanded = layoutDendrogram(prepared, { ...options, leafSpacing: 80, depthSpacing: 140 })
|
|
45
|
+
const clamped = layoutDendrogram(prepared, { ...options, leafSpacing: 0, depthSpacing: 0, nodeRadius: 0 })
|
|
46
|
+
|
|
47
|
+
expect(Math.max(...expanded.nodes.map(node => node.x))).toBeGreaterThan(
|
|
48
|
+
Math.max(...regular.nodes.map(node => node.x))
|
|
49
|
+
)
|
|
50
|
+
expect(Math.max(...expanded.nodes.map(node => node.y))).toBeGreaterThan(
|
|
51
|
+
Math.max(...regular.nodes.map(node => node.y))
|
|
52
|
+
)
|
|
53
|
+
expect(Math.min(...clamped.nodes.map(node => node.x))).toBeGreaterThanOrEqual(14)
|
|
54
|
+
expect(Math.min(...clamped.nodes.map(node => node.y))).toBeGreaterThanOrEqual(14)
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it('handles a one-node hierarchy and returns no layout for invalid input', () => {
|
|
58
|
+
const oneNode = prepareDendrogramData([{ id: 'root', parentId: '' }], { node: 'id', parent: 'parentId' })
|
|
59
|
+
const invalid = prepareDendrogramData(
|
|
60
|
+
[
|
|
61
|
+
{ id: 'root', parentId: '' },
|
|
62
|
+
{ id: 'child', parentId: 'missing' }
|
|
63
|
+
],
|
|
64
|
+
{ node: 'id', parent: 'parentId' }
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
expect(layoutDendrogram(oneNode, options)).toEqual({
|
|
68
|
+
nodes: [expect.objectContaining({ id: 'root', x: 18, y: 18 })],
|
|
69
|
+
links: []
|
|
70
|
+
})
|
|
71
|
+
expect(layoutDendrogram(invalid, options)).toEqual({ nodes: [], links: [] })
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
it('does not mutate prepared hierarchy data', () => {
|
|
75
|
+
const before = structuredClone(prepared)
|
|
76
|
+
layoutDendrogram(prepared, options)
|
|
77
|
+
expect(prepared).toEqual(before)
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it('preserves authored sibling order and safely lays out wide hierarchies', () => {
|
|
81
|
+
const ordered = layoutDendrogram(prepared, options)
|
|
82
|
+
expect(ordered.nodes.find(node => node.id === 'branch-b')!.y).toBeLessThan(
|
|
83
|
+
ordered.nodes.find(node => node.id === 'branch-a')!.y
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
const wide = prepareDendrogramData(
|
|
87
|
+
[
|
|
88
|
+
{ id: 'root', parentId: '' },
|
|
89
|
+
...Array.from({ length: 10000 }, (_, i) => ({ id: `leaf-${i}`, parentId: 'root' }))
|
|
90
|
+
],
|
|
91
|
+
{ node: 'id', parent: 'parentId' }
|
|
92
|
+
)
|
|
93
|
+
expect(() => layoutDendrogram(wide, { ...options, leafSpacing: Number.MAX_VALUE })).not.toThrow()
|
|
94
|
+
})
|
|
95
|
+
})
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { stratify } from '@visx/hierarchy'
|
|
2
|
+
import { cluster } from 'd3-hierarchy'
|
|
3
|
+
|
|
4
|
+
import type { DendrogramLayout, DendrogramLayoutNode, DendrogramOrientation, DendrogramPreparedData } from '../types'
|
|
5
|
+
|
|
6
|
+
type LayoutOptions = {
|
|
7
|
+
orientation: DendrogramOrientation
|
|
8
|
+
leafSpacing: number
|
|
9
|
+
depthSpacing: number
|
|
10
|
+
nodeRadius: number
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const getPositiveNumber = (value: unknown, fallback: number, minimum: number, maximum: number) => {
|
|
14
|
+
const parsed = Number(value)
|
|
15
|
+
return Math.min(maximum, Math.max(minimum, Number.isFinite(parsed) ? parsed : fallback))
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const layoutDendrogram = (prepared: DendrogramPreparedData, options: LayoutOptions): DendrogramLayout => {
|
|
19
|
+
if (!prepared.rootId || prepared.error || !prepared.nodes.length) return { nodes: [], links: [] }
|
|
20
|
+
|
|
21
|
+
const leafSpacing = getPositiveNumber(options.leafSpacing, 40, 12, 10000)
|
|
22
|
+
const depthSpacing = getPositiveNumber(options.depthSpacing, 140, 24, 10000)
|
|
23
|
+
const nodeRadius = getPositiveNumber(options.nodeRadius, 6, 2, 1000)
|
|
24
|
+
const root = stratify<(typeof prepared.nodes)[number]>()
|
|
25
|
+
.id(node => node.id)
|
|
26
|
+
.parentId(node => node.parentId)(prepared.nodes)
|
|
27
|
+
|
|
28
|
+
cluster<(typeof prepared.nodes)[number]>().nodeSize([leafSpacing, depthSpacing])(root)
|
|
29
|
+
|
|
30
|
+
const descendants = root.descendants()
|
|
31
|
+
const minimumCrossAxis = descendants.reduce((minimum, node) => Math.min(minimum, node.x), Infinity)
|
|
32
|
+
const padding = nodeRadius + 12
|
|
33
|
+
const nodes = descendants.map(node => {
|
|
34
|
+
const depthPosition = node.y + padding
|
|
35
|
+
const leafPosition = node.x - minimumCrossAxis + padding
|
|
36
|
+
const x = options.orientation === 'vertical' ? leafPosition : depthPosition
|
|
37
|
+
const y = options.orientation === 'vertical' ? depthPosition : leafPosition
|
|
38
|
+
return {
|
|
39
|
+
...node.data,
|
|
40
|
+
depth: node.depth,
|
|
41
|
+
x,
|
|
42
|
+
y
|
|
43
|
+
} satisfies DendrogramLayoutNode
|
|
44
|
+
})
|
|
45
|
+
const nodesById = new Map(nodes.map(node => [node.id, node]))
|
|
46
|
+
const links = nodes
|
|
47
|
+
.filter(node => node.parentId !== null)
|
|
48
|
+
.map(node => ({
|
|
49
|
+
id: JSON.stringify([node.parentId, node.id]),
|
|
50
|
+
source: nodesById.get(node.parentId!)!,
|
|
51
|
+
target: node,
|
|
52
|
+
style: node.style
|
|
53
|
+
}))
|
|
54
|
+
|
|
55
|
+
return { nodes, links }
|
|
56
|
+
}
|