svelteplot 0.8.1 → 0.9.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.
Files changed (114) hide show
  1. package/README.md +1 -1
  2. package/dist/Mark.svelte +1 -1
  3. package/dist/Mark.svelte.d.ts +1 -0
  4. package/dist/constants.d.ts +2 -0
  5. package/dist/constants.js +2 -0
  6. package/dist/core/Plot.svelte +4 -4
  7. package/dist/helpers/projection.js +7 -2
  8. package/dist/hooks/usePlot.svelte.d.ts +51 -0
  9. package/dist/hooks/usePlot.svelte.js +90 -0
  10. package/dist/index.d.ts +1 -0
  11. package/dist/index.js +1 -0
  12. package/dist/marks/Area.svelte +3 -5
  13. package/dist/marks/Area.svelte.d.ts +1 -0
  14. package/dist/marks/AreaX.svelte.d.ts +1 -0
  15. package/dist/marks/Arrow.svelte +3 -5
  16. package/dist/marks/Arrow.svelte.d.ts +1 -0
  17. package/dist/marks/AxisX.svelte +2 -3
  18. package/dist/marks/AxisX.svelte.d.ts +1 -0
  19. package/dist/marks/AxisY.svelte +3 -4
  20. package/dist/marks/AxisY.svelte.d.ts +1 -0
  21. package/dist/marks/BarX.svelte +2 -4
  22. package/dist/marks/BarX.svelte.d.ts +1 -0
  23. package/dist/marks/BarY.svelte +2 -4
  24. package/dist/marks/BarY.svelte.d.ts +1 -0
  25. package/dist/marks/BollingerX.svelte.d.ts +1 -0
  26. package/dist/marks/BollingerY.svelte.d.ts +1 -0
  27. package/dist/marks/BoxX.svelte +4 -138
  28. package/dist/marks/BoxY.svelte +20 -137
  29. package/dist/marks/BoxY.svelte.d.ts +8 -3
  30. package/dist/marks/Brush.svelte +3 -3
  31. package/dist/marks/Brush.svelte.d.ts +1 -0
  32. package/dist/marks/Cell.svelte +2 -4
  33. package/dist/marks/Cell.svelte.d.ts +1 -0
  34. package/dist/marks/ColorLegend.svelte +2 -4
  35. package/dist/marks/CustomMark.svelte.d.ts +1 -0
  36. package/dist/marks/CustomMarkHTML.svelte +5 -10
  37. package/dist/marks/DifferenceY.svelte +3 -5
  38. package/dist/marks/DifferenceY.svelte.d.ts +1 -0
  39. package/dist/marks/Dot.svelte +4 -5
  40. package/dist/marks/Dot.svelte.d.ts +1 -0
  41. package/dist/marks/DotX.svelte.d.ts +1 -0
  42. package/dist/marks/DotY.svelte.d.ts +1 -0
  43. package/dist/marks/Frame.svelte +3 -9
  44. package/dist/marks/Frame.svelte.d.ts +1 -0
  45. package/dist/marks/Geo.svelte +5 -5
  46. package/dist/marks/Geo.svelte.d.ts +2 -0
  47. package/dist/marks/GridX.svelte +3 -10
  48. package/dist/marks/GridX.svelte.d.ts +1 -0
  49. package/dist/marks/GridY.svelte +3 -4
  50. package/dist/marks/GridY.svelte.d.ts +1 -0
  51. package/dist/marks/HTMLTooltip.svelte +5 -5
  52. package/dist/marks/Image.svelte.d.ts +1 -0
  53. package/dist/marks/Line.svelte +7 -6
  54. package/dist/marks/Line.svelte.d.ts +1 -0
  55. package/dist/marks/LineX.svelte.d.ts +1 -0
  56. package/dist/marks/LineY.svelte.d.ts +1 -0
  57. package/dist/marks/Link.svelte +2 -4
  58. package/dist/marks/Link.svelte.d.ts +1 -0
  59. package/dist/marks/Pointer.svelte +4 -4
  60. package/dist/marks/Rect.svelte +2 -4
  61. package/dist/marks/Rect.svelte.d.ts +1 -0
  62. package/dist/marks/RectX.svelte +4 -4
  63. package/dist/marks/RectY.svelte +4 -4
  64. package/dist/marks/RuleX.svelte +2 -4
  65. package/dist/marks/RuleX.svelte.d.ts +1 -0
  66. package/dist/marks/RuleY.svelte +2 -4
  67. package/dist/marks/RuleY.svelte.d.ts +1 -0
  68. package/dist/marks/Spike.svelte.d.ts +1 -0
  69. package/dist/marks/SymbolLegend.svelte +2 -4
  70. package/dist/marks/SymbolLegend.svelte.d.ts +17 -2
  71. package/dist/marks/Text.svelte.d.ts +1 -0
  72. package/dist/marks/TickX.svelte +2 -3
  73. package/dist/marks/TickX.svelte.d.ts +1 -0
  74. package/dist/marks/TickY.svelte +2 -3
  75. package/dist/marks/TickY.svelte.d.ts +1 -0
  76. package/dist/marks/Trail.svelte +161 -0
  77. package/dist/marks/Trail.svelte.d.ts +107 -0
  78. package/dist/marks/Vector.svelte +3 -4
  79. package/dist/marks/Vector.svelte.d.ts +1 -0
  80. package/dist/marks/WaffleX.svelte +2 -3
  81. package/dist/marks/WaffleX.svelte.d.ts +1 -0
  82. package/dist/marks/WaffleY.svelte +2 -4
  83. package/dist/marks/WaffleY.svelte.d.ts +1 -0
  84. package/dist/marks/helpers/AreaCanvas.svelte +2 -4
  85. package/dist/marks/helpers/Box.svelte +271 -0
  86. package/dist/marks/helpers/Box.svelte.d.ts +118 -0
  87. package/dist/marks/helpers/CanvasLayer.svelte +2 -4
  88. package/dist/marks/helpers/DotCanvas.svelte +3 -5
  89. package/dist/marks/helpers/GeoCanvas.svelte +2 -4
  90. package/dist/marks/helpers/LineCanvas.svelte +2 -4
  91. package/dist/marks/helpers/LinearGradientX.svelte +3 -4
  92. package/dist/marks/helpers/LinearGradientY.svelte +3 -4
  93. package/dist/marks/helpers/MarkerPath.svelte +4 -5
  94. package/dist/marks/helpers/MarkerPath.svelte.d.ts +1 -0
  95. package/dist/marks/helpers/MultilineText.svelte +4 -4
  96. package/dist/marks/helpers/RectPath.svelte +5 -6
  97. package/dist/marks/helpers/Regression.svelte +4 -8
  98. package/dist/marks/helpers/TrailCanvas.svelte +138 -0
  99. package/dist/marks/helpers/TrailCanvas.svelte.d.ts +40 -0
  100. package/dist/marks/helpers/events.d.ts +2 -2
  101. package/dist/marks/helpers/events.js +4 -4
  102. package/dist/marks/helpers/trail.d.ts +23 -0
  103. package/dist/marks/helpers/trail.js +372 -0
  104. package/dist/marks/index.d.ts +1 -0
  105. package/dist/marks/index.js +1 -0
  106. package/dist/transforms/bollinger.d.ts +1 -0
  107. package/dist/transforms/interval.d.ts +2 -0
  108. package/dist/transforms/select.d.ts +7 -0
  109. package/dist/transforms/sort.d.ts +4 -0
  110. package/dist/transforms/window.d.ts +2 -0
  111. package/dist/types/mark.d.ts +2 -1
  112. package/dist/types/plot.d.ts +6 -1
  113. package/dist/ui/Spiral.svelte +4 -0
  114. package/package.json +24 -23
@@ -0,0 +1,118 @@
1
+ import type { ChannelAccessor, DataRecord, RawValue } from '../../types';
2
+ declare function $$render<Datum extends DataRecord>(): {
3
+ props: Pick<Partial<{
4
+ filter: import("../../types").ConstantAccessor<boolean, Datum>;
5
+ facet: "auto" | "include" | "exclude";
6
+ fx: ChannelAccessor<Datum>;
7
+ fy: ChannelAccessor<Datum>;
8
+ dx: import("../../types").ConstantAccessor<number, Datum>;
9
+ dy: import("../../types").ConstantAccessor<number, Datum>;
10
+ dodgeX: import("../../transforms/dodge").DodgeXOptions;
11
+ dodgeY: import("../../transforms/dodge").DodgeYOptions;
12
+ fill: ChannelAccessor<Datum>;
13
+ fillOpacity: import("../../types").ConstantAccessor<number, Datum>;
14
+ sort: ((a: RawValue, b: RawValue) => number) | {
15
+ channel: string;
16
+ order?: "ascending" | "descending";
17
+ } | import("../../types").ConstantAccessor<RawValue, Datum>;
18
+ stroke: ChannelAccessor<Datum>;
19
+ strokeWidth: import("../../types").ConstantAccessor<number, Datum>;
20
+ strokeOpacity: import("../../types").ConstantAccessor<number, Datum>;
21
+ strokeLinejoin: import("../../types").ConstantAccessor<import("csstype").Property.StrokeLinejoin, Datum>;
22
+ strokeLinecap: import("../../types").ConstantAccessor<import("csstype").Property.StrokeLinecap, Datum>;
23
+ strokeMiterlimit: import("../../types").ConstantAccessor<number, Datum>;
24
+ opacity: ChannelAccessor<Datum>;
25
+ strokeDasharray: import("../../types").ConstantAccessor<string, Datum>;
26
+ strokeDashoffset: import("../../types").ConstantAccessor<number, Datum>;
27
+ mixBlendMode: import("../../types").ConstantAccessor<import("csstype").Property.MixBlendMode, Datum>;
28
+ clipPath: string;
29
+ mask: string;
30
+ imageFilter: import("../../types").ConstantAccessor<string, Datum>;
31
+ shapeRendering: import("../../types").ConstantAccessor<import("csstype").Property.ShapeRendering, Datum>;
32
+ paintOrder: import("../../types").ConstantAccessor<string, Datum>;
33
+ onclick: import("svelte/elements").MouseEventHandler<SVGPathElement>;
34
+ ondblclick: import("svelte/elements").MouseEventHandler<SVGPathElement>;
35
+ onmouseup: import("svelte/elements").MouseEventHandler<SVGPathElement>;
36
+ onmousedown: import("svelte/elements").MouseEventHandler<SVGPathElement>;
37
+ onmouseenter: import("svelte/elements").MouseEventHandler<SVGPathElement>;
38
+ onmousemove: import("svelte/elements").MouseEventHandler<SVGPathElement>;
39
+ onmouseleave: import("svelte/elements").MouseEventHandler<SVGPathElement>;
40
+ onmouseout: import("svelte/elements").MouseEventHandler<SVGPathElement>;
41
+ onmouseover: import("svelte/elements").MouseEventHandler<SVGPathElement>;
42
+ onpointercancel: import("svelte/elements").MouseEventHandler<SVGPathElement>;
43
+ onpointerdown: import("svelte/elements").MouseEventHandler<SVGPathElement>;
44
+ onpointerup: import("svelte/elements").MouseEventHandler<SVGPathElement>;
45
+ onpointerenter: import("svelte/elements").MouseEventHandler<SVGPathElement>;
46
+ onpointerleave: import("svelte/elements").MouseEventHandler<SVGPathElement>;
47
+ onpointermove: import("svelte/elements").MouseEventHandler<SVGPathElement>;
48
+ onpointerover: import("svelte/elements").MouseEventHandler<SVGPathElement>;
49
+ onpointerout: import("svelte/elements").MouseEventHandler<SVGPathElement>;
50
+ ondrag: import("svelte/elements").MouseEventHandler<SVGPathElement>;
51
+ ondrop: import("svelte/elements").MouseEventHandler<SVGPathElement>;
52
+ ondragstart: import("svelte/elements").MouseEventHandler<SVGPathElement>;
53
+ ondragenter: import("svelte/elements").MouseEventHandler<SVGPathElement>;
54
+ ondragleave: import("svelte/elements").MouseEventHandler<SVGPathElement>;
55
+ ondragover: import("svelte/elements").MouseEventHandler<SVGPathElement>;
56
+ ondragend: import("svelte/elements").MouseEventHandler<SVGPathElement>;
57
+ ontouchstart: import("svelte/elements").MouseEventHandler<SVGPathElement>;
58
+ ontouchmove: import("svelte/elements").MouseEventHandler<SVGPathElement>;
59
+ ontouchend: import("svelte/elements").MouseEventHandler<SVGPathElement>;
60
+ ontouchcancel: import("svelte/elements").MouseEventHandler<SVGPathElement>;
61
+ oncontextmenu: import("svelte/elements").MouseEventHandler<SVGPathElement>;
62
+ onwheel: import("svelte/elements").MouseEventHandler<SVGPathElement>;
63
+ class: string;
64
+ style: string;
65
+ cursor: import("../../types").ConstantAccessor<import("csstype").Property.Cursor, Datum>;
66
+ }>, "fx" | "fy" | "fill" | "stroke" | "class"> & {
67
+ data: Datum[];
68
+ x: ChannelAccessor;
69
+ y: ChannelAccessor;
70
+ /**
71
+ * Custom sort order for grouped box plot data
72
+ */
73
+ sort?: "min" | "max" | "median" | "p25" | "p75" | ((d: Datum) => RawValue);
74
+ /**
75
+ * Options for the rule marks that represent the min/max range
76
+ */
77
+ rule: Record<string, ChannelAccessor<Datum>>;
78
+ /**
79
+ * Options for the bar marks that represent the IQR range
80
+ */
81
+ bar: Record<string, ChannelAccessor<Datum>>;
82
+ /**
83
+ * Options for the tick marks that represent the median
84
+ */
85
+ tickMedian: Record<string, ChannelAccessor<Datum>> | boolean;
86
+ /**
87
+ * Options for the tick marks that represent the min/max range
88
+ */
89
+ tickMinMax: Record<string, ChannelAccessor<Datum>> | boolean;
90
+ /**
91
+ * Options for the dot marks that represent the outliers
92
+ */
93
+ dot: Record<string, ChannelAccessor<Datum>>;
94
+ orientation: "x" | "y";
95
+ };
96
+ exports: {};
97
+ bindings: "";
98
+ slots: {};
99
+ events: {};
100
+ };
101
+ declare class __sveltets_Render<Datum extends DataRecord> {
102
+ props(): ReturnType<typeof $$render<Datum>>['props'];
103
+ events(): ReturnType<typeof $$render<Datum>>['events'];
104
+ slots(): ReturnType<typeof $$render<Datum>>['slots'];
105
+ bindings(): "";
106
+ exports(): {};
107
+ }
108
+ interface $$IsomorphicComponent {
109
+ new <Datum extends DataRecord>(options: import('svelte').ComponentConstructorOptions<ReturnType<__sveltets_Render<Datum>['props']>>): import('svelte').SvelteComponent<ReturnType<__sveltets_Render<Datum>['props']>, ReturnType<__sveltets_Render<Datum>['events']>, ReturnType<__sveltets_Render<Datum>['slots']>> & {
110
+ $$bindings?: ReturnType<__sveltets_Render<Datum>['bindings']>;
111
+ } & ReturnType<__sveltets_Render<Datum>['exports']>;
112
+ <Datum extends DataRecord>(internal: unknown, props: ReturnType<__sveltets_Render<Datum>['props']> & {}): ReturnType<__sveltets_Render<Datum>['exports']>;
113
+ z_$$bindings?: ReturnType<__sveltets_Render<any>['bindings']>;
114
+ }
115
+ /** Internal shared box plot implementation for BoxX and BoxY */
116
+ declare const Box: $$IsomorphicComponent;
117
+ type Box<Datum extends DataRecord> = InstanceType<typeof Box<Datum>>;
118
+ export default Box;
@@ -1,9 +1,8 @@
1
1
  <script lang="ts">
2
- import { getContext } from 'svelte';
3
- import type { PlotContext } from '../../types/plot';
4
2
  import { devicePixelRatio } from 'svelte/reactivity/window';
5
3
  import { MediaQuery } from 'svelte/reactivity';
6
4
  import type { Attachment } from 'svelte/attachments';
5
+ import { usePlot } from '../../hooks/usePlot.svelte.js';
7
6
 
8
7
  const darkMode = new MediaQuery('prefers-color-scheme: dark');
9
8
  let colorScheme = $state();
@@ -26,8 +25,7 @@
26
25
 
27
26
  let restProps: {} = $props();
28
27
 
29
- const { getPlotState } = getContext<PlotContext>('svelteplot');
30
- const plot = $derived(getPlotState());
28
+ const plot = usePlot();
31
29
  </script>
32
30
 
33
31
  <!--
@@ -3,20 +3,18 @@
3
3
  PlotState,
4
4
  Mark,
5
5
  BaseMarkProps,
6
- ScaledDataRecord,
7
- PlotContext
6
+ ScaledDataRecord
8
7
  } from '../../types/index.js';
9
8
  import { resolveProp } from '../../helpers/resolve.js';
10
9
  import { maybeSymbol } from '../../helpers/symbols.js';
11
10
  import { symbol as d3Symbol } from 'd3-shape';
12
11
  import type { Attachment } from 'svelte/attachments';
13
12
  import CanvasLayer from './CanvasLayer.svelte';
14
- import { getContext } from 'svelte';
15
13
  import { devicePixelRatio } from 'svelte/reactivity/window';
16
14
  import { resolveColor } from './canvas.js';
15
+ import { usePlot } from '../../hooks/usePlot.svelte.js';
17
16
 
18
- const { getPlotState } = getContext<PlotContext>('svelteplot');
19
- const plot = $derived(getPlotState());
17
+ const plot = usePlot();
20
18
 
21
19
  let {
22
20
  mark,
@@ -2,18 +2,17 @@
2
2
  import type {
3
3
  Mark,
4
4
  BaseMarkProps,
5
- PlotContext,
6
5
  ScaledDataRecord,
7
6
  UsedScales
8
7
  } from '../../types/index.js';
9
8
  import { CSS_VAR } from '../../constants.js';
10
9
  import { resolveProp, resolveScaledStyleProps } from '../../helpers/resolve.js';
11
- import { getContext } from 'svelte';
12
10
  import { type GeoPath } from 'd3-geo';
13
11
  import CanvasLayer from './CanvasLayer.svelte';
14
12
  import type { Attachment } from 'svelte/attachments';
15
13
  import { devicePixelRatio } from 'svelte/reactivity/window';
16
14
  import { GEOJSON_PREFER_STROKE } from '../../helpers/index.js';
15
+ import { usePlot } from '../../hooks/usePlot.svelte.js';
17
16
 
18
17
  let {
19
18
  mark,
@@ -27,8 +26,7 @@
27
26
  usedScales: UsedScales;
28
27
  } = $props();
29
28
 
30
- const { getPlotState } = getContext<PlotContext>('svelteplot');
31
- const plot = $derived(getPlotState());
29
+ const plot = usePlot();
32
30
 
33
31
  function maybeOpacity(value) {
34
32
  return value == null ? 1 : +value;
@@ -2,17 +2,16 @@
2
2
  import type {
3
3
  Mark,
4
4
  BaseMarkProps,
5
- PlotContext,
6
5
  ScaledDataRecord,
7
6
  UsedScales
8
7
  } from '../../types/index.js';
9
8
  import { resolveProp, resolveScaledStyleProps } from '../../helpers/resolve.js';
10
- import { getContext } from 'svelte';
11
9
  import { type Line } from 'd3-shape';
12
10
  import CanvasLayer from './CanvasLayer.svelte';
13
11
  import type { Attachment } from 'svelte/attachments';
14
12
  import { devicePixelRatio } from 'svelte/reactivity/window';
15
13
  import { resolveColor } from './canvas.js';
14
+ import { usePlot } from '../../hooks/usePlot.svelte.js';
16
15
 
17
16
  let {
18
17
  mark,
@@ -27,8 +26,7 @@
27
26
  groupByKey?: unknown;
28
27
  } = $props();
29
28
 
30
- const { getPlotState } = getContext<PlotContext>('svelteplot');
31
- const plot = $derived(getPlotState());
29
+ const plot = usePlot();
32
30
 
33
31
  function maybeOpacity(value: unknown) {
34
32
  return value == null ? 1 : +value;
@@ -1,6 +1,6 @@
1
1
  <script lang="ts">
2
- import { getContext } from 'svelte';
3
- import type { PlotContext, RawValue } from '../../index.js';
2
+ import type { RawValue } from '../../index.js';
3
+ import { usePlot } from '../../hooks/usePlot.svelte.js';
4
4
 
5
5
  let {
6
6
  id,
@@ -10,8 +10,7 @@
10
10
  stops: { x: RawValue; color: string }[];
11
11
  } = $props();
12
12
 
13
- const { getPlotState } = getContext<PlotContext>('svelteplot');
14
- const plot = $derived(getPlotState());
13
+ const plot = usePlot();
15
14
 
16
15
  const projectedStops = $derived(
17
16
  stops
@@ -1,6 +1,6 @@
1
1
  <script lang="ts">
2
- import { getContext } from 'svelte';
3
- import type { PlotContext, RawValue } from '../../index.js';
2
+ import type { RawValue } from '../../index.js';
3
+ import { usePlot } from '../../hooks/usePlot.svelte.js';
4
4
 
5
5
  let {
6
6
  id,
@@ -10,8 +10,7 @@
10
10
  stops: { y: RawValue; color: string }[];
11
11
  } = $props();
12
12
 
13
- const { getPlotState } = getContext<PlotContext>('svelteplot');
14
- const plot = $derived(getPlotState());
13
+ const plot = usePlot();
15
14
 
16
15
  const projectedStops = $derived(
17
16
  stops
@@ -11,11 +11,10 @@
11
11
  ConstantAccessor,
12
12
  DataRecord,
13
13
  Mark,
14
- PlotContext,
15
14
  PlotScales
16
15
  } from '../../types/index.js';
17
16
  import { addEventHandlers } from './events.js';
18
- import { getContext } from 'svelte';
17
+ import { usePlot } from '../../hooks/usePlot.svelte.js';
19
18
 
20
19
  type MarkerPathProps = BaseMarkProps<Datum> & {
21
20
  /**
@@ -78,7 +77,7 @@
78
77
 
79
78
  const id = randomId();
80
79
 
81
- const { getPlotState } = getContext<PlotContext>('svelteplot');
80
+ const plot = usePlot();
82
81
 
83
82
  const points = $derived(text && d != null ? d.split(/[LMC]/).slice(1) : []);
84
83
  const hasPath = $derived(points.length > 0);
@@ -96,7 +95,7 @@
96
95
  class={className}
97
96
  stroke-width={strokeWidth_}
98
97
  {@attach addEventHandlers({
99
- getPlotState,
98
+ plot,
100
99
  options: mark.options,
101
100
  datum: datum
102
101
  })}>
@@ -127,7 +126,7 @@
127
126
  {d}
128
127
  {style}
129
128
  {@attach addEventHandlers({
130
- getPlotState,
129
+ plot,
131
130
  options: mark.options,
132
131
  datum: datum
133
132
  })} />
@@ -27,6 +27,7 @@ declare function $$render<Datum extends DataRecord>(): {
27
27
  strokeDashoffset: ConstantAccessor<number, Datum>;
28
28
  mixBlendMode: ConstantAccessor<import("csstype").Property.MixBlendMode, Datum>;
29
29
  clipPath: string;
30
+ mask: string;
30
31
  imageFilter: ConstantAccessor<string, Datum>;
31
32
  shapeRendering: ConstantAccessor<import("csstype").Property.ShapeRendering, Datum>;
32
33
  paintOrder: ConstantAccessor<string, Datum>;
@@ -1,10 +1,11 @@
1
1
  <script lang="ts">
2
2
  import { resolveProp, resolveStyles } from '../../helpers/resolve';
3
- import { getContext, type ComponentProps } from 'svelte';
4
- import type { PlotContext, ScaledDataRecord, UsedScales } from '../../index.js';
3
+ import { type ComponentProps } from 'svelte';
4
+ import type { ScaledDataRecord, UsedScales } from '../../index.js';
5
5
  import type Text from '../Text.svelte';
6
6
  import { CSS_VAR } from '../../constants';
7
7
  import { maybeFromPixel, maybeFromRem } from '../../helpers/getBaseStyles';
8
+ import { usePlot } from '../../hooks/usePlot.svelte.js';
8
9
 
9
10
  const LINE_ANCHOR = {
10
11
  bottom: 'auto',
@@ -12,8 +13,7 @@
12
13
  top: 'hanging'
13
14
  } as const;
14
15
 
15
- const { getPlotState } = getContext<PlotContext>('svelteplot');
16
- const plot = $derived(getPlotState());
16
+ const plot = usePlot();
17
17
 
18
18
  let {
19
19
  textLines,
@@ -29,11 +29,11 @@ Helper component for rendering rectangular marks in SVG
29
29
  import { resolveProp, resolveStyles } from '../../helpers/resolve';
30
30
  import { roundedRect } from '../../helpers/roundedRect';
31
31
  import { addEventHandlers } from './events.js';
32
- import { getContext } from 'svelte';
33
32
  import Anchor from './Anchor.svelte';
34
33
  import type { BaseMarkProps, BaseRectMarkProps, BorderRadius } from '../../types/mark.js';
35
34
  import type { DataRecord, ScaledDataRecord } from '../../types/data.js';
36
- import type { PlotContext, UsedScales } from '../../types/index.js';
35
+ import type { UsedScales } from '../../types/index.js';
36
+ import { usePlot } from '../../hooks/usePlot.svelte.js';
37
37
 
38
38
  let {
39
39
  datum,
@@ -49,8 +49,7 @@ Helper component for rendering rectangular marks in SVG
49
49
  fallbackStyle
50
50
  }: RectPathProps = $props();
51
51
 
52
- const { getPlotState } = getContext<PlotContext>('svelteplot');
53
- const plot = $derived(getPlotState());
52
+ const plot = usePlot();
54
53
 
55
54
  const inset = $derived(+(resolveProp(options.inset, datum?.datum, 0) as number));
56
55
  const insetLeft = $derived(
@@ -119,7 +118,7 @@ Helper component for rendering rectangular marks in SVG
119
118
  class={[styleClass, className]}
120
119
  {style}
121
120
  {@attach addEventHandlers({
122
- getPlotState,
121
+ plot,
123
122
  options,
124
123
  datum: datum?.datum
125
124
  })} />
@@ -131,7 +130,7 @@ Helper component for rendering rectangular marks in SVG
131
130
  class={[styleClass, className]}
132
131
  {style}
133
132
  {@attach addEventHandlers({
134
- getPlotState,
133
+ plot,
135
134
  options,
136
135
  datum: datum?.datum
137
136
  })} />
@@ -1,10 +1,5 @@
1
1
  <script module lang="ts">
2
- import type {
3
- BaseMarkProps,
4
- ChannelAccessor,
5
- FacetContext,
6
- PlotContext
7
- } from '../../types/index.js';
2
+ import type { BaseMarkProps, ChannelAccessor, FacetContext } from '../../types/index.js';
8
3
 
9
4
  type RegressionType = 'linear' | 'quad' | 'poly' | 'exp' | 'log' | 'pow' | 'loess';
10
5
 
@@ -65,8 +60,9 @@
65
60
  throw new Error('unknown regression ' + name);
66
61
  }
67
62
 
68
- const { getPlotState } = getContext<PlotContext>('svelteplot');
69
- let plot = $derived(getPlotState());
63
+ import { usePlot } from '../../hooks/usePlot.svelte.js';
64
+
65
+ const plot = usePlot();
70
66
 
71
67
  let {
72
68
  data,
@@ -0,0 +1,138 @@
1
+ <script lang="ts" generics="Datum extends DataRecord">
2
+ import type {
3
+ Mark,
4
+ BaseMarkProps,
5
+ ScaledDataRecord,
6
+ UsedScales,
7
+ CurveName,
8
+ ConstantAccessor
9
+ } from '../../types/index.js';
10
+ import CanvasLayer from './CanvasLayer.svelte';
11
+ import type { Attachment } from 'svelte/attachments';
12
+ import { devicePixelRatio } from 'svelte/reactivity/window';
13
+ import { resolveColor } from './canvas.js';
14
+ import type { CurveFactory } from 'd3-shape';
15
+ import { trailPath, type TrailSample } from './trail';
16
+ import { resolveProp, resolveScaledStyleProps } from '../../helpers/resolve';
17
+ import { usePlot } from '../../hooks/usePlot.svelte.js';
18
+
19
+ interface TrailCanvasProps<Datum> {
20
+ curve?: CurveName | CurveFactory;
21
+ tension?: number;
22
+ cap?: 'butt' | 'round';
23
+ resolution?: number | 'auto';
24
+ data: ScaledDataRecord<Datum>[][];
25
+ usedScales: UsedScales;
26
+ options: {
27
+ fill?: ConstantAccessor<string, Datum>;
28
+ defined?: ConstantAccessor<boolean, Datum>;
29
+ opacity?: ConstantAccessor<number, Datum>;
30
+ 'fill-opacity'?: ConstantAccessor<number, Datum>;
31
+ };
32
+ }
33
+
34
+ let {
35
+ curve,
36
+ cap,
37
+ tension,
38
+ resolution,
39
+ usedScales,
40
+ data: groupedTrailData,
41
+ options
42
+ }: TrailCanvasProps<Datum> = $props();
43
+
44
+ function maybeOpacity(value: unknown) {
45
+ return value == null ? 1 : +value;
46
+ }
47
+
48
+ const plot = usePlot();
49
+
50
+ const render = ((canvas: HTMLCanvasElement) => {
51
+ const context = canvas.getContext('2d');
52
+
53
+ $effect(() => {
54
+ if (context) {
55
+ context.resetTransform();
56
+ context.scale(devicePixelRatio.current ?? 1, devicePixelRatio.current ?? 1);
57
+
58
+ for (const trailData of groupedTrailData) {
59
+ if (trailData.length < 2) continue;
60
+
61
+ // Get the first point to determine line styles
62
+ const firstPoint = trailData[0];
63
+ if (!firstPoint || !firstPoint.valid) continue;
64
+
65
+ const samples = trailData.map((d) => ({
66
+ x: Number(d.x),
67
+ y: Number(d.y),
68
+ r: Number(d.r ?? 0)
69
+ })) satisfies TrailSample[];
70
+
71
+ const defined = trailData.map(
72
+ (d) =>
73
+ d.valid &&
74
+ d.r >= 0 &&
75
+ (resolveProp(options.defined, d.datum, true) ?? true)
76
+ );
77
+
78
+ let { fill, ...restStyles } = resolveScaledStyleProps(
79
+ firstPoint.datum,
80
+ options,
81
+ usedScales,
82
+ plot,
83
+ 'fill'
84
+ );
85
+
86
+ const opacity = maybeOpacity(restStyles['opacity']);
87
+ const fillOpacity = maybeOpacity(restStyles['fill-opacity']);
88
+
89
+ fill = resolveColor(fill, canvas);
90
+
91
+ context.fillStyle = fill ? fill : 'currentColor';
92
+ context.beginPath();
93
+
94
+ trailPath(samples, defined, context, {
95
+ curve,
96
+ cap,
97
+ tension,
98
+ ...(typeof resolution === 'number' ? { samplesPerSegment: resolution } : {})
99
+ });
100
+
101
+ context.globalAlpha = opacity * fillOpacity;
102
+ context.fill();
103
+
104
+ // {#each groupedTrailData as trailData, i (i)}
105
+ // {@const samples = trailData.map((d) => ({
106
+ // x: Number(d.x),
107
+ // y: Number(d.y),
108
+ // r: Number(d.r ?? 0)
109
+ // })) satisfies TrailSample[]}
110
+ // {@const defined = trailData.map(
111
+ // (d) =>
112
+ // d.valid &&
113
+ // d.r >= 0 &&
114
+ // (resolveProp(options.defined, d.datum, true) ?? true)
115
+ // )}
116
+ // {@const pathString = trailPath(samples, defined, d3Path(), {
117
+ // curve,
118
+ // cap,
119
+ // tension,
120
+ // ...(typeof resolution === 'number'
121
+ // ? { samplesPerSegment: resolution }
122
+ // : {})
123
+ }
124
+ }
125
+
126
+ return () => {
127
+ context?.clearRect(
128
+ 0,
129
+ 0,
130
+ plot.width * (devicePixelRatio.current ?? 1),
131
+ plot.height * (devicePixelRatio.current ?? 1)
132
+ );
133
+ };
134
+ });
135
+ }) as Attachment;
136
+ </script>
137
+
138
+ <CanvasLayer {@attach render} />
@@ -0,0 +1,40 @@
1
+ import type { ScaledDataRecord, UsedScales, CurveName, ConstantAccessor } from '../../types/index.js';
2
+ import type { CurveFactory } from 'd3-shape';
3
+ interface TrailCanvasProps<Datum> {
4
+ curve?: CurveName | CurveFactory;
5
+ tension?: number;
6
+ cap?: 'butt' | 'round';
7
+ resolution?: number | 'auto';
8
+ data: ScaledDataRecord<Datum>[][];
9
+ usedScales: UsedScales;
10
+ options: {
11
+ fill?: ConstantAccessor<string, Datum>;
12
+ defined?: ConstantAccessor<boolean, Datum>;
13
+ opacity?: ConstantAccessor<number, Datum>;
14
+ 'fill-opacity'?: ConstantAccessor<number, Datum>;
15
+ };
16
+ }
17
+ declare function $$render<Datum extends DataRecord>(): {
18
+ props: TrailCanvasProps<Datum>;
19
+ exports: {};
20
+ bindings: "";
21
+ slots: {};
22
+ events: {};
23
+ };
24
+ declare class __sveltets_Render<Datum extends DataRecord> {
25
+ props(): ReturnType<typeof $$render<Datum>>['props'];
26
+ events(): ReturnType<typeof $$render<Datum>>['events'];
27
+ slots(): ReturnType<typeof $$render<Datum>>['slots'];
28
+ bindings(): "";
29
+ exports(): {};
30
+ }
31
+ interface $$IsomorphicComponent {
32
+ new <Datum extends DataRecord>(options: import('svelte').ComponentConstructorOptions<ReturnType<__sveltets_Render<Datum>['props']>>): import('svelte').SvelteComponent<ReturnType<__sveltets_Render<Datum>['props']>, ReturnType<__sveltets_Render<Datum>['events']>, ReturnType<__sveltets_Render<Datum>['slots']>> & {
33
+ $$bindings?: ReturnType<__sveltets_Render<Datum>['bindings']>;
34
+ } & ReturnType<__sveltets_Render<Datum>['exports']>;
35
+ <Datum extends DataRecord>(internal: unknown, props: ReturnType<__sveltets_Render<Datum>['props']> & {}): ReturnType<__sveltets_Render<Datum>['exports']>;
36
+ z_$$bindings?: ReturnType<__sveltets_Render<any>['bindings']>;
37
+ }
38
+ declare const TrailCanvas: $$IsomorphicComponent;
39
+ type TrailCanvas<Datum extends DataRecord> = InstanceType<typeof TrailCanvas<Datum>>;
40
+ export default TrailCanvas;
@@ -13,8 +13,8 @@ declare global {
13
13
  * of the plot frame, regardless of which element triggered the event
14
14
  */
15
15
  export declare function clientToLayerCoordinates(event: MouseEvent, plotBody: HTMLElement | null | undefined): [number, number];
16
- export declare function addEventHandlers<T extends DataRow>({ options, datum, getPlotState }: {
16
+ export declare function addEventHandlers<T extends DataRow>({ options, datum, plot }: {
17
17
  options: BaseMarkProps<T>;
18
18
  datum: DataRecord;
19
- getPlotState: () => PlotState;
19
+ plot: PlotState;
20
20
  }): Attachment;
@@ -19,7 +19,7 @@ export function clientToLayerCoordinates(event, plotBody) {
19
19
  // Calculate the coordinates relative to the plot body
20
20
  return [event.clientX - plotBodyRect.left, event.clientY - plotBodyRect.top];
21
21
  }
22
- export function addEventHandlers({ options, datum, getPlotState }) {
22
+ export function addEventHandlers({ options, datum, plot }) {
23
23
  const events = pick(options, [
24
24
  'onclick',
25
25
  'oncontextmenu',
@@ -57,7 +57,7 @@ export function addEventHandlers({ options, datum, getPlotState }) {
57
57
  for (const [eventName, eventHandler] of Object.entries(events)) {
58
58
  if (eventHandler) {
59
59
  const wrappedHandler = (origEvent) => {
60
- const { scales, body, options } = getPlotState();
60
+ const { scales, body, options: plotOptions } = plot;
61
61
  if (origEvent instanceof MouseEvent || origEvent instanceof PointerEvent) {
62
62
  let facetEl = origEvent.target;
63
63
  while (facetEl &&
@@ -69,8 +69,8 @@ export function addEventHandlers({ options, datum, getPlotState }) {
69
69
  facetEl = facetEl.parentElement;
70
70
  }
71
71
  const facetRect = (facetEl?.firstElementChild ?? body).getBoundingClientRect();
72
- const relativeX = origEvent.clientX - facetRect.left + (options.marginLeft ?? 0);
73
- const relativeY = origEvent.clientY - facetRect.top + (options.marginTop ?? 0);
72
+ const relativeX = origEvent.clientX - facetRect.left + (plotOptions.marginLeft ?? 0);
73
+ const relativeY = origEvent.clientY - facetRect.top + (plotOptions.marginTop ?? 0);
74
74
  if (scales.projection) {
75
75
  const [x, y] = scales.projection.invert([relativeX, relativeY]);
76
76
  origEvent.dataX = x;
@@ -0,0 +1,23 @@
1
+ import type { Path } from 'd3-path';
2
+ import type { CurveName } from '../../types/index.js';
3
+ import type { CurveFactory } from 'd3-shape';
4
+ export type TrailContext = CanvasRenderingContext2D | Path;
5
+ export type TrailCurve = CurveName | CurveFactory;
6
+ export type TrailCap = 'round' | 'butt';
7
+ export type TrailOptions = {
8
+ curve?: TrailCurve;
9
+ samplesPerSegment?: number;
10
+ cap?: TrailCap;
11
+ tension?: number;
12
+ };
13
+ export type TrailSample = {
14
+ x: number;
15
+ y: number;
16
+ r: number;
17
+ };
18
+ /**
19
+ * Draw a stroked capsule trail along successive points with varying widths.
20
+ * Adapted from Vega's trail mark implementation.
21
+ */
22
+ export declare function trailPath(samples: TrailSample[], defined: boolean[], context: TrailContext, options?: TrailOptions): string | void;
23
+ export default trailPath;