/* D3 experimentation...
*
* This demo draws a simple bar chart from left to right, using DOM and CSS.
*/
// The numbers we want to chart...totally meaningless
var dataset = [{
name: "Java",
value: 50
}, {
name: "C++",
value: 22
},{
name: "Python",
value: 98
},{
name: "JavaScript",
value: 77
},{
name: "Ruby",
value: 50
},{
name: "Erlang",
value: 20
},{
name: "PHP",
value: 123
},{
name: "C",
value: 100
},{
name: "SQL",
value: 78
},{
name: "VB",
value: 18
},{
name: "C#",
value: 108
},{
name: "ML",
value: 38
},{
name: "Lisp",
value: 56
},{
name: "Fortran",
value: 79
},{
name: "COBOL",
value: 31
}],
maxOutputRange = 500, // the highest number permitted to be returned by our scale function
barChart,
scale = d3.scale.linear()
.domain([0, d3.max(dataset, function(e) {
return e.value;
})]) // input "domain" is range of possible input values
.range([0, maxOutputRange]); // output "range" is range of possible output values.
// Make our bar chart as wide as the widest possible bar given our scale function
barChart = d3.select(".simpleBarChart");
barChart.style("width", maxOutputRange+"px");
function createBars() {
// this code creates an empty div for each entry in the data set
// these will be the bars in the chart.
barChart.selectAll("div")
.data(dataset)
.enter()
.append("div")
.attr("class", "bar") // with a class of bar...
.attr("title", function(d) {
return d.name + ": "+ d.value;
})
.transition().duration(1000) // animate the width
.style("width", function(d) {
// the width of the bar is the datset value
// scaled by our function.
return scale(d.value)+ "px";
})
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.