diff --git a/src/core/config/index.js b/src/core/config/index.js index dd25d98..ca58a57 100644 --- a/src/core/config/index.js +++ b/src/core/config/index.js @@ -72,6 +72,8 @@ export const DEFAULT_PROPS = { // Accessibility related props svgAriaLabel: 'React d3 speedometer', // aria-label of speedometer + + onSegmentClick: () => {}, // Default no-op function } // default config @@ -150,6 +152,8 @@ export const getConfig = ({ PROPS, parentWidth, parentHeight }) => { // Accessibility related props svgAriaLabel: PROPS.svgAriaLabel, // aria-label of speedometer + + onSegmentClick: PROPS.onSegmentClick, } return Object.assign({}, DEFAULT_CONFIG, config) diff --git a/src/core/render/index.js b/src/core/render/index.js index 4947e51..20d9cc2 100644 --- a/src/core/render/index.js +++ b/src/core/render/index.js @@ -105,6 +105,30 @@ function _renderArcs({ config, svg, centerTx }) { return config.arcColorFn(d * i) }) .attr('d', arc) + .attr('data-label', (d, i) => { + return config.customSegmentLabels[i]?.text || 'Default Label'; + }) + .attr('data-sstop', (d, i) => { + const startStop = config.customSegmentStops[i]; + const endStop = config.customSegmentStops[i + 1]; + + // Check if startStop or endStop are undefined, null, or not numbers + if (typeof startStop !== 'number' || startStop === null || isNaN(startStop) || + typeof endStop !== 'number' || endStop === null || isNaN(endStop)) { + // Provide a default value or skip setting the attribute + return 'unknown'; + } else { + // Both startStop and endStop are valid numbers + return `${startStop}-${endStop}`; + } + }) + .on('click', function (event, d) { + const label = event.target.getAttribute('data-label'); + const value = event.target.getAttribute('data-sstop'); + if (config.onSegmentClick) { + config.onSegmentClick(label,value); + } + }); } export function _renderLabels({ config, svg, centerTx, r }) { diff --git a/src/stories/ReactSpeedometer.stories.jsx b/src/stories/ReactSpeedometer.stories.jsx index 22080c9..c2e6e79 100644 --- a/src/stories/ReactSpeedometer.stories.jsx +++ b/src/stories/ReactSpeedometer.stories.jsx @@ -38,83 +38,93 @@ export const ConfiguringValues = () => ( /> ) -export const CustomSegmentLabels = () => ( -