|
function topBar() { |
|
// Nested Single Bars and Lines |
|
var settings = { |
|
chartContainerClass: ".g-chart-container-top-bar", |
|
dataPointFocus: "Jazz", |
|
tickCount : "8", |
|
transSpeed: 700 |
|
}; |
|
|
|
var optionsArray = [ |
|
"Jazz", |
|
"Rocksteady", |
|
"Tropicalia" |
|
] |
|
var chartContainer = d3.select(settings.chartContainerClass).html(""); |
|
|
|
var margin = { |
|
top:0, |
|
left:100, |
|
right:120, |
|
bottom:80 |
|
}; |
|
|
|
var w = chartContainer.node().clientWidth - margin.left - margin.right, |
|
h = 600 - margin.top - margin.bottom; |
|
|
|
d3.queue() |
|
.defer(d3.csv, "block-data.csv") |
|
.await(ready); |
|
|
|
// user control |
|
var viewByUI = d3.select("div.view-by-ui").selectAll("button"); |
|
value = "By Risk"; |
|
viewByUI.on("click", function(){ |
|
var value = d3.select(this).html(); |
|
}); |
|
if (value == "By Risk") { |
|
|
|
} else if (value == "By Country") { |
|
console.log("hey hey"); |
|
} |
|
|
|
function ready(error, topriskdata) { |
|
if (error) throw "error: not loading data, bro"; |
|
|
|
// type of chart |
|
var headers = d3.keys(topriskdata[0]); |
|
|
|
topriskdata.forEach(function(d){ |
|
d["Jazz"] = +d.JAZZ; |
|
d["Rocksteady"] = +d.ROCKSTEADY; |
|
d["Tropicalia"] = +d.TROPICALIA; |
|
}); |
|
|
|
var optWrap = chartContainer.append("div.btn-wrap"); |
|
var select = optWrap // data join for buttons |
|
.append('select') |
|
.attr('class','select') |
|
.on('change',onchange); |
|
|
|
var options = select |
|
.selectAll('option') |
|
.data(optionsArray).enter() |
|
.append('option') |
|
.text(function (d) { return d; }); |
|
|
|
function onchange() { |
|
selectValue = d3.select('select').property('value'); |
|
updateChart(selectValue); |
|
}; |
|
|
|
topriskdata.sort(function(a,b){ |
|
return d3.descending(+a[settings.dataPointFocus], +b[settings.dataPointFocus]) |
|
}); |
|
|
|
var topFive = topriskdata.filter(function(d, i) { |
|
return i == 0 || i == 1 || i == 2 || i == 3 || i == 4; |
|
}); |
|
|
|
var topFiveWrap = chartContainer.append("div.top-five-wrap"); |
|
|
|
topFiveWrap.selectAll("p") |
|
.data(topFive) |
|
.enter() |
|
.append("p") |
|
.html(function(d){ |
|
return d.Country; |
|
}); |
|
|
|
// scales |
|
var yscale = d3.scaleBand() |
|
.rangeRound([0,h]) |
|
.domain(topriskdata.map(function(d) { return d.Country; })) |
|
.paddingInner(0.15); |
|
|
|
var yaxis = d3.axisLeft(yscale); |
|
|
|
var xscale = d3.scaleLinear() |
|
.domain([0, 30]) |
|
.range([margin.left,w]); |
|
|
|
var xaxis = d3.axisBottom() |
|
.scale(xscale); |
|
|
|
// svg |
|
var svg = chartContainer |
|
.append("svg") |
|
.style("width", (w + margin.left + margin.right) + "px") |
|
.style("height", (h + margin.top + margin.bottom) + "px") |
|
.append("g.wrap") |
|
.translate([margin.left,margin.top]); |
|
|
|
svg.insert("canvas"); |
|
|
|
svg.append("g.x-axis") |
|
.call(xaxis) |
|
.attr("transform","translate(0," + h + ")"); |
|
|
|
var gRects = svg.selectAll(".g-comp-rect") |
|
.data(topriskdata) |
|
.enter() |
|
.append("g.g-comp-rect"); |
|
|
|
gRects |
|
.append("rect.comp-rect") |
|
.attr("y",function(d){ |
|
return yscale(d.Country); |
|
}) |
|
.attr("x",margin.left) |
|
.attr("height",yscale.bandwidth()) |
|
.attr("width", function(d){ |
|
return xscale(d[settings.dataPointFocus]); |
|
}) |
|
.on("mouseover", function(){ |
|
d3.select(this).style("fill", "#ff4747"); |
|
}) |
|
.on("mouseout", function(){ |
|
d3.select(this).style("fill", "#45555f"); |
|
}); |
|
|
|
svg.append("g.y-axis").call(yaxis).attr("transform","translate(" + margin.left + ",0)"); |
|
|
|
gRects.append("text.bar-label") |
|
.attr("y", function(d) { return yscale(d.Country) + yscale.bandwidth()/2; }) |
|
.attr("x", function(d) { return (xscale(d[settings.dataPointFocus]) + 15 + margin.left); }) |
|
.attr("dy", "4px") |
|
.text(function(d) { return d[settings.dataPointFocus]; }); |
|
|
|
function updateChart(updatedData) { |
|
|
|
topriskdata.sort(function(a,b){ |
|
return d3.descending(+a[updatedData], +b[updatedData]) |
|
}); |
|
|
|
|
|
var uptopFive = topriskdata.filter(function(d, i){ |
|
return i == 0 || i == 1 || i == 2 || i == 3 || i == 4; |
|
}); |
|
|
|
topFiveWrap.selectAll("p") |
|
.data(uptopFive) |
|
.html(function(d){ |
|
return d.Country; |
|
}); |
|
|
|
yscale |
|
.domain(topriskdata.map(function(d) { return d.Country; })) |
|
|
|
yaxis.scale(yscale); |
|
|
|
svg.select(".y-axis") |
|
.transition().duration(settings.transSpeed) |
|
.call(yaxis); |
|
|
|
var upgRects = svg.selectAll(".g-comp-rect") |
|
.data(topriskdata); |
|
|
|
upgRects |
|
.selectAll("rect.comp-rect") |
|
.transition().duration(settings.transSpeed) |
|
.attr("y",function(d){ |
|
return yscale(d.Country); |
|
}) |
|
.attr("x",margin.left) |
|
.attr("height",yscale.bandwidth()) |
|
.attr("width", function(d){ |
|
return xscale(d[updatedData]); |
|
}); |
|
|
|
upgRects |
|
.selectAll("text.bar-label") |
|
.transition().duration(settings.transSpeed) |
|
.attr("y", function(d) { return yscale(d.Country) + yscale.bandwidth()/2; }) |
|
.attr("x", function(d) { return (xscale(d[updatedData]) + 15 + margin.left); }) |
|
.attr("dy", "4px") |
|
.text(function(d) { return d[updatedData]; }); |
|
} |
|
// update end |
|
} |
|
} |
|
|
|
$( window ).resize(fireAll); |
|
function fireAll() {topBar();} |
|
;(function(){topBar();})(); |