layerchart 0.54.0 → 0.55.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/dist/components/Arc.svelte +170 -144
- package/dist/components/Area.svelte +96 -67
- package/dist/components/Area.svelte.d.ts +1 -0
- package/dist/components/Axis.svelte +205 -155
- package/dist/components/Bar.svelte +72 -45
- package/dist/components/Bars.svelte +45 -34
- package/dist/components/Blur.svelte +5 -3
- package/dist/components/Bounds.svelte +37 -21
- package/dist/components/Brush.svelte +269 -112
- package/dist/components/Brush.svelte.d.ts +7 -0
- package/dist/components/Calendar.svelte +51 -38
- package/dist/components/Chart.svelte +295 -74
- package/dist/components/Chart.svelte.d.ts +17 -17
- package/dist/components/ChartClipPath.svelte +8 -5
- package/dist/components/ChartContext.svelte +243 -93
- package/dist/components/ChartContext.svelte.d.ts +15 -23
- package/dist/components/Circle.svelte +25 -16
- package/dist/components/CircleClipPath.svelte +16 -10
- package/dist/components/ClipPath.svelte +11 -7
- package/dist/components/ColorRamp.svelte +12 -10
- package/dist/components/ForceSimulation.svelte +185 -116
- package/dist/components/Frame.svelte +10 -6
- package/dist/components/GeoCircle.svelte +15 -9
- package/dist/components/GeoContext.svelte +109 -62
- package/dist/components/GeoEdgeFade.svelte +20 -14
- package/dist/components/GeoPath.svelte +107 -69
- package/dist/components/GeoPoint.svelte +32 -18
- package/dist/components/GeoSpline.svelte +30 -22
- package/dist/components/GeoTile.svelte +40 -30
- package/dist/components/GeoVisible.svelte +10 -7
- package/dist/components/Graticule.svelte +14 -8
- package/dist/components/Grid.svelte +75 -48
- package/dist/components/Group.svelte +43 -31
- package/dist/components/Highlight.svelte +284 -243
- package/dist/components/HitCanvas.svelte +75 -42
- package/dist/components/Hull.svelte +40 -20
- package/dist/components/Labels.svelte +81 -70
- package/dist/components/Legend.svelte +105 -74
- package/dist/components/Legend.svelte.d.ts +1 -1
- package/dist/components/Line.svelte +65 -19
- package/dist/components/Line.svelte.d.ts +13 -1
- package/dist/components/LinearGradient.svelte +21 -15
- package/dist/components/Link.svelte +94 -22
- package/dist/components/Link.svelte.d.ts +17 -1
- package/dist/components/Marker.svelte +81 -0
- package/dist/components/Marker.svelte.d.ts +28 -0
- package/dist/components/MonthPath.svelte +23 -16
- package/dist/components/MotionPath.svelte +34 -25
- package/dist/components/Pack.svelte +21 -14
- package/dist/components/Partition.svelte +35 -20
- package/dist/components/Pattern.svelte +8 -6
- package/dist/components/Pie.svelte +76 -57
- package/dist/components/Point.svelte +11 -7
- package/dist/components/Points.svelte +178 -143
- package/dist/components/RadialGradient.svelte +25 -18
- package/dist/components/Rect.svelte +33 -19
- package/dist/components/RectClipPath.svelte +16 -11
- package/dist/components/Rule.svelte +50 -42
- package/dist/components/Sankey.svelte +55 -30
- package/dist/components/Spline.svelte +167 -96
- package/dist/components/Spline.svelte.d.ts +15 -0
- package/dist/components/Text.svelte +137 -104
- package/dist/components/Threshold.svelte +18 -7
- package/dist/components/TileImage.svelte +56 -50
- package/dist/components/TransformContext.svelte +235 -135
- package/dist/components/TransformControls.svelte +57 -29
- package/dist/components/TransformControls.svelte.d.ts +1 -1
- package/dist/components/Tree.svelte +33 -23
- package/dist/components/Treemap.svelte +69 -41
- package/dist/components/Voronoi.svelte +55 -28
- package/dist/components/charts/AreaChart.svelte +138 -87
- package/dist/components/charts/AreaChart.svelte.d.ts +4 -4
- package/dist/components/charts/BarChart.svelte +179 -114
- package/dist/components/charts/BarChart.svelte.d.ts +4 -4
- package/dist/components/charts/LineChart.svelte +97 -53
- package/dist/components/charts/LineChart.svelte.d.ts +4 -4
- package/dist/components/charts/PieChart.svelte +104 -54
- package/dist/components/charts/PieChart.svelte.d.ts +3 -3
- package/dist/components/charts/ScatterChart.svelte +83 -48
- package/dist/components/charts/ScatterChart.svelte.d.ts +4 -4
- package/dist/components/layout/Canvas.svelte +63 -43
- package/dist/components/layout/Html.svelte +28 -18
- package/dist/components/layout/Svg.svelte +47 -32
- package/dist/components/tooltip/Tooltip.svelte +137 -91
- package/dist/components/tooltip/Tooltip.svelte.d.ts +1 -1
- package/dist/components/tooltip/TooltipContext.svelte +315 -249
- package/dist/components/tooltip/TooltipHeader.svelte +9 -3
- package/dist/components/tooltip/TooltipItem.svelte +17 -9
- package/dist/components/tooltip/TooltipList.svelte +2 -1
- package/dist/components/tooltip/TooltipSeparator.svelte +3 -2
- package/dist/docs/Blockquote.svelte +4 -3
- package/dist/docs/Code.svelte +15 -8
- package/dist/docs/CurveMenuField.svelte +17 -12
- package/dist/docs/GeoDebug.svelte +13 -9
- package/dist/docs/Header1.svelte +2 -1
- package/dist/docs/Json.svelte +6 -4
- package/dist/docs/Layout.svelte +6 -6
- package/dist/docs/PathDataMenuField.svelte +52 -44
- package/dist/docs/Preview.svelte +39 -33
- package/dist/docs/TilesetField.svelte +80 -62
- package/dist/docs/TransformDebug.svelte +8 -5
- package/dist/docs/ViewSourceButton.svelte +13 -9
- package/dist/stores/motionStore.d.ts +1 -1
- package/dist/utils/scales.d.ts +3 -3
- package/package.json +29 -30
|
@@ -1,70 +1,103 @@
|
|
|
1
|
-
<script>
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { createEventDispatcher, onMount, setContext } from 'svelte';
|
|
3
|
+
import { writable } from 'svelte/store';
|
|
4
|
+
import { scaleCanvas } from 'layercake';
|
|
5
|
+
import { cls } from '@layerstack/tailwind';
|
|
6
|
+
|
|
7
|
+
import { chartContext } from './ChartContext.svelte';
|
|
8
|
+
import Canvas from './layout/Canvas.svelte';
|
|
9
|
+
import { transformContext } from './TransformContext.svelte';
|
|
10
|
+
|
|
11
|
+
const { width, height } = chartContext();
|
|
12
|
+
|
|
13
|
+
// @ts-expect-error: this will immediately be defined on mount via `bind:context`
|
|
14
|
+
export let context: CanvasRenderingContext2D = undefined;
|
|
15
|
+
|
|
16
|
+
/** Show canvas for debugging */
|
|
17
|
+
export let debug = false;
|
|
18
|
+
|
|
19
|
+
const dispatch = createEventDispatcher<{
|
|
20
|
+
pointermove: {
|
|
21
|
+
event: PointerEvent;
|
|
22
|
+
data: any;
|
|
23
|
+
};
|
|
24
|
+
click: {
|
|
25
|
+
event: MouseEvent;
|
|
26
|
+
data: any;
|
|
27
|
+
};
|
|
28
|
+
}>();
|
|
29
|
+
|
|
30
|
+
const cntxt = {
|
|
15
31
|
ctx: writable({}),
|
|
16
|
-
};
|
|
17
|
-
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
onMount(() => {
|
|
18
35
|
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/getContext#willreadfrequently
|
|
19
36
|
scaleCanvas(context, $width, $height);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
$: cntxt.ctx.set(context);
|
|
40
|
+
setContext('canvas', cntxt);
|
|
41
|
+
|
|
42
|
+
function* rgbColorGenerator(step = 500) {
|
|
24
43
|
let nextColor = 1;
|
|
44
|
+
|
|
25
45
|
while (nextColor < 16777216) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
46
|
+
const rgb = [
|
|
47
|
+
nextColor & 0xff, // red
|
|
48
|
+
(nextColor & 0xff00) >> 8, // green
|
|
49
|
+
(nextColor & 0xff0000) >> 16, // blue
|
|
50
|
+
];
|
|
51
|
+
|
|
52
|
+
nextColor += step;
|
|
53
|
+
yield `rgb(${rgb.join(',')})`;
|
|
33
54
|
}
|
|
55
|
+
|
|
34
56
|
return 'rgb(0,0,0)';
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
$: colorGenerator = rgbColorGenerator();
|
|
60
|
+
|
|
61
|
+
// Reset color generator whenever updated (width/height) so always reusing same colors (and not exhausting)
|
|
62
|
+
const { translate, scale, dragging } = transformContext();
|
|
63
|
+
$: {
|
|
40
64
|
$width;
|
|
41
65
|
$height;
|
|
42
66
|
$translate;
|
|
43
67
|
$scale;
|
|
68
|
+
|
|
44
69
|
colorGenerator = rgbColorGenerator();
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const dataByColor = new Map<string, any>();
|
|
73
|
+
function setColorData(color: string, data: any) {
|
|
48
74
|
dataByColor.set(color, data);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
let activePointer = false;
|
|
78
|
+
|
|
79
|
+
function getPointerData(e: PointerEvent | MouseEvent) {
|
|
52
80
|
const { offsetX, offsetY } = e;
|
|
81
|
+
|
|
53
82
|
const dpr = window.devicePixelRatio ?? 1;
|
|
54
83
|
const imageData = context.getImageData(offsetX * dpr, offsetY * dpr, 1, 1);
|
|
55
84
|
const [r, g, b, a] = imageData.data;
|
|
56
85
|
const colorKey = `rgb(${r},${g},${b})`;
|
|
57
86
|
const data = dataByColor.get(colorKey);
|
|
87
|
+
|
|
58
88
|
return data;
|
|
59
|
-
}
|
|
60
|
-
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function dispatchPointerMove(e: PointerEvent) {
|
|
61
92
|
const data = getPointerData(e);
|
|
93
|
+
|
|
62
94
|
if (data) {
|
|
63
|
-
|
|
95
|
+
activePointer = true;
|
|
64
96
|
}
|
|
97
|
+
|
|
65
98
|
// Still dispatch with `undefined data` to hide tooltip, etc
|
|
66
99
|
dispatch('pointermove', { event: e, data });
|
|
67
|
-
}
|
|
100
|
+
}
|
|
68
101
|
</script>
|
|
69
102
|
|
|
70
103
|
<Canvas
|
|
@@ -1,31 +1,51 @@
|
|
|
1
|
-
<script>
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
import {
|
|
11
|
-
import
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { createEventDispatcher, type ComponentProps } from 'svelte';
|
|
3
|
+
import { min } from 'd3-array';
|
|
4
|
+
import { Delaunay } from 'd3-delaunay';
|
|
5
|
+
// @ts-expect-error
|
|
6
|
+
import { geoVoronoi } from 'd3-geo-voronoi';
|
|
7
|
+
import { curveLinearClosed } from 'd3-shape';
|
|
8
|
+
import { cls } from '@layerstack/tailwind';
|
|
9
|
+
|
|
10
|
+
import { chartContext } from './ChartContext.svelte';
|
|
11
|
+
import GeoPath from './GeoPath.svelte';
|
|
12
|
+
import { geoContext, type GeoContext } from './GeoContext.svelte';
|
|
13
|
+
import Spline from './Spline.svelte';
|
|
14
|
+
|
|
15
|
+
const { flatData, x: xContext, y: yContext } = chartContext();
|
|
16
|
+
const geo = geoContext() as GeoContext | undefined;
|
|
17
|
+
|
|
18
|
+
/** Override data instead of using context */
|
|
19
|
+
export let data: any = undefined;
|
|
20
|
+
|
|
21
|
+
export let curve: ComponentProps<Spline>['curve'] = curveLinearClosed;
|
|
22
|
+
|
|
23
|
+
export let classes: {
|
|
24
|
+
root?: string;
|
|
25
|
+
path?: string;
|
|
26
|
+
} = {};
|
|
27
|
+
|
|
28
|
+
const dispatch = createEventDispatcher<{
|
|
29
|
+
click: { points: [number, number][]; polygon: Delaunay.Polygon };
|
|
30
|
+
pointermove: {
|
|
31
|
+
event: PointerEvent;
|
|
32
|
+
points: [number, number][];
|
|
33
|
+
polygon: Delaunay.Polygon;
|
|
34
|
+
};
|
|
35
|
+
}>();
|
|
36
|
+
|
|
37
|
+
$: points = (data ?? $flatData).map((d: any) => {
|
|
20
38
|
const xValue = $xContext(d);
|
|
21
39
|
const yValue = $yContext(d);
|
|
40
|
+
|
|
22
41
|
const x = Array.isArray(xValue) ? min(xValue) : xValue;
|
|
23
42
|
const y = Array.isArray(yValue) ? min(yValue) : yValue;
|
|
43
|
+
|
|
24
44
|
const point = [x, y];
|
|
25
45
|
// @ts-expect-error
|
|
26
46
|
point.data = d;
|
|
27
47
|
return point;
|
|
28
|
-
});
|
|
48
|
+
});
|
|
29
49
|
</script>
|
|
30
50
|
|
|
31
51
|
<g {...$$restProps} class={cls(classes.root, $$props.class)}>
|
|
@@ -1,78 +1,89 @@
|
|
|
1
|
-
<script>
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { type ComponentProps } from 'svelte';
|
|
3
|
+
import { format as formatValue, type FormatType } from '@layerstack/utils';
|
|
4
|
+
import { cls } from '@layerstack/tailwind';
|
|
5
|
+
|
|
6
|
+
import Text from './Text.svelte';
|
|
7
|
+
import { isScaleBand } from '../utils/scales.js';
|
|
8
|
+
import { chartContext } from './ChartContext.svelte';
|
|
9
|
+
import Points, { type Point } from './Points.svelte';
|
|
10
|
+
import { accessor, type Accessor } from '../utils/common.js';
|
|
11
|
+
|
|
12
|
+
const { xScale, yScale } = chartContext();
|
|
13
|
+
|
|
14
|
+
/** Override display value accessor. By default, uses `y` unless yScale is band scale */
|
|
15
|
+
export let value: Accessor = undefined;
|
|
16
|
+
|
|
17
|
+
export let placement: 'inside' | 'outside' | 'center' = 'outside';
|
|
18
|
+
export let offset = placement === 'center' ? 0 : 4;
|
|
19
|
+
export let format: FormatType | undefined = undefined;
|
|
20
|
+
|
|
21
|
+
$: getTextProps = (point: Point): ComponentProps<Text> => {
|
|
16
22
|
// Used for positioning
|
|
17
23
|
const pointValue = isScaleBand($yScale) ? point.xValue : point.yValue;
|
|
24
|
+
|
|
18
25
|
const displayValue = value
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
26
|
+
? accessor(value)(point.data)
|
|
27
|
+
: isScaleBand($yScale)
|
|
28
|
+
? point.xValue
|
|
29
|
+
: point.yValue;
|
|
30
|
+
|
|
31
|
+
const formattedValue = formatValue(
|
|
32
|
+
displayValue,
|
|
33
|
+
format ??
|
|
34
|
+
(value ? undefined : isScaleBand($yScale) ? $xScale.tickFormat?.() : $yScale.tickFormat?.())
|
|
35
|
+
);
|
|
36
|
+
|
|
25
37
|
if (isScaleBand($yScale)) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
//
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}
|
|
38
|
+
// Position label left/right on horizontal bars
|
|
39
|
+
if (pointValue < 0) {
|
|
40
|
+
// left
|
|
41
|
+
return {
|
|
42
|
+
value: formattedValue,
|
|
43
|
+
x: point.x + (placement === 'outside' ? -offset : offset),
|
|
44
|
+
y: point.y,
|
|
45
|
+
textAnchor: placement === 'outside' ? 'end' : 'start',
|
|
46
|
+
verticalAnchor: 'middle',
|
|
47
|
+
capHeight: '.6rem',
|
|
48
|
+
};
|
|
49
|
+
} else {
|
|
50
|
+
// right
|
|
51
|
+
return {
|
|
52
|
+
value: formattedValue,
|
|
53
|
+
x: point.x + (placement === 'outside' ? offset : -offset),
|
|
54
|
+
y: point.y,
|
|
55
|
+
textAnchor: placement === 'outside' ? 'start' : 'end',
|
|
56
|
+
verticalAnchor: 'middle',
|
|
57
|
+
capHeight: '.6rem',
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
} else {
|
|
61
|
+
// Position label top/bottom on vertical bars
|
|
62
|
+
if (pointValue < 0) {
|
|
63
|
+
// bottom
|
|
64
|
+
return {
|
|
65
|
+
value: formattedValue,
|
|
66
|
+
x: point.x,
|
|
67
|
+
y: point.y + (placement === 'outside' ? offset : -offset),
|
|
68
|
+
capHeight: '.6rem',
|
|
69
|
+
textAnchor: 'middle',
|
|
70
|
+
verticalAnchor:
|
|
71
|
+
placement === 'center' ? 'middle' : placement === 'outside' ? 'start' : 'end',
|
|
72
|
+
};
|
|
73
|
+
} else {
|
|
74
|
+
// top
|
|
75
|
+
return {
|
|
76
|
+
value: formattedValue,
|
|
77
|
+
x: point.x,
|
|
78
|
+
y: point.y + (placement === 'outside' ? -offset : offset),
|
|
79
|
+
capHeight: '.6rem',
|
|
80
|
+
textAnchor: 'middle',
|
|
81
|
+
verticalAnchor:
|
|
82
|
+
placement === 'center' ? 'middle' : placement === 'outside' ? 'end' : 'start',
|
|
83
|
+
};
|
|
84
|
+
}
|
|
74
85
|
}
|
|
75
|
-
};
|
|
86
|
+
};
|
|
76
87
|
</script>
|
|
77
88
|
|
|
78
89
|
<g class="Labels">
|
|
@@ -1,106 +1,137 @@
|
|
|
1
|
-
<script>
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { SVGAttributes } from 'svelte/elements';
|
|
3
|
+
import { scaleBand, scaleLinear } from 'd3-scale';
|
|
4
|
+
import { quantize, interpolate, interpolateRound } from 'd3-interpolate';
|
|
5
|
+
import { quantile, range } from 'd3-array';
|
|
6
|
+
import { format, type FormatType } from '@layerstack/utils';
|
|
7
|
+
|
|
8
|
+
import { chartContext } from './ChartContext.svelte';
|
|
9
|
+
import ColorRamp from './ColorRamp.svelte';
|
|
10
|
+
import { cls } from '@layerstack/tailwind';
|
|
11
|
+
|
|
12
|
+
const { cScale } = chartContext() ?? {};
|
|
13
|
+
|
|
14
|
+
type AnyScale = any;
|
|
15
|
+
type Placement =
|
|
16
|
+
| 'top-left'
|
|
17
|
+
| 'top'
|
|
18
|
+
| 'top-right'
|
|
19
|
+
| 'left'
|
|
20
|
+
| 'center'
|
|
21
|
+
| 'right'
|
|
22
|
+
| 'bottom-left'
|
|
23
|
+
| 'bottom'
|
|
24
|
+
| 'bottom-right';
|
|
25
|
+
|
|
26
|
+
export let scale: AnyScale = undefined;
|
|
27
|
+
export let title = '';
|
|
28
|
+
export let width = 320;
|
|
29
|
+
export let height = 10;
|
|
30
|
+
export let ticks = width / 64;
|
|
31
|
+
export let tickFormat: FormatType | undefined = undefined;
|
|
32
|
+
export let tickValues: any[] | undefined = undefined;
|
|
33
|
+
export let tickFontSize = 10;
|
|
34
|
+
export let tickLength = 4;
|
|
35
|
+
export let placement: Placement | undefined = undefined;
|
|
36
|
+
export let orientation: 'horizontal' | 'vertical' = 'horizontal';
|
|
37
|
+
|
|
38
|
+
/** Determine display ramp (individual color swatches or continuous ramp)*/
|
|
39
|
+
export let variant: 'ramp' | 'swatches' = 'ramp';
|
|
40
|
+
|
|
41
|
+
export let classes: {
|
|
42
|
+
root?: string;
|
|
43
|
+
title?: string;
|
|
44
|
+
label?: string;
|
|
45
|
+
tick?: string;
|
|
46
|
+
swatches?: string;
|
|
47
|
+
swatch?: string;
|
|
48
|
+
} = {};
|
|
49
|
+
|
|
50
|
+
$: if (scale == null && cScale) {
|
|
24
51
|
// Read scale from chart context
|
|
25
52
|
scale = $cScale;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
let
|
|
29
|
-
let
|
|
30
|
-
let
|
|
31
|
-
let
|
|
32
|
-
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
let xScale: AnyScale;
|
|
56
|
+
let interpolator: ((t: number) => string) | undefined;
|
|
57
|
+
let swatches: SVGAttributes<SVGRectElement>[];
|
|
58
|
+
let tickLabelOffset = 0;
|
|
59
|
+
let tickLine = true;
|
|
60
|
+
|
|
61
|
+
$: if (!scale) {
|
|
33
62
|
// do nothing
|
|
34
|
-
}
|
|
35
|
-
else if (scale.interpolate) {
|
|
63
|
+
} else if (scale.interpolate) {
|
|
36
64
|
// Continuous
|
|
37
65
|
const n = Math.min(scale.domain().length, scale.range().length);
|
|
38
66
|
xScale = scale.copy().rangeRound(quantize(interpolate(0, width), n));
|
|
39
67
|
interpolator = scale.copy().domain(quantize(interpolate(0, 1), n));
|
|
40
68
|
tickFormat = tickFormat ?? xScale.tickFormat?.();
|
|
41
|
-
}
|
|
42
|
-
else if (scale.interpolator) {
|
|
69
|
+
} else if (scale.interpolator) {
|
|
43
70
|
// Sequential
|
|
44
71
|
xScale = Object.assign(scale.copy().interpolator(interpolateRound(0, width)), {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
72
|
+
range() {
|
|
73
|
+
return [0, width];
|
|
74
|
+
},
|
|
48
75
|
});
|
|
49
76
|
interpolator = scale.interpolator();
|
|
77
|
+
|
|
50
78
|
// scaleSequentialQuantile doesn’t implement ticks or tickFormat.
|
|
51
79
|
if (!xScale.ticks) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
80
|
+
if (tickValues === undefined) {
|
|
81
|
+
const n = Math.round(ticks + 1);
|
|
82
|
+
tickValues = range(n).map((i) => quantile(scale.domain(), i / (n - 1)));
|
|
83
|
+
}
|
|
84
|
+
// if (typeof tickFormat !== "function") {
|
|
85
|
+
// tickFormat = d3.format(tickFormat === undefined ? ",f" : tickFormat);
|
|
86
|
+
// }
|
|
59
87
|
}
|
|
60
88
|
tickFormat = tickFormat ?? xScale.tickFormat?.();
|
|
61
|
-
}
|
|
62
|
-
else if (scale.invertExtent) {
|
|
89
|
+
} else if (scale.invertExtent) {
|
|
63
90
|
// Threshold
|
|
64
91
|
const thresholds = scale.thresholds
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
92
|
+
? scale.thresholds() // scaleQuantize
|
|
93
|
+
: scale.quantiles
|
|
94
|
+
? scale.quantiles() // scaleQuantile
|
|
95
|
+
: scale.domain(); // scaleThreshold
|
|
96
|
+
|
|
69
97
|
xScale = scaleLinear()
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
98
|
+
.domain([-1, scale.range().length - 1])
|
|
99
|
+
.rangeRound([0, width]);
|
|
100
|
+
|
|
101
|
+
swatches = scale.range().map((d: any, i: number) => {
|
|
102
|
+
return {
|
|
103
|
+
x: xScale(i - 1),
|
|
104
|
+
y: 0,
|
|
105
|
+
width: xScale(i) - xScale(i - 1),
|
|
106
|
+
height,
|
|
107
|
+
fill: d,
|
|
108
|
+
};
|
|
80
109
|
});
|
|
110
|
+
|
|
81
111
|
tickValues = range(thresholds.length);
|
|
82
112
|
tickFormat = (i) => {
|
|
83
|
-
|
|
84
|
-
|
|
113
|
+
const value = thresholds[i];
|
|
114
|
+
return $$props.tickFormat ? format(value, $$props.tickFormat) : value;
|
|
85
115
|
};
|
|
86
|
-
}
|
|
87
|
-
else {
|
|
116
|
+
} else {
|
|
88
117
|
// Ordinal
|
|
89
118
|
xScale = scaleBand().domain(scale.domain()).rangeRound([0, width]);
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
119
|
+
|
|
120
|
+
swatches = scale.domain().map((d: any) => {
|
|
121
|
+
return {
|
|
122
|
+
x: xScale(d),
|
|
123
|
+
y: 0,
|
|
124
|
+
width: Math.max(0, xScale.bandwidth() - 1),
|
|
125
|
+
height,
|
|
126
|
+
fill: scale(d),
|
|
127
|
+
};
|
|
98
128
|
});
|
|
129
|
+
|
|
99
130
|
tickValues = scale.domain();
|
|
100
131
|
tickLabelOffset = xScale.bandwidth() / 2;
|
|
101
132
|
tickLine = false;
|
|
102
133
|
tickLength = 0;
|
|
103
|
-
}
|
|
134
|
+
}
|
|
104
135
|
</script>
|
|
105
136
|
|
|
106
137
|
<div
|
|
@@ -181,7 +212,7 @@ else {
|
|
|
181
212
|
<div
|
|
182
213
|
class={cls('h-4 w-4 rounded-full', classes.swatch)}
|
|
183
214
|
style:background-color={scale(tick)}
|
|
184
|
-
|
|
215
|
+
></div>
|
|
185
216
|
<div class={cls('text-xs text-surface-content whitespace-nowrap', classes.label)}>
|
|
186
217
|
{tickFormat ? format(tick, tickFormat) : tick}
|
|
187
218
|
</div>
|
|
@@ -12,7 +12,7 @@ declare const __propDef: {
|
|
|
12
12
|
tickValues?: any[] | undefined;
|
|
13
13
|
tickFontSize?: number | undefined;
|
|
14
14
|
tickLength?: number | undefined;
|
|
15
|
-
placement?: ("
|
|
15
|
+
placement?: ("center" | "left" | "right" | "bottom" | "top" | "top-left" | "top-right" | "bottom-left" | "bottom-right") | undefined;
|
|
16
16
|
orientation?: ("horizontal" | "vertical") | undefined;
|
|
17
17
|
variant?: ("ramp" | "swatches") | undefined;
|
|
18
18
|
classes?: {
|