@rokkit/chart 1.3.7 → 1.3.9

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 (162) hide show
  1. package/dist/AnimatedPlot.svelte +26 -8
  2. package/dist/AnimatedPlot.svelte.d.ts +5 -0
  3. package/dist/Chart.svelte +51 -65
  4. package/dist/FacetPlot.svelte +1 -1
  5. package/dist/Plot/Axis.svelte +19 -10
  6. package/dist/Plot/Root.svelte +67 -46
  7. package/dist/Plot/Root.svelte.d.ts +29 -2
  8. package/dist/Plot/Timeline.svelte +39 -13
  9. package/dist/Plot/index.d.ts +5 -5
  10. package/dist/Plot/index.js +5 -5
  11. package/dist/Plot.svelte +95 -178
  12. package/dist/Plot.svelte.d.ts +4 -0
  13. package/dist/PlotState.svelte.d.ts +29 -1
  14. package/dist/PlotState.svelte.js +163 -19
  15. package/dist/PlotSurface.svelte +162 -0
  16. package/dist/PlotSurface.svelte.d.ts +25 -0
  17. package/dist/charts/AreaChart.svelte +12 -2
  18. package/dist/charts/AreaChart.svelte.d.ts +6 -0
  19. package/dist/charts/BarChart.svelte +40 -4
  20. package/dist/charts/BarChart.svelte.d.ts +15 -1
  21. package/dist/charts/BoxPlot.svelte +7 -3
  22. package/dist/charts/BoxPlot.svelte.d.ts +2 -0
  23. package/dist/charts/BubbleChart.svelte +37 -4
  24. package/dist/charts/BubbleChart.svelte.d.ts +12 -1
  25. package/dist/charts/LineChart.svelte +4 -2
  26. package/dist/charts/LineChart.svelte.d.ts +1 -0
  27. package/dist/charts/PieChart.svelte +5 -3
  28. package/dist/charts/PieChart.svelte.d.ts +1 -0
  29. package/dist/charts/ScatterPlot.svelte +35 -4
  30. package/dist/charts/ScatterPlot.svelte.d.ts +11 -1
  31. package/dist/charts/ViolinPlot.svelte +7 -3
  32. package/dist/charts/ViolinPlot.svelte.d.ts +2 -0
  33. package/dist/examples/BarChartExample.svelte +1 -1
  34. package/dist/geoms/Arc.svelte +21 -54
  35. package/dist/geoms/Arc.svelte.d.ts +4 -4
  36. package/dist/geoms/Area.svelte +45 -44
  37. package/dist/geoms/Area.svelte.d.ts +8 -1
  38. package/dist/geoms/Bar.svelte +33 -45
  39. package/dist/geoms/Bar.svelte.d.ts +9 -0
  40. package/dist/geoms/Box.svelte +103 -61
  41. package/dist/geoms/Box.svelte.d.ts +12 -3
  42. package/dist/geoms/Candlestick.svelte +26 -75
  43. package/dist/geoms/Candlestick.svelte.d.ts +2 -0
  44. package/dist/geoms/Heatmap.svelte +21 -66
  45. package/dist/geoms/Heatmap.svelte.d.ts +2 -0
  46. package/dist/geoms/Hexbin.svelte +20 -100
  47. package/dist/geoms/Hexbin.svelte.d.ts +2 -0
  48. package/dist/geoms/Jitter.svelte +67 -0
  49. package/dist/geoms/Jitter.svelte.d.ts +17 -0
  50. package/dist/geoms/Line.svelte +27 -31
  51. package/dist/geoms/Line.svelte.d.ts +5 -0
  52. package/dist/geoms/Point.svelte +31 -60
  53. package/dist/geoms/Point.svelte.d.ts +5 -1
  54. package/dist/geoms/Ribbon.svelte +30 -124
  55. package/dist/geoms/Ribbon.svelte.d.ts +2 -0
  56. package/dist/geoms/Rule.svelte +117 -0
  57. package/dist/geoms/Rule.svelte.d.ts +19 -0
  58. package/dist/geoms/Violin.svelte +36 -31
  59. package/dist/geoms/Violin.svelte.d.ts +10 -3
  60. package/dist/geoms/Waterfall.svelte +25 -79
  61. package/dist/geoms/Waterfall.svelte.d.ts +2 -0
  62. package/dist/geoms/lib/GeomState.svelte.d.ts +51 -0
  63. package/dist/geoms/lib/GeomState.svelte.js +79 -0
  64. package/dist/geoms/lib/aesthetics.d.ts +36 -0
  65. package/dist/geoms/lib/aesthetics.js +45 -0
  66. package/dist/geoms/lib/areas.d.ts +6 -2
  67. package/dist/geoms/lib/areas.js +20 -12
  68. package/dist/geoms/lib/bars.d.ts +18 -3
  69. package/dist/geoms/lib/bars.js +80 -101
  70. package/dist/geoms/lib/marks/arc.d.ts +24 -0
  71. package/dist/geoms/lib/marks/arc.js +28 -0
  72. package/dist/geoms/lib/marks/area.d.ts +23 -0
  73. package/dist/geoms/lib/marks/area.js +38 -0
  74. package/dist/geoms/lib/marks/bar.d.ts +16 -0
  75. package/dist/geoms/lib/marks/bar.js +43 -0
  76. package/dist/geoms/lib/marks/box.d.ts +14 -0
  77. package/dist/geoms/lib/marks/box.js +30 -0
  78. package/dist/geoms/lib/marks/candlestick.d.ts +27 -0
  79. package/dist/geoms/lib/marks/candlestick.js +52 -0
  80. package/dist/geoms/lib/marks/heatmap.d.ts +24 -0
  81. package/dist/geoms/lib/marks/heatmap.js +41 -0
  82. package/dist/geoms/lib/marks/hexbin.d.ts +29 -0
  83. package/dist/geoms/lib/marks/hexbin.js +62 -0
  84. package/dist/geoms/lib/marks/jitter.d.ts +21 -0
  85. package/dist/geoms/lib/marks/jitter.js +36 -0
  86. package/dist/geoms/lib/marks/line.d.ts +26 -0
  87. package/dist/geoms/lib/marks/line.js +25 -0
  88. package/dist/geoms/lib/marks/point.d.ts +25 -0
  89. package/dist/geoms/lib/marks/point.js +58 -0
  90. package/dist/geoms/lib/marks/ribbon.d.ts +31 -0
  91. package/dist/geoms/lib/marks/ribbon.js +77 -0
  92. package/dist/geoms/lib/marks/violin.d.ts +13 -0
  93. package/dist/geoms/lib/marks/violin.js +31 -0
  94. package/dist/geoms/lib/marks/waterfall.d.ts +25 -0
  95. package/dist/geoms/lib/marks/waterfall.js +58 -0
  96. package/dist/index.d.ts +25 -12
  97. package/dist/index.js +26 -13
  98. package/dist/lib/brewing/marks/arcs.js +7 -1
  99. package/dist/lib/brewing/marks/boxes.d.ts +11 -2
  100. package/dist/lib/brewing/marks/boxes.js +32 -11
  101. package/dist/lib/brewing/marks/lines.d.ts +4 -1
  102. package/dist/lib/brewing/marks/lines.js +18 -15
  103. package/dist/lib/brewing/marks/swarm.d.ts +32 -0
  104. package/dist/lib/brewing/marks/swarm.js +125 -0
  105. package/dist/lib/brewing/marks/violins.d.ts +4 -20
  106. package/dist/lib/brewing/marks/violins.js +41 -25
  107. package/dist/lib/brewing/stats.d.ts +3 -1
  108. package/dist/lib/brewing/stats.js +28 -11
  109. package/dist/lib/plot/scales.js +12 -7
  110. package/dist/lib/plot/types.d.ts +9 -1
  111. package/dist/lib/plot/types.js +3 -1
  112. package/dist/lib/preset.d.ts +11 -1
  113. package/dist/lib/preset.js +16 -4
  114. package/package.json +1 -1
  115. package/dist/Plot/Arc.svelte +0 -30
  116. package/dist/Plot/Arc.svelte.d.ts +0 -18
  117. package/dist/Plot/Area.svelte +0 -54
  118. package/dist/Plot/Area.svelte.d.ts +0 -14
  119. package/dist/Plot/Bar.svelte +0 -69
  120. package/dist/Plot/Bar.svelte.d.ts +0 -11
  121. package/dist/Plot/Line.svelte +0 -54
  122. package/dist/Plot/Line.svelte.d.ts +0 -13
  123. package/dist/Plot/Point.svelte +0 -58
  124. package/dist/Plot/Point.svelte.d.ts +0 -13
  125. package/dist/lib/brewer.d.ts +0 -9
  126. package/dist/lib/brewer.js +0 -25
  127. package/dist/lib/brewing/BoxBrewer.svelte.d.ts +0 -8
  128. package/dist/lib/brewing/BoxBrewer.svelte.js +0 -14
  129. package/dist/lib/brewing/CartesianBrewer.svelte.d.ts +0 -8
  130. package/dist/lib/brewing/CartesianBrewer.svelte.js +0 -21
  131. package/dist/lib/brewing/PieBrewer.svelte.d.ts +0 -8
  132. package/dist/lib/brewing/PieBrewer.svelte.js +0 -14
  133. package/dist/lib/brewing/QuartileBrewer.svelte.d.ts +0 -9
  134. package/dist/lib/brewing/QuartileBrewer.svelte.js +0 -51
  135. package/dist/lib/brewing/ViolinBrewer.svelte.d.ts +0 -8
  136. package/dist/lib/brewing/ViolinBrewer.svelte.js +0 -14
  137. package/dist/lib/brewing/axes.svelte.d.ts +0 -66
  138. package/dist/lib/brewing/axes.svelte.js +0 -270
  139. package/dist/lib/brewing/bars.svelte.d.ts +0 -56
  140. package/dist/lib/brewing/bars.svelte.js +0 -201
  141. package/dist/lib/brewing/brewer.svelte.d.ts +0 -124
  142. package/dist/lib/brewing/brewer.svelte.js +0 -277
  143. package/dist/lib/brewing/dimensions.svelte.d.ts +0 -35
  144. package/dist/lib/brewing/dimensions.svelte.js +0 -56
  145. package/dist/lib/brewing/index.svelte.d.ts +0 -118
  146. package/dist/lib/brewing/index.svelte.js +0 -205
  147. package/dist/lib/brewing/legends.svelte.d.ts +0 -48
  148. package/dist/lib/brewing/legends.svelte.js +0 -137
  149. package/dist/lib/brewing/marks/areas.d.ts +0 -31
  150. package/dist/lib/brewing/marks/areas.js +0 -72
  151. package/dist/lib/brewing/marks/bars.d.ts +0 -1
  152. package/dist/lib/brewing/marks/bars.js +0 -49
  153. package/dist/lib/brewing/scales.svelte.d.ts +0 -24
  154. package/dist/lib/brewing/scales.svelte.js +0 -82
  155. package/dist/lib/context.d.ts +0 -13
  156. package/dist/lib/context.js +0 -131
  157. package/dist/lib/plot/chartProps.d.ts +0 -177
  158. package/dist/lib/plot/chartProps.js +0 -76
  159. package/dist/lib/scales.svelte.d.ts +0 -35
  160. package/dist/lib/scales.svelte.js +0 -151
  161. package/dist/lib/ticks.d.ts +0 -36
  162. package/dist/lib/ticks.js +0 -46
@@ -1,270 +0,0 @@
1
- // import { scaleBand } from 'd3-scale'
2
-
3
- import {} from './types.js'
4
-
5
- /**
6
- * @typedef {import('./types').TickData} TickData
7
- * @typedef {import('./types').AxisData} AxisData
8
- * @typedef {import('./types').ChartScales} ChartScales
9
- * @typedef {import('./types').ChartDimensions} ChartDimensions
10
- */
11
-
12
- const IDENTITY = (v) => v
13
-
14
- /**
15
- * @param {Object} xScale
16
- * @param {number|null} tickCount
17
- * @returns {Array}
18
- */
19
- function xTicksFromContinuous(xScale, tickCount) {
20
- return tickCount !== null ? xScale.ticks(tickCount) : xScale.ticks()
21
- }
22
-
23
- /**
24
- * @param {Array} domain
25
- * @param {number} tickCount
26
- * @returns {Array}
27
- */
28
- function downsampleDomain(domain, tickCount) {
29
- const step = Math.max(1, Math.floor(domain.length / tickCount))
30
- return domain.filter((_, i) => i % step === 0)
31
- }
32
-
33
- /**
34
- * @param {Object} xScale
35
- * @param {number|null} tickCount
36
- * @returns {Array}
37
- */
38
- function xTicksFromBand(xScale, tickCount) {
39
- const domain = xScale.domain()
40
- if (tickCount === null || tickCount >= domain.length) return domain
41
- return downsampleDomain(domain, tickCount)
42
- }
43
-
44
- /**
45
- * @param {Object} xScale
46
- * @param {number|null} tickCount
47
- * @returns {Array}
48
- */
49
- function resolveXTicks(xScale, tickCount) {
50
- return xScale.ticks ? xTicksFromContinuous(xScale, tickCount) : xTicksFromBand(xScale, tickCount)
51
- }
52
-
53
- /**
54
- * @param {Object} xScale
55
- * @param {unknown} value
56
- * @returns {number}
57
- */
58
- function xPosition(xScale, value) {
59
- return xScale.bandwidth ? xScale(value) + xScale.bandwidth() / 2 : xScale(value)
60
- }
61
-
62
- /**
63
- * @param {Object} scale
64
- * @param {Array} values
65
- * @param {Function} formatter
66
- * @param {(scale: Object, v: unknown) => number} getPosition
67
- * @returns {Array}
68
- */
69
- function mapTicks(scale, values, formatter, getPosition) {
70
- return values.map((value) => ({
71
- value,
72
- position: getPosition(scale, value),
73
- formattedValue: formatter(value)
74
- }))
75
- }
76
-
77
- /**
78
- * @param {Object} options
79
- * @returns {{ tickCount: number|null, formatter: Function, label: string }}
80
- */
81
- function parseAxisOptions(options) {
82
- const opts = options || {}
83
- return {
84
- tickCount: opts.tickCount !== undefined ? opts.tickCount : null,
85
- formatter: opts.tickFormat ? opts.tickFormat : IDENTITY,
86
- label: opts.label ? opts.label : ''
87
- }
88
- }
89
-
90
- /**
91
- * Creates x-axis tick data for rendering
92
- *
93
- * @param {Object} scales - Chart scales
94
- * @param {Function} scales.x - X-axis scale
95
- * @param {Object} dimensions - Chart dimensions
96
- * @param {Object} options - Axis options
97
- * @param {number} [options.tickCount] - Number of ticks to show
98
- * @param {Function} [options.tickFormat] - Tick formatting function
99
- * @param {string} [options.label] - Axis label
100
- * @returns {AxisData} Axis rendering data
101
- */
102
- export function createXAxis(scales, dimensions, options) {
103
- if (!scales.x) return { ticks: [] }
104
- const { tickCount, formatter, label } = parseAxisOptions(options)
105
- const ticks = mapTicks(scales.x, resolveXTicks(scales.x, tickCount), formatter, xPosition)
106
- return {
107
- ticks,
108
- label,
109
- transform: `translate(0, ${dimensions.innerHeight})`,
110
- labelTransform: `translate(${dimensions.innerWidth / 2}, 35)`
111
- }
112
- }
113
-
114
- /**
115
- * @param {Object} yScale
116
- * @param {number|null} tickCount
117
- * @returns {Array}
118
- */
119
- function yTicksFromDomain(yScale, tickCount) {
120
- const domain = yScale.domain()
121
- if (tickCount === null || domain.length <= tickCount) return domain
122
- return downsampleDomain(domain, tickCount)
123
- }
124
-
125
- /**
126
- * @param {Object} yScale
127
- * @param {number|null} tickCount
128
- * @returns {Array}
129
- */
130
- function resolveYTicks(yScale, tickCount) {
131
- if (yScale.ticks) return tickCount !== null ? yScale.ticks(tickCount) : yScale.ticks()
132
- if (yScale.domain) return yTicksFromDomain(yScale, tickCount)
133
- return []
134
- }
135
-
136
- /**
137
- * @param {Object} scale
138
- * @param {unknown} value
139
- * @returns {unknown}
140
- */
141
- function yPositionPassthrough(scale, value) {
142
- return scale(value)
143
- }
144
-
145
- /**
146
- * Creates y-axis tick data for rendering
147
- *
148
- * @param {Object} scales - Chart scales
149
- * @param {Function} scales.y - Y-axis scale
150
- * @param {Object} dimensions - Chart dimensions
151
- * @param {Object} options - Axis options
152
- * @param {number} [options.tickCount] - Number of ticks to show
153
- * @param {Function} [options.tickFormat] - Tick formatting function
154
- * @param {string} [options.label] - Axis label
155
- * @returns {AxisData} Axis rendering data
156
- */
157
- export function createYAxis(scales, dimensions, options) {
158
- if (!scales.y) return { ticks: [] }
159
- const { tickCount, formatter, label } = parseAxisOptions(options)
160
- const ticks = mapTicks(
161
- scales.y,
162
- resolveYTicks(scales.y, tickCount),
163
- formatter,
164
- yPositionPassthrough
165
- )
166
- return {
167
- ticks,
168
- label,
169
- transform: 'translate(0, 0)',
170
- labelTransform: `translate(-40, ${dimensions.innerHeight / 2}) rotate(-90)`
171
- }
172
- }
173
-
174
- /**
175
- * @param {Object} scales
176
- * @param {Object} dimensions
177
- * @param {number|null} xTickCount
178
- * @returns {Array}
179
- */
180
- function buildXLines(scales, dimensions, xTickCount) {
181
- return createXAxis(scales, dimensions, { tickCount: xTickCount }).ticks.map((tick) => ({
182
- x1: tick.position,
183
- y1: 0,
184
- x2: tick.position,
185
- y2: dimensions.innerHeight
186
- }))
187
- }
188
-
189
- /**
190
- * @param {Object} scales
191
- * @param {Object} dimensions
192
- * @param {number|null} yTickCount
193
- * @returns {Array}
194
- */
195
- function buildYLines(scales, dimensions, yTickCount) {
196
- return createYAxis(scales, dimensions, { tickCount: yTickCount }).ticks.map((tick) => ({
197
- x1: 0,
198
- y1: tick.position,
199
- x2: dimensions.innerWidth,
200
- y2: tick.position
201
- }))
202
- }
203
-
204
- /**
205
- * @param {Object} opts
206
- * @returns {{ direction: string, xTickCount: number|null, yTickCount: number|null }}
207
- */
208
- function parseGridOptions(opts) {
209
- const o = opts || {}
210
- return {
211
- direction: o.direction !== undefined ? o.direction : 'both',
212
- xTickCount: o.xTickCount !== undefined ? o.xTickCount : null,
213
- yTickCount: o.yTickCount !== undefined ? o.yTickCount : null
214
- }
215
- }
216
-
217
- /**
218
- * @param {string} direction
219
- * @returns {{ showX: boolean, showY: boolean }}
220
- */
221
- function gridDirections(direction) {
222
- return {
223
- showX: direction === 'x' || direction === 'both',
224
- showY: direction === 'y' || direction === 'both'
225
- }
226
- }
227
-
228
- /**
229
- * Creates grid line data for rendering
230
- *
231
- * @param {Object} scales - Chart scales
232
- * @param {Object} dimensions - Chart dimensions
233
- * @param {Object} options - Grid options
234
- * @param {string} [options.direction='both'] - Grid direction ('x', 'y', or 'both')
235
- * @param {number} [options.xTickCount] - Number of x-axis ticks
236
- * @param {number} [options.yTickCount] - Number of y-axis ticks
237
- * @returns {Object} Grid rendering data
238
- */
239
- export function createGrid(scales, dimensions, options) {
240
- const { direction, xTickCount, yTickCount } = parseGridOptions(options)
241
- const { showX, showY } = gridDirections(direction)
242
- return {
243
- xLines: showX && scales.x ? buildXLines(scales, dimensions, xTickCount) : [],
244
- yLines: showY && scales.y ? buildYLines(scales, dimensions, yTickCount) : []
245
- }
246
- }
247
-
248
- /**
249
- * Creates DOM attributes for a tick element
250
- *
251
- * @param {TickData} tick - Tick data
252
- * @param {string} axis - Axis type ('x' or 'y')
253
- * @returns {Object} Attributes for the tick
254
- */
255
- export function createTickAttributes(tick, axis) {
256
- if (axis === 'x') {
257
- return {
258
- 'data-plot-tick': 'major',
259
- transform: `translate(${tick.position}, 0)`,
260
- 'text-anchor': 'middle'
261
- }
262
- } else {
263
- return {
264
- 'data-plot-tick': 'major',
265
- transform: `translate(0, ${tick.position})`,
266
- 'text-anchor': 'end',
267
- 'dominant-baseline': 'middle'
268
- }
269
- }
270
- }
@@ -1,56 +0,0 @@
1
- /**
2
- * Creates bar data for rendering
3
- *
4
- * @param {Array} data - Chart data
5
- * @param {Object} fields - Field mappings
6
- * @param {string} fields.x - X-axis field
7
- * @param {string} fields.y - Y-axis field
8
- * @param {string} fields.color - Color field (optional)
9
- * @param {Object} scales - Chart scales
10
- * @param {Function} scales.x - X-axis scale
11
- * @param {Function} scales.y - Y-axis scale
12
- * @param {Function} scales.color - Color scale
13
- * @param {Object} options - Options including dimensions and defaultColor
14
- * @param {Object} options.dimensions - Chart dimensions
15
- * @param {string} [options.defaultColor='#4682b4'] - Default color if no color scale
16
- * @returns {BarData[]} Bar data for rendering
17
- */
18
- export function createBars(data: any[], fields: {
19
- x: string;
20
- y: string;
21
- color: string;
22
- }, scales: {
23
- x: Function;
24
- y: Function;
25
- color: Function;
26
- }, options: {
27
- dimensions: Object;
28
- defaultColor?: string | undefined;
29
- }): BarData[];
30
- /**
31
- * Filter bars based on a selection criteria
32
- *
33
- * @param {BarData[]} bars - Bar data array
34
- * @param {Object} selection - Selection criteria
35
- * @returns {BarData[]} Filtered bars
36
- */
37
- export function filterBars(bars: BarData[], selection: Object): BarData[];
38
- /**
39
- * Creates a grouped bars layout
40
- *
41
- * @param {Array} data - Chart data
42
- * @param {Object} fields - Field mappings
43
- * @param {Object} scales - Chart scales
44
- * @param {Object} options - Options including dimensions and padding
45
- * @param {Object} options.dimensions - Chart dimensions
46
- * @param {number} [options.padding=0.1] - Padding between bars in a group
47
- * @returns {Object} Grouped bar data
48
- */
49
- export function createGroupedBars(data: any[], fields: Object, scales: Object, options: {
50
- dimensions: Object;
51
- padding?: number | undefined;
52
- }): Object;
53
- export type BarData = import("./types").BarData;
54
- export type ScaleFields = import("./types").ScaleFields;
55
- export type ChartScales = import("./types").ChartScales;
56
- export type ChartDimensions = import("./types").ChartDimensions;
@@ -1,201 +0,0 @@
1
- import { SvelteSet } from 'svelte/reactivity'
2
- import {} from './types.js'
3
-
4
- /**
5
- * @typedef {import('./types').BarData} BarData
6
- * @typedef {import('./types').ScaleFields} ScaleFields
7
- * @typedef {import('./types').ChartScales} ChartScales
8
- * @typedef {import('./types').ChartDimensions} ChartDimensions
9
- */
10
-
11
- const DEFAULT_COLOR = '#4682b4'
12
-
13
- /**
14
- * @param {Object} d - data item
15
- * @param {{ fields: Object, scales: Object, dimensions: Object, defaultColor: string }} ctx
16
- * @returns {BarData}
17
- */
18
- function buildBar(d, ctx) {
19
- const { fields, scales, dimensions, defaultColor } = ctx
20
- const { x: xField, y: yField, color: colorField } = fields
21
- const barWidth = scales.x.bandwidth ? scales.x.bandwidth() : 10
22
- const barX = scales.x.bandwidth ? scales.x(d[xField]) : scales.x(d[xField]) - barWidth / 2
23
- return {
24
- data: d,
25
- x: barX,
26
- y: scales.y(d[yField]),
27
- width: barWidth,
28
- height: dimensions.innerHeight - scales.y(d[yField]),
29
- color: colorField && scales.color ? scales.color(d[colorField]) : defaultColor
30
- }
31
- }
32
-
33
- /**
34
- * @param {Object} options
35
- * @param {Object} fields
36
- * @param {Object} scales
37
- * @returns {{ ctx: Object }|null} null if input is invalid
38
- */
39
- function parseBarsInput(options, fields, scales) {
40
- if (!scales.x || !scales.y) return null
41
- const opts = options || {}
42
- return {
43
- fields,
44
- scales,
45
- dimensions: opts.dimensions,
46
- defaultColor: opts.defaultColor !== undefined ? opts.defaultColor : DEFAULT_COLOR
47
- }
48
- }
49
-
50
- /**
51
- * Creates bar data for rendering
52
- *
53
- * @param {Array} data - Chart data
54
- * @param {Object} fields - Field mappings
55
- * @param {string} fields.x - X-axis field
56
- * @param {string} fields.y - Y-axis field
57
- * @param {string} fields.color - Color field (optional)
58
- * @param {Object} scales - Chart scales
59
- * @param {Function} scales.x - X-axis scale
60
- * @param {Function} scales.y - Y-axis scale
61
- * @param {Function} scales.color - Color scale
62
- * @param {Object} options - Options including dimensions and defaultColor
63
- * @param {Object} options.dimensions - Chart dimensions
64
- * @param {string} [options.defaultColor='#4682b4'] - Default color if no color scale
65
- * @returns {BarData[]} Bar data for rendering
66
- */
67
- export function createBars(data, fields, scales, options) {
68
- if (!data || !data.length) return []
69
- const ctx = parseBarsInput(options, fields, scales)
70
- if (!ctx) return []
71
- return data.map((d) => buildBar(d, ctx))
72
- }
73
-
74
- /**
75
- * Filter bars based on a selection criteria
76
- *
77
- * @param {BarData[]} bars - Bar data array
78
- * @param {Object} selection - Selection criteria
79
- * @returns {BarData[]} Filtered bars
80
- */
81
- export function filterBars(bars, selection) {
82
- if (!selection) return bars
83
-
84
- return bars.filter((bar) => {
85
- for (const [key, value] of Object.entries(selection)) {
86
- if (bar.data[key] !== value) return false
87
- }
88
- return true
89
- })
90
- }
91
-
92
- /**
93
- * @param {Object} item - data item
94
- * @param {Object} scales - chart scales
95
- * @param {{ yField: string, colorField: string, group: unknown, barX: number, barWidth: number, dimensions: Object }} ctx
96
- * @returns {BarData}
97
- */
98
- function buildGroupBar(item, scales, ctx) {
99
- const { yField, colorField, group, barX, barWidth, dimensions } = ctx
100
- return {
101
- data: item,
102
- group,
103
- x: barX,
104
- y: scales.y(item[yField]),
105
- width: barWidth,
106
- height: dimensions.innerHeight - scales.y(item[yField]),
107
- color: scales.color ? scales.color(item[colorField]) : DEFAULT_COLOR
108
- }
109
- }
110
-
111
- /**
112
- * @param {Object} scales
113
- * @param {{ groupItems: Array, groupField: string, group: unknown, i: number, yField: string, colorField: string, barWidth: number, padding: number, dimensions: Object, xPos: number }} ctx
114
- * @returns {BarData|null}
115
- */
116
- function buildOneGroupBar(scales, ctx) {
117
- const {
118
- groupItems,
119
- groupField,
120
- group,
121
- i,
122
- yField,
123
- colorField,
124
- barWidth,
125
- padding,
126
- dimensions,
127
- xPos
128
- } = ctx
129
- const item = groupItems.find((d) => d[groupField] === group)
130
- if (!item) return null
131
- const barX = xPos + i * (barWidth + padding)
132
- return buildGroupBar(item, scales, { yField, colorField, group, barX, barWidth, dimensions })
133
- }
134
-
135
- /**
136
- * @param {Object} fields
137
- * @param {Object} options
138
- * @returns {{ xField: string, yField: string, groupField: string, colorField: string, dimensions: Object, padding: number }}
139
- */
140
- function parseGroupedBarsConfig(fields, options) {
141
- const opts = options || {}
142
- return {
143
- xField: fields.x,
144
- yField: fields.y,
145
- groupField: fields.group,
146
- colorField: fields.color !== undefined ? fields.color : fields.group,
147
- dimensions: opts.dimensions,
148
- padding: opts.padding !== undefined ? opts.padding : 0.1
149
- }
150
- }
151
-
152
- /**
153
- * Creates a grouped bars layout
154
- *
155
- * @param {Array} data - Chart data
156
- * @param {Object} fields - Field mappings
157
- * @param {Object} scales - Chart scales
158
- * @param {Object} options - Options including dimensions and padding
159
- * @param {Object} options.dimensions - Chart dimensions
160
- * @param {number} [options.padding=0.1] - Padding between bars in a group
161
- * @returns {Object} Grouped bar data
162
- */
163
- export function createGroupedBars(data, fields, scales, options) {
164
- if (!data || !data.length || !fields.group) return { groups: [], bars: [] }
165
-
166
- const { xField, yField, groupField, colorField, dimensions, padding } = parseGroupedBarsConfig(
167
- fields,
168
- options
169
- )
170
-
171
- const groups = [...new SvelteSet(data.map((d) => d[groupField]))]
172
- const xValues = [...new SvelteSet(data.map((d) => d[xField]))]
173
-
174
- const xScale = scales.x
175
- const groupWidth = xScale.bandwidth ? xScale.bandwidth() : 20
176
- const barWidth = (groupWidth - padding * (groups.length - 1)) / groups.length
177
-
178
- const bars = []
179
- xValues.forEach((xValue) => {
180
- const groupItems = data.filter((d) => d[xField] === xValue)
181
- const xPos = xScale(xValue)
182
- groups.forEach((group, i) => {
183
- const ctx = {
184
- groupItems,
185
- groupField,
186
- group,
187
- i,
188
- yField,
189
- colorField,
190
- barWidth,
191
- padding,
192
- dimensions,
193
- xPos
194
- }
195
- const bar = buildOneGroupBar(scales, ctx)
196
- if (bar) bars.push(bar)
197
- })
198
- })
199
-
200
- return { groups, bars }
201
- }
@@ -1,124 +0,0 @@
1
- export function buildLegendGroups(channels: any, colorMap: any, patternMap: any, symbolMap: any): {
2
- field: any;
3
- items: any;
4
- }[];
5
- export class ChartBrewer {
6
- /**
7
- * Override in subclasses to apply stat aggregation.
8
- * @param {Object[]} data
9
- * @param {Object} channels
10
- * @param {string|Function} stat
11
- * @returns {Object[]}
12
- */
13
- transform(data: Object[], _channels: any, _stat: any): Object[];
14
- /** Aggregated data — all derived marks read this, not #rawData */
15
- processedData: Object[];
16
- /** Exposes channels to subclasses for use in their own $derived properties */
17
- get channels(): {};
18
- /** @type {Map<unknown, {fill:string,stroke:string}>} */
19
- colorMap: Map<unknown, {
20
- fill: string;
21
- stroke: string;
22
- }>;
23
- /** @type {Map<unknown, string>} */
24
- patternMap: Map<unknown, string>;
25
- /**
26
- * Unified pattern defs for ChartPatternDefs.
27
- * When fill and pattern map the same field, pattern key = color key (simple case).
28
- * When they differ, each unique (fillKey, patternKey) pair gets its own pattern def
29
- * so bars/areas can have distinct colors per region AND distinct textures per category.
30
- * @type {Array<{ id: string, name: string, fill: string, stroke: string }>}
31
- */
32
- patternDefs: Array<{
33
- id: string;
34
- name: string;
35
- fill: string;
36
- stroke: string;
37
- }>;
38
- /** @type {Map<unknown, string>} */
39
- symbolMap: Map<unknown, string>;
40
- get innerWidth(): number;
41
- get innerHeight(): number;
42
- xScale: import("d3-scale").ScaleBand<string> | (number[] & import("d3-scale").ScaleLinear<number, number, never>) | null;
43
- yScale: import("d3-scale").ScaleLinear<number, number, never> | null;
44
- sizeScale: import("d3-scale").ScalePower<number, number, never> | null;
45
- bars: any;
46
- lines: {
47
- d: string | null;
48
- fill: string;
49
- stroke: string;
50
- points: {
51
- x: number;
52
- y: number;
53
- data: Record<string, unknown>;
54
- i: number;
55
- }[];
56
- key?: unknown;
57
- }[];
58
- areas: {
59
- d: string | null;
60
- fill: any;
61
- stroke: string;
62
- key: any;
63
- colorKey: any;
64
- patternKey: any;
65
- patternId: string | null;
66
- }[] | {
67
- d: never;
68
- fill: any;
69
- stroke: string;
70
- colorKey: null;
71
- patternKey: null;
72
- patternId: null;
73
- }[];
74
- arcs: {
75
- d: string | null;
76
- fill: string;
77
- stroke: string;
78
- key: unknown;
79
- patternId: string | null;
80
- pct: number;
81
- centroid: [number, number];
82
- data: Record<string, unknown>;
83
- }[];
84
- points: {
85
- data: Record<string, unknown>;
86
- cx: number;
87
- cy: number;
88
- r: number;
89
- fill: string;
90
- stroke: string;
91
- symbolPath: string | null;
92
- key: unknown;
93
- }[];
94
- legendGroups: {
95
- field: any;
96
- items: any;
97
- }[];
98
- get margin(): {
99
- top: number;
100
- right: number;
101
- bottom: number;
102
- left: number;
103
- };
104
- get width(): number;
105
- get height(): number;
106
- get mode(): string;
107
- /**
108
- * @param {{ data?: Object[], channels?: Object, width?: number, height?: number, mode?: string, margin?: Object, layers?: Object[], curve?: string, stat?: string|Function }} opts
109
- * Supported channel keys: `x`, `y`, `fill`, `color`, `pattern`, `symbol`, `size`, `label`.
110
- * `frame` is reserved for future animation use (no-op).
111
- */
112
- update(opts?: {
113
- data?: Object[];
114
- channels?: Object;
115
- width?: number;
116
- height?: number;
117
- mode?: string;
118
- margin?: Object;
119
- layers?: Object[];
120
- curve?: string;
121
- stat?: string | Function;
122
- }): void;
123
- #private;
124
- }