Skip to content

Instantly share code, notes, and snippets.

@mikesurowiec
Created October 7, 2017 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikesurowiec/38338fa1a7d503fdd511730d2cace64b to your computer and use it in GitHub Desktop.
Save mikesurowiec/38338fa1a7d503fdd511730d2cace64b to your computer and use it in GitHub Desktop.
// The React.Component from before.
// This is just highlighting the differences.
import * as d3 from 'd3';
class DCMetroMap extends React.Component {
componentDidMount() {
// For every group inside of #Stations...
d3.selectAll('#Stations g')
// Append a circle element...
.append('circle')
// style, position, and size the circle
.style('fill', 'rgba(0,0,0,0)')
.style('pointer-events', 'all')
.attr('r', getRadius)
.attr('cy', getY)
.attr('cx', getX)
// handle events on the circle!
.on('click', handleClick);
// When a circle is clicked, we change it's color
function handleClick() {
// Un-highlight previously selected station
d3.selectAll('#Stations g circle').style('fill', 'rgba(0,0,0,0)');
// Here's that semantic information we hand coded into the svg file!
const stationId = this.parentNode.id;
// Highlight this station
// `this` context is the `circle` we appended above
d3.select(this).style('fill', 'rgba(250,0,0,0.7)');
// We pass off the stationId so our parent component can load the data about
// metro arrival times. SWISH
this.props.onClick(stationId);
}
}
// ... render(), etc
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment