Skip to content

Instantly share code, notes, and snippets.

@basilesimon
Created January 18, 2018 15:12
Show Gist options
  • Save basilesimon/cbe512b643b72d613267c77087ec07cb to your computer and use it in GitHub Desktop.
Save basilesimon/cbe512b643b72d613267c77087ec07cb to your computer and use it in GitHub Desktop.
// config
var config = {
// width, height, etc.
};
function loadAndPrepareData(filter) {
d3.csv('data.csv', function(error, data) {
if (error) throw error;
// empty store
var massagedData = [];
// if we pass in a filter, do the highlighting
// if not, i.e. on initial render, set all highlights to false
if (filter) {
// this iterates over each data point
// I can send you the longhand version of this ternary, but I saw you using it
// it works this way:
// var foo = conditionalCheck ? what to do if true : what to do if false
// so it's strictly equal to:
// if (conditionalCheck === true) { something } else { somehting else}
massagedData = data.map(function(d) {
return { d: d, highlight: d.team === filter ? true : false };
});
} else {
massagedData = data.map(function(d) {
return { d: d, highlight: false };
});
}
// massagedData is now an array of objects containing:
// {
// d: your data,
// highlight: true or false
// }
// @TODO uncomment when you're ready
// var stratifiedData = d3.stratify()
// etc. do the rest
// drawViz(stratifiedData);
});
}
// this takes in our data now all ready for us
function drawViz(data) {
// clean up before we draw
d3.select('#chart').html = '';
// create d3 layout and hierarchy
var vSlices = canvas
.data(vNodes)
.enter()
.append('g');
vSlices
.append('circle')
// etc
// if that's a highlight, full opacity
// otherwise, 50%
.attr('opacity', function(d) {
return d.highlight ? 1 : 0.5;
});
// any visual highlighting you want to do, you do this way:
// .attr('something', function(d) {
// return d.highlight ? if true : if false;
//})
}
function handleClickSearch(event) {
// prevents the page from reloading
event.preventDefault();
// stores whatever has been searched
var currentSearchTerm = document
.getElementById('mySearchTerm')
.value.toLowerCase();
loadAndPrepareData(currentSearchTerm);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment