Skip to content

Instantly share code, notes, and snippets.

View limscoder's full-sized avatar

Dave Thompson limscoder

  • workday
  • Boulder, CO
View GitHub Profile
@limscoder
limscoder / ImmutableComponent.js
Created October 14, 2016 20:43
Playing with ways to express immutable react elements with flowtype
type FunctionComponent<P, S> = (props: P, context: S) => ?React$Element<any>;
type ClassComponent<D, P, S> = Class<React$Component<D, P, S>>;
type ReactComponent<D, P, S> = FunctionComponent<P, S> | ClassComponent<D, P, S>;
function ImmutableComponent<D, P, S, T: ReactComponent<D, P, S>>({ component: Component, componentProps }: {
component: T,
componentProps: P
}) {
return <Component { ...componentProps }/>;
}
@limscoder
limscoder / select.js
Last active November 16, 2017 18:46
d3 current selection
// html:
// <ul>
// <li></li>
// <li></li>
// </ul>
const users = [{
id: 1,
name: 'Nancy',
role: 'person'
@limscoder
limscoder / properties.js
Last active November 19, 2017 17:01
DOM properties from data
selection.attr('text', d => d.name)
.style('color', d => (d.role === 'human' ? 'purple' : 'yellow'));
@limscoder
limscoder / key.js
Last active November 19, 2017 17:42
D3 key function
const renderedUsers = d3.selectAll('li.app-user')
.data(users, d => d.id);
@limscoder
limscoder / enter.js
Last active November 19, 2017 17:39
D3 enter selection
// html:
// <ul>
// <li></li>
// <li></li>
// </ul>
const users = [{
id: 1,
name: 'Nancy',
role: 'person'
@limscoder
limscoder / exit.js
Last active November 19, 2017 17:41
D3 exit selection
const exitUsers = d3.select('ul.app-user-list')
.selectAll('li.app-user')
.data(users)
.exit();
// remove stale elements from the document
exitUsers.remove();
@limscoder
limscoder / transition.js
Created November 16, 2017 19:08
D3 animations
const userList = d3.select('ul.app-user-list');
const userSelection = userList.selectAll('li.app-user')
.data(users);
const updateUsers = userSelection;
const enterUsers = userSelection.enter();
const exitUsers = userSelection.exit();
// blend from old color to new color
updateUsers.transition()
@limscoder
limscoder / domSelect.js
Created November 19, 2017 16:49
D3 DOM selections
// change the color of all .app-user elements
d3.select('.app-user-list')
.selectAll('.app-user')
.style('color', 'green');
@limscoder
limscoder / dataSelection.js
Last active November 19, 2017 17:04
D3 data selection
// html:
// <ul>
// <li></li>
// <li></li>
// </ul>
const users = [{
id: 1,
name: 'Nancy',
role: 'person'
@limscoder
limscoder / hexagons.js
Last active November 20, 2017 18:40
Draw a grid of hexagons
// defines a path for a single hexagon.
const h = Math.sqrt(3) / 2;
const r = 30;
const x = 0;
const y = 0;
const hexPath = d3.line()
.x(d => d.x)
.y(d => d.y)
.curve(d3.curveCardinalClosed.tension('0.8'))([
{ x: r + x, y },