I create a chart, include an event listener that updates a variable used by the chart and repaints the chart. The problem is that when the chart is updated with a separate variable elsewhere, the updated value from the listener is lost and reverts to the original.
percentileTheory = {
const svg = d3.create(“svg”)
.attr(“viewBox”, [0, 0, width, height]);
svg.on(“mousedown”, (event) => {
meanDiff = zScale.invert(d3.pointer(event)[0]);
mouseDragging = true;
paint();
})
function paint() {
//modifies the svg using meanDiff
}
return svg.node();
}
if I define meanDiff inside the chart
let meanDiff = 0.8;
it works fine for the initial paint and updates correctly for the dragging. However, when an input option changes a value (not related to meanDiff), the chart reverts to the original value of meanDiff. All attempts to define meanDiff outside the scope of the chart produces an error when I try to assign a new value to meanDiff inside the mouse down function. How do I save the new value of meanDiff?