Skip to content

Commit

Permalink
Fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharcoux committed May 30, 2024
1 parent 0534eab commit f839755
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"args": "none",
"ignoreRestSiblings": false
}
]
],
"semi": ["error", "never"]
}
}
76 changes: 39 additions & 37 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ import h from 'virtual-dom/h.js'
* @param {number} distance - The distance from the origin.
* @returns {number} The X coordinate.
*/
const polarToX = (angle, distance) => Math.cos(angle - Math.PI / 2) * distance;
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;
const polarToY = (angle, distance) => Math.sin(angle - Math.PI / 2) * distance

// Converts an array of Points into a space-separated string for SVG paths
/**
Expand All @@ -75,21 +75,21 @@ const polarToY = (angle, distance) => Math.sin(angle - Math.PI / 2) * distance;
const points = (points) => {
return points
.map(point => point[0].toFixed(4) + ',' + point[1].toFixed(4))
.join(' ');
};
.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);
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);
d += 'L' + points[i][0].toFixed(4) + ',' + points[i][1].toFixed(4)
}
return d + 'z';
};
return d + 'z'
}

// Returns a function that generates SVG polyline elements for each axis
/**
Expand All @@ -102,10 +102,10 @@ const axis = (opt) => (col) => {
points: points([
[0, 0],
[polarToX(col.angle, opt.chartSize / 2),
polarToY(col.angle, opt.chartSize / 2)]
polarToY(col.angle, opt.chartSize / 2)]
])
}));
};
}))
}

// Returns a function that generates SVG path elements for each data shape
/**
Expand All @@ -117,18 +117,18 @@ const axis = (opt) => (col) => {
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];
const val = data[col.key]
if (typeof val !== 'number') {
throw new Error(`Data set ${i} is invalid.`);
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
/**
Expand All @@ -140,8 +140,8 @@ const shape = (columns, opt) => (data, i) => {
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
/**
Expand All @@ -154,8 +154,8 @@ const caption = (opt) => (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);
};
}), col.caption)
}

// Default configuration options for the radar chart
const defaults = /** @type {Options<string>} */ ({
Expand All @@ -173,7 +173,7 @@ const defaults = /** @type {Options<string>} */ ({
textAnchor: 'middle', fontSize: 3,
fontFamily: 'sans-serif'
})
});
})

// Main function to render the radar chart with given columns and data
/**
Expand All @@ -185,42 +185,44 @@ const defaults = /** @type {Options<string>} */ ({
*/
const renderRadarChart = (columnsData, data, opt = {}) => {
if (typeof columnsData !== 'object' || Array.isArray(columnsData)) {
throw new Error('columns must be an object');
throw new Error('columns must be an object')
}
if (!Array.isArray(data)) {
throw new Error('data must be an array');
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');
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 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.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 = [];
const scales = []
for (let i = options.scales; i > 0; i--) {
scales.push(scale(options, i / options.scales));
scales.push(scale(options, i / options.scales))
}
groups.unshift(h('g', scales));
groups.unshift(h('g', scales))
}

const delta = (options.size / 2).toFixed(4);
const delta = (options.size / 2).toFixed(4)
return h('g', {
transform: `translate(${delta},${delta})`
}, groups);
};
}, groups)
}

// Export the radar chart rendering function
export {
renderRadarChart as radar,
};
}

export * from './smoothing.js'

0 comments on commit f839755

Please sign in to comment.