@rokkit/chart 1.0.0-next.11

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 (68) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +55 -0
  3. package/package.json +63 -0
  4. package/src/chart/FacetGrid.svelte +51 -0
  5. package/src/chart/Grid.svelte +34 -0
  6. package/src/chart/Legend.svelte +16 -0
  7. package/src/chart/PatternDefs.svelte +13 -0
  8. package/src/chart/Swatch.svelte +93 -0
  9. package/src/chart/SwatchButton.svelte +29 -0
  10. package/src/chart/SwatchGrid.svelte +55 -0
  11. package/src/chart/Symbol.svelte +37 -0
  12. package/src/chart/Texture.svelte +16 -0
  13. package/src/chart/TexturedShape.svelte +27 -0
  14. package/src/chart/TimelapseChart.svelte +97 -0
  15. package/src/chart/Timer.svelte +27 -0
  16. package/src/chart.js +9 -0
  17. package/src/components/charts/Axis.svelte +66 -0
  18. package/src/components/charts/Chart.svelte +35 -0
  19. package/src/components/index.js +23 -0
  20. package/src/components/lib/axis.js +0 -0
  21. package/src/components/lib/chart.js +187 -0
  22. package/src/components/lib/color.js +327 -0
  23. package/src/components/lib/funnel.js +204 -0
  24. package/src/components/lib/index.js +19 -0
  25. package/src/components/lib/pattern.js +190 -0
  26. package/src/components/lib/rollup.js +55 -0
  27. package/src/components/lib/shape.js +199 -0
  28. package/src/components/lib/summary.js +145 -0
  29. package/src/components/lib/theme.js +23 -0
  30. package/src/components/lib/timer.js +41 -0
  31. package/src/components/lib/utils.js +165 -0
  32. package/src/components/plots/BarPlot.svelte +36 -0
  33. package/src/components/plots/BoxPlot.svelte +54 -0
  34. package/src/components/plots/ScatterPlot.svelte +30 -0
  35. package/src/components/store.js +70 -0
  36. package/src/constants.js +66 -0
  37. package/src/elements/Bar.svelte +35 -0
  38. package/src/elements/ColorRamp.svelte +51 -0
  39. package/src/elements/ContinuousLegend.svelte +46 -0
  40. package/src/elements/DiscreteLegend.svelte +41 -0
  41. package/src/elements/Label.svelte +12 -0
  42. package/src/elements/PatternDefs.svelte +13 -0
  43. package/src/elements/PatternMask.svelte +20 -0
  44. package/src/elements/Symbol.svelte +38 -0
  45. package/src/elements/Tooltip.svelte +23 -0
  46. package/src/funnel.svelte +35 -0
  47. package/src/geom.js +105 -0
  48. package/src/index.js +16 -0
  49. package/src/lib/axis.js +75 -0
  50. package/src/lib/colors.js +32 -0
  51. package/src/lib/geom.js +4 -0
  52. package/src/lib/shapes.js +144 -0
  53. package/src/lib/timer.js +44 -0
  54. package/src/lib/utils.js +157 -0
  55. package/src/lookup.js +29 -0
  56. package/src/plots/BarPlot.svelte +55 -0
  57. package/src/plots/BoxPlot.svelte +0 -0
  58. package/src/plots/FunnelPlot.svelte +33 -0
  59. package/src/plots/HeatMap.svelte +5 -0
  60. package/src/plots/HeatMapCalendar.svelte +129 -0
  61. package/src/plots/LinePlot.svelte +55 -0
  62. package/src/plots/Plot.svelte +25 -0
  63. package/src/plots/RankBarPlot.svelte +38 -0
  64. package/src/plots/ScatterPlot.svelte +20 -0
  65. package/src/plots/ViolinPlot.svelte +11 -0
  66. package/src/plots/heatmap.js +70 -0
  67. package/src/plots/index.js +10 -0
  68. package/src/swatch.js +11 -0
@@ -0,0 +1,204 @@
1
+ import { sum, median, max, mean, min, quantile, cumsum } from 'd3-array'
2
+ import { nest } from 'd3-collection'
3
+ import { pick, flatten } from 'ramda'
4
+ import { area, curveBasis, curveBumpX, curveBumpY } from 'd3-shape'
5
+ import { scaleLinear } from 'd3-scale'
6
+
7
+ const aggregate = {
8
+ count: (values) => values.length,
9
+ sum: (values) => sum(values),
10
+ min: (values) => min(values),
11
+ max: (values) => max(values),
12
+ mean: (values) => mean(values),
13
+ median: (values) => median(values),
14
+ q1: (values) => quantile(values, 0.25),
15
+ q3: (values) => quantile(values, 0.75)
16
+ }
17
+
18
+ export function summarize(data, by, attr, stat = 'count') {
19
+ const stats = Array.isArray(stat) ? stat : [stat]
20
+ const grouped = nest()
21
+ .key((d) => by.map((f) => d[f]).join('|'))
22
+ .rollup((rows) => {
23
+ let agg = pick(by, rows[0])
24
+ stats.map(
25
+ (stat) => (agg[stat] = aggregate[stat](rows.map((d) => d[attr])))
26
+ )
27
+ return [agg]
28
+ })
29
+ .entries(data)
30
+ return flatten(grouped.map((group) => group.value))
31
+ }
32
+
33
+ export function getUniques(input, aes) {
34
+ const attrs = ['x', 'y', 'fill']
35
+ let values = {}
36
+
37
+ attrs.map((attr) => {
38
+ if (attr in aes) {
39
+ values[attr] = [...new Set(input.map((d) => d[aes[attr]]))]
40
+ }
41
+ })
42
+ return values
43
+ }
44
+
45
+ export function fillMissing(fill, rows, key, aes) {
46
+ const filled = fill.map((f) => {
47
+ let matched = rows.filter((r) => r[aes.fill] === f)
48
+ if (matched.length == 0) {
49
+ let row = {}
50
+ row[key] = rows[0][key]
51
+ row[aes.fill] = f
52
+ row[aes.stat] = 0
53
+ return row
54
+ } else {
55
+ return matched[0]
56
+ }
57
+ })
58
+ return filled
59
+ }
60
+
61
+ export function convertToPhases(input, aes) {
62
+ const uniques = getUniques(input, aes)
63
+ let vertical = 'y' in uniques && uniques.y.some(isNaN)
64
+ const horizontal = 'x' in uniques && uniques.x.some(isNaN)
65
+
66
+ let summary = []
67
+
68
+ if (horizontal && vertical) {
69
+ if ((aes.stat || 'count') === 'count') {
70
+ vertical = false
71
+ console.warn('Assuming horizontal layout becuse stat is count')
72
+ } else {
73
+ console.error(
74
+ 'cannot plot without at least one axis having numeric values'
75
+ )
76
+ return { uniques, vertical }
77
+ }
78
+ }
79
+
80
+ const key = vertical ? aes.y : aes.x
81
+ const value = vertical ? aes.x : aes.y
82
+
83
+ let by = [key]
84
+ if ('fill' in aes) {
85
+ by.push(aes.fill)
86
+ }
87
+ summary = summarize(input, by, value, aes.stat)
88
+ const phases = nest()
89
+ .key((d) => d[key])
90
+ .rollup((rows) => {
91
+ return 'fill' in aes ? fillMissing(uniques.fill, rows, key, aes) : rows
92
+ })
93
+ .entries(summary)
94
+
95
+ return { phases, uniques, vertical }
96
+ }
97
+
98
+ export function mirror(input, aes) {
99
+ let domain = 0
100
+
101
+ const stats = input.phases.map((phase) => {
102
+ const stat = cumsum(phase.value.map((row) => row[aes.stat]))
103
+ const midpoint = max(stat) / 2
104
+ domain = Math.max(domain, midpoint)
105
+
106
+ const rows = phase.value.map((row, index) => {
107
+ if (input.vertical) {
108
+ return {
109
+ ...row,
110
+ y: input.uniques.y.indexOf(row[aes.y]),
111
+ x1: stat[index] - midpoint,
112
+ x0: stat[index] - midpoint - row[aes.stat]
113
+ }
114
+ } else {
115
+ return {
116
+ ...row,
117
+ x: input.uniques.x.indexOf(row[aes.x]),
118
+ y1: stat[index] - midpoint,
119
+ y0: stat[index] - midpoint - row[aes.stat]
120
+ }
121
+ }
122
+ })
123
+
124
+ phase.value = rows
125
+ return phase
126
+ })
127
+
128
+ return { ...input, stats, domain }
129
+ }
130
+
131
+ export function getScales(input, width, height) {
132
+ let scale
133
+ if (input.vertical) {
134
+ scale = {
135
+ x: scaleLinear()
136
+ .domain([-input.domain * 1.4, input.domain * 1.4])
137
+ .range([0, width]),
138
+ y: scaleLinear().domain([0, input.uniques.y.length]).range([0, height])
139
+ }
140
+ } else {
141
+ scale = {
142
+ x: scaleLinear().domain([0, input.uniques.x.length]).range([0, width]),
143
+ y: scaleLinear()
144
+ .domain([-input.domain, input.domain * 1.4])
145
+ .range([height - 20, 0])
146
+ }
147
+ }
148
+ return scale
149
+ }
150
+
151
+ function getLabels(data) {
152
+ let key = data.vertical ? 'y' : 'x'
153
+ let opp = key === 'x' ? 'y' : 'x'
154
+
155
+ let labels = data.uniques[key].map((label, index) => {
156
+ let row = { label }
157
+ let domain = data.scale[opp].domain()
158
+ row[`${key}1`] = row[`${key}2`] = data.scale[key](index + 1)
159
+ row[`${opp}1`] = data.scale[opp](domain[0])
160
+ row[`${opp}2`] = data.scale[opp](domain[1])
161
+ row[opp] = 20
162
+ row[key] = data.scale[key](index) + 20
163
+ return row
164
+ })
165
+ return labels
166
+ }
167
+
168
+ export function getPaths(vertical, scale, curve) {
169
+ return vertical
170
+ ? area()
171
+ .x0((d) => scale.x(d.x0))
172
+ .x1((d) => scale.x(d.x1))
173
+ .y((d) => scale.y(d.y))
174
+ .curve(curve)
175
+ : area()
176
+ .x((d) => scale.x(d.x))
177
+ .y0((d) => scale.y(d.y0))
178
+ .y1((d) => scale.y(d.y1))
179
+ .curve(curve)
180
+ }
181
+ export function funnel(input, aes, width, height) {
182
+ let data = convertToPhases(input, aes)
183
+ data = mirror(data, aes)
184
+ const curve =
185
+ aes.curve === 'basis' ? curveBasis : data.vertical ? curveBumpY : curveBumpX
186
+ // console.log(data)
187
+
188
+ if ('fill' in aes) {
189
+ let stats = flatten(data.stats.map((phase) => phase.value))
190
+ // console.log(stats)
191
+
192
+ data.stats = nest()
193
+ .key((d) => d[aes.fill])
194
+ .rollup((rows) => {
195
+ let last = data.vertical ? { y: rows.length } : { x: rows.length }
196
+ return [...rows, { ...rows[rows.length - 1], ...last }]
197
+ })
198
+ .entries(stats)
199
+ }
200
+ data.scale = getScales(data, width, height)
201
+ data.path = getPaths(data.vertical, data.scale, curve)
202
+ data.labels = getLabels(data)
203
+ return data
204
+ }
@@ -0,0 +1,19 @@
1
+ import { ChartBrewer } from './chart'
2
+ import { ColorBrewer } from './color'
3
+ import { ShapeBrewer } from './shape'
4
+ import { ThemeBrewer } from './theme'
5
+ import { PatternBrewer } from './pattern'
6
+
7
+ export { toHexString, swatch } from './utils'
8
+ export { uniques, slidingWindow } from './rollup'
9
+ export { colors } from './color'
10
+
11
+ export function brewer() {
12
+ return {
13
+ pattern: () => new PatternBrewer(),
14
+ color: () => new ColorBrewer(),
15
+ theme: () => new ThemeBrewer(),
16
+ shape: () => new ShapeBrewer(),
17
+ chart: (data, x, y) => new ChartBrewer(data, x, y)
18
+ }
19
+ }
@@ -0,0 +1,190 @@
1
+ import { clamp } from 'yootils'
2
+ import { ColorBrewer } from './color'
3
+ import { toHexString, uniqueId } from './utils'
4
+
5
+ export const builtIn = [
6
+ { path: 'M0 5A6 6 0 0 0 10 5', minAngle: 0, maxAngle: 90 },
7
+ { path: 'M0 10L10 0', minAngle: 0, maxAngle: 90 },
8
+ { path: 'M0 0A10 10 0 0 0 10 10', minAngle: 0, maxAngle: 90 },
9
+ { path: 'M0 0L10 10', minAngle: 0, maxAngle: 90 },
10
+ { path: 'M10 5A6 6 0 0 0 0 5', minAngle: 0, maxAngle: 90 },
11
+
12
+ { path: 'M10 10A10 10 0 0 0 0 0', minAngle: 0, maxAngle: 90 },
13
+ { path: 'M0 0L10 10ZM10 0L0 10Z', minAngle: 0, maxAngle: 90 },
14
+ { path: 'M1 1L9 1L9 9L1 9Z', minAngle: 0, maxAngle: 90 },
15
+ { path: 'M4 0L4 10M6 10L6 0M0 4L10 4M10 6L0 6', minAngle: 0, maxAngle: 90 },
16
+ { path: 'M0 2L8 10M2 0L10 8M0 8L8 0M2 10L10 2', minAngle: 0, maxAngle: 90 },
17
+
18
+ {
19
+ path: 'M5 1A 4 4 0 0 0 9 5A4 4 0 0 0 5 9A4 4 0 0 0 1 5A4 4 0 0 0 5 1',
20
+ minAngle: 0,
21
+ maxAngle: 45
22
+ },
23
+ { path: 'M1 3L7 9M3 1L9 7M1 7L7 1M3 9L9 3', minAngle: 0, maxAngle: 90 },
24
+ {
25
+ path: 'M2 2A4 4 0 0 0 8 2A4 4 0 0 0 8 8A4 4 0 0 0 2 8A4 4 0 0 0 2 2',
26
+ minAngle: 0,
27
+ maxAngle: 90
28
+ },
29
+ {
30
+ path: 'M0 0A5 5 0 0 0 10 0A5 5 0 0 0 10 10A5 5 0 0 0 0 10A5 5 0 0 0 0 0',
31
+ minAngle: 0,
32
+ maxAngle: 45
33
+ },
34
+ {
35
+ path: 'M5 2A 3 3 0 0 0 8 5A3 3 0 0 0 5 8A3 3 0 0 0 2 5A3 3 0 0 0 5 2',
36
+ minAngle: 0,
37
+ maxAngle: 90
38
+ },
39
+
40
+ { path: 'M2 5L5 2L8 5L5 8Z', minAngle: 0, maxAngle: 90 },
41
+ {
42
+ path: 'M3 5A2 2 0 0 0 7 5A2 2 0 0 0 3 5M1 5L9 5M5 1L5 9',
43
+ minAngle: 0,
44
+ maxAngle: 90
45
+ },
46
+ {
47
+ path: 'M2 8L8 2ZM1.5 3.5L3.5 1.5ZM6.5 8.5L8.5 6.5ZM0 0L10 10Z',
48
+ minAngle: 0,
49
+ maxAngle: 90
50
+ },
51
+
52
+ {
53
+ path:
54
+ 'M2 8L8 2ZM1.5 3.5L3.5 1.5Z' +
55
+ 'M6.5 8.5L8.5 6.5Z' +
56
+ 'M2 2L8 8M1.5 6.5L3.5 8.5' +
57
+ 'M6.5 1.5L8.5 3.5',
58
+ minAngle: 0,
59
+ maxAngle: 90
60
+ },
61
+
62
+ {
63
+ path:
64
+ 'M5 1 A6 6 0 0 0 5 9' +
65
+ 'A6 6 0 0 0 5 1' +
66
+ 'M1 5A6 6 0 0 0 9 5A6 6 0 0 0 1 5',
67
+ minAngle: 0,
68
+ maxAngle: 90
69
+ },
70
+ {
71
+ path:
72
+ 'M1.5 5A1 1 0 0 0 3.5 5A1 1 0 0 0 1.5 5' +
73
+ 'M6.5 5A1 1 0 0 0 8.5 5A1 1 0 0 0 6.5 5' +
74
+ 'M5 1.5A1 1 0 0 0 5 3.5A1 1 0 0 0 5 1.5' +
75
+ 'M5 6.5A1 1 0 0 0 5 8.5A1 1 0 0 0 5 6.5',
76
+ minAngle: 0,
77
+ maxAngle: 90
78
+ },
79
+ {
80
+ path:
81
+ 'M1.5 2.5A1 1 0 0 0 3.5 2.5A1 1 0 0 0 1.5 2.5' +
82
+ 'M6.5 2.5A1 1 0 0 0 8.5 2.5A1 1 0 0 0 6.5 2.5' +
83
+ 'M2.5 6.5A1 1 0 0 0 2.5 8.5A1 1 0 0 0 2.5 6.5' +
84
+ 'M7.5 6.5A1 1 0 0 0 7.5 8.5A1 1 0 0 0 7.5 6.5' +
85
+ 'M3.5 5A1 1 0 0 0 6.5 5A1 1 0 0 0 3.5 5',
86
+ minAngle: 0,
87
+ maxAngle: 90
88
+ },
89
+ {
90
+ path:
91
+ 'M5 0L6 4L10 5L6 6L5 10L4 6L0 5L4 4Z' + 'M2 1V3M1 2H3' + 'M8 9V7M9 8H7',
92
+ minAngle: 0,
93
+ maxAngle: 90
94
+ },
95
+ { path: 'M5 2L2.5 9L8.8 4.6L1.2 4.6L7.5 9Z', minAngle: 0, maxAngle: 90 },
96
+ {
97
+ path: 'M0 5A5 5 0 0 0 5 0' + 'M5 10A5 5 0 0 0 0 5' + 'M5 10A5 5 0 0 0 5 0',
98
+ minAngle: 0,
99
+ maxAngle: 90
100
+ },
101
+ {
102
+ path: 'M0 0L10 10M5 0L10 5M0 5 L5 10',
103
+ minAngle: 0,
104
+ maxAngle: 90
105
+ },
106
+ {
107
+ path: 'M0 0L10 10M3 0L10 7M0 7 L3 10',
108
+ minAngle: 0,
109
+ maxAngle: 90
110
+ }
111
+ ]
112
+
113
+ export class PatternBrewer {
114
+ constructor() {
115
+ this.paths = builtIn
116
+ this.shades = []
117
+ this.gray = new ColorBrewer().gray()
118
+ this.bw = false
119
+ this.repeat = false
120
+ this.indices = [...this.paths.keys()]
121
+ }
122
+
123
+ clear() {
124
+ this.paths = []
125
+ }
126
+
127
+ add(path) {
128
+ let paths = Array.isArray(path) ? path : [path]
129
+ this.paths = [...this.paths, ...paths]
130
+
131
+ return this
132
+ }
133
+
134
+ filter(indices) {
135
+ indices = Array.isArray(indices) ? indices : [indices]
136
+ this.indices = indices.filter((i) => i >= 0 && i < this.paths.length)
137
+ return this
138
+ }
139
+
140
+ colors(shades, repeat = false) {
141
+ this.shades = Array.isArray(shades) ? shades : [shades]
142
+ this.repeat = repeat
143
+ return this
144
+ }
145
+
146
+ variants(count) {
147
+ count = clamp(count, 1, 15)
148
+ this.paths = this.paths.map((path) => ({
149
+ ...path,
150
+ angles: [...Array(count).keys()].map(
151
+ (i) => (i * (path.maxAngle - path.minAngle)) / count
152
+ )
153
+ }))
154
+ return this
155
+ }
156
+
157
+ brew() {
158
+ // const hexPrefix = toHexString(prefix)
159
+ const hexPrefix = uniqueId()
160
+
161
+ let patterns = []
162
+
163
+ // apply filter and add angle variations
164
+ this.indices
165
+ .map((i) => this.paths[i])
166
+ .map((path) => {
167
+ if ('angles' in path) {
168
+ path.angles.map((angle) => patterns.push({ ...path, angle }))
169
+ } else {
170
+ patterns.push(path)
171
+ }
172
+ })
173
+
174
+ patterns = patterns
175
+ // Add reference ids
176
+ .map((path, index) => ({
177
+ id: hexPrefix + '-' + toHexString(index),
178
+ ...path,
179
+ fillUrl: `url(#${hexPrefix}-${toHexString(index)})`
180
+ }))
181
+ // Add colors
182
+ .map((pattern, i) => {
183
+ return i < this.shades.length || this.repeat
184
+ ? { ...pattern, ...this.shades[i % this.shades.length] }
185
+ : { ...pattern, ...this.gray }
186
+ })
187
+
188
+ return patterns
189
+ }
190
+ }
@@ -0,0 +1,55 @@
1
+ import { min, max, add, format } from 'date-fns'
2
+
3
+ export function uniques(data, attr) {
4
+ return data && attr ? [...new Set(data.map((item) => item[attr]))] : []
5
+ }
6
+
7
+ export function slidingWindow(values, size, step, offset, fmt) {
8
+ return size && typeof size === 'object'
9
+ ? slidingWindowForDates(values, size, step, offset, fmt)
10
+ : slidingWindowForNumbers(values, size, step, offset, fmt)
11
+ }
12
+
13
+ function slidingWindowForNumbers(values, size, step, offset = 0, fmt = 0) {
14
+ const smallest = Number(Math.min(...values).toFixed(fmt))
15
+ let largest = Math.max(...values)
16
+ let count = Math.ceil((largest - smallest + offset) / step)
17
+
18
+ if (smallest + count * step + offset == largest) {
19
+ count += 1
20
+ }
21
+ // console.log('count:', count)
22
+ const range = [...Array(count).keys()]
23
+
24
+ const result = range.map((key) => ({
25
+ key: smallest + key * step,
26
+ lowerBound: smallest + key * step + offset,
27
+ upperBound: smallest + key * step + offset + size
28
+ }))
29
+ return result
30
+ }
31
+
32
+ function slidingWindowForDates(
33
+ values,
34
+ size,
35
+ step,
36
+ offset = {},
37
+ fmt = 'yyyy-MM-dd'
38
+ ) {
39
+ const largest = max(values)
40
+ let current = new Date(format(min(values), fmt))
41
+ let blocks = []
42
+ let lowerBound = current
43
+
44
+ while (lowerBound <= largest) {
45
+ lowerBound = add(current, offset)
46
+
47
+ blocks.push({
48
+ key: current,
49
+ lowerBound,
50
+ upperBound: add(lowerBound, size)
51
+ })
52
+ current = add(current, step)
53
+ }
54
+ return blocks
55
+ }
@@ -0,0 +1,199 @@
1
+ import { ColorBrewer } from './color'
2
+
3
+ export const namedShapes = {
4
+ square: (s) =>
5
+ `M${0.1 * s} 0` +
6
+ `A${0.1 * s} ${0.1 * s} 0 0 0 0 ${0.1 * s}V${0.9 * s}` +
7
+ `A${0.1 * s} ${0.1 * s} 0 0 0 ${0.1 * s} ${s}H${0.9 * s}` +
8
+ `A${0.1 * s} ${0.1 * s} 0 0 0 ${s} ${0.9 * s}V${0.1 * s}` +
9
+ `A${0.1 * s} ${0.1 * s} 0 0 0 ${0.9 * s} 0Z`,
10
+ circle: (s) =>
11
+ `M0 ${0.5 * s}` +
12
+ `A${0.5 * s} ${0.5 * s} 0 0 0 ${s} ${0.5 * s}` +
13
+ `A${0.5 * s} ${0.5 * s} 0 0 0 0 ${0.5 * s}`,
14
+ diamond: (s) =>
15
+ `M${0.5 * s} 0` +
16
+ `A${0.6 * s} ${0.6 * s} 0 0 0 ${s} ${0.5 * s}` +
17
+ `A${0.6 * s} ${0.6 * s} 0 0 0 ${0.5 * s} ${s}` +
18
+ `A${0.6 * s} ${0.6 * s} 0 0 0 0 ${0.5 * s}` +
19
+ `A${0.6 * s} ${0.6 * s} 0 0 0 ${0.5 * s} 0`,
20
+ triangle: (s) =>
21
+ `M${0.5 * s} ${0.0866 * s}L0 ${0.9234 * s}L${s} ${0.9234 * s}Z`,
22
+ rhombus: (s) =>
23
+ `M${0.5 * s} 0` +
24
+ `L${s} ${0.5 * s}` +
25
+ `L${0.5 * s} ${s}` +
26
+ `L0 ${0.5 * s}Z`,
27
+ star: (s) =>
28
+ `M${0.5 * s} ${0.05 * s}` +
29
+ `L${0.606 * s} ${0.36 * s}` +
30
+ `L${s} ${0.36 * s}` +
31
+ `L${0.685 * s} ${0.59 * s}` +
32
+ `L${0.81 * s} ${0.95 * s}` +
33
+ `L${0.5 * s} ${0.725 * s}` +
34
+ `L${0.19 * s} ${0.95 * s}` +
35
+ `L${0.315 * s} ${0.59 * s}` +
36
+ `L0 ${0.36 * s}` +
37
+ `L${0.394 * s} ${0.36 * s}Z`,
38
+ heart: (s) =>
39
+ `M${0.9 * s} ${0.5 * s}` +
40
+ `A${0.08 * s} ${0.08 * s} 0 0 0 ${0.5 * s} ${0.2 * s}` +
41
+ `A${0.08 * s} ${0.08 * s} 0 0 0 ${0.1 * s} ${0.5 * s}` +
42
+ `L${0.5 * s} ${0.9 * s}` +
43
+ `L${0.9 * s} ${0.5 * s}`,
44
+ shurikan: (s) =>
45
+ `M${0.3 * s} ${0.1 * s}L${0.5 * s} 0L${0.7 * s} ${0.1 * s}` +
46
+ `A ${0.05 * s} ${0.05 * s} 0 0 0 ${0.9 * s} ${0.35 * s}` +
47
+ `L${s} ${0.5 * s}L${0.9 * s} ${0.7 * s}` +
48
+ `A ${0.05 * s} ${0.05 * s} 0 0 0 ${0.7 * s} ${0.9 * s}` +
49
+ `L${0.5 * s} ${s}L${0.3 * s} ${0.9 * s}` +
50
+ `A${0.05 * s} ${0.05 * s} 0 0 0 ${0.1 * s} ${0.7 * s}` +
51
+ `L0 ${0.5 * s}L${0.1 * s} ${0.3 * s}` +
52
+ `A${0.05 * s} ${0.05 * s} 0 0 0 ${0.3 * s} ${0.1 * s}` +
53
+ `M${0.4 * s} ${0.5 * s}` +
54
+ `A${0.1 * s} ${0.1 * s} 0 0 0 ${0.6 * s} ${0.5 * s}` +
55
+ `A${0.1 * s} ${0.1 * s} 0 0 0 ${0.4 * s} ${0.5 * s}`,
56
+ target: (s) =>
57
+ `M${0.2 * s} ${0.5 * s}` +
58
+ `A${0.3 * s} ${0.3 * s} 0 0 0 ${0.8 * s} ${0.5 * s}` +
59
+ `A${0.3 * s} ${0.3 * s} 0 0 0 ${0.2 * s} ${0.5 * s}` +
60
+ `M0 ${0.5 * s}` +
61
+ `L${s} ${0.5 * s}` +
62
+ `M${0.5 * s} 0` +
63
+ `L${0.5 * s} ${s}`
64
+ }
65
+
66
+ export const builtIn = [
67
+ ...Object.keys(namedShapes).map((key) => ({ shape: namedShapes[key] })),
68
+ {
69
+ shape: (s) =>
70
+ `M${0.1 * s} ${0.1 * s}` +
71
+ `A${0.5 * s} ${0.5 * s} 0 0 0 ${0.9 * s} ${0.1 * s}` +
72
+ `A${0.5 * s} ${0.5 * s} 0 0 0 ${0.9 * s} ${0.9 * s}` +
73
+ `A${0.5 * s} ${0.5 * s} 0 0 0 ${0.1 * s} ${0.9 * s}` +
74
+ `A${0.5 * s} ${0.5 * s} 0 0 0 ${0.1 * s} ${0.1 * s}`
75
+ },
76
+ {
77
+ shape: (s) =>
78
+ `M${0.5 * s} ${0.3 * s}` +
79
+ `A${0.2 * s} ${0.1 * s} 0 0 0 ${0.5 * s} ${0.1 * s}` +
80
+ `L${0.5 * s} ${0.9 * s}` +
81
+ `M${0.5 * s} ${0.7 * s}` +
82
+ `A${0.2 * s} ${0.1 * s} 0 0 0 ${0.5 * s} ${0.9 * s}` +
83
+ `M${0.3 * s} ${0.5 * s}` +
84
+ `A${0.1 * s} ${0.2 * s} 0 0 0 ${0.1 * s} ${0.5 * s}` +
85
+ `L${0.9 * s} ${0.5 * s}` +
86
+ `M${0.7 * s} ${0.5 * s}` +
87
+ `A${0.1 * s} ${0.2 * s} 0 0 0 ${0.9 * s} ${0.5 * s}`
88
+ },
89
+ {
90
+ shape: (s) =>
91
+ `M${0.1 * s} ${0.3 * s}` +
92
+ `L${0.7 * s} ${0.9 * s}` +
93
+ `L${0.9 * s} ${0.7 * s}` +
94
+ `L${0.3 * s} ${0.1 * s}Z` +
95
+ `M${0.1 * s} ${0.7 * s}` +
96
+ `L${0.7 * s} ${0.1 * s}` +
97
+ `L${0.9 * s} ${0.3 * s}` +
98
+ `L${0.3 * s} ${0.9 * s}Z`
99
+ },
100
+ {
101
+ shape: (s) =>
102
+ `M${0.1 * s} ${0.4 * s}` +
103
+ `L${0.9 * s} ${0.4 * s}` +
104
+ `L${0.9 * s} ${0.6 * s}` +
105
+ `L${0.1 * s} ${0.6 * s}Z` +
106
+ `M${0.4 * s} ${0.1 * s}` +
107
+ `L${0.4 * s} ${0.9 * s}` +
108
+ `L${0.6 * s} ${0.9 * s}` +
109
+ `L${0.6 * s} ${0.1 * s}Z`
110
+ },
111
+ {
112
+ shape: (s) =>
113
+ `M${0.5 * s} ${0.05 * s}` +
114
+ `L${0.19 * s} ${0.95 * s}` +
115
+ `L${s} ${0.36 * s}` +
116
+ `L0 ${0.36 * s}` +
117
+ `L${0.81 * s} ${0.95 * s}Z`
118
+ },
119
+ {
120
+ shape: (s) =>
121
+ `M${0.1 * s} ${0.1 * s}` +
122
+ `L${0.1 * s} ${0.9 * s}` +
123
+ `L${0.9 * s} ${0.9 * s}` +
124
+ `L${0.9 * s} ${0.1 * s}Z` +
125
+ `M${0.1 * s} ${0.1 * s}` +
126
+ `L${0.1 * s} ${0.5 * s}` +
127
+ `L${0.5 * s} ${0.5 * s}` +
128
+ `L${0.5 * s} ${0.1 * s}Z` +
129
+ `M${0.5 * s} ${0.5 * s}` +
130
+ `L${0.5 * s} ${0.9 * s}` +
131
+ `L${0.9 * s} ${0.9 * s}` +
132
+ `L${0.9 * s} ${0.5 * s}Z`
133
+ },
134
+ {
135
+ shape: (s) =>
136
+ `M${0.5 * s} 0` +
137
+ `L${s} ${0.5 * s}` +
138
+ `L${0.5 * s} ${s}` +
139
+ `L0 ${0.5 * s}Z` +
140
+ `M${0.5 * s} 0` +
141
+ `L${s} ${0.5 * s}` +
142
+ `L${0.5 * s} ${s}Z`
143
+ },
144
+ {
145
+ shape: (s) =>
146
+ `M0 ${0.5 * s}` +
147
+ `A${0.6 * s} ${0.4 * s} 0 0 0 ${s} ${0.5 * s}` +
148
+ `A${0.6 * s} ${0.4 * s} 0 0 0 0 ${0.5 * s}` +
149
+ `M${0.5 * s} 0` +
150
+ `A${0.4 * s} ${0.6 * s} 0 0 0 ${0.5 * s} ${s}` +
151
+ `A${0.4 * s} ${0.6 * s} 0 0 0 ${0.5 * s} 0`
152
+ }
153
+ ]
154
+
155
+ export class ShapeBrewer {
156
+ constructor() {
157
+ this.shapes = builtIn
158
+ this.repeat = false
159
+ this.indices = [...this.shapes.keys()]
160
+ this.gray = new ColorBrewer().gray()
161
+ this.shades = []
162
+ this.repeat = false
163
+ }
164
+
165
+ clear() {
166
+ this.shapes = []
167
+ return this
168
+ }
169
+
170
+ add(shape) {
171
+ const shapes = Array.isArray(shape) ? shape : [shape]
172
+ this.shapes = [...shapes]
173
+
174
+ return this
175
+ }
176
+
177
+ filter(indices) {
178
+ indices = Array.isArray(indices) ? indices : [indices]
179
+ this.indices = indices.filter((i) => i > 0 && i < this.shapes.length)
180
+
181
+ return this
182
+ }
183
+
184
+ colors(shades, repeat = false) {
185
+ this.shades = Array.isArray(shades) ? shades : [shades]
186
+ this.repeat = repeat
187
+ return this
188
+ }
189
+
190
+ brew() {
191
+ return this.indices
192
+ .map((i) => this.shapes[i])
193
+ .map((shape, i) => {
194
+ return i < this.shades.length || this.repeat
195
+ ? { ...shape, ...this.shades[i % this.shades.length] }
196
+ : { ...shape, ...this.gray }
197
+ })
198
+ }
199
+ }