Dispatching Events
Two linked views of U.S. census data show population by age group for a selected state: a bar chart displays total population, while a pie chart breaks down the seven age cohorts. Using d3.dispatch, custom "load" and "statechange" events coordinate a dropdown menu, bar chart, and pie chart through named listeners like "load.bar" and "statechange.pie." The pie chart animates slice transitions with transition.attrTween and d3.interpolate, while the bar chart animates its height via a rect transition. Data loads from a CSV file, rendered as SVG with d3.axis.
AI-generated descriptionThis example demonstrates how to use d3.dispatch to coordinate views with shared state. Here, a bar chart and a pie chart show two views of the same data: population by age group for a given state. (For brevity, a legend is omitted.) This example uses two custom events: a load event when data is available, and a statechange event when the displayed state is changed.
Custom events allow loose coupling of components: views can listen for events and update the DOM accordingly, without needing to explicitly tie each view together. When the drop-down menu changes, a statechange event is triggered which causes any interested listeners to be notified.
Each view uses a unique name, such as "bar" or "pie", so that multiple listeners can be notified for a single event. (D3 requires listeners to be named, rather than anonymous, so that it’s easier to remove or inspect registered listeners.) Thus, the "load.menu", "load.bar" and "load.pie" listeners are all notified when the data is loaded.