@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.
- package/README.md +43 -35
- package/package.json +1 -1
- package/src/Plot/Axis.svelte +87 -95
- package/src/Plot/Bar.svelte +86 -86
- package/src/Plot/Grid.svelte +57 -57
- package/src/Plot/Legend.svelte +120 -122
- package/src/Plot/Root.svelte +107 -105
- package/src/elements/ColorRamp.svelte +19 -19
- package/src/elements/ContinuousLegend.svelte +1 -1
- package/src/elements/DefinePatterns.svelte +1 -1
- package/src/elements/DiscreteLegend.svelte +1 -1
- package/src/elements/SymbolGrid.svelte +1 -1
- package/src/examples/BarChartExample.svelte +42 -42
- package/src/index.js +13 -13
- package/src/lib/brewing/axes.svelte.js +120 -122
- package/src/lib/brewing/bars.svelte.js +70 -70
- package/src/lib/brewing/dimensions.svelte.js +25 -23
- package/src/lib/brewing/index.svelte.js +192 -204
- package/src/lib/brewing/legends.svelte.js +51 -52
- package/src/lib/brewing/scales.svelte.js +59 -68
- package/src/lib/brewing/types.js +2 -2
- package/src/lib/context.js +117 -107
- package/src/lib/scales.svelte.js +80 -87
- package/src/lib/utils.js +70 -72
- package/src/old_lib/chart.js +4 -4
- package/src/patterns/Brick.svelte +1 -1
- package/src/patterns/Circles.svelte +1 -1
- package/src/patterns/CrossHatch.svelte +1 -1
- package/src/patterns/Dots.svelte +1 -1
- package/src/patterns/OutlineCircles.svelte +1 -1
- package/src/patterns/Tile.svelte +1 -1
- package/src/patterns/Triangles.svelte +1 -1
- package/src/symbols/RoundedSquare.svelte +0 -1
- package/src/template/shapes/Circles.svelte +1 -1
- package/src/template/shapes/Lines.svelte +1 -1
- 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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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
|
-
|
|
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
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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
|
-
|
|
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
|
-
|
|
135
|
-
}
|
|
132
|
+
return `translate(${x}, ${y})`
|
|
133
|
+
}
|
package/src/old_lib/chart.js
CHANGED
|
@@ -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
|
}
|
package/src/patterns/Dots.svelte
CHANGED
package/src/patterns/Tile.svelte
CHANGED