@rokkit/chart 1.0.0-next.121 → 1.0.0-next.123

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 (36) hide show
  1. package/README.md +43 -35
  2. package/package.json +3 -3
  3. package/src/Plot/Axis.svelte +87 -95
  4. package/src/Plot/Bar.svelte +86 -86
  5. package/src/Plot/Grid.svelte +57 -57
  6. package/src/Plot/Legend.svelte +120 -122
  7. package/src/Plot/Root.svelte +107 -105
  8. package/src/elements/ColorRamp.svelte +19 -19
  9. package/src/elements/ContinuousLegend.svelte +1 -1
  10. package/src/elements/DefinePatterns.svelte +1 -1
  11. package/src/elements/DiscreteLegend.svelte +1 -1
  12. package/src/elements/SymbolGrid.svelte +1 -1
  13. package/src/examples/BarChartExample.svelte +42 -42
  14. package/src/index.js +13 -13
  15. package/src/lib/brewing/axes.svelte.js +120 -122
  16. package/src/lib/brewing/bars.svelte.js +70 -70
  17. package/src/lib/brewing/dimensions.svelte.js +25 -23
  18. package/src/lib/brewing/index.svelte.js +192 -204
  19. package/src/lib/brewing/legends.svelte.js +51 -52
  20. package/src/lib/brewing/scales.svelte.js +59 -68
  21. package/src/lib/brewing/types.js +2 -2
  22. package/src/lib/context.js +117 -107
  23. package/src/lib/scales.svelte.js +80 -87
  24. package/src/lib/utils.js +70 -72
  25. package/src/old_lib/chart.js +4 -4
  26. package/src/patterns/Brick.svelte +1 -1
  27. package/src/patterns/Circles.svelte +1 -1
  28. package/src/patterns/CrossHatch.svelte +1 -1
  29. package/src/patterns/Dots.svelte +1 -1
  30. package/src/patterns/OutlineCircles.svelte +1 -1
  31. package/src/patterns/Tile.svelte +1 -1
  32. package/src/patterns/Triangles.svelte +1 -1
  33. package/src/symbols/RoundedSquare.svelte +0 -1
  34. package/src/template/shapes/Circles.svelte +1 -1
  35. package/src/template/shapes/Lines.svelte +1 -1
  36. package/src/template/shapes/Polygons.svelte +1 -1
@@ -1,4 +1,4 @@
1
- import { } from './types.js';
1
+ import {} from './types.js'
2
2
 
3
3
  /**
4
4
  * @typedef {import('./types').ChartMargin} ChartMargin
@@ -9,29 +9,29 @@ import { } from './types.js';
9
9
  * Default chart margin
10
10
  * @type {ChartMargin}
11
11
  */
12
- export const DEFAULT_MARGIN = { top: 20, right: 30, bottom: 40, left: 50 };
12
+ export const DEFAULT_MARGIN = { top: 20, right: 30, bottom: 40, left: 50 }
13
13
 
14
14
  /**
15
15
  * Creates chart dimensions based on width, height and margins
16
- *
16
+ *
17
17
  * @param {number} width - Total chart width
18
18
  * @param {number} height - Total chart height
19
19
  * @param {ChartMargin} margin - Chart margins
20
20
  * @returns {ChartDimensions} Chart dimensions
21
21
  */
22
22
  export function createDimensions(width = 600, height = 400, margin = DEFAULT_MARGIN) {
23
- return {
24
- width,
25
- height,
26
- margin: { ...margin },
27
- innerWidth: width - margin.left - margin.right,
28
- innerHeight: height - margin.top - margin.bottom
29
- };
23
+ return {
24
+ width,
25
+ height,
26
+ margin: { ...margin },
27
+ innerWidth: width - margin.left - margin.right,
28
+ innerHeight: height - margin.top - margin.bottom
29
+ }
30
30
  }
31
31
 
32
32
  /**
33
33
  * Updates existing dimensions with new values
34
- *
34
+ *
35
35
  * @param {ChartDimensions} dimensions - Current dimensions
36
36
  * @param {Object} updates - Values to update
37
37
  * @param {number} [updates.width] - New width
@@ -40,15 +40,17 @@ export function createDimensions(width = 600, height = 400, margin = DEFAULT_MAR
40
40
  * @returns {ChartDimensions} Updated dimensions
41
41
  */
42
42
  export function updateDimensions(dimensions, updates = {}) {
43
- const newDimensions = { ...dimensions };
44
-
45
- if (updates.width !== undefined) newDimensions.width = updates.width;
46
- if (updates.height !== undefined) newDimensions.height = updates.height;
47
- if (updates.margin !== undefined) newDimensions.margin = { ...updates.margin };
48
-
49
- // Recalculate inner dimensions
50
- newDimensions.innerWidth = newDimensions.width - newDimensions.margin.left - newDimensions.margin.right;
51
- newDimensions.innerHeight = newDimensions.height - newDimensions.margin.top - newDimensions.margin.bottom;
52
-
53
- return newDimensions;
54
- }
43
+ const newDimensions = { ...dimensions }
44
+
45
+ if (updates.width !== undefined) newDimensions.width = updates.width
46
+ if (updates.height !== undefined) newDimensions.height = updates.height
47
+ if (updates.margin !== undefined) newDimensions.margin = { ...updates.margin }
48
+
49
+ // Recalculate inner dimensions
50
+ newDimensions.innerWidth =
51
+ newDimensions.width - newDimensions.margin.left - newDimensions.margin.right
52
+ newDimensions.innerHeight =
53
+ newDimensions.height - newDimensions.margin.top - newDimensions.margin.bottom
54
+
55
+ return newDimensions
56
+ }
@@ -1,214 +1,202 @@
1
- import { createDimensions, updateDimensions } from './dimensions.svelte.js';
2
- import { createScales, getOrigin } from './scales.svelte.js';
3
- import { createBars, filterBars, createGroupedBars } from './bars.svelte.js';
4
- import {
5
- createXAxis,
6
- createYAxis,
7
- createGrid,
8
- createTickAttributes
9
- } from './axes.svelte.js';
10
- import {
11
- createLegend,
12
- filterByLegend,
13
- createLegendItemAttributes
14
- } from './legends.svelte.js';
1
+ import { createDimensions, updateDimensions } from './dimensions.svelte.js'
2
+ import { createScales, getOrigin } from './scales.svelte.js'
3
+ import { createBars, filterBars, createGroupedBars } from './bars.svelte.js'
4
+ import { createXAxis, createYAxis, createGrid, createTickAttributes } from './axes.svelte.js'
5
+ import { createLegend, filterByLegend, createLegendItemAttributes } from './legends.svelte.js'
15
6
 
16
7
  /**
17
8
  * Chart Brewing - A collection of pure functions for chart data preparation
18
9
  */
19
10
  export {
20
- // Dimensions
21
- createDimensions,
22
- updateDimensions,
23
-
24
- // Scales
25
- createScales,
26
- getOrigin,
27
-
28
- // Bars
29
- createBars,
30
- filterBars,
31
- createGroupedBars,
32
-
33
- // Axes
34
- createXAxis,
35
- createYAxis,
36
- createGrid,
37
- createTickAttributes,
38
-
39
- // Legends
40
- createLegend,
41
- filterByLegend,
42
- createLegendItemAttributes
43
- };
11
+ // Dimensions
12
+ createDimensions,
13
+ updateDimensions,
14
+
15
+ // Scales
16
+ createScales,
17
+ getOrigin,
18
+
19
+ // Bars
20
+ createBars,
21
+ filterBars,
22
+ createGroupedBars,
23
+
24
+ // Axes
25
+ createXAxis,
26
+ createYAxis,
27
+ createGrid,
28
+ createTickAttributes,
29
+
30
+ // Legends
31
+ createLegend,
32
+ filterByLegend,
33
+ createLegendItemAttributes
34
+ }
44
35
 
45
36
  /**
46
37
  * Main class that manages chart state and provides access to all brewing functions
47
38
  */
48
39
  export class ChartBrewer {
49
- #data = [];
50
- #dimensions = createDimensions();
51
- #scales = { x: null, y: null, color: null };
52
- #fields = { x: null, y: null, color: null };
53
- #options = {
54
- padding: 0.2,
55
- animationDuration: 300
56
- };
57
-
58
- /**
59
- * Creates a new ChartBrewer instance
60
- *
61
- * @param {Object} options Configuration options
62
- */
63
- constructor(options = {}) {
64
- this.#dimensions = createDimensions(
65
- options.width,
66
- options.height,
67
- options.margin
68
- );
69
-
70
- if (options.padding !== undefined) this.#options.padding = options.padding;
71
- if (options.animationDuration !== undefined) this.#options.animationDuration = options.animationDuration;
72
- }
73
-
74
- /**
75
- * Sets the data for the chart
76
- *
77
- * @param {Array} data Data array
78
- * @returns {ChartBrewer} this for method chaining
79
- */
80
- setData(data) {
81
- this.#data = Array.isArray(data) ? data : [];
82
- return this;
83
- }
84
-
85
- /**
86
- * Sets the field mappings for axes and color
87
- *
88
- * @param {Object} fields Field mappings
89
- * @returns {ChartBrewer} this for method chaining
90
- */
91
- setFields({ x, y, color }) {
92
- if (x !== undefined) this.#fields.x = x;
93
- if (y !== undefined) this.#fields.y = y;
94
- if (color !== undefined) this.#fields.color = color;
95
- return this;
96
- }
97
-
98
- /**
99
- * Sets the dimensions of the chart
100
- *
101
- * @param {Object} dimensions Chart dimensions
102
- * @returns {ChartBrewer} this for method chaining
103
- */
104
- setDimensions({ width, height, margin }) {
105
- this.#dimensions = updateDimensions(this.#dimensions, { width, height, margin });
106
- return this;
107
- }
108
-
109
- /**
110
- * Creates scales based on data and dimensions
111
- *
112
- * @returns {ChartBrewer} this for method chaining
113
- */
114
- createScales() {
115
- this.#scales = createScales(this.#data, this.#fields, this.#dimensions, {
116
- padding: this.#options.padding
117
- });
118
- return this;
119
- }
120
-
121
- /**
122
- * Creates bar data for rendering
123
- *
124
- * @returns {Array} Data for rendering bars
125
- */
126
- createBars() {
127
- return createBars(this.#data, this.#fields, this.#scales, this.#dimensions);
128
- }
129
-
130
- /**
131
- * Creates x-axis tick data for rendering
132
- *
133
- * @param {Object} options Axis options
134
- * @returns {Object} Axis rendering data
135
- */
136
- createXAxis(options = {}) {
137
- return createXAxis(this.#scales, this.#dimensions, options);
138
- }
139
-
140
- /**
141
- * Creates y-axis tick data for rendering
142
- *
143
- * @param {Object} options Axis options
144
- * @returns {Object} Axis rendering data
145
- */
146
- createYAxis(options = {}) {
147
- return createYAxis(this.#scales, this.#dimensions, options);
148
- }
149
-
150
- /**
151
- * Creates grid line data for rendering
152
- *
153
- * @param {Object} options Grid options
154
- * @returns {Object} Grid rendering data
155
- */
156
- createGrid(options = {}) {
157
- return createGrid(this.#scales, this.#dimensions, options);
158
- }
159
-
160
- /**
161
- * Creates legend data for rendering
162
- *
163
- * @param {Object} options Legend options
164
- * @returns {Object} Legend rendering data
165
- */
166
- createLegend(options = {}) {
167
- return createLegend(this.#data, this.#fields, this.#scales, this.#dimensions, options);
168
- }
169
-
170
- /**
171
- * Gets all chart dimensions
172
- *
173
- * @returns {Object} Chart dimensions
174
- */
175
- getDimensions() {
176
- return { ...this.#dimensions };
177
- }
178
-
179
- /**
180
- * Gets all chart scales
181
- *
182
- * @returns {Object} Chart scales
183
- */
184
- getScales() {
185
- return { ...this.#scales };
186
- }
187
-
188
- /**
189
- * Gets the animation duration
190
- *
191
- * @returns {number} Animation duration in ms
192
- */
193
- getAnimationDuration() {
194
- return this.#options.animationDuration;
195
- }
196
-
197
- /**
198
- * Gets the data being used
199
- *
200
- * @returns {Array} Chart data
201
- */
202
- getData() {
203
- return [...this.#data];
204
- }
205
-
206
- /**
207
- * Gets the fields configuration
208
- *
209
- * @returns {Object} Fields configuration
210
- */
211
- getFields() {
212
- return { ...this.#fields };
213
- }
214
- }
40
+ #data = []
41
+ #dimensions = createDimensions()
42
+ #scales = { x: null, y: null, color: null }
43
+ #fields = { x: null, y: null, color: null }
44
+ #options = {
45
+ padding: 0.2,
46
+ animationDuration: 300
47
+ }
48
+
49
+ /**
50
+ * Creates a new ChartBrewer instance
51
+ *
52
+ * @param {Object} options Configuration options
53
+ */
54
+ constructor(options = {}) {
55
+ this.#dimensions = createDimensions(options.width, options.height, options.margin)
56
+
57
+ if (options.padding !== undefined) this.#options.padding = options.padding
58
+ if (options.animationDuration !== undefined)
59
+ this.#options.animationDuration = options.animationDuration
60
+ }
61
+
62
+ /**
63
+ * Sets the data for the chart
64
+ *
65
+ * @param {Array} data Data array
66
+ * @returns {ChartBrewer} this for method chaining
67
+ */
68
+ setData(data) {
69
+ this.#data = Array.isArray(data) ? data : []
70
+ return this
71
+ }
72
+
73
+ /**
74
+ * Sets the field mappings for axes and color
75
+ *
76
+ * @param {Object} fields Field mappings
77
+ * @returns {ChartBrewer} this for method chaining
78
+ */
79
+ setFields({ x, y, color }) {
80
+ if (x !== undefined) this.#fields.x = x
81
+ if (y !== undefined) this.#fields.y = y
82
+ if (color !== undefined) this.#fields.color = color
83
+ return this
84
+ }
85
+
86
+ /**
87
+ * Sets the dimensions of the chart
88
+ *
89
+ * @param {Object} dimensions Chart dimensions
90
+ * @returns {ChartBrewer} this for method chaining
91
+ */
92
+ setDimensions({ width, height, margin }) {
93
+ this.#dimensions = updateDimensions(this.#dimensions, { width, height, margin })
94
+ return this
95
+ }
96
+
97
+ /**
98
+ * Creates scales based on data and dimensions
99
+ *
100
+ * @returns {ChartBrewer} this for method chaining
101
+ */
102
+ createScales() {
103
+ this.#scales = createScales(this.#data, this.#fields, this.#dimensions, {
104
+ padding: this.#options.padding
105
+ })
106
+ return this
107
+ }
108
+
109
+ /**
110
+ * Creates bar data for rendering
111
+ *
112
+ * @returns {Array} Data for rendering bars
113
+ */
114
+ createBars() {
115
+ return createBars(this.#data, this.#fields, this.#scales, this.#dimensions)
116
+ }
117
+
118
+ /**
119
+ * Creates x-axis tick data for rendering
120
+ *
121
+ * @param {Object} options Axis options
122
+ * @returns {Object} Axis rendering data
123
+ */
124
+ createXAxis(options = {}) {
125
+ return createXAxis(this.#scales, this.#dimensions, options)
126
+ }
127
+
128
+ /**
129
+ * Creates y-axis tick data for rendering
130
+ *
131
+ * @param {Object} options Axis options
132
+ * @returns {Object} Axis rendering data
133
+ */
134
+ createYAxis(options = {}) {
135
+ return createYAxis(this.#scales, this.#dimensions, options)
136
+ }
137
+
138
+ /**
139
+ * Creates grid line data for rendering
140
+ *
141
+ * @param {Object} options Grid options
142
+ * @returns {Object} Grid rendering data
143
+ */
144
+ createGrid(options = {}) {
145
+ return createGrid(this.#scales, this.#dimensions, options)
146
+ }
147
+
148
+ /**
149
+ * Creates legend data for rendering
150
+ *
151
+ * @param {Object} options Legend options
152
+ * @returns {Object} Legend rendering data
153
+ */
154
+ createLegend(options = {}) {
155
+ return createLegend(this.#data, this.#fields, this.#scales, this.#dimensions, options)
156
+ }
157
+
158
+ /**
159
+ * Gets all chart dimensions
160
+ *
161
+ * @returns {Object} Chart dimensions
162
+ */
163
+ getDimensions() {
164
+ return { ...this.#dimensions }
165
+ }
166
+
167
+ /**
168
+ * Gets all chart scales
169
+ *
170
+ * @returns {Object} Chart scales
171
+ */
172
+ getScales() {
173
+ return { ...this.#scales }
174
+ }
175
+
176
+ /**
177
+ * Gets the animation duration
178
+ *
179
+ * @returns {number} Animation duration in ms
180
+ */
181
+ getAnimationDuration() {
182
+ return this.#options.animationDuration
183
+ }
184
+
185
+ /**
186
+ * Gets the data being used
187
+ *
188
+ * @returns {Array} Chart data
189
+ */
190
+ getData() {
191
+ return [...this.#data]
192
+ }
193
+
194
+ /**
195
+ * Gets the fields configuration
196
+ *
197
+ * @returns {Object} Fields configuration
198
+ */
199
+ getFields() {
200
+ return { ...this.#fields }
201
+ }
202
+ }
@@ -1,4 +1,4 @@
1
- import { } from './types.js';
1
+ import {} from './types.js'
2
2
 
3
3
  /**
4
4
  * @typedef {import('./types').LegendItem} LegendItem
@@ -10,7 +10,7 @@ import { } from './types.js';
10
10
 
11
11
  /**
12
12
  * Creates legend data for rendering
13
- *
13
+ *
14
14
  * @param {Array} data - Chart data
15
15
  * @param {Object} fields - Field mappings
16
16
  * @param {string} fields.color - Color field
@@ -25,71 +25,70 @@ import { } from './types.js';
25
25
  * @returns {LegendData} Legend rendering data
26
26
  */
27
27
  export function createLegend(data, fields, scales, dimensions, options = {}) {
28
- if (!data || !fields.color || !scales.color) {
29
- return { items: [], title: '', transform: 'translate(0, 0)' };
30
- }
31
-
32
- const { title = '', align = 'right', shape = 'rect', markerSize = 10 } = options;
33
-
34
- // Get unique color values
35
- const colorValues = [...new Set(data.map(d => d[fields.color]))];
36
-
37
- // Create legend items
38
- const items = colorValues.map((value, index) => ({
39
- value,
40
- color: scales.color(value),
41
- y: index * (markerSize + 5) + (title ? 15 : 0),
42
- shape,
43
- markerSize
44
- }));
45
-
46
- // Calculate approximate width for alignment
47
- const approximateWidth = Math.max(
48
- ...colorValues.map(v => v.toString().length)
49
- ) * 8 + markerSize + 10;
50
-
51
- // Calculate position based on alignment
52
- let x = 0;
53
- if (align === 'right') {
54
- x = dimensions.innerWidth - approximateWidth;
55
- } else if (align === 'center') {
56
- x = (dimensions.innerWidth - approximateWidth) / 2;
57
- }
58
-
59
- return {
60
- items,
61
- title,
62
- transform: `translate(${x}, 0)`
63
- };
28
+ if (!data || !fields.color || !scales.color) {
29
+ return { items: [], title: '', transform: 'translate(0, 0)' }
30
+ }
31
+
32
+ const { title = '', align = 'right', shape = 'rect', markerSize = 10 } = options
33
+
34
+ // Get unique color values
35
+ const colorValues = [...new Set(data.map((d) => d[fields.color]))]
36
+
37
+ // Create legend items
38
+ const items = colorValues.map((value, index) => ({
39
+ value,
40
+ color: scales.color(value),
41
+ y: index * (markerSize + 5) + (title ? 15 : 0),
42
+ shape,
43
+ markerSize
44
+ }))
45
+
46
+ // Calculate approximate width for alignment
47
+ const approximateWidth =
48
+ Math.max(...colorValues.map((v) => v.toString().length)) * 8 + markerSize + 10
49
+
50
+ // Calculate position based on alignment
51
+ let x = 0
52
+ if (align === 'right') {
53
+ x = dimensions.innerWidth - approximateWidth
54
+ } else if (align === 'center') {
55
+ x = (dimensions.innerWidth - approximateWidth) / 2
56
+ }
57
+
58
+ return {
59
+ items,
60
+ title,
61
+ transform: `translate(${x}, 0)`
62
+ }
64
63
  }
65
64
 
66
65
  /**
67
66
  * Filter data based on legend selection
68
- *
67
+ *
69
68
  * @param {Array} data - Chart data
70
69
  * @param {string} colorField - Field used for color mapping
71
70
  * @param {Array} selectedValues - Selected legend values
72
71
  * @returns {Array} Filtered data
73
72
  */
74
73
  export function filterByLegend(data, colorField, selectedValues) {
75
- if (!selectedValues || selectedValues.length === 0) {
76
- return data;
77
- }
78
-
79
- return data.filter(d => selectedValues.includes(d[colorField]));
74
+ if (!selectedValues || selectedValues.length === 0) {
75
+ return data
76
+ }
77
+
78
+ return data.filter((d) => selectedValues.includes(d[colorField]))
80
79
  }
81
80
 
82
81
  /**
83
82
  * Create attributes for legend items
84
- *
83
+ *
85
84
  * @param {LegendItem} item - Legend item
86
85
  * @returns {Object} Attributes for the legend item
87
86
  */
88
87
  export function createLegendItemAttributes(item) {
89
- return {
90
- 'data-plot-legend-item': '',
91
- 'transform': `translate(0, ${item.y})`,
92
- 'role': 'img',
93
- 'aria-label': `Legend item for ${item.value}`
94
- };
95
- }
88
+ return {
89
+ 'data-plot-legend-item': '',
90
+ transform: `translate(0, ${item.y})`,
91
+ role: 'img',
92
+ 'aria-label': `Legend item for ${item.value}`
93
+ }
94
+ }