svelteplot 0.0.1-alpha.9 → 0.1.3-next.12

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 (251) hide show
  1. package/LICENSE.md +5 -0
  2. package/README.md +3 -35
  3. package/dist/Mark.svelte +292 -0
  4. package/dist/Mark.svelte.d.ts +22 -0
  5. package/dist/Plot.svelte +148 -156
  6. package/dist/Plot.svelte.d.ts +15 -15
  7. package/dist/constants.d.ts +15 -0
  8. package/dist/constants.js +110 -0
  9. package/dist/core/Facet.svelte +59 -0
  10. package/dist/core/Facet.svelte.d.ts +18 -0
  11. package/dist/core/FacetAxes.svelte +66 -0
  12. package/dist/core/FacetAxes.svelte.d.ts +4 -0
  13. package/dist/core/FacetGrid.svelte +86 -0
  14. package/dist/core/FacetGrid.svelte.d.ts +13 -0
  15. package/dist/core/Plot.svelte +568 -0
  16. package/dist/core/Plot.svelte.d.ts +14 -0
  17. package/dist/helpers/arrowPath.d.ts +14 -0
  18. package/dist/helpers/arrowPath.js +129 -0
  19. package/dist/helpers/autoProjection.d.ts +19 -0
  20. package/dist/helpers/autoProjection.js +87 -0
  21. package/dist/helpers/autoScales.d.ts +23 -0
  22. package/dist/helpers/autoScales.js +203 -0
  23. package/dist/helpers/autoTicks.d.ts +3 -0
  24. package/dist/helpers/autoTicks.js +40 -0
  25. package/dist/helpers/autoTimeFormat.d.ts +2 -2
  26. package/dist/helpers/autoTimeFormat.js +34 -5
  27. package/dist/helpers/callWithProps.d.ts +8 -0
  28. package/dist/helpers/callWithProps.js +13 -0
  29. package/dist/helpers/colors.js +17 -2
  30. package/dist/helpers/curves.d.ts +3 -0
  31. package/dist/helpers/curves.js +42 -0
  32. package/dist/helpers/data.d.ts +9 -0
  33. package/dist/helpers/data.js +16 -0
  34. package/dist/helpers/facets.d.ts +12 -0
  35. package/dist/helpers/facets.js +49 -0
  36. package/dist/helpers/formats.d.ts +3 -0
  37. package/dist/helpers/formats.js +3 -0
  38. package/dist/helpers/getBaseStyles.d.ts +7 -2
  39. package/dist/helpers/getBaseStyles.js +34 -10
  40. package/dist/helpers/getLogTicks.js +5 -5
  41. package/dist/helpers/group.d.ts +6 -0
  42. package/dist/helpers/group.js +53 -0
  43. package/dist/helpers/index.d.ts +18 -0
  44. package/dist/helpers/index.js +55 -0
  45. package/dist/helpers/isRawValue.d.ts +2 -0
  46. package/dist/helpers/isRawValue.js +5 -0
  47. package/dist/helpers/isValid.d.ts +6 -0
  48. package/dist/helpers/isValid.js +6 -0
  49. package/dist/helpers/math.d.ts +19 -0
  50. package/dist/helpers/math.js +116 -0
  51. package/dist/helpers/mergeDeep.d.ts +1 -1
  52. package/dist/helpers/noise.d.ts +1 -0
  53. package/dist/helpers/noise.js +72 -0
  54. package/dist/helpers/projection.d.ts +33 -0
  55. package/dist/helpers/projection.js +100 -0
  56. package/dist/helpers/reduce.d.ts +10 -0
  57. package/dist/helpers/reduce.js +85 -0
  58. package/dist/helpers/regressionLoess.d.ts +12 -0
  59. package/dist/helpers/regressionLoess.js +47 -0
  60. package/dist/helpers/removeIdenticalLines.d.ts +8 -1
  61. package/dist/helpers/removeIdenticalLines.js +14 -7
  62. package/dist/helpers/resolve.d.ts +21 -0
  63. package/dist/helpers/resolve.js +156 -0
  64. package/dist/helpers/roundedRect.d.ts +9 -0
  65. package/dist/helpers/roundedRect.js +31 -0
  66. package/dist/helpers/scales.d.ts +42 -0
  67. package/dist/helpers/scales.js +311 -0
  68. package/dist/helpers/time.d.ts +6 -0
  69. package/dist/helpers/time.js +282 -0
  70. package/dist/helpers/typeChecks.d.ts +8 -5
  71. package/dist/helpers/typeChecks.js +27 -6
  72. package/dist/index.d.ts +49 -1
  73. package/dist/index.js +53 -2
  74. package/dist/marks/Area.svelte +146 -0
  75. package/dist/marks/Area.svelte.d.ts +30 -0
  76. package/dist/marks/AreaX.svelte +27 -0
  77. package/dist/marks/AreaX.svelte.d.ts +12 -0
  78. package/dist/marks/AreaY.svelte +38 -0
  79. package/dist/marks/AreaY.svelte.d.ts +19 -0
  80. package/dist/marks/Arrow.svelte +139 -0
  81. package/dist/marks/Arrow.svelte.d.ts +44 -0
  82. package/dist/marks/AxisX.svelte +198 -93
  83. package/dist/marks/AxisX.svelte.d.ts +17 -16
  84. package/dist/marks/AxisY.svelte +176 -62
  85. package/dist/marks/AxisY.svelte.d.ts +17 -14
  86. package/dist/marks/BarX.svelte +93 -0
  87. package/dist/marks/BarX.svelte.d.ts +4 -0
  88. package/dist/marks/BarY.svelte +103 -0
  89. package/dist/marks/BarY.svelte.d.ts +25 -0
  90. package/dist/marks/BollingerX.svelte +44 -0
  91. package/dist/marks/BollingerX.svelte.d.ts +18 -0
  92. package/dist/marks/BollingerY.svelte +39 -0
  93. package/dist/marks/BollingerY.svelte.d.ts +18 -0
  94. package/dist/marks/BoxX.svelte +89 -0
  95. package/dist/marks/BoxX.svelte.d.ts +4 -0
  96. package/dist/marks/BoxY.svelte +110 -0
  97. package/dist/marks/BoxY.svelte.d.ts +29 -0
  98. package/dist/marks/Cell.svelte +110 -0
  99. package/dist/marks/Cell.svelte.d.ts +16 -0
  100. package/dist/marks/CellX.svelte +24 -0
  101. package/dist/marks/CellX.svelte.d.ts +3 -0
  102. package/dist/marks/CellY.svelte +24 -0
  103. package/dist/marks/CellY.svelte.d.ts +3 -0
  104. package/dist/marks/ColorLegend.svelte +148 -27
  105. package/dist/marks/ColorLegend.svelte.d.ts +12 -13
  106. package/dist/marks/CustomMark.svelte +43 -0
  107. package/dist/marks/CustomMark.svelte.d.ts +16 -0
  108. package/dist/marks/CustomMarkHTML.svelte +103 -0
  109. package/dist/marks/CustomMarkHTML.svelte.d.ts +17 -0
  110. package/dist/marks/DifferenceY.svelte +144 -0
  111. package/dist/marks/DifferenceY.svelte.d.ts +30 -0
  112. package/dist/marks/Dot.svelte +128 -73
  113. package/dist/marks/Dot.svelte.d.ts +24 -14
  114. package/dist/marks/DotX.svelte +15 -3
  115. package/dist/marks/DotX.svelte.d.ts +8 -16
  116. package/dist/marks/DotY.svelte +8 -3
  117. package/dist/marks/DotY.svelte.d.ts +5 -17
  118. package/dist/marks/Frame.svelte +39 -31
  119. package/dist/marks/Frame.svelte.d.ts +7 -14
  120. package/dist/marks/Geo.svelte +102 -0
  121. package/dist/marks/Geo.svelte.d.ts +10 -0
  122. package/dist/marks/Graticule.svelte +28 -0
  123. package/dist/marks/Graticule.svelte.d.ts +9 -0
  124. package/dist/marks/GridX.svelte +67 -36
  125. package/dist/marks/GridX.svelte.d.ts +7 -18
  126. package/dist/marks/GridY.svelte +64 -25
  127. package/dist/marks/GridY.svelte.d.ts +7 -14
  128. package/dist/marks/HTMLTooltip.svelte +91 -0
  129. package/dist/marks/HTMLTooltip.svelte.d.ts +11 -0
  130. package/dist/marks/Line.svelte +219 -58
  131. package/dist/marks/Line.svelte.d.ts +30 -14
  132. package/dist/marks/LineX.svelte +8 -8
  133. package/dist/marks/LineX.svelte.d.ts +4 -17
  134. package/dist/marks/LineY.svelte +7 -8
  135. package/dist/marks/LineY.svelte.d.ts +4 -17
  136. package/dist/marks/Link.svelte +180 -0
  137. package/dist/marks/Link.svelte.d.ts +21 -0
  138. package/dist/marks/Pointer.svelte +126 -0
  139. package/dist/marks/Pointer.svelte.d.ts +23 -0
  140. package/dist/marks/Rect.svelte +103 -0
  141. package/dist/marks/Rect.svelte.d.ts +15 -0
  142. package/dist/marks/RectX.svelte +33 -0
  143. package/dist/marks/RectX.svelte.d.ts +15 -0
  144. package/dist/marks/RectY.svelte +33 -0
  145. package/dist/marks/RectY.svelte.d.ts +15 -0
  146. package/dist/marks/RegressionX.svelte +26 -0
  147. package/dist/marks/RegressionX.svelte.d.ts +4 -0
  148. package/dist/marks/RegressionY.svelte +26 -0
  149. package/dist/marks/RegressionY.svelte.d.ts +4 -0
  150. package/dist/marks/RuleX.svelte +52 -28
  151. package/dist/marks/RuleX.svelte.d.ts +14 -14
  152. package/dist/marks/RuleY.svelte +52 -28
  153. package/dist/marks/RuleY.svelte.d.ts +14 -14
  154. package/dist/marks/Sphere.svelte +8 -0
  155. package/dist/marks/Sphere.svelte.d.ts +51 -0
  156. package/dist/marks/Spike.svelte +15 -0
  157. package/dist/marks/Spike.svelte.d.ts +4 -0
  158. package/dist/marks/SymbolLegend.svelte +27 -12
  159. package/dist/marks/SymbolLegend.svelte.d.ts +8 -14
  160. package/dist/marks/Text.svelte +189 -0
  161. package/dist/marks/Text.svelte.d.ts +26 -0
  162. package/dist/marks/TickX.svelte +89 -0
  163. package/dist/marks/TickX.svelte.d.ts +22 -0
  164. package/dist/marks/TickY.svelte +90 -0
  165. package/dist/marks/TickY.svelte.d.ts +22 -0
  166. package/dist/marks/Vector.svelte +219 -0
  167. package/dist/marks/Vector.svelte.d.ts +31 -0
  168. package/dist/marks/helpers/BaseAxisX.svelte +210 -0
  169. package/dist/marks/helpers/BaseAxisX.svelte.d.ts +24 -0
  170. package/dist/marks/helpers/BaseAxisY.svelte +187 -0
  171. package/dist/marks/helpers/BaseAxisY.svelte.d.ts +23 -0
  172. package/dist/marks/helpers/CanvasLayer.svelte +38 -0
  173. package/dist/marks/helpers/CanvasLayer.svelte.d.ts +13 -0
  174. package/dist/marks/helpers/DotCanvas.svelte +184 -0
  175. package/dist/marks/helpers/DotCanvas.svelte.d.ts +11 -0
  176. package/dist/marks/helpers/GeoCanvas.svelte +165 -0
  177. package/dist/marks/helpers/GeoCanvas.svelte.d.ts +13 -0
  178. package/dist/marks/helpers/GroupMultiple.svelte +17 -0
  179. package/dist/marks/helpers/GroupMultiple.svelte.d.ts +9 -0
  180. package/dist/marks/helpers/Marker.svelte +93 -0
  181. package/dist/marks/helpers/Marker.svelte.d.ts +10 -0
  182. package/dist/marks/helpers/MarkerPath.svelte +141 -0
  183. package/dist/marks/helpers/MarkerPath.svelte.d.ts +44 -0
  184. package/dist/marks/helpers/Regression.svelte +174 -0
  185. package/dist/marks/helpers/Regression.svelte.d.ts +26 -0
  186. package/dist/marks/helpers/events.d.ts +8 -0
  187. package/dist/marks/helpers/events.js +74 -0
  188. package/dist/transforms/bin.d.ts +51 -0
  189. package/dist/transforms/bin.js +171 -0
  190. package/dist/transforms/bollinger.d.ts +21 -0
  191. package/dist/transforms/bollinger.js +53 -0
  192. package/dist/transforms/centroid.d.ts +9 -0
  193. package/dist/transforms/centroid.js +13 -0
  194. package/dist/transforms/facet.d.ts +1 -0
  195. package/dist/transforms/facet.js +1 -0
  196. package/dist/transforms/filter.d.ts +2 -0
  197. package/dist/transforms/filter.js +8 -0
  198. package/dist/transforms/group.d.ts +66 -0
  199. package/dist/transforms/group.js +109 -0
  200. package/dist/transforms/interval.d.ts +11 -0
  201. package/dist/transforms/interval.js +34 -0
  202. package/dist/transforms/jitter.d.ts +1 -0
  203. package/dist/transforms/jitter.js +1 -0
  204. package/dist/transforms/map.d.ts +10 -0
  205. package/dist/transforms/map.js +89 -0
  206. package/dist/transforms/normalize.d.ts +9 -0
  207. package/dist/transforms/normalize.js +86 -0
  208. package/dist/transforms/recordize.d.ts +15 -0
  209. package/dist/transforms/recordize.js +78 -0
  210. package/dist/transforms/rename.d.ts +14 -0
  211. package/dist/transforms/rename.js +42 -0
  212. package/dist/transforms/select.d.ts +35 -0
  213. package/dist/transforms/select.js +55 -0
  214. package/dist/transforms/shift.d.ts +13 -0
  215. package/dist/transforms/shift.js +45 -0
  216. package/dist/transforms/sort.d.ts +28 -0
  217. package/dist/transforms/sort.js +66 -0
  218. package/dist/transforms/stack.d.ts +10 -0
  219. package/dist/transforms/stack.js +110 -0
  220. package/dist/transforms/window.d.ts +24 -0
  221. package/dist/transforms/window.js +73 -0
  222. package/dist/types.d.ts +625 -188
  223. package/dist/ui/Checkbox.svelte +6 -0
  224. package/dist/ui/Checkbox.svelte.d.ts +13 -0
  225. package/dist/ui/RadioInput.svelte +27 -0
  226. package/dist/ui/RadioInput.svelte.d.ts +9 -0
  227. package/dist/ui/Select.svelte +27 -0
  228. package/dist/ui/Select.svelte.d.ts +9 -0
  229. package/dist/ui/Slider.svelte +47 -0
  230. package/dist/ui/Slider.svelte.d.ts +11 -0
  231. package/dist/ui/Spiral.svelte +46 -0
  232. package/dist/ui/Spiral.svelte.d.ts +15 -0
  233. package/dist/ui/index.d.ts +4 -0
  234. package/dist/ui/index.js +4 -0
  235. package/package.json +81 -42
  236. package/LICENSE +0 -11
  237. package/dist/classes/Channel.svelte.js +0 -74
  238. package/dist/classes/Mark.svelte.js +0 -17
  239. package/dist/classes/Plot.svelte.js +0 -98
  240. package/dist/contants.d.ts +0 -3
  241. package/dist/contants.js +0 -40
  242. package/dist/helpers/GroupMultiple.svelte +0 -8
  243. package/dist/helpers/GroupMultiple.svelte.d.ts +0 -19
  244. package/dist/helpers/createScale.d.ts +0 -5
  245. package/dist/helpers/createScale.js +0 -57
  246. package/dist/helpers/resolveChannel.d.ts +0 -2
  247. package/dist/helpers/resolveChannel.js +0 -28
  248. package/dist/helpers/wrapArray.d.ts +0 -2
  249. package/dist/helpers/wrapArray.js +0 -4
  250. package/dist/marks/BaseMark.svelte +0 -22
  251. package/dist/marks/BaseMark.svelte.d.ts +0 -19
@@ -1,61 +1,222 @@
1
- <script>import resolveChannel from "../helpers/resolveChannel.js";
2
- import { getContext } from "svelte";
3
- import BaseMark from "./BaseMark.svelte";
4
- import getBaseStyles from "../helpers/getBaseStyles.js";
5
- import { line } from "d3-shape";
6
- import { groupBy } from "underscore";
7
- import isDataRecord from "../helpers/isDataRecord.js";
8
- const BaseMark_Line = BaseMark;
9
- const plot = getContext("svelteplot");
10
- let {
11
- data,
12
- x = null,
13
- y = null,
14
- z = null,
15
- fill,
16
- stroke,
17
- r = 5,
18
- sort,
19
- ...styleProps
20
- } = $props();
21
- let groups = $derived(
22
- z || fill || stroke ? Object.values(groupBy(data, (d) => resolveChannel("z", d, z || fill || stroke))) : [data]
23
- );
24
- let sortedGroups = $derived(
25
- sort ? groups.sort(
26
- (a, b) => resolveChannel("sort", a[0], sort) > resolveChannel("sort", b[0], sort) ? 1 : -1
27
- ) : groups
28
- );
29
- let linePath = line().x((d) => plot.xScale(resolveChannel("x", d, x))).y((d) => plot.yScale(resolveChannel("y", d, y)));
1
+ <!--
2
+ @component
3
+ Line mark, useful for line charts
4
+ -->
5
+ <script module lang="ts">
6
+ import type {
7
+ CurveName,
8
+ PlotContext,
9
+ DataRecord,
10
+ BaseMarkProps,
11
+ ConstantAccessor,
12
+ ChannelAccessor,
13
+ MarkerOptions,
14
+ ScaledDataRecord
15
+ } from '../types.js';
16
+
17
+ export type BaseLineMarkProps = {
18
+ data: DataRecord[];
19
+ z?: ChannelAccessor;
20
+ stroke?: ChannelAccessor;
21
+ outlineStroke?: ConstantAccessor<string>;
22
+ outlineStrokeWidth?: ConstantAccessor<number>;
23
+ dx?: ConstantAccessor<number>;
24
+ dy?: ConstantAccessor<number>;
25
+ curve?: CurveName | CurveFactory;
26
+ tension?: number;
27
+ sort?: ConstantAccessor<RawValue> | { channel: 'stroke' | 'fill' };
28
+ text?: ConstantAccessor<string>;
29
+ textFill?: ConstantAccessor<string>;
30
+ textStroke?: ConstantAccessor<string>;
31
+ textStartOffset?: ConstantAccessor<string>;
32
+ textStrokeWidth?: ConstantAccessor<number>;
33
+ lineClass?: ConstantAccessor<string>;
34
+ } & MarkerOptions;
30
35
  </script>
31
36
 
32
- <BaseMark_Line
33
- type="dot"
34
- {data}
35
- channels={['x', 'y', 'radius', 'color']}
36
- {x}
37
- {y}
38
- {r}
39
- {fill}
40
- {stroke}
41
- {...styleProps}
42
- >
43
- <g class="lines">
44
- {#each sortedGroups as lineData}
45
- <path
46
- d={linePath(lineData)}
47
- stroke={stroke
48
- ? plot.colorScale(resolveChannel('color', lineData[0], stroke))
49
- : 'currentColor'}
50
- fill={fill ? plot.colorScale(resolveChannel('color', lineData[0], fill)) : 'none'}
51
- style={getBaseStyles(lineData[0], styleProps)}
52
- />
53
- {/each}
54
- </g>
55
- </BaseMark_Line>
56
-
57
- <style>
58
- .lines path {
59
- stroke-width: 1.4px;
37
+ <script lang="ts">
38
+ import Mark from '../Mark.svelte';
39
+ import MarkerPath from './helpers/MarkerPath.svelte';
40
+ import { getContext } from 'svelte';
41
+ import { resolveProp, resolveStyles } from '../helpers/resolve.js';
42
+ import { line, type CurveFactory } from 'd3-shape';
43
+ import { geoPath } from 'd3-geo';
44
+ import callWithProps from '../helpers/callWithProps.js';
45
+ import { maybeCurve } from '../helpers/curves.js';
46
+ import { pick } from 'es-toolkit';
47
+
48
+ type LineMarkProps = BaseMarkProps & {
49
+ x?: ChannelAccessor;
50
+ y?: ChannelAccessor;
51
+ } & BaseLineMarkProps;
52
+
53
+ import type { RawValue } from '../types.js';
54
+ import { isValid } from '../helpers/index.js';
55
+ import { sort } from '../transforms/sort.js';
56
+ import { recordizeXY } from '../transforms/recordize.js';
57
+ import GroupMultiple from './helpers/GroupMultiple.svelte';
58
+
59
+ let {
60
+ data = [{}],
61
+ curve = 'auto',
62
+ tension = 0,
63
+ text,
64
+ class: className = null,
65
+ lineClass = null,
66
+ ...options
67
+ }: LineMarkProps = $props();
68
+
69
+ const args = $derived(sort(recordizeXY({ data, ...options })));
70
+
71
+ function groupIndex(data: ScaledDataRecord[], groupByKey) {
72
+ if (!groupByKey) return [data];
73
+ let group = [];
74
+ const groups = [group];
75
+ let lastGroupValue;
76
+ for (const d of data) {
77
+ const groupValue = resolveProp(groupByKey, d.datum);
78
+ if (groupValue === lastGroupValue) {
79
+ group.push(d);
80
+ } else {
81
+ if (group.length === 1) {
82
+ // just one point makes a bad line, add this one, too
83
+ group.push(d);
84
+ }
85
+ // new group
86
+ group = [d];
87
+ groups.push(group);
88
+ lastGroupValue = groupValue;
89
+ }
90
+ }
91
+ return groups;
92
+ }
93
+
94
+ const groupByKey = $derived(args.z || args.stroke);
95
+
96
+ const { getPlotState } = getContext<PlotContext>('svelteplot');
97
+ const plot = $derived(getPlotState());
98
+
99
+ type LinePath = (dr: DataRecord[]) => string;
100
+
101
+ const linePath: LinePath = $derived(
102
+ plot.scales.projection && curve === 'auto'
103
+ ? sphereLine(plot.scales.projection)
104
+ : callWithProps(line, [], {
105
+ curve: maybeCurve(curve === 'auto' ? 'linear' : curve, tension),
106
+ x: (d) => d.x,
107
+ y: (d) => d.y,
108
+ defined: (d) => isValid(d.x) && isValid(d.y)
109
+ })
110
+ );
111
+
112
+ function sphereLine(projection) {
113
+ const path = geoPath(projection);
114
+ return (lineData: ScaledDataRecord[]) => {
115
+ let line = [];
116
+ const lines = [line];
117
+ for (const { x, y } of lineData) {
118
+ // if x or y is undefined, start a new line segment
119
+ if (!isValid(x) || !isValid(y)) {
120
+ line = [];
121
+ lines.push(line);
122
+ } else {
123
+ line.push([x, y]);
124
+ }
125
+ }
126
+ return path({ type: 'MultiLineString', coordinates: lines });
127
+ };
60
128
  }
61
- </style>
129
+ </script>
130
+
131
+ <Mark
132
+ type="line"
133
+ channels={['x', 'y', 'opacity', 'stroke', 'strokeOpacity']}
134
+ required={['x', 'y']}
135
+ {...args}>
136
+ {#snippet children({ mark, usedScales, scaledData })}
137
+ {#if scaledData.length > 0}
138
+ <g class={['lines', className]}>
139
+ {#each groupIndex(scaledData, groupByKey) as lineData, i}
140
+ {@const pathString = linePath(lineData)}
141
+ {#if pathString}
142
+ <GroupMultiple class={resolveProp(lineClass, lineData[0])}>
143
+ {#if options.outlineStroke}
144
+ {@const [outlineStyle, outlineStyleClass] = resolveStyles(
145
+ plot,
146
+ { ...lineData[0], stroke: options.outlineStroke },
147
+ {
148
+ strokeLinejoin: 'round',
149
+ ...args,
150
+ stroke: options.outlineStroke,
151
+ strokeWidth:
152
+ options.outlineStrokeWidth ||
153
+ (+options.strokeWidth || 1.4) + 2
154
+ },
155
+ 'stroke',
156
+ usedScales
157
+ )}
158
+ <path
159
+ d={pathString}
160
+ style={outlineStyle}
161
+ class={['is-outline', outlineStyleClass]} />
162
+ {/if}
163
+ {@const [style, styleClass] = resolveStyles(
164
+ plot,
165
+ lineData[0],
166
+ {
167
+ strokeWidth: 1.4,
168
+ strokeLinejoin: 'round',
169
+ ...args,
170
+ stroke: lineData[0].stroke
171
+ },
172
+ 'stroke',
173
+ usedScales
174
+ )}
175
+ {@const [textStyle, textStyleClass] = resolveStyles(
176
+ plot,
177
+ lineData[0],
178
+ {
179
+ textAnchor: 'middle',
180
+ ...pick(args, [
181
+ 'fontSize',
182
+ 'fontWeight',
183
+ 'fontStyle',
184
+ 'textAnchor'
185
+ ]),
186
+ strokeWidth: args.textStrokeWidth
187
+ ? args.textStrokeWidth
188
+ : args.textStroke
189
+ ? 2
190
+ : 0,
191
+ fill: args.textFill || args.stroke,
192
+ stroke: args.textStroke
193
+ },
194
+ 'fill',
195
+ usedScales,
196
+ true
197
+ )}
198
+ <MarkerPath
199
+ {mark}
200
+ scales={plot.scales}
201
+ markerStart={args.markerStart}
202
+ markerMid={args.markerMid}
203
+ markerEnd={args.markerEnd}
204
+ marker={args.marker}
205
+ strokeWidth={args.strokeWidth}
206
+ datum={lineData[0]}
207
+ d={pathString}
208
+ dInv={text ? linePath(lineData.toReversed()) : null}
209
+ color={lineData[0].stroke || 'currentColor'}
210
+ {style}
211
+ class={styleClass}
212
+ text={text ? resolveProp(text, lineData[0]) : null}
213
+ startOffset={resolveProp(args.textStartOffset, lineData[0], '50%')}
214
+ {textStyle}
215
+ {textStyleClass} />
216
+ </GroupMultiple>
217
+ {/if}
218
+ {/each}
219
+ </g>
220
+ {/if}
221
+ {/snippet}
222
+ </Mark>
@@ -1,15 +1,31 @@
1
- import { SvelteComponent } from "svelte";
2
- import type { LineMarkProps } from '../types.js';
3
- declare const __propDef: {
4
- props: LineMarkProps;
5
- events: {
6
- [evt: string]: CustomEvent<any>;
1
+ import type { CurveName, DataRecord, BaseMarkProps, ConstantAccessor, ChannelAccessor, MarkerOptions } from '../types.js';
2
+ export type BaseLineMarkProps = {
3
+ data: DataRecord[];
4
+ z?: ChannelAccessor;
5
+ stroke?: ChannelAccessor;
6
+ outlineStroke?: ConstantAccessor<string>;
7
+ outlineStrokeWidth?: ConstantAccessor<number>;
8
+ dx?: ConstantAccessor<number>;
9
+ dy?: ConstantAccessor<number>;
10
+ curve?: CurveName | CurveFactory;
11
+ tension?: number;
12
+ sort?: ConstantAccessor<RawValue> | {
13
+ channel: 'stroke' | 'fill';
7
14
  };
8
- slots: {};
9
- };
10
- export type LineProps = typeof __propDef.props;
11
- export type LineEvents = typeof __propDef.events;
12
- export type LineSlots = typeof __propDef.slots;
13
- export default class Line extends SvelteComponent<LineProps, LineEvents, LineSlots> {
14
- }
15
- export {};
15
+ text?: ConstantAccessor<string>;
16
+ textFill?: ConstantAccessor<string>;
17
+ textStroke?: ConstantAccessor<string>;
18
+ textStartOffset?: ConstantAccessor<string>;
19
+ textStrokeWidth?: ConstantAccessor<number>;
20
+ lineClass?: ConstantAccessor<string>;
21
+ } & MarkerOptions;
22
+ import { type CurveFactory } from 'd3-shape';
23
+ import type { RawValue } from '../types.js';
24
+ type LineMarkProps = BaseMarkProps & {
25
+ x?: ChannelAccessor;
26
+ y?: ChannelAccessor;
27
+ } & BaseLineMarkProps;
28
+ /** Line mark, useful for line charts */
29
+ declare const Line: import("svelte").Component<LineMarkProps, {}, "">;
30
+ type Line = ReturnType<typeof Line>;
31
+ export default Line;
@@ -1,10 +1,10 @@
1
- <script>import Line from "./Line.svelte";
2
- let { data, ...rest } = $props();
1
+ <script lang="ts">
2
+ import Line, { type BaseLineMarkProps } from './Line.svelte';
3
+ import { recordizeX } from '../index.js';
4
+
5
+ let { data = [{}], ...rest }: BaseLineMarkProps = $props();
6
+
7
+ let args = $derived(recordizeX({ data, ...rest }));
3
8
  </script>
4
9
 
5
- <Line
6
- data={data.map((value, index) => ({ value, index, ___orig___: value }))}
7
- x="value"
8
- y="index"
9
- {...rest}
10
- />
10
+ <Line {...args} />
@@ -1,17 +1,4 @@
1
- import { SvelteComponent } from "svelte";
2
- import type { RawValue } from '../types.js';
3
- declare const __propDef: {
4
- props: {
5
- data: RawValue[];
6
- };
7
- events: {
8
- [evt: string]: CustomEvent<any>;
9
- };
10
- slots: {};
11
- };
12
- export type LineXProps = typeof __propDef.props;
13
- export type LineXEvents = typeof __propDef.events;
14
- export type LineXSlots = typeof __propDef.slots;
15
- export default class LineX extends SvelteComponent<LineXProps, LineXEvents, LineXSlots> {
16
- }
17
- export {};
1
+ import { type BaseLineMarkProps } from './Line.svelte';
2
+ declare const LineX: import("svelte").Component<BaseLineMarkProps, {}, "">;
3
+ type LineX = ReturnType<typeof LineX>;
4
+ export default LineX;
@@ -1,10 +1,9 @@
1
- <script>import Line from "./Line.svelte";
2
- let { data, ...rest } = $props();
1
+ <script lang="ts">
2
+ import Line, { type BaseLineMarkProps } from './Line.svelte';
3
+ import { recordizeY } from '../index.js';
4
+
5
+ let { data = [{}], ...rest }: BaseLineMarkProps = $props();
6
+ let args = $derived(recordizeY({ data, ...rest }));
3
7
  </script>
4
8
 
5
- <Line
6
- data={data.map((value, index) => ({ value, index, ___orig___: value }))}
7
- x="index"
8
- y="value"
9
- {...rest}
10
- />
9
+ <Line {...args} />
@@ -1,17 +1,4 @@
1
- import { SvelteComponent } from "svelte";
2
- import type { RawValue } from '../types.js';
3
- declare const __propDef: {
4
- props: {
5
- data: RawValue[];
6
- };
7
- events: {
8
- [evt: string]: CustomEvent<any>;
9
- };
10
- slots: {};
11
- };
12
- export type LineYProps = typeof __propDef.props;
13
- export type LineYEvents = typeof __propDef.events;
14
- export type LineYSlots = typeof __propDef.slots;
15
- export default class LineY extends SvelteComponent<LineYProps, LineYEvents, LineYSlots> {
16
- }
17
- export {};
1
+ import { type BaseLineMarkProps } from './Line.svelte';
2
+ declare const LineY: import("svelte").Component<BaseLineMarkProps, {}, "">;
3
+ type LineY = ReturnType<typeof LineY>;
4
+ export default LineY;
@@ -0,0 +1,180 @@
1
+ <script lang="ts">
2
+ import { getContext, type Snippet } from 'svelte';
3
+ import type {
4
+ PlotContext,
5
+ DataRecord,
6
+ BaseMarkProps,
7
+ ConstantAccessor,
8
+ ChannelAccessor,
9
+ CurveName,
10
+ MarkerOptions,
11
+ RawValue,
12
+ ScaledDataRecord
13
+ } from '../types.js';
14
+ import { resolveChannel, resolveProp, resolveStyles } from '../helpers/resolve.js';
15
+ import { maybeData } from '../helpers/index.js';
16
+ import Mark from '../Mark.svelte';
17
+ import MarkerPath from './helpers/MarkerPath.svelte';
18
+ import { replaceChannels } from '../transforms/rename.js';
19
+ import { line, type CurveFactory } from 'd3-shape';
20
+ import callWithProps from '../helpers/callWithProps.js';
21
+ import { maybeCurve } from '../helpers/curves.js';
22
+ import { geoPath } from 'd3-geo';
23
+ import { pick } from 'es-toolkit';
24
+
25
+ type LinkMarkProps = BaseMarkProps & {
26
+ data: DataRecord[];
27
+ sort?: ConstantAccessor<RawValue> | { channel: 'stroke' | 'fill' };
28
+ x1: ChannelAccessor;
29
+ y1: ChannelAccessor;
30
+ x2: ChannelAccessor;
31
+ y2: ChannelAccessor;
32
+ stroke?: ChannelAccessor;
33
+ curve?: 'auto' | CurveName | CurveFactory;
34
+ tension?: number;
35
+ text: ConstantAccessor<string>;
36
+ children?: Snippet;
37
+ } & MarkerOptions;
38
+
39
+ let {
40
+ data = [{}],
41
+ curve = 'auto',
42
+ tension = 0,
43
+ text,
44
+ class: className = null,
45
+ ...options
46
+ }: LinkMarkProps = $props();
47
+
48
+ const { getPlotState } = getContext<PlotContext>('svelteplot');
49
+ let plot = $derived(getPlotState());
50
+
51
+ function isValid(value: RawValue): value is number | Date | string {
52
+ return value !== null && (typeof value === 'string' || !Number.isNaN(value));
53
+ }
54
+
55
+ const sorted = $derived(
56
+ options.sort
57
+ ? maybeData(data).toSorted((a, b) =>
58
+ resolveChannel('sort', a, options) > resolveChannel('sort', b, options) ? 1 : -1
59
+ )
60
+ : maybeData(data)
61
+ );
62
+
63
+ const args = $derived(
64
+ replaceChannels(
65
+ { data: sorted, stroke: 'currentColor', ...options },
66
+ { y: ['y1', 'y2'], x: ['x1', 'x2'] }
67
+ )
68
+ );
69
+
70
+ const sphericalLine = $derived(plot.scales.projection && curve === 'auto');
71
+
72
+ const linePath: (d: ScaledDataRecord, reversed: boolean) => string = $derived.by(() => {
73
+ const fn = callWithProps(line, [], {
74
+ curve: maybeCurve(curve === 'auto' ? 'linear' : curve, tension),
75
+ x: (d) => d[0],
76
+ y: (d) => d[1]
77
+ });
78
+
79
+ return (d: ScaledDataRecord, reversed = false) =>
80
+ fn(
81
+ reversed
82
+ ? [
83
+ [d.x2, d.y2],
84
+ [d.x1, d.y1]
85
+ ]
86
+ : [
87
+ [d.x1, d.y1],
88
+ [d.x2, d.y2]
89
+ ]
90
+ );
91
+ });
92
+
93
+ const sphericalLinePath: (d: ScaledDataRecord, reversed: boolean) => string = $derived.by(
94
+ () => {
95
+ const fn = sphereLine(plot.scales.projection);
96
+ return (d: ScaledDataRecord, reversed = false) => {
97
+ const x1 = resolveChannel('x1', d.datum, args);
98
+ const y1 = resolveChannel('y1', d.datum, args);
99
+ const x2 = resolveChannel('x2', d.datum, args);
100
+ const y2 = resolveChannel('y2', d.datum, args);
101
+ return reversed ? fn(x2, y2, x1, y1) : fn(x1, y1, x2, y2);
102
+ };
103
+ }
104
+ );
105
+ // sphericalLine
106
+ // ?
107
+
108
+ // sphereLine(plot.scales.projection)
109
+ // :
110
+ // );
111
+
112
+ function sphereLine(projection) {
113
+ const path = geoPath(projection);
114
+ return (x1: number, y1: number, x2: number, y2: number) => {
115
+ return path({
116
+ type: 'LineString',
117
+ coordinates: [
118
+ [x1, y1],
119
+ [x2, y2]
120
+ ]
121
+ });
122
+ };
123
+ }
124
+ </script>
125
+
126
+ <Mark
127
+ type="link"
128
+ required={['x1', 'x2', 'y1', 'y2']}
129
+ channels={['x1', 'y1', 'x2', 'y2', 'opacity', 'stroke', 'strokeOpacity']}
130
+ {...args}>
131
+ {#snippet children({ mark, scaledData, usedScales })}
132
+ <g class={['link', className]} data-use-x={usedScales.x ? 1 : 0}>
133
+ {#each scaledData as d}
134
+ {#if d.valid || true}
135
+ {@const dx = resolveProp(args.dx, d.datum, 0)}
136
+ {@const dy = resolveProp(args.dx, d.datum, 0)}
137
+ {@const [style, styleClass] = resolveStyles(
138
+ plot,
139
+ d,
140
+ { strokeWidth: 1.6, ...args },
141
+ 'stroke',
142
+ usedScales
143
+ )}
144
+ {@const [textStyle, textStyleClass] = resolveStyles(
145
+ plot,
146
+ d,
147
+ {
148
+ textAnchor: 'middle',
149
+ ...pick(args, ['fontSize', 'fontWeight', 'fontStyle', 'textAnchor']),
150
+ fill: args.textFill || args.stroke,
151
+ stroke: args.textStroke,
152
+ strokeWidth: args.textStrokeWidth
153
+ },
154
+ 'fill',
155
+ usedScales
156
+ )}
157
+
158
+ <MarkerPath
159
+ mark={{ ...mark, options: args }}
160
+ scales={plot.scales}
161
+ markerStart={args.markerStart}
162
+ markerEnd={args.markerEnd}
163
+ marker={args.marker}
164
+ class={styleClass}
165
+ strokeWidth={args.strokeWidth}
166
+ datum={d.datum}
167
+ color={d.stroke}
168
+ d={sphericalLine ? sphericalLinePath(d) : linePath(d)}
169
+ dInv={sphericalLine ? sphericalLinePath(d, true) : linePath(d, true)}
170
+ {style}
171
+ text={text ? resolveProp(text, d.datum) : null}
172
+ startOffset={resolveProp(args.textStartOffset, d.datum, '50%')}
173
+ {textStyle}
174
+ {textStyleClass}
175
+ transform={dx || dy ? `translate(${dx}, ${dy})` : null} />
176
+ {/if}
177
+ {/each}
178
+ </g>
179
+ {/snippet}
180
+ </Mark>
@@ -0,0 +1,21 @@
1
+ import { type Snippet } from 'svelte';
2
+ import type { DataRecord, BaseMarkProps, ConstantAccessor, ChannelAccessor, CurveName, MarkerOptions, RawValue } from '../types.js';
3
+ import { type CurveFactory } from 'd3-shape';
4
+ type LinkMarkProps = BaseMarkProps & {
5
+ data: DataRecord[];
6
+ sort?: ConstantAccessor<RawValue> | {
7
+ channel: 'stroke' | 'fill';
8
+ };
9
+ x1: ChannelAccessor;
10
+ y1: ChannelAccessor;
11
+ x2: ChannelAccessor;
12
+ y2: ChannelAccessor;
13
+ stroke?: ChannelAccessor;
14
+ curve?: 'auto' | CurveName | CurveFactory;
15
+ tension?: number;
16
+ text: ConstantAccessor<string>;
17
+ children?: Snippet;
18
+ } & MarkerOptions;
19
+ declare const Link: import("svelte").Component<LinkMarkProps, {}, "">;
20
+ type Link = ReturnType<typeof Link>;
21
+ export default Link;