Skip to content

Instantly share code, notes, and snippets.

@tsyber1an
Created March 8, 2013 08:43
Show Gist options
  • Save tsyber1an/5115049 to your computer and use it in GitHub Desktop.
Save tsyber1an/5115049 to your computer and use it in GitHub Desktop.
var drawCharWithInfo = function(base, dataset){
$(base+" > .total.counters > .ways span").html(dataset.length);
$(base+" > .total.counters > .searches span").html(get_count_by(dataset, 'x'));
drawBarChart(base+" > .ways", dataset);
};
var drawBarChart = function(container, dataset){
var max_in_dataset = d3.max(dataset.map(function(d){ return d.x }));
var w = $('.content-main').width();
var h = dataset.length*35 + 20;
var barPadding = 5;
var xPadding = 257; // Fixed, bad style :)
var svg = d3.select(container)
.append('svg')
.attr('width', w)
.attr('height', h);
svg.selectAll('rect')
.data(dataset.map(function(d){ return d.x }))
.enter()
.append('rect')
.attr('x', xPadding)
.attr('y', function(d, i){
return i*(30 + barPadding);
})
.attr('width', function(d){
if(d == max_in_dataset){
return w - 30;
}
else {
return (w * d)/ max_in_dataset;
}
})
.attr('height', 30)
.attr('fill', function(d){
return "rgb(59,130,192)"
});
svg.selectAll("text")
.data(dataset.map(function(d){ return d.x }))
.enter()
.append('text')
.text(function(d){
return d;
})
.attr('x', function(d, i){
return 247; // Fixed, bad style :)
/*
* Text info right on bars
*
*/
/*
if(d == max_in_dataset){
return w - 45;
}
else {
return (w * d)/ max_in_dataset + 5 + xPadding;
}
*/
})
.attr('y', function(d, i){
return i*(30 + barPadding) + 20;
})
.attr("font-family", "sans-serif")
.attr("font-size", "14px")
.attr("font-weight", "300")
.attr("fill", "black")
.attr("text-anchor", "end")
svg.selectAll("labels")
.data(dataset.map(function(d){ return d.y }))
.enter()
.append('text')
.text(function(d){
return d;
})
.attr('x', 0)
.attr('y', function(d, i){
return i*(30 + barPadding) + 20;
})
.attr("font-family", "sans-serif")
.attr("font-size", "14px")
.attr("font-weight", "300")
.attr("fill", "black")
return;
};
var get_count_by = function(set, field){
var i = 0, count = 0, n = set.length;
for(i=0; i<n; i++){
count += set[i][field];
}
return count;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment