-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathindex.js
More file actions
228 lines (206 loc) · 8.53 KB
/
Copy pathindex.js
File metadata and controls
228 lines (206 loc) · 8.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// @ts-check
import h from 'virtual-dom/h.js'
// Type definitions for a Point and Points to improve clarity and reusability in the code
/**
* @typedef {[number, number]} Point
* Represents a point in 2D space as a tuple of two numbers [x, y].
*/
/**
* @typedef {Point[]} Points
* Represents an array of Points, i.e., a path or a shape in 2D space.
*/
// Data type definition for use with radar chart columns
/**
* @template {Exclude<string, 'class'>} T
* @typedef {Record<T, string | number> & { class: string }} Data<T>
* Represents a record with keys of type T, and values of type string or number, with a special 'class' key that must be a string.
*/
// Column type definition with generics, excluding 'class' from keys
/**
* @template {Exclude<string, 'class'>} T
* @typedef {Object} Column<T>
* @property {T} key - The unique key for identifying each column.
* @property {string} caption - The caption that will be displayed on each axis.
* @property {number} angle - The angle at which the axis is displayed.
*/
// Options type definition including methods for different visual components
/**
* @template {Exclude<string, 'class'>} T
* @typedef {Object} Options
* @property {number} size - The overall size of the radar chart.
* @property {number} scales - The number of concentric circles (scales) to show.
* @property {boolean} axes - Whether to display the axes.
* @property {boolean} captions - Whether to display the captions.
* @property {number} captionsPosition - The radial position of the captions.
* @property {(points: Points) => string} smoothing - Function to smooth the path between points.
* @property {(col: Column<T>) => { className: string } & import('react').SVGProps<SVGSVGElement>} axisProps - Function to define properties for axis elements.
* @property {(scale: number) => { className: string } & import('react').SVGProps<SVGSVGElement>} scaleProps - Function to define properties for scale elements.
* @property {(col: Data<T>) => { className: string } & import('react').SVGProps<SVGSVGElement>} shapeProps - Function to define properties for shape elements.
* @property {(col: Column<T>) => { className: string } & import('react').SVGProps<SVGSVGElement>} captionProps - Function to define properties for caption elements.
*/
// Extends basic options with additional chart size property
/**
* @template {Exclude<string, 'class'>} T
* @typedef {Options<T> & { chartSize: number }} ExtendedOptions
*/
// Converts polar coordinates to Cartesian X coordinate
/**
* @param {number} angle - The angle in radians.
* @param {number} distance - The distance from the origin.
* @returns {number} The X coordinate.
*/
const polarToX = (angle, distance) => Math.cos(angle - Math.PI / 2) * distance
// Converts polar coordinates to Cartesian Y coordinate
/**
* @param {number} angle - The angle in radians.
* @param {number} distance - The distance from the origin.
* @returns {number} The Y coordinate.
*/
const polarToY = (angle, distance) => Math.sin(angle - Math.PI / 2) * distance
// Converts an array of Points into a space-separated string for SVG paths
/**
* @param {Points} points - An array of Points.
* @returns {string} A string representing the 'points' attribute in SVG.
*/
const points = (points) => {
return points
.map(point => point[0].toFixed(4) + ',' + point[1].toFixed(4))
.join(' ')
}
// Creates an SVG path data string with no smoothing (just straight lines)
/**
* @param {Points} points - An array of Points to connect with lines.
* @returns {string} An SVG path data string.
*/
const noSmoothing = (points) => {
let d = 'M' + points[0][0].toFixed(4) + ',' + points[0][1].toFixed(4)
for (let i = 1; i < points.length; i++) {
d += 'L' + points[i][0].toFixed(4) + ',' + points[i][1].toFixed(4)
}
return d + 'z'
}
// Returns a function that generates SVG polyline elements for each axis
/**
* @template {Exclude<string, 'class'>} T
* @param {ExtendedOptions<T>} opt - The options for rendering the radar chart.
* @returns {(col: Column<T>) => string} A function that returns SVG polyline elements for each axis.
*/
const axis = (opt) => (col) => {
return h('polyline', Object.assign(opt.axisProps(col), {
points: points([
[0, 0],
[
polarToX(col.angle, opt.chartSize / 2),
polarToY(col.angle, opt.chartSize / 2)
]
])
}))
}
// Returns a function that generates SVG path elements for each data shape
/**
* @template {Exclude<string, 'class'>} T
* @param {Column<T>[]} columns - The columns defining the axes of the radar chart.
* @param {ExtendedOptions<T>} opt - The options for rendering the radar chart.
* @returns {(data: Data<T>, i: number) => Point} A function that returns SVG path elements for each data shape.
*/
const shape = (columns, opt) => (data, i) => {
return h('path', Object.assign(opt.shapeProps(data), {
d: opt.smoothing(columns.map((col) => {
const val = data[col.key]
if (typeof val !== 'number') {
throw new Error(`Data set ${i} is invalid.`)
}
return [
polarToX(col.angle, val * opt.chartSize / 2),
polarToY(col.angle, val * opt.chartSize / 2)
]
}))
}))
}
// Returns an SVG circle element for each scale
/**
* @template {Exclude<string, 'class'>} T
* @param {ExtendedOptions<T>} opt - The options for rendering the radar chart.
* @param {number} value - The value at which the scale is placed.
* @returns {string} An SVG circle element for the scale.
*/
const scale = (opt, value) => {
return h('circle', Object.assign(opt.scaleProps(value), {
cx: 0, cy: 0, r: value * opt.chartSize / 2
}))
}
// Returns a function that generates SVG text elements for each caption
/**
* @template {Exclude<string, 'class'>} T
* @param {ExtendedOptions<T>} opt - The options for rendering the radar chart.
* @returns {(col: Column<T>) => string} A function that returns SVG text elements for each caption.
*/
const caption = (opt) => (col) => {
return h('text', Object.assign(opt.captionProps(col), {
x: polarToX(col.angle, opt.size / 2 * 0.95).toFixed(4),
y: polarToY(col.angle, opt.size / 2 * 0.95).toFixed(4),
dy: (parseInt(opt.captionProps(col).fontSize + '') || 2) / 2
}), col.caption)
}
// Default configuration options for the radar chart
const defaults = /** @type {Options<string>} */ ({
size: 100, // size of the chart (including captions)
axes: true, // whether to show axes
scales: 3, // number of concentric scales to show
captions: true, // whether to show captions
captionsPosition: 1.2, // radial position of captions
smoothing: noSmoothing, // default smoothing function
axisProps: () => ({className: 'axis'}), // default axis properties
scaleProps: () => ({className: 'scale', fill: 'none'}), // default scale properties
shapeProps: () => ({className: 'shape'}), // default shape properties
captionProps: () => ({ // default caption properties
className: 'caption',
textAnchor: 'middle', fontSize: 3,
fontFamily: 'sans-serif'
})
})
// Main function to render the radar chart with given columns and data
/**
* @template {Exclude<string, 'class'>} T
* @param {Record<Exclude<T, 'class'>, string>} columnsData - The data for the columns.
* @param {Data<T>[]} data - The array of data to be plotted.
* @param {Partial<Options<T>>} opt - Additional options to override the defaults.
* @returns {string} The rendered radar chart as an SVG element.
*/
const renderRadarChart = (columnsData, data, opt = {}) => {
if (typeof columnsData !== 'object' || Array.isArray(columnsData)) {
throw new Error('columns must be an object')
}
if (!Array.isArray(data)) {
throw new Error('data must be an array')
}
if (data.some(data => Object.keys(data).some(key => key !== 'class' && typeof data[key] !== 'number'))) {
throw new Error('data must contain set of numbers')
}
const options = /** @type {ExtendedOptions<T>} */({...defaults, ...opt, chartSize: 0})
options.chartSize = options.size / options.captionsPosition
const columns = /** @type {Column<T>[]} */ (Object.keys(columnsData).map((key, i, all) => ({
key, caption: columnsData[key],
angle: Math.PI * 2 * i / all.length
})))
const groups = [
h('g', data.map(shape(columns, options)))
]
if (options.captions) groups.push(h('g', columns.map(caption(options))))
if (options.axes) groups.unshift(h('g', columns.map(axis(options))))
if (options.scales > 0) {
const scales = []
for (let i = options.scales; i > 0; i--) {
scales.push(scale(options, i / options.scales))
}
groups.unshift(h('g', scales))
}
const delta = (options.size / 2).toFixed(4)
return h('g', {
transform: `translate(${delta},${delta})`
}, groups)
}
// Export the radar chart rendering function
export {
renderRadarChart as radar,
}