@d3plus/core 4.11.0 → 4.12.0
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/es/internal.js +15 -5
- package/es/src/charts/Alluvial/emit.js +2 -2
- package/es/src/charts/Alluvial/index.js +7 -1
- package/es/src/charts/Alluvial/titles.js +24 -4
- package/es/src/charts/ArcDiagram/emit.js +27 -13
- package/es/src/charts/ArcDiagram/events.js +6 -2
- package/es/src/charts/ArcDiagram/index.js +2 -0
- package/es/src/charts/BarChart/index.js +3 -10
- package/es/src/charts/Calendar/index.js +2 -0
- package/es/src/charts/Candlestick/index.js +4 -10
- package/es/src/charts/Gantt/applyLayout.js +2 -4
- package/es/src/charts/Geomap/flowEmit.js +11 -6
- package/es/src/charts/Hexbin/index.js +2 -0
- package/es/src/charts/Icicle/index.js +3 -1
- package/es/src/charts/Matrix/applyLayout.js +20 -9
- package/es/src/charts/Matrix/index.js +19 -1
- package/es/src/charts/Network/applyLayout.js +36 -5
- package/es/src/charts/Network/bundle.js +5 -4
- package/es/src/charts/Network/emit.js +39 -40
- package/es/src/charts/Network/graph.js +34 -5
- package/es/src/charts/Network/graphFluent.js +5 -3
- package/es/src/charts/Network/index.js +6 -1
- package/es/src/charts/Pack/applyLayout.js +16 -5
- package/es/src/charts/Pack/emit.js +15 -8
- package/es/src/charts/Pack/groupRows.js +61 -0
- package/es/src/charts/Pack/index.js +3 -0
- package/es/src/charts/Pack/neutralPaint.js +20 -0
- package/es/src/charts/Pack/nodeNames.js +35 -0
- package/es/src/charts/ParallelCoordinates/axes.js +22 -22
- package/es/src/charts/ParallelCoordinates/chrome.js +18 -0
- package/es/src/charts/ParallelCoordinates/index.js +3 -1
- package/es/src/charts/ParallelCoordinates/titles.js +17 -9
- package/es/src/charts/Pie/emit.js +9 -1
- package/es/src/charts/Pie/index.js +10 -0
- package/es/src/charts/Plot/areaEnter.js +68 -0
- package/es/src/charts/Plot/draw.js +26 -12
- package/es/src/charts/Plot/index.js +4 -10
- package/es/src/charts/Plot/lineMarkerKeys.js +19 -0
- package/es/src/charts/Plot/plotInit.js +6 -0
- package/es/src/charts/Plot/shapeLabelLegend.js +67 -0
- package/es/src/charts/Plot/sharedHover.js +2 -1
- package/es/src/charts/Plot/sharedTooltip.js +13 -3
- package/es/src/charts/PolarArea/index.js +2 -1
- package/es/src/charts/Priestley/applyLayout.js +11 -7
- package/es/src/charts/Priestley/index.js +0 -2
- package/es/src/charts/Priestley/timeAxis.js +11 -8
- package/es/src/charts/Pyramid/gutter.js +52 -16
- package/es/src/charts/Pyramid/index.js +55 -7
- package/es/src/charts/Pyramid/scene.js +4 -4
- package/es/src/charts/Radar/applyLayout.js +31 -33
- package/es/src/charts/Radar/axisDecorations.js +29 -17
- package/es/src/charts/Radar/curvedLabels.js +43 -2
- package/es/src/charts/Radar/emit.js +30 -15
- package/es/src/charts/Radar/index.js +41 -2
- package/es/src/charts/Radar/points.js +85 -0
- package/es/src/charts/RadialBar/emit.js +6 -1
- package/es/src/charts/RadialBar/index.js +10 -0
- package/es/src/charts/RadialMatrix/applyLayout.js +115 -31
- package/es/src/charts/RadialMatrix/index.js +15 -2
- package/es/src/charts/Rings/applyLayout.js +11 -150
- package/es/src/charts/Rings/emit.js +80 -40
- package/es/src/charts/Rings/index.js +27 -4
- package/es/src/charts/Rings/ringsCurves.js +156 -0
- package/es/src/charts/Rings/ringsParallel.js +20 -3
- package/es/src/charts/Rings/ringsPlacement.js +152 -0
- package/es/src/charts/Rings/ringsSizing.js +61 -38
- package/es/src/charts/Sankey/applyLayout.js +18 -4
- package/es/src/charts/Sankey/index.js +7 -1
- package/es/src/charts/Sunburst/emit.js +9 -1
- package/es/src/charts/Sunburst/index.js +13 -1
- package/es/src/charts/Tree/applyLayout.js +44 -14
- package/es/src/charts/Tree/emit.js +5 -5
- package/es/src/charts/Tree/index.js +10 -12
- package/es/src/charts/Tree/keys.js +33 -0
- package/es/src/charts/Tree/nodeLabel.js +15 -0
- package/es/src/charts/Treemap/emitGroupHeaders.js +3 -5
- package/es/src/charts/definition/makeChart.js +6 -2
- package/es/src/charts/drawSteps/minimap.js +4 -20
- package/es/src/charts/drawSteps/zoomActions.js +36 -0
- package/es/src/charts/drawSteps/zoomControls.js +31 -18
- package/es/src/charts/drawSteps/zoomControlsMarkup.js +23 -4
- package/es/src/charts/drawSteps/zoomDrag.js +188 -0
- package/es/src/charts/drawSteps/zoomFit.js +69 -0
- package/es/src/charts/events/click.legend.js +4 -2
- package/es/src/charts/events/click.shape.js +5 -3
- package/es/src/charts/events/mouseenter.js +6 -16
- package/es/src/charts/events/mouseleave.js +9 -2
- package/es/src/charts/events/mousemove.legend.js +28 -32
- package/es/src/charts/events/mousemove.shape.js +44 -44
- package/es/src/charts/features/axes.js +15 -4
- package/es/src/charts/features/axesFinal.js +7 -2
- package/es/src/charts/features/axesMeasure.js +26 -8
- package/es/src/charts/features/axisTooltip.js +138 -0
- package/es/src/charts/features/centerLabel.js +177 -0
- package/es/src/charts/features/colorScaleBivariate.js +83 -0
- package/es/src/charts/features/colorScaleBucket.js +18 -0
- package/es/src/charts/features/colorScaleHover.js +30 -5
- package/es/src/charts/features/colorScaleLimit.js +73 -0
- package/es/src/charts/features/colorScaleTooltip.js +77 -0
- package/es/src/charts/features/colorScaleTop.js +61 -0
- package/es/src/charts/features/curvedArrows.js +33 -0
- package/es/src/charts/features/depthKey.js +26 -0
- package/es/src/charts/features/edgeArrows.js +61 -2
- package/es/src/charts/features/featuresColorScale.js +57 -16
- package/es/src/charts/features/hoverTicks.js +41 -0
- package/es/src/charts/features/idleQueue.js +72 -0
- package/es/src/charts/features/linkInteraction.js +59 -7
- package/es/src/charts/features/networkMeasures.js +241 -0
- package/es/src/charts/features/nodeStats.js +309 -0
- package/es/src/charts/features/plotEndLabels.js +28 -7
- package/es/src/charts/features/plotPaint.js +9 -2
- package/es/src/charts/features/shapeEmit.js +7 -1
- package/es/src/charts/features/textHalo.js +3 -2
- package/es/src/charts/partition/chartFields.js +12 -3
- package/es/src/charts/partition/interaction.js +10 -5
- package/es/src/charts/pipeline/insetPlacement.js +8 -3
- package/es/src/charts/polar/chrome.js +81 -9
- package/es/src/charts/polar/config.js +4 -0
- package/es/src/charts/polar/wedgeLayout.js +2 -1
- package/es/src/charts/viz/Viz.js +24 -15
- package/es/src/charts/viz/VizBase.js +39 -4
- package/es/src/charts/viz/backgroundInk.js +10 -0
- package/es/src/charts/viz/emphasis.js +14 -0
- package/es/src/charts/viz/fillStroke.js +10 -0
- package/es/src/charts/viz/interactionOpacity.js +115 -32
- package/es/src/charts/viz/reportDrawError.js +15 -0
- package/es/src/charts/viz/setCursor.js +29 -0
- package/es/src/charts/viz/tooltipEnabled.js +38 -0
- package/es/src/charts/viz/userClick.js +34 -0
- package/es/src/charts/viz/vizDataPolling.js +0 -1
- package/es/src/charts/viz/vizDefaults.js +13 -34
- package/es/src/charts/viz/vizFields.js +6 -1
- package/es/src/charts/viz/vizRender.js +35 -5
- package/es/src/charts/viz/vizVisibility.js +12 -2
- package/es/src/components/Axis/Axis.js +29 -2
- package/es/src/components/Axis/axisBreak.js +29 -7
- package/es/src/components/Axis/axisDefaults.js +11 -0
- package/es/src/components/Axis/axisLabelKinds.js +36 -0
- package/es/src/components/Axis/axisLabelLineHeight.js +19 -0
- package/es/src/components/Axis/axisLabelRoom.js +37 -0
- package/es/src/components/Axis/axisLabelSize.js +171 -0
- package/es/src/components/Axis/axisLabelThinning.js +98 -4
- package/es/src/components/Axis/axisLayout.js +71 -46
- package/es/src/components/Axis/axisLayoutLabels.js +82 -140
- package/es/src/components/Axis/axisLayoutScale.js +9 -10
- package/es/src/components/Axis/axisLimit.js +27 -0
- package/es/src/components/Axis/axisMaxSize.js +39 -0
- package/es/src/components/Axis/axisRender.js +20 -8
- package/es/src/components/Axis/axisRounding.js +16 -1
- package/es/src/components/Axis/axisScene.js +11 -2
- package/es/src/components/Axis/axisTitle.js +53 -0
- package/es/src/components/Axis/axisTooltipTargets.js +128 -0
- package/es/src/components/ColorScale/ColorScale.js +62 -18
- package/es/src/components/ColorScale/bivariateRender.js +353 -0
- package/es/src/components/ColorScale/bivariateScale.js +150 -0
- package/es/src/components/ColorScale/colorScaleRender.js +32 -2
- package/es/src/components/ColorScale/colorScaleSwatchTooltips.js +77 -0
- package/es/src/components/Legend/Legend.js +10 -2
- package/es/src/components/Legend/legendCut.js +108 -0
- package/es/src/components/Legend/legendRender.js +38 -13
- package/es/src/components/TextBox.js +5 -2
- package/es/src/components/Timeline/timelineRender.js +2 -1
- package/es/src/components/textBoxScene.js +3 -0
- package/es/src/components/textEllipsis.js +7 -0
- package/es/src/components/tooltipDom.js +8 -5
- package/es/src/fluent.js +48 -0
- package/es/src/utils/BaseClass.js +7 -2
- package/es/src/utils/configValue.js +9 -0
- package/package.json +10 -10
- package/types/index.d.ts +1 -1
- package/types/internal.d.ts +22 -5
- package/types/src/charts/Alluvial/titles.d.ts +16 -3
- package/types/src/charts/ArcDiagram/emit.d.ts +3 -2
- package/types/src/charts/ArcDiagram/events.d.ts +2 -1
- package/types/src/charts/Geomap/flowEmit.d.ts +3 -2
- package/types/src/charts/Matrix/applyLayout.d.ts +4 -2
- package/types/src/charts/Network/applyLayout.d.ts +11 -0
- package/types/src/charts/Network/bundle.d.ts +3 -1
- package/types/src/charts/Network/graph.d.ts +12 -4
- package/types/src/charts/Pack/applyLayout.d.ts +2 -0
- package/types/src/charts/Pack/groupRows.d.ts +46 -0
- package/types/src/charts/Pack/index.d.ts +3 -0
- package/types/src/charts/Pack/neutralPaint.d.ts +14 -0
- package/types/src/charts/Pack/nodeNames.d.ts +23 -0
- package/types/src/charts/ParallelCoordinates/chrome.d.ts +12 -0
- package/types/src/charts/ParallelCoordinates/index.d.ts +1 -0
- package/types/src/charts/ParallelCoordinates/titles.d.ts +7 -2
- package/types/src/charts/Plot/areaEnter.d.ts +34 -0
- package/types/src/charts/Plot/index.d.ts +4 -10
- package/types/src/charts/Plot/lineMarkerKeys.d.ts +11 -0
- package/types/src/charts/Plot/shapeLabelLegend.d.ts +19 -0
- package/types/src/charts/Plot/sharedTooltip.d.ts +7 -1
- package/types/src/charts/Priestley/timeAxis.d.ts +7 -4
- package/types/src/charts/Pyramid/gutter.d.ts +28 -6
- package/types/src/charts/Pyramid/scene.d.ts +1 -1
- package/types/src/charts/Radar/applyLayout.d.ts +2 -3
- package/types/src/charts/Radar/axisDecorations.d.ts +5 -2
- package/types/src/charts/Radar/curvedLabels.d.ts +35 -0
- package/types/src/charts/Radar/emit.d.ts +5 -2
- package/types/src/charts/Radar/index.d.ts +7 -2
- package/types/src/charts/Radar/points.d.ts +50 -0
- package/types/src/charts/RadialMatrix/applyLayout.d.ts +6 -4
- package/types/src/charts/Rings/emit.d.ts +24 -2
- package/types/src/charts/Rings/index.d.ts +4 -2
- package/types/src/charts/Rings/ringsCurves.d.ts +50 -0
- package/types/src/charts/Rings/ringsGraph.d.ts +29 -5
- package/types/src/charts/Rings/ringsParallel.d.ts +4 -2
- package/types/src/charts/Rings/ringsPlacement.d.ts +47 -0
- package/types/src/charts/Rings/ringsSizing.d.ts +36 -9
- package/types/src/charts/Tree/applyLayout.d.ts +20 -0
- package/types/src/charts/Tree/keys.d.ts +25 -0
- package/types/src/charts/Tree/nodeLabel.d.ts +11 -0
- package/types/src/charts/definition/ChartDefinition.d.ts +13 -0
- package/types/src/charts/definition/makeChart.d.ts +2 -2
- package/types/src/charts/drawSteps/zoomActions.d.ts +30 -0
- package/types/src/charts/drawSteps/zoomControls.d.ts +15 -0
- package/types/src/charts/drawSteps/zoomControlsMarkup.d.ts +16 -1
- package/types/src/charts/drawSteps/zoomDrag.d.ts +71 -0
- package/types/src/charts/drawSteps/zoomFit.d.ts +30 -0
- package/types/src/charts/features/axisTooltip.d.ts +49 -0
- package/types/src/charts/features/centerLabel.d.ts +75 -0
- package/types/src/charts/features/colorScaleBivariate.d.ts +50 -0
- package/types/src/charts/features/colorScaleBucket.d.ts +10 -0
- package/types/src/charts/features/colorScaleHover.d.ts +13 -3
- package/types/src/charts/features/colorScaleLimit.d.ts +60 -0
- package/types/src/charts/features/colorScaleTooltip.d.ts +43 -0
- package/types/src/charts/features/colorScaleTop.d.ts +30 -0
- package/types/src/charts/features/curvedArrows.d.ts +14 -0
- package/types/src/charts/features/depthKey.d.ts +19 -0
- package/types/src/charts/features/edgeArrows.d.ts +29 -2
- package/types/src/charts/features/hoverTicks.d.ts +30 -0
- package/types/src/charts/features/idleQueue.d.ts +46 -0
- package/types/src/charts/features/linkInteraction.d.ts +13 -4
- package/types/src/charts/features/networkMeasures.d.ts +53 -0
- package/types/src/charts/features/nodeStats.d.ts +93 -0
- package/types/src/charts/features/plotEndLabels.d.ts +14 -1
- package/types/src/charts/features/textHalo.d.ts +4 -3
- package/types/src/charts/partition/chartFields.d.ts +6 -1
- package/types/src/charts/pipeline/insetPlacement.d.ts +6 -0
- package/types/src/charts/polar/chrome.d.ts +19 -6
- package/types/src/charts/viz/Viz.d.ts +10 -3
- package/types/src/charts/viz/VizBase.d.ts +31 -2
- package/types/src/charts/viz/backgroundInk.d.ts +8 -0
- package/types/src/charts/viz/emphasis.d.ts +17 -0
- package/types/src/charts/viz/fillStroke.d.ts +7 -0
- package/types/src/charts/viz/reportDrawError.d.ts +12 -0
- package/types/src/charts/viz/setCursor.d.ts +26 -0
- package/types/src/charts/viz/tooltipEnabled.d.ts +31 -0
- package/types/src/charts/viz/userClick.d.ts +35 -0
- package/types/src/charts/viz/vizFields.d.ts +5 -0
- package/types/src/charts/viz/vizTypes.d.ts +16 -0
- package/types/src/charts/viz/vizVisibility.d.ts +7 -0
- package/types/src/components/Axis/Axis.d.ts +33 -3
- package/types/src/components/Axis/axisBreak.d.ts +19 -3
- package/types/src/components/Axis/axisDefaults.d.ts +12 -0
- package/types/src/components/Axis/axisLabelKinds.d.ts +22 -0
- package/types/src/components/Axis/axisLabelLineHeight.d.ts +27 -0
- package/types/src/components/Axis/axisLabelRoom.d.ts +17 -0
- package/types/src/components/Axis/axisLabelSize.d.ts +82 -0
- package/types/src/components/Axis/axisLabelThinning.d.ts +38 -2
- package/types/src/components/Axis/axisLayout.d.ts +9 -2
- package/types/src/components/Axis/axisLayoutLabels.d.ts +28 -33
- package/types/src/components/Axis/axisLayoutScale.d.ts +3 -1
- package/types/src/components/Axis/axisLimit.d.ts +18 -0
- package/types/src/components/Axis/axisMaxSize.d.ts +48 -0
- package/types/src/components/Axis/axisRounding.d.ts +8 -0
- package/types/src/components/Axis/axisTitle.d.ts +30 -0
- package/types/src/components/Axis/axisTooltipTargets.d.ts +82 -0
- package/types/src/components/ColorScale/ColorScale.d.ts +21 -11
- package/types/src/components/ColorScale/bivariateRender.d.ts +53 -0
- package/types/src/components/ColorScale/bivariateScale.d.ts +92 -0
- package/types/src/components/ColorScale/colorScaleSwatchTooltips.d.ts +48 -0
- package/types/src/components/Legend/Legend.d.ts +5 -0
- package/types/src/components/Legend/legendCut.d.ts +31 -0
- package/types/src/components/Legend/legendRender.d.ts +6 -1
- package/types/src/components/TextBox.d.ts +4 -0
- package/types/src/components/textEllipsis.d.ts +6 -0
- package/types/src/fluent.d.ts +18 -0
- package/types/src/shapes/emitLabels.d.ts +1 -1
- package/types/src/utils/D3plusConfig.d.ts +485 -609
- package/types/src/utils/configValue.d.ts +8 -0
- package/types/src/utils/index.d.ts +1 -1
- package/types/umd-entry.d.ts +4 -0
- package/umd/d3plus-core.full.js +22930 -17936
- package/umd/d3plus-core.full.min.js +2 -2
- package/umd/d3plus-core.js +21240 -16404
- package/umd/d3plus-core.min.js +2 -2
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
/**
|
|
2
|
+
The bivariate ColorScale's key: its grid of colors turned 45° into a
|
|
3
|
+
diamond, low/low at the bottom corner and high/high at the top, the first
|
|
4
|
+
variable rising toward the right corner and the second toward the left,
|
|
5
|
+
each named along its lower edge.
|
|
6
|
+
*/ import { textWidth } from "@d3plus/dom";
|
|
7
|
+
import { formatAbbreviate } from "@d3plus/format";
|
|
8
|
+
import { fontFamilyStringify, textWrap } from "@d3plus/text";
|
|
9
|
+
import { formatYear, isYearLike } from "../Axis/yearValues.js";
|
|
10
|
+
import { bivariateBucketCount, bivariatePalette, bivariateScale, isBivariatePair } from "./bivariateScale.js";
|
|
11
|
+
/** The side of the key's grid before it turns, in pixels, when it fits. */ const GRID_SIDE = 80;
|
|
12
|
+
/** The gap between the diamond's edge and its labels. */ const LABEL_GAP = 4;
|
|
13
|
+
/** Grid point `(u, v)` (`u` along the first variable, `v` the second) turned into the diamond, low/low at the origin. */ function toDiamond(u, v) {
|
|
14
|
+
return [
|
|
15
|
+
(u - v) * Math.SQRT1_2,
|
|
16
|
+
-(u + v) * Math.SQRT1_2
|
|
17
|
+
];
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
The names the key gives its two variables: `bivariateLabels`, else the
|
|
21
|
+
names the chart passes (its field names).
|
|
22
|
+
@param cs The ColorScale.
|
|
23
|
+
*/ export function bivariateAxisLabels(cs) {
|
|
24
|
+
const own = cs.schema.bivariateLabels;
|
|
25
|
+
const fields = cs._bivariateFields ?? [];
|
|
26
|
+
const pick = (k)=>{
|
|
27
|
+
const label = Array.isArray(own) ? own[k] : undefined;
|
|
28
|
+
const value = label ?? fields[k];
|
|
29
|
+
return value === undefined || value === null ? "" : `${value}`;
|
|
30
|
+
};
|
|
31
|
+
return [
|
|
32
|
+
pick(0),
|
|
33
|
+
pick(1)
|
|
34
|
+
];
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
How a ColorScale's values read in a bivariate key's ranges and in a
|
|
38
|
+
chart's tooltip: `axisConfig.tickFormat`, else a variable whose values
|
|
39
|
+
all read as years as years, else abbreviated (`formatAbbreviate`).
|
|
40
|
+
@param cs The ColorScale.
|
|
41
|
+
@param values The variable's values.
|
|
42
|
+
*/ export function colorScaleValueFormat(cs, values) {
|
|
43
|
+
const tickFormat = cs.schema.axisConfig?.tickFormat;
|
|
44
|
+
if (tickFormat) return tickFormat;
|
|
45
|
+
if (values.length && isYearLike(values)) return (v)=>formatYear(v, cs.schema.locale);
|
|
46
|
+
return (v)=>formatAbbreviate(v, cs.schema.locale);
|
|
47
|
+
}
|
|
48
|
+
/** A bin's range as text (`"lo - hi"`, or one value), or "" for an empty bin. */ function rangeText(cs, bins, b, format) {
|
|
49
|
+
const range = bins.ranges[b];
|
|
50
|
+
if (!range) return "";
|
|
51
|
+
return cs.schema.bucketJoiner(format(range[0]), format(range[1]));
|
|
52
|
+
}
|
|
53
|
+
/** A cell's title: each variable's name and the range of its bin. */ function cellTitle(cs, scale, [i, j], labels, formats) {
|
|
54
|
+
return [
|
|
55
|
+
[
|
|
56
|
+
labels[0],
|
|
57
|
+
rangeText(cs, scale.x, i, formats[0])
|
|
58
|
+
],
|
|
59
|
+
[
|
|
60
|
+
labels[1],
|
|
61
|
+
rangeText(cs, scale.y, j, formats[1])
|
|
62
|
+
]
|
|
63
|
+
].filter(([, range])=>range).map(([label, range])=>label ? `${label}: ${range}` : range).join(", ");
|
|
64
|
+
}
|
|
65
|
+
/** The label font the key reads off `labelConfig`, for `text`. */ function labelFont(cs, text) {
|
|
66
|
+
const read = (key)=>{
|
|
67
|
+
const v = cs._labelClass[key]();
|
|
68
|
+
return typeof v === "function" ? v(text, 0) : v;
|
|
69
|
+
};
|
|
70
|
+
const family = read("fontFamily");
|
|
71
|
+
return {
|
|
72
|
+
family: fontFamilyStringify(family),
|
|
73
|
+
size: Number(read("fontSize")) || 12,
|
|
74
|
+
weight: read("fontWeight") ?? 400,
|
|
75
|
+
color: `${read("fontColor")}`
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
One variable's name along its lower edge of the diamond: up to two lines
|
|
80
|
+
as long as the edge, an arrow pointing the way the variable rises.
|
|
81
|
+
*/ function edgeLabel(cs, text, axis, side) {
|
|
82
|
+
if (!text) return null;
|
|
83
|
+
const font = labelFont(cs, text);
|
|
84
|
+
const style = {
|
|
85
|
+
"font-family": font.family,
|
|
86
|
+
"font-size": font.size,
|
|
87
|
+
"font-weight": font.weight
|
|
88
|
+
};
|
|
89
|
+
const arrow = axis === 0 ? " →" : "← ";
|
|
90
|
+
const arrowWidth = textWidth(arrow, style);
|
|
91
|
+
// Lines as long as the edge, never splitting a word.
|
|
92
|
+
const widest = Math.max(...text.split(/\s+/).map((word)=>textWidth(word, style)));
|
|
93
|
+
const wrapped = textWrap().fontFamily(font.family).fontSize(font.size).fontWeight(font.weight).lineHeight(font.size * 1.2).maxLines(2)// Whole words only: no hyphenating a long one across the lines.
|
|
94
|
+
.split((sentence)=>sentence.match(/\S+\s*/g) ?? []).width(Math.max(side - arrowWidth, widest + 1)).height(font.size * 1.2 * 2 + 1)(text);
|
|
95
|
+
const lines = wrapped.lines.length ? wrapped.lines.slice() : [
|
|
96
|
+
text
|
|
97
|
+
];
|
|
98
|
+
// The arrow rides the line nearest the far corner: the first variable's
|
|
99
|
+
// reads toward its corner at the end of the last line, the second's
|
|
100
|
+
// toward its corner at the start of the first.
|
|
101
|
+
if (axis === 0) lines[lines.length - 1] += arrow;
|
|
102
|
+
else lines[0] = arrow + lines[0];
|
|
103
|
+
const lineHeight = font.size * 1.2;
|
|
104
|
+
const widths = lines.map((l)=>textWidth(l, style));
|
|
105
|
+
const width = Math.max(...widths);
|
|
106
|
+
// The edge's direction (x) and outward normal (y) the label's lines lie
|
|
107
|
+
// along, and the label's center: the edge's midpoint, or, for a label
|
|
108
|
+
// longer than the edge, past it toward the far corner, so the two labels
|
|
109
|
+
// never meet below the bottom corner.
|
|
110
|
+
const rotate = axis === 0 ? -45 : 45;
|
|
111
|
+
const ex = axis === 0 ? [
|
|
112
|
+
Math.SQRT1_2,
|
|
113
|
+
-Math.SQRT1_2
|
|
114
|
+
] : [
|
|
115
|
+
Math.SQRT1_2,
|
|
116
|
+
Math.SQRT1_2
|
|
117
|
+
];
|
|
118
|
+
const shift = (axis === 0 ? 1 : -1) * Math.max(0, width - side) / 2;
|
|
119
|
+
const [ox, oy] = axis === 0 ? toDiamond(side / 2, 0) : toDiamond(0, side / 2);
|
|
120
|
+
const mx = ox + ex[0] * shift;
|
|
121
|
+
const my = oy + ex[1] * shift;
|
|
122
|
+
const ey = axis === 0 ? [
|
|
123
|
+
Math.SQRT1_2,
|
|
124
|
+
Math.SQRT1_2
|
|
125
|
+
] : [
|
|
126
|
+
-Math.SQRT1_2,
|
|
127
|
+
Math.SQRT1_2
|
|
128
|
+
];
|
|
129
|
+
const depth = LABEL_GAP + lineHeight * lines.length;
|
|
130
|
+
const corners = [
|
|
131
|
+
[
|
|
132
|
+
-width / 2,
|
|
133
|
+
LABEL_GAP
|
|
134
|
+
],
|
|
135
|
+
[
|
|
136
|
+
width / 2,
|
|
137
|
+
LABEL_GAP
|
|
138
|
+
],
|
|
139
|
+
[
|
|
140
|
+
-width / 2,
|
|
141
|
+
depth
|
|
142
|
+
],
|
|
143
|
+
[
|
|
144
|
+
width / 2,
|
|
145
|
+
depth
|
|
146
|
+
]
|
|
147
|
+
].map(([lx, ly])=>[
|
|
148
|
+
mx + ex[0] * lx + ey[0] * ly,
|
|
149
|
+
my + ex[1] * lx + ey[1] * ly
|
|
150
|
+
]);
|
|
151
|
+
const node = {
|
|
152
|
+
type: "text",
|
|
153
|
+
key: `bivariate-label-${axis}`,
|
|
154
|
+
x: 0,
|
|
155
|
+
y: 0,
|
|
156
|
+
transform: {
|
|
157
|
+
x: mx,
|
|
158
|
+
y: my,
|
|
159
|
+
rotate
|
|
160
|
+
},
|
|
161
|
+
lines: lines.map((line, k)=>({
|
|
162
|
+
text: line,
|
|
163
|
+
x: 0,
|
|
164
|
+
y: LABEL_GAP + lineHeight * (k + 0.5),
|
|
165
|
+
width: widths[k]
|
|
166
|
+
})),
|
|
167
|
+
font: {
|
|
168
|
+
family: font.family,
|
|
169
|
+
size: font.size,
|
|
170
|
+
weight: font.weight,
|
|
171
|
+
anchor: "middle",
|
|
172
|
+
baseline: "middle"
|
|
173
|
+
},
|
|
174
|
+
paint: {
|
|
175
|
+
fill: font.color
|
|
176
|
+
},
|
|
177
|
+
interactive: false,
|
|
178
|
+
aria: {
|
|
179
|
+
hidden: true
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
return {
|
|
183
|
+
node,
|
|
184
|
+
corners
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
A key cell's stroke: the one `legendConfig.shapeConfig` gives the
|
|
189
|
+
colorScale's swatches, which a chart sets to its own shapes' stroke.
|
|
190
|
+
*/ function cellStroke(cs, datum, i) {
|
|
191
|
+
const sc = cs.schema.legendConfig?.shapeConfig ?? {};
|
|
192
|
+
const read = (key)=>{
|
|
193
|
+
const v = sc[key];
|
|
194
|
+
return typeof v === "function" ? v(datum, i) : v;
|
|
195
|
+
};
|
|
196
|
+
const stroke = read("stroke");
|
|
197
|
+
const strokeWidth = Number(read("strokeWidth"));
|
|
198
|
+
const strokeOpacity = read("strokeOpacity");
|
|
199
|
+
if (typeof stroke !== "string" || !stroke || !(strokeWidth > 0)) return {};
|
|
200
|
+
return {
|
|
201
|
+
stroke,
|
|
202
|
+
strokeWidth,
|
|
203
|
+
...typeof strokeOpacity === "number" ? {
|
|
204
|
+
strokeOpacity
|
|
205
|
+
} : {}
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
/** The key's cells, a path each, and their corners. */ function cellNodes(cs, scale, side) {
|
|
209
|
+
const n = scale.size;
|
|
210
|
+
const step = side / n;
|
|
211
|
+
const labels = bivariateAxisLabels(cs);
|
|
212
|
+
const values = (bins)=>scale.domain().map((p)=>p[bins]);
|
|
213
|
+
const formats = [
|
|
214
|
+
colorScaleValueFormat(cs, values(0)),
|
|
215
|
+
colorScaleValueFormat(cs, values(1))
|
|
216
|
+
];
|
|
217
|
+
const nodes = [];
|
|
218
|
+
for(let j = 0; j < n; j++)for(let i = 0; i < n; i++){
|
|
219
|
+
const points = [
|
|
220
|
+
toDiamond(i * step, j * step),
|
|
221
|
+
toDiamond((i + 1) * step, j * step),
|
|
222
|
+
toDiamond((i + 1) * step, (j + 1) * step),
|
|
223
|
+
toDiamond(i * step, (j + 1) * step)
|
|
224
|
+
];
|
|
225
|
+
const datum = {
|
|
226
|
+
color: scale.colors[j * n + i],
|
|
227
|
+
id: cellTitle(cs, scale, [
|
|
228
|
+
i,
|
|
229
|
+
j
|
|
230
|
+
], labels, formats),
|
|
231
|
+
_isColorScaleBucket: true,
|
|
232
|
+
_bivariateX: i,
|
|
233
|
+
_bivariateY: j
|
|
234
|
+
};
|
|
235
|
+
nodes.push({
|
|
236
|
+
type: "path",
|
|
237
|
+
key: `bivariate-cell-${i}-${j}`,
|
|
238
|
+
d: `M${points.map((p)=>p.map((v)=>+v.toFixed(3)).join(",")).join("L")}Z`,
|
|
239
|
+
datum,
|
|
240
|
+
index: j * n + i,
|
|
241
|
+
paint: {
|
|
242
|
+
fill: datum.color,
|
|
243
|
+
...cellStroke(cs, datum, j * n + i)
|
|
244
|
+
},
|
|
245
|
+
aria: {
|
|
246
|
+
label: datum.id
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
return {
|
|
251
|
+
nodes,
|
|
252
|
+
corners: [
|
|
253
|
+
toDiamond(0, 0),
|
|
254
|
+
toDiamond(side, 0),
|
|
255
|
+
toDiamond(0, side),
|
|
256
|
+
toDiamond(side, side)
|
|
257
|
+
]
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
/** Lays the key out with a grid `side` pixels across, low/low corner at the origin. */ function layoutAt(cs, scale, side) {
|
|
261
|
+
const cells = cellNodes(cs, scale, side);
|
|
262
|
+
const labels = bivariateAxisLabels(cs);
|
|
263
|
+
const edges = [
|
|
264
|
+
edgeLabel(cs, labels[0], 0, side),
|
|
265
|
+
edgeLabel(cs, labels[1], 1, side)
|
|
266
|
+
].filter(Boolean);
|
|
267
|
+
const corners = cells.corners.concat(...edges.map((e)=>e.corners));
|
|
268
|
+
const xs = corners.map((c)=>c[0]);
|
|
269
|
+
const ys = corners.map((c)=>c[1]);
|
|
270
|
+
const x = Math.min(...xs);
|
|
271
|
+
const y = Math.min(...ys);
|
|
272
|
+
return {
|
|
273
|
+
nodes: [
|
|
274
|
+
...cells.nodes,
|
|
275
|
+
...edges.map((e)=>e.node)
|
|
276
|
+
],
|
|
277
|
+
bounds: {
|
|
278
|
+
x,
|
|
279
|
+
y,
|
|
280
|
+
width: Math.max(...xs) - x,
|
|
281
|
+
height: Math.max(...ys) - y
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
Lays the key out within the ColorScale's `width` × `height`: its grid
|
|
287
|
+
shrinks from its full size to fit, and it sits along the side its labels
|
|
288
|
+
face (`orient`) at its `align`ment, as the swatch legend does.
|
|
289
|
+
@param cs The ColorScale.
|
|
290
|
+
@param scale Its bivariate scale.
|
|
291
|
+
*/ export function layoutBivariate(cs, scale) {
|
|
292
|
+
const width = Number(cs.schema.width) || 0;
|
|
293
|
+
const height = Number(cs.schema.height) || 0;
|
|
294
|
+
let side = GRID_SIDE;
|
|
295
|
+
let layout = layoutAt(cs, scale, side);
|
|
296
|
+
// Labels keep their size as the grid shrinks (and may wrap anew): shrink
|
|
297
|
+
// the grid by what the whole key overflows until it fits, down to a third
|
|
298
|
+
// of its size.
|
|
299
|
+
for(let tries = 0; tries < 4 && side > GRID_SIDE / 3; tries++){
|
|
300
|
+
const over = Math.max(layout.bounds.width - width, layout.bounds.height - height);
|
|
301
|
+
if (over <= 0 || !width || !height) break;
|
|
302
|
+
side = Math.max(GRID_SIDE / 3, side - over * Math.SQRT1_2 - 1);
|
|
303
|
+
layout = layoutAt(cs, scale, side);
|
|
304
|
+
}
|
|
305
|
+
const horizontal = [
|
|
306
|
+
"bottom",
|
|
307
|
+
"top"
|
|
308
|
+
].includes(cs.schema.orient);
|
|
309
|
+
const spare = (room, size)=>{
|
|
310
|
+
const align = cs.schema.align;
|
|
311
|
+
const free = Math.max(0, room - size);
|
|
312
|
+
return align === "start" ? 0 : align === "end" ? free : free / 2;
|
|
313
|
+
};
|
|
314
|
+
const left = horizontal ? Math.max(0, width - layout.bounds.width) / 2 : spare(width, layout.bounds.width);
|
|
315
|
+
const top = horizontal ? spare(height, layout.bounds.height) : Math.max(0, height - layout.bounds.height) / 2;
|
|
316
|
+
const dx = left - layout.bounds.x;
|
|
317
|
+
const dy = top - layout.bounds.y;
|
|
318
|
+
return {
|
|
319
|
+
nodes: [
|
|
320
|
+
{
|
|
321
|
+
type: "group",
|
|
322
|
+
key: "bivariate-key",
|
|
323
|
+
transform: {
|
|
324
|
+
x: dx,
|
|
325
|
+
y: dy
|
|
326
|
+
},
|
|
327
|
+
children: layout.nodes
|
|
328
|
+
}
|
|
329
|
+
],
|
|
330
|
+
bounds: {
|
|
331
|
+
x: left,
|
|
332
|
+
y: top,
|
|
333
|
+
width: layout.bounds.width,
|
|
334
|
+
height: layout.bounds.height
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
Builds a bivariate ColorScale's scale from its data (each row's `value`
|
|
340
|
+
a pair of numbers), assigns it as the ColorScale's `_colorScale`, and
|
|
341
|
+
lays out its diamond key.
|
|
342
|
+
@param cs The ColorScale.
|
|
343
|
+
*/ export function renderBivariate(cs) {
|
|
344
|
+
const n = bivariateBucketCount(cs.schema.bivariateBuckets);
|
|
345
|
+
const pairs = cs._data.map((d, i)=>cs.schema.value(d, i)).filter(isBivariatePair);
|
|
346
|
+
const scale = bivariateScale(pairs, n, bivariatePalette(cs.schema.bivariateColors, n));
|
|
347
|
+
cs._colorScale = scale;
|
|
348
|
+
cs._barGeometry = undefined;
|
|
349
|
+
cs._labelClass.config(cs.schema.labelConfig);
|
|
350
|
+
const layout = layoutBivariate(cs, scale);
|
|
351
|
+
cs._outerBounds = layout.bounds;
|
|
352
|
+
return layout;
|
|
353
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
The scale behind a bivariate ColorScale: each of two variables split into
|
|
3
|
+
quantile bins, and the grid of colors the pair of bins picks from.
|
|
4
|
+
*/ import { ascending, bisectLeft, bisectRight } from "d3-array";
|
|
5
|
+
import { rgb } from "d3-color";
|
|
6
|
+
import { interpolateRgb } from "d3-interpolate";
|
|
7
|
+
/**
|
|
8
|
+
Joshua Stevens' blue–red 3×3 scheme, from "Bivariate Choropleth Maps: A
|
|
9
|
+
How-to Guide" (joshuastevens.net/cartography/make-a-bivariate-choropleth-map/),
|
|
10
|
+
with the hex values as published in the R `pals` package
|
|
11
|
+
(`stevens.bluered`). Row-major from the low/low corner: each row is one
|
|
12
|
+
bin of the second variable (low to high), each column one bin of the
|
|
13
|
+
first (low to high). The first variable rises toward red (#c85a5a), the
|
|
14
|
+
second toward blue (#64acbe), and both together toward #574249. Each
|
|
15
|
+
inner color is the two ramps multiplied over the shared #e8e8e8 base.
|
|
16
|
+
*/ export const BIVARIATE_COLORS = [
|
|
17
|
+
"#e8e8e8",
|
|
18
|
+
"#e4acac",
|
|
19
|
+
"#c85a5a",
|
|
20
|
+
"#b0d5df",
|
|
21
|
+
"#ad9ea5",
|
|
22
|
+
"#985356",
|
|
23
|
+
"#64acbe",
|
|
24
|
+
"#627f8c",
|
|
25
|
+
"#574249"
|
|
26
|
+
];
|
|
27
|
+
/** The fewest and most bins a bivariate ColorScale splits each variable into. */ export const BIVARIATE_MIN_BUCKETS = 2;
|
|
28
|
+
export const BIVARIATE_MAX_BUCKETS = 4;
|
|
29
|
+
/** The bins per variable when none is set. */ export const BIVARIATE_BUCKETS = 3;
|
|
30
|
+
/**
|
|
31
|
+
The number of bins per variable: `value` rounded and held within 2–4, or
|
|
32
|
+
3 when it isn't a number.
|
|
33
|
+
@param value The configured bin count.
|
|
34
|
+
*/ export function bivariateBucketCount(value) {
|
|
35
|
+
if (typeof value !== "number" || !Number.isFinite(value)) return BIVARIATE_BUCKETS;
|
|
36
|
+
return Math.min(BIVARIATE_MAX_BUCKETS, Math.max(BIVARIATE_MIN_BUCKETS, Math.round(value)));
|
|
37
|
+
}
|
|
38
|
+
/** The side of a square grid of `length` colors, or 0 when it isn't one of at least 2×2. */ function gridSide(length) {
|
|
39
|
+
const side = Math.round(Math.sqrt(length));
|
|
40
|
+
return side >= 2 && side * side === length ? side : 0;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
Resamples a square grid of colors (row-major, `k×k`) to `n×n`: a grid
|
|
44
|
+
already `n×n` is returned as given, and any other is blended
|
|
45
|
+
bilinearly between its nearest colors, so its corners stay put. A 3×3
|
|
46
|
+
scheme resampled to 4×4 keeps its corners and blends its middle colors
|
|
47
|
+
across the new cells; one resampled to 2×2 keeps only its corners.
|
|
48
|
+
@param grid The colors, row-major.
|
|
49
|
+
@param n The side of the grid to return.
|
|
50
|
+
*/ export function resampleGrid(grid, n) {
|
|
51
|
+
const k = gridSide(grid.length);
|
|
52
|
+
if (!k) return [];
|
|
53
|
+
if (k === n) return grid.slice();
|
|
54
|
+
const at = (x, y)=>grid[y * k + x];
|
|
55
|
+
const out = [];
|
|
56
|
+
const position = (step)=>{
|
|
57
|
+
const g = n > 1 ? step * (k - 1) / (n - 1) : 0;
|
|
58
|
+
const lo = Math.min(k - 2, Math.floor(g));
|
|
59
|
+
return [
|
|
60
|
+
lo,
|
|
61
|
+
g - lo
|
|
62
|
+
];
|
|
63
|
+
};
|
|
64
|
+
for(let j = 0; j < n; j++){
|
|
65
|
+
const [y, ty] = position(j);
|
|
66
|
+
for(let i = 0; i < n; i++){
|
|
67
|
+
const [x, tx] = position(i);
|
|
68
|
+
const low = interpolateRgb(at(x, y), at(x + 1, y))(tx);
|
|
69
|
+
const high = interpolateRgb(at(x, y + 1), at(x + 1, y + 1))(tx);
|
|
70
|
+
out.push(rgb(interpolateRgb(low, high)(ty)).formatHex());
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
The `n×n` colors a bivariate ColorScale paints with (row-major, as
|
|
77
|
+
`BIVARIATE_COLORS`): `colors` when it is a square grid of at least 2×2
|
|
78
|
+
color strings, resampled to `n×n` (see `resampleGrid`), else Stevens'
|
|
79
|
+
3×3 scheme resampled the same way.
|
|
80
|
+
@param colors The configured grid.
|
|
81
|
+
@param n The bins per variable.
|
|
82
|
+
*/ export function bivariatePalette(colors, n) {
|
|
83
|
+
const valid = Array.isArray(colors) && gridSide(colors.length) > 0 && colors.every((c)=>typeof c === "string" && rgb(c).displayable());
|
|
84
|
+
return resampleGrid(valid ? colors : BIVARIATE_COLORS, n);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
Splits one variable's values into `n` quantile bins by rank: a value's
|
|
88
|
+
bin is its share of the values ranked below it (ties counted at their
|
|
89
|
+
middle rank), so each bin holds about as many values as the next, and
|
|
90
|
+
equal values always share a bin. A value outside the data ranks by
|
|
91
|
+
where it would sort.
|
|
92
|
+
@param values The variable's values (non-finite ones are ignored).
|
|
93
|
+
@param n The number of bins.
|
|
94
|
+
*/ export function bivariateBins(values, n) {
|
|
95
|
+
const sorted = values.filter(Number.isFinite).sort(ascending);
|
|
96
|
+
const count = sorted.length;
|
|
97
|
+
const bin = (value)=>{
|
|
98
|
+
if (!count || !Number.isFinite(value)) return 0;
|
|
99
|
+
// Twice the tie group's middle rank, in whole numbers so a value right
|
|
100
|
+
// on a bin's edge doesn't slip below it to float error.
|
|
101
|
+
const ranks = bisectLeft(sorted, value) + bisectRight(sorted, value);
|
|
102
|
+
return Math.min(n - 1, Math.floor(ranks * n / (2 * count)));
|
|
103
|
+
};
|
|
104
|
+
const ranges = Array.from({
|
|
105
|
+
length: n
|
|
106
|
+
}, ()=>null);
|
|
107
|
+
for (const v of sorted){
|
|
108
|
+
const b = bin(v);
|
|
109
|
+
const range = ranges[b];
|
|
110
|
+
if (!range) ranges[b] = [
|
|
111
|
+
v,
|
|
112
|
+
v
|
|
113
|
+
];
|
|
114
|
+
else range[1] = v;
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
bin,
|
|
118
|
+
ranges
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
/** Whether `value` is a pair of finite numbers. */ export function isBivariatePair(value) {
|
|
122
|
+
return Array.isArray(value) && value.length === 2 && Number.isFinite(value[0]) && Number.isFinite(value[1]);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
Builds a bivariate color scale from the value pairs in the data: each
|
|
126
|
+
variable binned into `n` quantiles (see `bivariateBins`), colored from
|
|
127
|
+
`colors`, an `n×n` grid (see `bivariatePalette`).
|
|
128
|
+
@param pairs The data's value pairs.
|
|
129
|
+
@param n The bins per variable.
|
|
130
|
+
@param colors The `n×n` colors, row-major.
|
|
131
|
+
*/ export function bivariateScale(pairs, n, colors) {
|
|
132
|
+
const x = bivariateBins(pairs.map((p)=>p[0]), n);
|
|
133
|
+
const y = bivariateBins(pairs.map((p)=>p[1]), n);
|
|
134
|
+
const cell = (value)=>isBivariatePair(value) ? [
|
|
135
|
+
x.bin(value[0]),
|
|
136
|
+
y.bin(value[1])
|
|
137
|
+
] : null;
|
|
138
|
+
const scale = (value)=>{
|
|
139
|
+
const c = cell(value);
|
|
140
|
+
return c ? colors[c[1] * n + c[0]] : undefined;
|
|
141
|
+
};
|
|
142
|
+
scale.cell = cell;
|
|
143
|
+
scale.size = n;
|
|
144
|
+
scale.x = x;
|
|
145
|
+
scale.y = y;
|
|
146
|
+
scale.colors = colors;
|
|
147
|
+
scale.domain = ()=>pairs;
|
|
148
|
+
scale.range = ()=>colors;
|
|
149
|
+
return scale;
|
|
150
|
+
}
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { scaleLinear } from "d3-scale";
|
|
2
2
|
import { assign, elem, textWidth } from "@d3plus/dom";
|
|
3
3
|
import { gradientToken } from "@d3plus/render";
|
|
4
|
+
import { resetKeys } from "../../fluent.js";
|
|
5
|
+
/** The config a Legend merges each render's into, rather than replaces. */ const LEGEND_BAGS = [
|
|
6
|
+
"shapeConfig",
|
|
7
|
+
"titleConfig"
|
|
8
|
+
];
|
|
9
|
+
/** The config keys each ColorScale's Legend was last given. */ const legendKeys = new WeakMap();
|
|
4
10
|
/** Builds the axis/label configs and trims the gradient for end labels. */ function prepareGradientAxis(cs, compute, offsets) {
|
|
5
11
|
const { horizontal, height, width, domain, labels, ticks } = compute;
|
|
6
12
|
const axisDomain = domain.slice();
|
|
@@ -68,6 +74,9 @@ import { gradientToken } from "@d3plus/render";
|
|
|
68
74
|
}
|
|
69
75
|
/** Renders the measurement + visible axes, returning bounds and the position scale. */ function renderGradientAxes(cs, compute, groups, axisConfig, offsets) {
|
|
70
76
|
const { horizontal, height, width, x, y } = compute;
|
|
77
|
+
// A bucketed bar labels every bucket, and hides some to give the rest
|
|
78
|
+
// as many lines as a gradient's labels take.
|
|
79
|
+
cs._axisTest._thinForLines = cs._axisClass._thinForLines = !!cs.schema.bucketAxis;
|
|
71
80
|
cs._axisTest.renderMode("compute").select(elem("g.d3plus-ColorScale-axisTest", {
|
|
72
81
|
enter: {
|
|
73
82
|
opacity: 0
|
|
@@ -219,22 +228,32 @@ import { gradientToken } from "@d3plus/render";
|
|
|
219
228
|
});
|
|
220
229
|
renderGradientLabels(cs, compute, groups, labelConfig, rectConfig, labelData);
|
|
221
230
|
}
|
|
231
|
+
/** Whether the Legend's last render left out or cut short its swatches' labels, or drew nothing. */ function labelsLeftOut(legend) {
|
|
232
|
+
if (!legend.data().length) return false;
|
|
233
|
+
return !legend.outerBounds().width || legend._lineData.some((d)=>d.sentence && (!d.width || d.cut));
|
|
234
|
+
}
|
|
222
235
|
/** Renders the discrete legend ColorScale variant (jenks/buckets/quantile). */ export function renderLegendVariant(cs, compute, groups) {
|
|
223
236
|
const { horizontal, allValues, ticks, colors } = compute;
|
|
224
237
|
cs._barGeometry = undefined;
|
|
225
238
|
elem("g.d3plus-ColorScale-axis", Object.assign({
|
|
226
239
|
condition: groups.gradient
|
|
227
240
|
}, groups.groupParams));
|
|
241
|
+
cs._swatchBuckets = new Map();
|
|
228
242
|
let legendData = ticks.reduce((arr, tick, i)=>{
|
|
229
243
|
const label = cs.schema.bucketFormat.bind(cs)(tick, i, ticks, allValues);
|
|
230
244
|
// `_isColorScaleBucket` marks these as range buckets (not groupBy rows)
|
|
231
245
|
// so a parent Viz's `_drawLabel` titles their tooltip with the bucket
|
|
232
246
|
// label (`id`) instead of resolving to "undefined" via groupBy.
|
|
233
|
-
|
|
247
|
+
const row = {
|
|
234
248
|
color: colors[i + 1],
|
|
235
249
|
id: label,
|
|
236
250
|
_isColorScaleBucket: true
|
|
251
|
+
};
|
|
252
|
+
cs._swatchBuckets.set(row, {
|
|
253
|
+
value: tick,
|
|
254
|
+
index: i
|
|
237
255
|
});
|
|
256
|
+
arr.push(row);
|
|
238
257
|
return arr;
|
|
239
258
|
}, []);
|
|
240
259
|
if (!horizontal) legendData = legendData.reverse();
|
|
@@ -260,6 +279,17 @@ import { gradientToken } from "@d3plus/render";
|
|
|
260
279
|
end: "bottom"
|
|
261
280
|
})[cs.schema.align] : "middle"
|
|
262
281
|
}, cs.schema.legendConfig);
|
|
263
|
-
|
|
282
|
+
// Each render starts from the Legend's defaults, so config an earlier
|
|
283
|
+
// render gave it and this one doesn't is dropped.
|
|
284
|
+
const legend = cs._legendClass;
|
|
285
|
+
legend.config(resetKeys(legend, [
|
|
286
|
+
...LEGEND_BAGS,
|
|
287
|
+
...legendKeys.get(legend) ?? []
|
|
288
|
+
]));
|
|
289
|
+
legendKeys.set(legend, Object.keys(legendConfig));
|
|
290
|
+
legend.renderMode("compute").data(legendData).select(groups.legendGroup.node()).config(legendConfig).render();
|
|
291
|
+
// Like the axis of a gradient, the swatches leave out their title before
|
|
292
|
+
// they cut short or leave out their labels.
|
|
293
|
+
if (legend.title() && labelsLeftOut(legend)) legend.title(undefined).render();
|
|
264
294
|
cs._outerBounds = cs._legendClass.outerBounds();
|
|
265
295
|
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
The axis tooltips of a color scale drawn as swatches: the legend's title
|
|
3
|
+
takes the scale's `axisConfig.titleTooltip`, and each swatch label its
|
|
4
|
+
`axisConfig.tickTooltip`, given the same value and index as the label at
|
|
5
|
+
that bucket's start on a `bucketAxis` bar. A label drawn cut short shows
|
|
6
|
+
its whole text. The swatches keep their bucket's tooltip and highlight,
|
|
7
|
+
and a label showing its tooltip lights its bucket too (see
|
|
8
|
+
`handleSwatchLabelHover`).
|
|
9
|
+
*/ import { canShow, inertAxis, labelCut, stampTitleTooltip } from "../Axis/axisTooltipTargets.js";
|
|
10
|
+
/** The legend row a swatch label draws, if `node` is one. */ const rowOf = (node)=>node.datum?.data?.data;
|
|
11
|
+
/**
|
|
12
|
+
Stamps swatch label nodes with their tooltip target: the bucket's tick for
|
|
13
|
+
the value, its place from the lowest for the index, and the label's whole
|
|
14
|
+
text. A label no tooltip could show for is left unstamped.
|
|
15
|
+
@param nodes The swatch labels' text nodes.
|
|
16
|
+
@param option The scale's `axisConfig.tickTooltip`.
|
|
17
|
+
@param buckets Each swatch's legend row, with its bucket.
|
|
18
|
+
*/ export function stampSwatchLabels(nodes, option, buckets) {
|
|
19
|
+
if (option === false) return;
|
|
20
|
+
for (const node of nodes){
|
|
21
|
+
const bucket = buckets.get(rowOf(node));
|
|
22
|
+
if (node.type !== "text" || !bucket) continue;
|
|
23
|
+
const swatch = node.datum.data;
|
|
24
|
+
const text = `${swatch?.label ?? swatch?.id ?? ""}`;
|
|
25
|
+
const truncated = labelCut(node, text);
|
|
26
|
+
if (canShow(option, truncated)) node.axisTooltip = {
|
|
27
|
+
part: "tick",
|
|
28
|
+
value: bucket.value,
|
|
29
|
+
index: bucket.index,
|
|
30
|
+
text,
|
|
31
|
+
truncated,
|
|
32
|
+
option
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/** A copy of a scene subtree, each node copied. */ const copy = (node)=>node.type === "group" ? {
|
|
37
|
+
...node,
|
|
38
|
+
children: node.children.map(copy)
|
|
39
|
+
} : {
|
|
40
|
+
...node
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
A swatch-drawn scale's legend scene with its axis tooltips: the title is
|
|
44
|
+
chrome (see `inertAxis`) that takes the pointer only to show its tooltip,
|
|
45
|
+
a swatch label with a tooltip to show takes the pointer as axis chrome,
|
|
46
|
+
and every other node is left as the legend drew it.
|
|
47
|
+
@param scene The legend's scene.
|
|
48
|
+
@param options What the labels take their tooltips from.
|
|
49
|
+
*/ export function swatchTooltips(scene, options) {
|
|
50
|
+
const next = copy(scene);
|
|
51
|
+
const titles = new Set();
|
|
52
|
+
const labels = [];
|
|
53
|
+
const collect = (node)=>{
|
|
54
|
+
if (node.type === "group") node.children.forEach(collect);
|
|
55
|
+
else if (node.type !== "text") return;
|
|
56
|
+
else if (options.titleDatum !== undefined && node.datum === options.titleDatum) titles.add(node);
|
|
57
|
+
else if (options.buckets.has(rowOf(node))) labels.push(node);
|
|
58
|
+
};
|
|
59
|
+
collect(next);
|
|
60
|
+
stampTitleTooltip([
|
|
61
|
+
...titles
|
|
62
|
+
], options.title, options.titleTooltip);
|
|
63
|
+
stampSwatchLabels(labels, options.tickTooltip, options.buckets);
|
|
64
|
+
const finish = (node)=>{
|
|
65
|
+
if (node.type === "group") return {
|
|
66
|
+
...node,
|
|
67
|
+
children: node.children.map(finish)
|
|
68
|
+
};
|
|
69
|
+
if (titles.has(node)) return inertAxis(node);
|
|
70
|
+
return node.axisTooltip ? {
|
|
71
|
+
...node,
|
|
72
|
+
interactive: true,
|
|
73
|
+
interactionGroup: "axis"
|
|
74
|
+
} : node;
|
|
75
|
+
};
|
|
76
|
+
return finish(next);
|
|
77
|
+
}
|