Skip to content

Instantly share code, notes, and snippets.

@asm-jaime
Created November 22, 2017 11:38
Show Gist options
  • Save asm-jaime/29f99711e68feb314fe53c62dd1b98e4 to your computer and use it in GitHub Desktop.
Save asm-jaime/29f99711e68feb314fe53c62dd1b98e4 to your computer and use it in GitHub Desktop.
d3+redux example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.7.2/redux.min.js"></script>
</head>
<body>
<script>
const initialState = {
items: [
{name: 'anon', score: 0},
{name: 'kek', score: 100},
{name: 'realtek', score: 100},
{name: 'uluzek', score: 150},
{name: 'vek', score: 120},
{name: 'rek', score: 10},
]
};
function resolver(state = initialState, action = null){{{{
switch(action.type){
case 'ADD':
const items = state.items;
items.push(action.payload);
state = { ...state, items };
return state;
default:
return state;
}
}}}}
const store = Redux.createStore(resolver);
store.subscribe(() => {
console.info('we in store');
const state = store.getState();
show_items(state.items);
});
function show_items(items){
console.info('items: ', items);
d3.select('body')
.selectAll('div')
.data(items)
.enter()
.insert('div')
.attr('width', (d) => d.score)
.attr('height', (d) => d.score)
.text((d) => d.name)
}
store.dispatch({type: 'ADD', payload: {name: 'noone', score: 60}});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment