From 59ed3fe62e905485c71f03893536281519ec1c84 Mon Sep 17 00:00:00 2001 From: netil Date: Fri, 1 Dec 2023 17:31:56 +0900 Subject: [PATCH] refactor(radar): Delegate event binding Event binding done unefficiently and called whenever redraws happens. Make event binding done in delegation mode. --- src/ChartInternal/interactions/interaction.ts | 3 +- src/ChartInternal/shape/radar.ts | 69 +++++++++---------- 2 files changed, 35 insertions(+), 37 deletions(-) diff --git a/src/ChartInternal/interactions/interaction.ts b/src/ChartInternal/interactions/interaction.ts index 53a546464..36ec825de 100644 --- a/src/ChartInternal/interactions/interaction.ts +++ b/src/ChartInternal/interactions/interaction.ts @@ -207,7 +207,8 @@ export default { screenX: x, screenY: y, clientX: x, - clientY: y + clientY: y, + bubbles: hasRadar // radar type needs to bubble up event }; emulateEvent[/^(mouse|click)/.test(type) ? "mouse" : "touch"]( diff --git a/src/ChartInternal/shape/radar.ts b/src/ChartInternal/shape/radar.ts index cb0383b00..e1d4f818e 100644 --- a/src/ChartInternal/shape/radar.ts +++ b/src/ChartInternal/shape/radar.ts @@ -51,6 +51,8 @@ export default { .attr("class", $SHAPE.shapes); current.dataMax = config.radar_axis_max || $$.getMinMaxData().max[0].value; + + config.interaction_enabled && config.radar_axis_text_show && $$.bindRadarEvent(); } }, @@ -289,59 +291,54 @@ export default { return `translate(${posX} ${posY})`; }); } - - $$.bindRadarEvent(); }, bindRadarEvent(): void { const $$ = this; - const {config, state, $el: {radar, svg}} = $$; + const {state, $el: {radar, svg}} = $$; const focusOnly = $$.isPointFocusOnly(); const {inputType, transiting} = state; + const isMouse = inputType === "mouse"; - if (config.interaction_enabled) { - const isMouse = inputType === "mouse"; + const hide = event => { + state.event = event; - const hide = event => { - state.event = event; + // const index = getIndex(event); - // const index = getIndex(event); + const index = $$.getDataIndexFromEvent(event); + const noIndex = isUndefined(index); - const index = $$.getDataIndexFromEvent(event); - const noIndex = isUndefined(index); + if (isMouse || noIndex) { + $$.hideTooltip(); - if (isMouse || noIndex) { - $$.hideTooltip(); + focusOnly ? + $$.hideCircleFocus() : + $$.unexpandCircles(); - focusOnly ? - $$.hideCircleFocus() : - $$.unexpandCircles(); - - if (isMouse) { - $$.setOverOut(false, index); - } else if (noIndex) { - $$.callOverOutForTouch(); - } + if (isMouse) { + $$.setOverOut(false, index); + } else if (noIndex) { + $$.callOverOutForTouch(); } - }; + } + }; - radar.axes.selectAll("text") - .on(isMouse ? "mouseover " : "touchstart", event => { - if (transiting) { // skip while transiting - return; - } + radar.axes + .on(isMouse ? "mouseover " : "touchstart", event => { + if (transiting) { // skip while transiting + return; + } - state.event = event; - const index = $$.getDataIndexFromEvent(event); + state.event = event; + const index = $$.getDataIndexFromEvent(event); - $$.selectRectForSingle(svg.node(), index); - isMouse ? $$.setOverOut(true, index) : $$.callOverOutForTouch(index); - }) - .on("mouseout", isMouse ? hide : null); + $$.selectRectForSingle(svg.node(), index); + isMouse ? $$.setOverOut(true, index) : $$.callOverOutForTouch(index); + }) + .on("mouseout", isMouse ? hide : null); - if (!isMouse) { - svg.on("touchstart", hide); - } + if (!isMouse) { + svg.on("touchstart", hide); } },