@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,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
The text a ring chart (Pie with a hole, Donut, Sunburst, RadialBar) draws
|
|
3
|
+
in its empty middle when `centerLabel` is set: the chart's total, a
|
|
4
|
+
string, or what a function returns, wrapped and shrunk to fit the largest
|
|
5
|
+
box in the hole (see `centerLabelBoxes`). It is chrome: it carries no
|
|
6
|
+
datum, takes no pointer events, and draws nothing when it can't fit whole.
|
|
7
|
+
*/ import { formatAbbreviate } from "@d3plus/format";
|
|
8
|
+
import constant from "../../utils/constant.js";
|
|
9
|
+
import { emitLabels } from "../../shapes/emitLabels.js";
|
|
10
|
+
import { isFullTurn } from "../Pie/arcFit.js";
|
|
11
|
+
import { largestSectorBox, sectorEdges } from "../Pie/sectorBox.js";
|
|
12
|
+
import { centerLabelLines } from "../Pie/straightLayout.js";
|
|
13
|
+
import { backgroundInk } from "../viz/backgroundInk.js";
|
|
14
|
+
/** The scene key of the center label's group. */ export const CENTER_LABEL_KEY = "center-label";
|
|
15
|
+
/** The width/height ratios tried for a partial sweep's hole. */ const SECTOR_ASPECTS = [
|
|
16
|
+
1,
|
|
17
|
+
2,
|
|
18
|
+
3
|
|
19
|
+
];
|
|
20
|
+
/**
|
|
21
|
+
The `centerLabelConfig` defaults: the chart's text color, sized between
|
|
22
|
+
10 and 40 pixels, 5 pixels clear of the ring.
|
|
23
|
+
@param viz The chart.
|
|
24
|
+
*/ export function centerLabelDefaults(viz) {
|
|
25
|
+
return {
|
|
26
|
+
fontColor: ()=>backgroundInk(viz),
|
|
27
|
+
fontMax: 40,
|
|
28
|
+
fontMin: 10,
|
|
29
|
+
padding: 5
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
The sum of `value` over `data`, leaving out values that aren't numbers.
|
|
34
|
+
@param data The drawn rows.
|
|
35
|
+
@param value The chart's value accessor.
|
|
36
|
+
*/ export function centerLabelTotal(data, value) {
|
|
37
|
+
return data.reduce((sum, d, i)=>{
|
|
38
|
+
const v = Number(value(d, i));
|
|
39
|
+
return Number.isFinite(v) ? sum + v : sum;
|
|
40
|
+
}, 0);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
The text a `centerLabel` setting draws: the total, formatted the way the
|
|
44
|
+
chart's tooltips format values, for `true`; the string itself; or a
|
|
45
|
+
function's return value. Empty for a falsy setting or return value.
|
|
46
|
+
@param setting The `centerLabel` setting.
|
|
47
|
+
@param data The drawn rows.
|
|
48
|
+
@param total Their total.
|
|
49
|
+
@param locale The chart's locale.
|
|
50
|
+
*/ export function centerLabelText(setting, data, total, locale) {
|
|
51
|
+
if (!setting) return "";
|
|
52
|
+
if (typeof setting === "function") {
|
|
53
|
+
const out = setting(data, total);
|
|
54
|
+
return out || out === 0 ? String(out) : "";
|
|
55
|
+
}
|
|
56
|
+
if (typeof setting === "string") return setting;
|
|
57
|
+
return formatAbbreviate(total, locale);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
The boxes the text may fill in a hole: the whole circle (its text wrapped
|
|
61
|
+
to the circle) for a full turn, or, for a partial sweep, the largest box
|
|
62
|
+
of each of a few shapes that fits in the slice of the circle it spans.
|
|
63
|
+
Empty when the hole has no room.
|
|
64
|
+
@param hole The hole.
|
|
65
|
+
*/ export function centerLabelBoxes(hole) {
|
|
66
|
+
const r = hole.radius;
|
|
67
|
+
if (!(r > 0)) return [];
|
|
68
|
+
const { startAngle, endAngle } = hole;
|
|
69
|
+
if (startAngle === undefined || endAngle === undefined || isFullTurn(startAngle, endAngle)) return [
|
|
70
|
+
{
|
|
71
|
+
x: -r,
|
|
72
|
+
y: -r,
|
|
73
|
+
width: r * 2,
|
|
74
|
+
height: r * 2,
|
|
75
|
+
shape: "circle"
|
|
76
|
+
}
|
|
77
|
+
];
|
|
78
|
+
const edges = sectorEdges({
|
|
79
|
+
innerRadius: 0,
|
|
80
|
+
outerRadius: r,
|
|
81
|
+
startAngle,
|
|
82
|
+
endAngle
|
|
83
|
+
});
|
|
84
|
+
if (!edges) return [];
|
|
85
|
+
return SECTOR_ASPECTS.flatMap((aspect)=>{
|
|
86
|
+
const box = largestSectorBox(edges, (s)=>[
|
|
87
|
+
s * aspect,
|
|
88
|
+
s
|
|
89
|
+
]);
|
|
90
|
+
return box ? [
|
|
91
|
+
{
|
|
92
|
+
...box,
|
|
93
|
+
shape: "square"
|
|
94
|
+
}
|
|
95
|
+
] : [];
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
The TextBox config the label is drawn with: the user's `centerLabelConfig`
|
|
100
|
+
over the centered defaults. A `fontSize` without `fontResize` fixes the
|
|
101
|
+
size; otherwise the text shrinks from `fontMax` to fit.
|
|
102
|
+
@param config The `centerLabelConfig`.
|
|
103
|
+
*/ export function centerLabelStyle(config) {
|
|
104
|
+
const fontResize = config.fontResize ?? config.fontSize === undefined;
|
|
105
|
+
return {
|
|
106
|
+
textAnchor: "middle",
|
|
107
|
+
verticalAlign: "middle",
|
|
108
|
+
...config,
|
|
109
|
+
fontResize,
|
|
110
|
+
pointerEvents: "none"
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
/** The label's text nodes in one box, or null when the text doesn't fit whole. */ function fitIn(text, box, style) {
|
|
114
|
+
const nodes = emitLabels({
|
|
115
|
+
data: [
|
|
116
|
+
{
|
|
117
|
+
text
|
|
118
|
+
}
|
|
119
|
+
],
|
|
120
|
+
label: ()=>text,
|
|
121
|
+
x: ()=>0,
|
|
122
|
+
y: ()=>0,
|
|
123
|
+
aes: ()=>({}),
|
|
124
|
+
rotate: constant(0),
|
|
125
|
+
id: ()=>CENTER_LABEL_KEY,
|
|
126
|
+
labelBounds: ()=>({
|
|
127
|
+
...box
|
|
128
|
+
}),
|
|
129
|
+
labelConfig: style
|
|
130
|
+
}).filter((n)=>n.type === "text");
|
|
131
|
+
if (!nodes.length || nodes.some((n)=>n.truncated)) return null;
|
|
132
|
+
return nodes;
|
|
133
|
+
}
|
|
134
|
+
/** The largest font size among text nodes. */ const fontSizeOf = (nodes)=>Math.max(...nodes.map((n)=>n.font?.size ?? 0));
|
|
135
|
+
/**
|
|
136
|
+
The center label's scene nodes: one inert group holding the text, at the
|
|
137
|
+
largest size that fits the hole whole, centered in it. Empty when
|
|
138
|
+
`centerLabel` is unset or the text fits nowhere.
|
|
139
|
+
@param viz The chart.
|
|
140
|
+
@param hole The chart's empty middle, around its origin.
|
|
141
|
+
@param value The chart's value accessor, summed for the total.
|
|
142
|
+
*/ export function centerLabelNodes(viz, hole, value) {
|
|
143
|
+
const setting = viz.schema.centerLabel;
|
|
144
|
+
if (!setting) return [];
|
|
145
|
+
const boxes = centerLabelBoxes(hole);
|
|
146
|
+
if (!boxes.length) return [];
|
|
147
|
+
const data = viz._filteredData ?? [];
|
|
148
|
+
const text = centerLabelText(setting, data, centerLabelTotal(data, value), viz.schema.locale);
|
|
149
|
+
if (!text) return [];
|
|
150
|
+
const config = viz.schema.centerLabelConfig ?? {};
|
|
151
|
+
const style = centerLabelStyle(config);
|
|
152
|
+
let best = null;
|
|
153
|
+
for (const box of boxes){
|
|
154
|
+
const nodes = fitIn(text, box, style);
|
|
155
|
+
if (nodes && (!best || fontSizeOf(nodes) > fontSizeOf(best.nodes))) best = {
|
|
156
|
+
nodes,
|
|
157
|
+
box
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
if (!best) return [];
|
|
161
|
+
// The text is chrome: no datum for hover to match or a tooltip to show.
|
|
162
|
+
const children = best.nodes.map((n)=>({
|
|
163
|
+
...n,
|
|
164
|
+
datum: undefined,
|
|
165
|
+
index: undefined,
|
|
166
|
+
interactive: false
|
|
167
|
+
}));
|
|
168
|
+
if (style.verticalAlign === "middle") centerLabelLines(children, best.box.y + best.box.height / 2);
|
|
169
|
+
return [
|
|
170
|
+
{
|
|
171
|
+
type: "group",
|
|
172
|
+
key: CENTER_LABEL_KEY,
|
|
173
|
+
interactive: false,
|
|
174
|
+
children
|
|
175
|
+
}
|
|
176
|
+
];
|
|
177
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
A bivariate `colorScale`: two fields (`colorScale: ["a", "b"]`) read as
|
|
3
|
+
one accessor that returns each row's pair of values, which the chart's
|
|
4
|
+
ColorScale bins and colors from a grid (see
|
|
5
|
+
`components/ColorScale/bivariateScale.ts`). Rows missing either value
|
|
6
|
+
have no colorScale value, like a row missing the one field of a
|
|
7
|
+
single-variable colorScale.
|
|
8
|
+
*/ import { bivariateAxisLabels, colorScaleValueFormat } from "../../components/ColorScale/bivariateRender.js";
|
|
9
|
+
import { fieldAccessor } from "../../utils/fieldAccessor.js";
|
|
10
|
+
const specs = new WeakMap();
|
|
11
|
+
/** `value` as a finite number, or null (null, undefined, "", and non-numbers have none). */ export function toNumber(value) {
|
|
12
|
+
if (value === null || value === undefined || value === "") return null;
|
|
13
|
+
if (typeof value !== "number" && typeof value !== "string") return null;
|
|
14
|
+
const n = +value;
|
|
15
|
+
return Number.isFinite(n) ? n : null;
|
|
16
|
+
}
|
|
17
|
+
/** Whether `value` names a bivariate `colorScale`: two keys or accessors. */ export function isBivariateSetting(value) {
|
|
18
|
+
return Array.isArray(value) && value.length === 2 && value.every((v)=>typeof v === "string" || typeof v === "function");
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
The accessor a bivariate `colorScale` stores: each row's two values as a
|
|
22
|
+
`[first, second]` pair of numbers, or undefined when either is missing.
|
|
23
|
+
@param fields The two data keys or accessors.
|
|
24
|
+
*/ export function bivariateAccessor(fields) {
|
|
25
|
+
const accessors = fields.map((f)=>typeof f === "function" ? f : fieldAccessor(f));
|
|
26
|
+
const fn = (d, i)=>{
|
|
27
|
+
const a = toNumber(accessors[0](d, i));
|
|
28
|
+
const b = toNumber(accessors[1](d, i));
|
|
29
|
+
return a === null || b === null ? undefined : [
|
|
30
|
+
a,
|
|
31
|
+
b
|
|
32
|
+
];
|
|
33
|
+
};
|
|
34
|
+
specs.set(fn, {
|
|
35
|
+
accessors,
|
|
36
|
+
fields: fields.map((f)=>typeof f === "string" ? f : undefined)
|
|
37
|
+
});
|
|
38
|
+
return fn;
|
|
39
|
+
}
|
|
40
|
+
/** The bivariate setting behind a stored `colorScale`, or null for any other. */ export function bivariateOf(fn) {
|
|
41
|
+
return typeof fn === "function" && specs.get(fn) || null;
|
|
42
|
+
}
|
|
43
|
+
/** The chart's bivariate `colorScale` setting, or null when it has none. */ export function bivariateSpec(viz) {
|
|
44
|
+
return bivariateOf(viz.schema.colorScale);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
The names a chart gives its bivariate scale's variables when
|
|
48
|
+
`bivariateLabels` doesn't: each field's name, or "Value 1" / "Value 2"
|
|
49
|
+
for an accessor.
|
|
50
|
+
@param viz The chart.
|
|
51
|
+
@param spec Its bivariate setting.
|
|
52
|
+
*/ export function bivariateFieldNames(viz, spec) {
|
|
53
|
+
return spec.fields.map((field, k)=>field ?? `${viz.schema.translate("Value")} ${k + 1}`);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
The tooltip rows a chart's bivariate `colorScale` adds for a row: each
|
|
57
|
+
variable's name (as the key labels it) and the row's value, formatted
|
|
58
|
+
as the key formats its ranges. A variable the row has no value for gets
|
|
59
|
+
no row; a colorScale cell, or a chart without a bivariate scale, gets
|
|
60
|
+
none at all.
|
|
61
|
+
@param viz The chart.
|
|
62
|
+
@param d The hovered row.
|
|
63
|
+
@param i Its index.
|
|
64
|
+
*/ export function bivariateTooltipRows(viz, d, i) {
|
|
65
|
+
const spec = bivariateSpec(viz);
|
|
66
|
+
const cs = viz._colorScaleClass;
|
|
67
|
+
if (!spec || !cs || !d || typeof d !== "object") return [];
|
|
68
|
+
if (d._isColorScaleBucket) return [];
|
|
69
|
+
const labels = bivariateAxisLabels(cs);
|
|
70
|
+
const scale = cs._colorScale;
|
|
71
|
+
const pairs = typeof scale?.domain === "function" ? scale.domain() : [];
|
|
72
|
+
const rows = [];
|
|
73
|
+
spec.accessors.forEach((accessor, k)=>{
|
|
74
|
+
const value = toNumber(accessor(d, i));
|
|
75
|
+
if (value === null) return;
|
|
76
|
+
const values = pairs.map((p)=>Array.isArray(p) ? p[k] : NaN);
|
|
77
|
+
rows.push([
|
|
78
|
+
labels[k],
|
|
79
|
+
colorScaleValueFormat(cs, values)(value)
|
|
80
|
+
]);
|
|
81
|
+
});
|
|
82
|
+
return rows;
|
|
83
|
+
}
|
|
@@ -71,6 +71,24 @@ import { colorScaleValue } from "./colorScaleValue.js";
|
|
|
71
71
|
if (!ctx) return null;
|
|
72
72
|
return (d, i)=>ctx.colorOf(d, i) === bucketColor;
|
|
73
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
Highlights a colorScale bucket's data shapes, as hovering its swatch does:
|
|
76
|
+
every datum whose colorScale value falls in the bucket's color range stays
|
|
77
|
+
bright while the rest dim, and `_hoverBucket` marks the hover as a
|
|
78
|
+
bucket's.
|
|
79
|
+
@param viz The chart.
|
|
80
|
+
@param d A swatch's datum, or its label's, at any depth of wrapping.
|
|
81
|
+
@returns Whether `d` is a bucket's, highlighted or not.
|
|
82
|
+
*/ export function hoverColorScaleBucket(viz, d) {
|
|
83
|
+
const bucket = colorScaleBucketOf(d);
|
|
84
|
+
if (!bucket) return false;
|
|
85
|
+
const predicate = colorScaleBucketPredicate(viz, bucket.color);
|
|
86
|
+
if (predicate) {
|
|
87
|
+
viz._hoverBucket = true;
|
|
88
|
+
viz.hover?.(predicate);
|
|
89
|
+
}
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
74
92
|
/**
|
|
75
93
|
The set of bucket colors that contain at least one datum satisfying
|
|
76
94
|
`predicate` — i.e. the swatches that should stay bright while the rest dim.
|
|
@@ -2,13 +2,15 @@
|
|
|
2
2
|
Drives the colorScale's hover mark (#770): hovering a chart shape marks
|
|
3
3
|
its colorScale value on the colorScale (see `ColorScale.hover`), and
|
|
4
4
|
leaving it, or hovering anything else, clears the mark.
|
|
5
|
-
*/ import {
|
|
5
|
+
*/ import { isBivariatePair } from "../../components/ColorScale/bivariateScale.js";
|
|
6
|
+
import { colorScaleBucketOf, hoverColorScaleBucket } from "./colorScaleBucket.js";
|
|
6
7
|
import { colorScaleValue } from "./colorScaleValue.js";
|
|
7
8
|
/**
|
|
8
9
|
The colorScale value of a hovered scene node: its datum unwrapped to the
|
|
9
10
|
source row (as the shapes' fill unwraps it), read through the chart's
|
|
10
|
-
`colorScale
|
|
11
|
-
|
|
11
|
+
`colorScale`: a pair of values on a bivariate colorScale. Null for
|
|
12
|
+
chrome (legend, axes, the colorScale itself), a node without a row, or a
|
|
13
|
+
row without a finite value.
|
|
12
14
|
@param viz The chart.
|
|
13
15
|
@param node The hovered scene node.
|
|
14
16
|
*/ export function colorScaleHoverValue(viz, node) {
|
|
@@ -24,11 +26,16 @@ import { colorScaleValue } from "./colorScaleValue.js";
|
|
|
24
26
|
}
|
|
25
27
|
if (!d || typeof d !== "object") return null;
|
|
26
28
|
const value = colorScale(d, i);
|
|
29
|
+
if (isBivariatePair(value)) return value;
|
|
27
30
|
if (value === null || value === undefined || value === "") return null;
|
|
28
31
|
if (typeof value !== "number" && typeof value !== "string") return null;
|
|
29
32
|
const n = +value;
|
|
30
33
|
return Number.isFinite(n) ? n : null;
|
|
31
34
|
}
|
|
35
|
+
/** Whether two marked values are the same value (or pair of values). */ function sameHover(a, b) {
|
|
36
|
+
if (Array.isArray(a) && Array.isArray(b)) return a[0] === b[0] && a[1] === b[1];
|
|
37
|
+
return a === b;
|
|
38
|
+
}
|
|
32
39
|
/**
|
|
33
40
|
Updates the colorScale's hover mark for a pointer event, repainting only
|
|
34
41
|
when the marked value changes. Moves are ignored while a transition is
|
|
@@ -44,7 +51,7 @@ import { colorScaleValue } from "./colorScaleValue.js";
|
|
|
44
51
|
if (cs.hoverMark() === false) return;
|
|
45
52
|
next = colorScaleHoverValue(viz, event.pick?.node) ?? false;
|
|
46
53
|
} else if (event.type !== "mouseleave") return;
|
|
47
|
-
if ((cs.hover() ?? false
|
|
54
|
+
if (sameHover(cs.hover() ?? false, next)) return;
|
|
48
55
|
cs.hover(next);
|
|
49
56
|
viz._scheduleSceneRepaint();
|
|
50
57
|
}
|
|
@@ -60,7 +67,8 @@ import { colorScaleValue } from "./colorScaleValue.js";
|
|
|
60
67
|
const cs = viz._colorScaleClass;
|
|
61
68
|
if (!cs || typeof viz._hover === "function") return null;
|
|
62
69
|
const value = cs.hover();
|
|
63
|
-
|
|
70
|
+
const marked = typeof value === "number" && Number.isFinite(value) || isBivariatePair(value);
|
|
71
|
+
if (!marked || cs.hoverMark() === false || cs._barGeometry || typeof cs._colorScale !== "function") return null;
|
|
64
72
|
const shapeConfig = viz.schema.shapeConfig;
|
|
65
73
|
const hoverOpacity = shapeConfig?.hoverOpacity;
|
|
66
74
|
return {
|
|
@@ -68,3 +76,20 @@ import { colorScaleValue } from "./colorScaleValue.js";
|
|
|
68
76
|
dimOpacity: typeof hoverOpacity === "number" && hoverOpacity < 1 ? hoverOpacity : 0.5
|
|
69
77
|
};
|
|
70
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
A swatch label showing its axis tooltip lights its bucket, as hovering its
|
|
81
|
+
swatch does (see `hoverColorScaleBucket`); moving off it, or onto a label
|
|
82
|
+
with nothing to show, clears that highlight.
|
|
83
|
+
@param viz The chart.
|
|
84
|
+
@param event The pointer event.
|
|
85
|
+
@param shown Whether an axis tooltip shows for the event (see `handleAxisTooltip`).
|
|
86
|
+
*/ export function handleSwatchLabelHover(viz, event, shown) {
|
|
87
|
+
const moving = event.type === "mousemove" || event.type === "mouseenter";
|
|
88
|
+
const bucket = shown && moving ? colorScaleBucketOf(event.pick?.node?.datum) : null;
|
|
89
|
+
if (bucket === (viz._swatchLabelBucket ?? null)) return;
|
|
90
|
+
if (viz._swatchLabelBucket) {
|
|
91
|
+
viz._swatchLabelBucket = undefined;
|
|
92
|
+
viz.hover?.(false);
|
|
93
|
+
}
|
|
94
|
+
if (bucket && hoverColorScaleBucket(viz, bucket)) viz._swatchLabelBucket = bucket;
|
|
95
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
The share of a chart a colorScale along one of its sides takes at most:
|
|
3
|
+
a third of the chart across the scale, its bar, padding, ticks, labels
|
|
4
|
+
and title together.
|
|
5
|
+
*/ import { isBucketScale } from "../../components/ColorScale/colorScaleType.js";
|
|
6
|
+
/** The most of a chart, across the scale, that a colorScale along one of its sides takes. */ export const MAX_SHARE = 1 / 3;
|
|
7
|
+
/** Whether a colorScale on the `orient` side of a chart runs along its width. */ export function isWide(orient) {
|
|
8
|
+
return orient === "top" || orient === "bottom";
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
The room left for a scale's axis (its ticks, labels and title) when the
|
|
12
|
+
whole scale takes at most `limit`: what its bar, `size` thick, and the
|
|
13
|
+
`padding` on either side of the scale leave, in whole pixels.
|
|
14
|
+
*/ export function axisRoom(limit, size, padding) {
|
|
15
|
+
return Math.max(0, Math.floor(limit - size - padding * 2));
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
Whether the ColorScale last drew a swatch per bucket, the form a Legend
|
|
19
|
+
lays out, rather than a bar along an axis or a bivariate key.
|
|
20
|
+
*/ export function drawsSwatches(cs) {
|
|
21
|
+
return !cs.schema.bivariate && !cs.schema.bucketAxis && isBucketScale(cs._scaleType);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
`frame` cut down to `limit` across the scale, still against the edge of
|
|
25
|
+
the chart its scale is aligned to.
|
|
26
|
+
*/ export function limitFrame(frame, limit) {
|
|
27
|
+
const far = frame.align === "end";
|
|
28
|
+
return isWide(frame.orient) ? {
|
|
29
|
+
...frame,
|
|
30
|
+
y: far ? frame.y + frame.height - limit : frame.y,
|
|
31
|
+
height: limit
|
|
32
|
+
} : {
|
|
33
|
+
...frame,
|
|
34
|
+
x: far ? frame.x + frame.width - limit : frame.x,
|
|
35
|
+
width: limit
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
`frame` with the `limit` a colorScale along a side of the chart keeps to:
|
|
40
|
+
`set`, the size across the scale its `colorScaleConfig` gives it (its
|
|
41
|
+
`width` at the left or right, its `height` at the top or bottom), up to
|
|
42
|
+
the frame's own, with the frame cut down to it against its edge; else a
|
|
43
|
+
third of the frame across the scale.
|
|
44
|
+
*/ export function sideFrame(frame, set) {
|
|
45
|
+
const across = frame[isWide(frame.orient) ? "height" : "width"];
|
|
46
|
+
if (set === undefined) return {
|
|
47
|
+
...frame,
|
|
48
|
+
limit: across * MAX_SHARE
|
|
49
|
+
};
|
|
50
|
+
const limit = Math.max(0, Math.min(set, across));
|
|
51
|
+
return {
|
|
52
|
+
...limitFrame(frame, limit),
|
|
53
|
+
limit
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/** The number `config` sets for `key`, or undefined when it sets none. */ export function configSize(config, key) {
|
|
57
|
+
const value = config[key];
|
|
58
|
+
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
The frame a scale drawn as swatches is laid out in again, or undefined
|
|
62
|
+
when it keeps `frame`: one as far across the scale as its Legend may
|
|
63
|
+
reach, the size the scale's `legendConfig` gives it there or else the
|
|
64
|
+
frame's `limit`, once its swatches claim more than that limit (a frame
|
|
65
|
+
already cut down to the limit keeps it).
|
|
66
|
+
*/ export function swatchFrame(cs, frame) {
|
|
67
|
+
if (frame.limit === undefined || !drawsSwatches(cs)) return undefined;
|
|
68
|
+
const across = isWide(frame.orient) ? "height" : "width";
|
|
69
|
+
const set = cs.legendConfig()[across];
|
|
70
|
+
if (typeof set === "number") return set === frame[across] ? undefined : limitFrame(frame, set);
|
|
71
|
+
const claim = cs.outerBounds()[across] + cs.padding() * 2;
|
|
72
|
+
return claim > frame.limit && frame[across] !== frame.limit ? limitFrame(frame, frame.limit) : undefined;
|
|
73
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
The rows a chart's `colorScale` adds to its shape tooltips: the row's
|
|
3
|
+
colorScale value, named and formatted as the colorScale shows it (two
|
|
4
|
+
rows for a bivariate colorScale, one per variable).
|
|
5
|
+
*/ import { colorScaleValueFormat } from "../../components/ColorScale/bivariateRender.js";
|
|
6
|
+
import { isSetEntry } from "../../fluent.js";
|
|
7
|
+
import { fieldNameOf } from "../../utils/fieldAccessor.js";
|
|
8
|
+
import { bivariateSpec, bivariateTooltipRows, toNumber } from "./colorScaleBivariate.js";
|
|
9
|
+
/**
|
|
10
|
+
The name a tooltip gives a single-variable colorScale's value: the
|
|
11
|
+
colorScale's `axisConfig.title`, else the field it reads, else the
|
|
12
|
+
translated "Value".
|
|
13
|
+
@param viz The chart.
|
|
14
|
+
*/ export function colorScaleValueLabel(viz) {
|
|
15
|
+
const title = viz._colorScaleClass?.schema.axisConfig?.title;
|
|
16
|
+
if (typeof title === "string" && title) return title;
|
|
17
|
+
return fieldNameOf(viz.schema.colorScale) ?? viz.schema.translate("Value");
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
The tooltip rows a chart's `colorScale` adds for a row: its value, named
|
|
21
|
+
(see `colorScaleValueLabel`) and formatted (see `colorScaleValueFormat`)
|
|
22
|
+
as the colorScale shows it, or, for a bivariate colorScale, each
|
|
23
|
+
variable's (see `bivariateTooltipRows`). None for a row without a value,
|
|
24
|
+
a colorScale bucket or key cell, a chart without a colorScale, one
|
|
25
|
+
whose tooltip already shows the value (`ChartDefinition.colorScaleTooltip`),
|
|
26
|
+
or one whose `tooltipConfig.tbody` the user has set.
|
|
27
|
+
@param viz The chart.
|
|
28
|
+
@param d The hovered row.
|
|
29
|
+
@param i Its index.
|
|
30
|
+
*/ export function colorScaleTooltipRows(viz, d, i) {
|
|
31
|
+
const cs = viz._colorScaleClass;
|
|
32
|
+
const accessor = viz.schema.colorScale;
|
|
33
|
+
if (viz._colorScaleTooltip === false || !cs) return [];
|
|
34
|
+
// A `tbody` the user sets is the whole table.
|
|
35
|
+
if (isSetEntry(viz, "tooltipConfig", "tbody")) return [];
|
|
36
|
+
if (typeof accessor !== "function" || !d || typeof d !== "object") return [];
|
|
37
|
+
if (d._isColorScaleBucket) return [];
|
|
38
|
+
if (bivariateSpec(viz)) return bivariateTooltipRows(viz, d, i);
|
|
39
|
+
const value = toNumber(accessor(d, i));
|
|
40
|
+
if (value === null) return [];
|
|
41
|
+
const read = cs.schema.value;
|
|
42
|
+
const values = (cs._data ?? []).map((row, k)=>read(row, k)).filter((v)=>typeof v === "number");
|
|
43
|
+
return [
|
|
44
|
+
[
|
|
45
|
+
colorScaleValueLabel(viz),
|
|
46
|
+
colorScaleValueFormat(cs, values)(value)
|
|
47
|
+
]
|
|
48
|
+
];
|
|
49
|
+
}
|
|
50
|
+
/** The `tbody` a wrapped one stood in for. */ const wrappedBase = new WeakMap();
|
|
51
|
+
/**
|
|
52
|
+
Leads `tooltip`'s `tbody` with the row's colorScale value rows (see
|
|
53
|
+
`colorScaleTooltipRows`), ahead of the chart's own rows. A `tbody` led on an earlier
|
|
54
|
+
hover is unwrapped first, so the rows never stack.
|
|
55
|
+
@param tooltip The chart's tooltip.
|
|
56
|
+
@param viz The chart.
|
|
57
|
+
@param d The hovered row.
|
|
58
|
+
@param i Its index.
|
|
59
|
+
*/ export function leadWithColorScaleRows(tooltip, viz, d, i) {
|
|
60
|
+
let base = tooltip.tbody();
|
|
61
|
+
if (typeof base === "function" && wrappedBase.has(base)) {
|
|
62
|
+
base = wrappedBase.get(base);
|
|
63
|
+
tooltip.tbody(base);
|
|
64
|
+
}
|
|
65
|
+
const rows = colorScaleTooltipRows(viz, d, i);
|
|
66
|
+
if (!rows.length) return tooltip;
|
|
67
|
+
const lead = (dd, ii)=>{
|
|
68
|
+
const own = typeof base === "function" ? base(dd, ii) : base;
|
|
69
|
+
return [
|
|
70
|
+
...rows,
|
|
71
|
+
...Array.isArray(own) ? own : []
|
|
72
|
+
];
|
|
73
|
+
};
|
|
74
|
+
wrappedBase.set(lead, base);
|
|
75
|
+
tooltip.tbody(lead);
|
|
76
|
+
return tooltip;
|
|
77
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Lays a colorScale at the top of a chart out clear of the corner control
|
|
3
|
+
panels (the top-left controls and the zoom controls), as a top legend is:
|
|
4
|
+
beside them when it draws there as well as across the whole width, and
|
|
5
|
+
below them when it doesn't.
|
|
6
|
+
*/ import { cornerPlacements } from "./features.js";
|
|
7
|
+
import { drawsSwatches } from "./colorScaleLimit.js";
|
|
8
|
+
/** The text of every label (and title) in a scene. */ function sceneLabels(node, out = []) {
|
|
9
|
+
if (node.type === "text") out.push(node.lines.map((line)=>line.text).join(" "));
|
|
10
|
+
else if (node.type === "group") node.children.forEach((child)=>sceneLabels(child, out));
|
|
11
|
+
return out;
|
|
12
|
+
}
|
|
13
|
+
/** What the chart's colorScale shows after its last layout. */ function scaleShown(viz) {
|
|
14
|
+
const cs = viz._colorScaleClass;
|
|
15
|
+
const { height } = cs.outerBounds();
|
|
16
|
+
return {
|
|
17
|
+
claim: height ? height + cs.padding() * 2 : 0,
|
|
18
|
+
labels: height ? sceneLabels(cs.toScene()) : []
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/** The labels cut short in `labels`. */ const cut = (labels)=>labels.filter((label)=>label.endsWith("...")).length;
|
|
22
|
+
/**
|
|
23
|
+
Whether a colorScale laid out beside the corner panels draws as well as
|
|
24
|
+
it does across the whole width (`whole`): no taller, its swatches with
|
|
25
|
+
every label they had, or its bar, shorter, with at least half as many
|
|
26
|
+
labels and no more of them cut short.
|
|
27
|
+
*/ export function drawsAsWell(swatches, beside, whole) {
|
|
28
|
+
if (!beside.claim || beside.claim > whole.claim) return false;
|
|
29
|
+
if (swatches) return beside.labels.join("\n") === whole.labels.join("\n");
|
|
30
|
+
return beside.labels.length >= Math.ceil(whole.labels.length / 2) && cut(beside.labels) <= cut(whole.labels);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
Paints a colorScale at the top of the chart with `paint`, into the first
|
|
34
|
+
of `cornerPlacements` it draws as well in as across all of `frame` (see
|
|
35
|
+
`drawsAsWell`): between the corner panels, then inset from each by its
|
|
36
|
+
own amount, and last across the whole width below them. `room` is the
|
|
37
|
+
height the chart has below `frame.y`. Returns how far the scale dropped
|
|
38
|
+
below `frame.y`.
|
|
39
|
+
*/ export function paintTopColorScale(viz, frame, room, paint) {
|
|
40
|
+
const right = viz.schema.width - frame.x - frame.width;
|
|
41
|
+
const placements = cornerPlacements(viz, frame.y, frame.x, right, true);
|
|
42
|
+
if (placements.length === 1 && !placements[0].drop) {
|
|
43
|
+
paint(frame);
|
|
44
|
+
return 0;
|
|
45
|
+
}
|
|
46
|
+
paint(frame);
|
|
47
|
+
const whole = scaleShown(viz);
|
|
48
|
+
const swatches = drawsSwatches(viz._colorScaleClass);
|
|
49
|
+
for (const [i, { left, right: r, drop }] of placements.entries()){
|
|
50
|
+
const last = i === placements.length - 1;
|
|
51
|
+
paint({
|
|
52
|
+
...frame,
|
|
53
|
+
x: frame.x + left,
|
|
54
|
+
y: frame.y + drop,
|
|
55
|
+
width: frame.width - left - r,
|
|
56
|
+
height: Math.max(0, Math.min(frame.height, room - drop))
|
|
57
|
+
});
|
|
58
|
+
if (last || drawsAsWell(swatches, scaleShown(viz), whole)) return drop;
|
|
59
|
+
}
|
|
60
|
+
return 0;
|
|
61
|
+
}
|
|
@@ -89,6 +89,39 @@ import { polylinePath, trimPolyline } from "./polyline.js";
|
|
|
89
89
|
target
|
|
90
90
|
};
|
|
91
91
|
}
|
|
92
|
+
/** Joined cubic Bézier curves as one path's `d`. */ export function cubicsPathD(curves) {
|
|
93
|
+
return curves.map((curve, k)=>{
|
|
94
|
+
const d = cubicPathD(curve);
|
|
95
|
+
return k ? d.replace(/^M[^C]*/, "") : d;
|
|
96
|
+
}).join("");
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
`cubicArrowEdge` for a path of joined cubics, from the first's start to
|
|
100
|
+
the last's end: each arrowed end is taken on its end's cubic.
|
|
101
|
+
*/ export function cubicsArrowEdge(curves, trims, tipBack = {
|
|
102
|
+
source: 0,
|
|
103
|
+
target: 0
|
|
104
|
+
}) {
|
|
105
|
+
if (curves.length === 1) return cubicArrowEdge(curves[0], trims, tipBack);
|
|
106
|
+
const { ends, size } = trims;
|
|
107
|
+
if (!ends.source && !ends.target) return {
|
|
108
|
+
d: cubicsPathD(curves)
|
|
109
|
+
};
|
|
110
|
+
const first = curves[0];
|
|
111
|
+
const last = curves[curves.length - 1];
|
|
112
|
+
const body = curves.slice();
|
|
113
|
+
body[0] = trimCubic(first, ends.source ? tipBack.source + size : 0, 0);
|
|
114
|
+
body[body.length - 1] = trimCubic(last, 0, ends.target ? tipBack.target + size : 0);
|
|
115
|
+
const target = ends.target ? arrowAlongChord(pathPointBack(cubicPolyline(last), tipBack.target), body[body.length - 1][3], trims.target) : undefined;
|
|
116
|
+
const source = ends.source ? arrowAlongChord(pathPointBack([
|
|
117
|
+
...cubicPolyline(first)
|
|
118
|
+
].reverse(), tipBack.source), body[0][0], trims.source) : undefined;
|
|
119
|
+
return {
|
|
120
|
+
d: chordEndsPathD(cubicsPathD(body), source, target),
|
|
121
|
+
source,
|
|
122
|
+
target
|
|
123
|
+
};
|
|
124
|
+
}
|
|
92
125
|
/** Whether `points` is a list of runs rather than one polyline. */ const isRuns = (points)=>Array.isArray(points[0]?.[0]);
|
|
93
126
|
/**
|
|
94
127
|
A polyline edge's visible stroke and its arrowed ends: each head's tip
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
The hierarchy depth Tree records on a node's row (1 for a top-level
|
|
3
|
+
node), so code handed the row alone, like a tooltip's title, knows the
|
|
4
|
+
node's level. It lives under its own key so a data field named `depth` is
|
|
5
|
+
never overwritten; the plain `depth` key mirrors it only on rows without
|
|
6
|
+
their own `depth` field.
|
|
7
|
+
*/ import { INTERNAL_KEY_PREFIX, unwrapRow } from "./shareKey.js";
|
|
8
|
+
/** The row key d3plus writes a node's hierarchy depth under. */ export const DEPTH_KEY = `${INTERNAL_KEY_PREFIX}Depth`;
|
|
9
|
+
/** Rows whose `depth` d3plus wrote, so a restamp updates its own value. */ const stampedRows = new WeakSet();
|
|
10
|
+
/**
|
|
11
|
+
Writes `depth` onto `row` under `DEPTH_KEY`, and as `depth` too unless the
|
|
12
|
+
row already has a `depth` of its own.
|
|
13
|
+
*/ export function stampDepth(row, depth) {
|
|
14
|
+
row[DEPTH_KEY] = depth;
|
|
15
|
+
if (row.depth === undefined || stampedRows.has(row)) {
|
|
16
|
+
row.depth = depth;
|
|
17
|
+
stampedRows.add(row);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
The hierarchy depth d3plus wrote on a row, or on the row under a shape or
|
|
22
|
+
legend wrapper. Undefined when it carries none.
|
|
23
|
+
*/ export function depthOf(d) {
|
|
24
|
+
const depth = d ? unwrapRow(d)[DEPTH_KEY] : undefined;
|
|
25
|
+
return typeof depth === "number" ? depth : undefined;
|
|
26
|
+
}
|