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

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 +1 -1
  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
package/src/lib/utils.js CHANGED
@@ -1,10 +1,10 @@
1
- import { scaleBand, scaleLinear, scaleOrdinal } from 'd3-scale';
2
- import { schemeCategory10 } from 'd3-scale-chromatic';
3
- import { max } from 'd3-array';
1
+ import { scaleBand, scaleLinear, scaleOrdinal } from 'd3-scale'
2
+ import { schemeCategory10 } from 'd3-scale-chromatic'
3
+ import { max } from 'd3-array'
4
4
 
5
5
  /**
6
6
  * Creates appropriate scales based on data and dimensions
7
- *
7
+ *
8
8
  * @param {Array} data The dataset
9
9
  * @param {string} xKey Field to use for x-axis
10
10
  * @param {string} yKey Field to use for y-axis
@@ -14,44 +14,42 @@ import { max } from 'd3-array';
14
14
  * @returns {Object} Object containing xScale, yScale, and colorScale
15
15
  */
16
16
  export function createScales(data, xKey, yKey, dimensions, options = {}) {
17
- if (!data || data.length === 0) return {};
18
-
19
- const xScale = scaleBand()
20
- .domain(data.map(d => d[xKey]))
21
- .range([0, dimensions.width])
22
- .padding(0.2);
23
-
24
- const yScale = scaleLinear()
25
- .domain([0, max(data, d => d[yKey]) * 1.1]) // Add 10% padding on top
26
- .nice()
27
- .range([dimensions.height, 0]);
28
-
29
- let colorScale = null;
30
-
31
- if (options.colorKey) {
32
- const uniqueCategories = [...new Set(data.map(d => d[options.colorKey]))];
33
- colorScale = scaleOrdinal()
34
- .domain(uniqueCategories)
35
- .range(schemeCategory10);
36
- }
37
-
38
- return { xScale, yScale, colorScale };
17
+ if (!data || data.length === 0) return {}
18
+
19
+ const xScale = scaleBand()
20
+ .domain(data.map((d) => d[xKey]))
21
+ .range([0, dimensions.width])
22
+ .padding(0.2)
23
+
24
+ const yScale = scaleLinear()
25
+ .domain([0, max(data, (d) => d[yKey]) * 1.1]) // Add 10% padding on top
26
+ .nice()
27
+ .range([dimensions.height, 0])
28
+
29
+ let colorScale = null
30
+
31
+ if (options.colorKey) {
32
+ const uniqueCategories = [...new Set(data.map((d) => d[options.colorKey]))]
33
+ colorScale = scaleOrdinal().domain(uniqueCategories).range(schemeCategory10)
34
+ }
35
+
36
+ return { xScale, yScale, colorScale }
39
37
  }
40
38
 
41
39
  /**
42
40
  * Calculates the actual chart dimensions after applying margins
43
- *
41
+ *
44
42
  * @param {Object} dimensions Original dimensions
45
43
  * @returns {Object} Dimensions with calculated inner width and height
46
44
  */
47
45
  export function calculateChartDimensions(dimensions) {
48
- const { width, height, margin } = dimensions;
49
-
50
- return {
51
- ...dimensions,
52
- innerWidth: width - margin.left - margin.right,
53
- innerHeight: height - margin.top - margin.bottom
54
- };
46
+ const { width, height, margin } = dimensions
47
+
48
+ return {
49
+ ...dimensions,
50
+ innerWidth: width - margin.left - margin.right,
51
+ innerHeight: height - margin.top - margin.bottom
52
+ }
55
53
  }
56
54
 
57
55
  /**
@@ -61,75 +59,75 @@ export function calculateChartDimensions(dimensions) {
61
59
  * @returns {Array} Normalized data array
62
60
  */
63
61
  export function normalizeData(inputData) {
64
- if (!inputData) return [];
65
-
66
- // If it's a dataset class instance, call select() to get the data
67
- if (inputData.select && typeof inputData.select === 'function') {
68
- return inputData.select();
69
- }
70
-
71
- // If it's already an array, return as is
72
- if (Array.isArray(inputData)) {
73
- return inputData;
74
- }
75
-
76
- return [];
62
+ if (!inputData) return []
63
+
64
+ // If it's a dataset class instance, call select() to get the data
65
+ if (inputData.select && typeof inputData.select === 'function') {
66
+ return inputData.select()
67
+ }
68
+
69
+ // If it's already an array, return as is
70
+ if (Array.isArray(inputData)) {
71
+ return inputData
72
+ }
73
+
74
+ return []
77
75
  }
78
76
 
79
77
  /**
80
78
  * Generates a unique ID for SVG elements
81
- *
79
+ *
82
80
  * @param {string} prefix Prefix for the ID
83
81
  * @returns {string} A unique ID
84
82
  */
85
83
  export function uniqueId(prefix = 'chart') {
86
- return `${prefix}-${Math.random().toString(36).substring(2, 10)}`;
84
+ return `${prefix}-${Math.random().toString(36).substring(2, 10)}`
87
85
  }
88
86
 
89
87
  /**
90
88
  * Formats tooltip content for a data point
91
- *
89
+ *
92
90
  * @param {Object} d Data point
93
91
  * @param {Object} options Tooltip format options
94
92
  * @returns {string} Formatted tooltip HTML content
95
93
  */
96
94
  export function formatTooltipContent(d, options = {}) {
97
- if (!d) return '';
98
-
99
- const { xKey, yKey, xFormat, yFormat } = options;
100
-
101
- if (xKey && yKey) {
102
- const xValue = d[xKey];
103
- const yValue = d[yKey];
104
-
105
- const xFormatted = xFormat ? xFormat(xValue) : xValue;
106
- const yFormatted = yFormat ? yFormat(yValue) : yValue;
107
-
108
- return `${xKey}: ${xFormatted}<br>${yKey}: ${yFormatted}`;
109
- }
110
-
111
- return Object.entries(d)
112
- .map(([key, value]) => `${key}: ${value}`)
113
- .join('<br>');
95
+ if (!d) return ''
96
+
97
+ const { xKey, yKey, xFormat, yFormat } = options
98
+
99
+ if (xKey && yKey) {
100
+ const xValue = d[xKey]
101
+ const yValue = d[yKey]
102
+
103
+ const xFormatted = xFormat ? xFormat(xValue) : xValue
104
+ const yFormatted = yFormat ? yFormat(yValue) : yValue
105
+
106
+ return `${xKey}: ${xFormatted}<br>${yKey}: ${yFormatted}`
107
+ }
108
+
109
+ return Object.entries(d)
110
+ .map(([key, value]) => `${key}: ${value}`)
111
+ .join('<br>')
114
112
  }
115
113
 
116
114
  /**
117
115
  * Generates a tooltip formatter function
118
- *
116
+ *
119
117
  * @param {Object} options Tooltip format options
120
118
  * @returns {Function} A function that formats tooltip content
121
119
  */
122
120
  export function createTooltipFormatter(options = {}) {
123
- return (d) => formatTooltipContent(d, options);
121
+ return (d) => formatTooltipContent(d, options)
124
122
  }
125
123
 
126
124
  /**
127
125
  * Calculates the transform attribute for SVG elements
128
- *
126
+ *
129
127
  * @param {number} x X position
130
128
  * @param {number} y Y position
131
129
  * @returns {string} Transform attribute value
132
130
  */
133
131
  export function transform(x, y) {
134
- return `translate(${x}, ${y})`;
135
- }
132
+ return `translate(${x}, ${y})`
133
+ }
@@ -1,6 +1,10 @@
1
1
  import { min, max } from 'd3-array'
2
2
  import { scaleBand, scaleLinear, scaleTime } from 'd3-scale'
3
3
 
4
+ function getOriginValue(scale) {
5
+ return scale.ticks ? scale(Math.max(0, Math.min(...scale.domain()))) : scale.range()[0]
6
+ }
7
+
4
8
  function getScale(domain, range, padding = 0) {
5
9
  if (domain.some(isNaN)) {
6
10
  return scaleBand().domain(domain).range(range).padding(padding)
@@ -204,10 +208,6 @@ class Chart {
204
208
  }
205
209
  }
206
210
 
207
- function getOriginValue(scale) {
208
- return scale.ticks ? scale(Math.max(0, Math.min(...scale.domain()))) : scale.range()[0]
209
- }
210
-
211
211
  export function chart(data, aes) {
212
212
  return new Chart(data, aes)
213
213
  }
@@ -12,6 +12,6 @@
12
12
  ]
13
13
  </script>
14
14
 
15
- {#each lines as line}
15
+ {#each lines as line, index (index)}
16
16
  <line {...line} {stroke} stroke-width={thickness} stroke-linecap="round" />
17
17
  {/each}
@@ -13,6 +13,6 @@
13
13
  }))
14
14
  </script>
15
15
 
16
- {#each data as { cx, cy, r }}
16
+ {#each data as { cx, cy, r }, index (index)}
17
17
  <circle {cx} {cy} {r} {fill} {stroke} />
18
18
  {/each}
@@ -9,6 +9,6 @@
9
9
  ]
10
10
  </script>
11
11
 
12
- {#each lines as line}
12
+ {#each lines as line, index (index)}
13
13
  <line {...line} {stroke} stroke-width={thickness} stroke-linecap="round" />
14
14
  {/each}
@@ -14,6 +14,6 @@
14
14
  ].map((x) => ({ ...x, r: 0.08 * size }))
15
15
  </script>
16
16
 
17
- {#each data as { cx, cy, r }}
17
+ {#each data as { cx, cy, r }, index (index)}
18
18
  <circle {cx} {cy} {r} {fill} />
19
19
  {/each}
@@ -10,6 +10,6 @@
10
10
  $: r = 0.5 * size
11
11
  </script>
12
12
 
13
- {#each centres as { cx, cy }}
13
+ {#each centres as { cx, cy }, index (index)}
14
14
  <circle {cx} {cy} {r} {fill} {stroke} />
15
15
  {/each}
@@ -12,6 +12,6 @@
12
12
  ]
13
13
  </script>
14
14
 
15
- {#each lines as line}
15
+ {#each lines as line, index (index)}
16
16
  <line {...line} {stroke} stroke-width={thickness} stroke-linecap="round" />
17
17
  {/each}
@@ -10,6 +10,6 @@
10
10
  ]
11
11
  </script>
12
12
 
13
- {#each polygons as points}
13
+ {#each polygons as points, index (index)}
14
14
  <polygon points={points.join(', ')} {fill} {stroke} stroke-width="0" />
15
15
  {/each}
@@ -9,7 +9,6 @@
9
9
  $: props = { rx: r * 0.1, ry: r * 0.1, ...$$restProps }
10
10
  </script>
11
11
 
12
- <!-- svelte-ignore a11y-click-events-have-key-events -->
13
12
  <rect
14
13
  x={x - r}
15
14
  y={y - r}
@@ -11,6 +11,6 @@
11
11
  }))
12
12
  </script>
13
13
 
14
- {#each circles as { cx, cy, r }}
14
+ {#each circles as { cx, cy, r }, index (index)}
15
15
  <circle {cx} {cy} {r} {fill} {stroke} />
16
16
  {/each}
@@ -12,6 +12,6 @@
12
12
  }))
13
13
  </script>
14
14
 
15
- {#each lines as line}
15
+ {#each lines as line, index (index)}
16
16
  <line {...line} {stroke} stroke-width={thickness} stroke-linecap="round" />
17
17
  {/each}
@@ -13,6 +13,6 @@
13
13
  // ]
14
14
  </script>
15
15
 
16
- {#each polygons as points}
16
+ {#each polygons as points, index (index)}
17
17
  <polygon {points} {fill} {stroke} stroke-width={thickness} />
18
18
  {/each}