Skip to content

Instantly share code, notes, and snippets.

@ColadaFF
Forked from anonymous/index.html
Created November 23, 2016 23:09
Show Gist options
  • Save ColadaFF/abfcc31ef8840c4419b17557e18d7ade to your computer and use it in GitHub Desktop.
Save ColadaFF/abfcc31ef8840c4419b17557e18d7ade to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/fawohujebo
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style id="jsbin-css">
.toolTip {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
position: absolute;
display: none;
width: auto;
height: auto;
background: none repeat scroll 0 0 white;
border: 0 none;
border-radius: 8px 8px 8px 8px;
box-shadow: -3px 3px 15px #888888;
color: black;
font: 12px sans-serif;
padding: 5px;
text-align: center;
}
text {
font: 10px sans-serif;
color: white;
}
text.value {
font-size: 120%;
fill: white;
}
.bar {
fill: lightskyblue;
}
</style>
</head>
<body>
<script src="https://fb.me/react-with-addons-15.1.0.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.2/lodash.min.js"></script>
<svg width="300" height="500"></svg>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"></script>
<script id="jsbin-javascript">
"use strict";
var data = {
"No": 530,
"Si": 419
};
var div = d3.select("body").append("div").attr("class", "toolTip");
function addLabelX(svg, text) {
svg.append("text") // text label for the x axis
.attr("x", 1000).attr("y", 2000).style("text-anchor", "middle").text(text);
}
function addLabelY(svg, text) {
svg.append("text").attr("transform", "rotate(-90)").attr("y", 0 - margin.left).attr("x", 0 - height / 2).attr("dy", "1em").style("text-anchor", "middle").text(text);
}
var ordered = _.chain(data).toPairs().sortBy(function (pair) {
return pair[1];
}).value();
var domain = ordered.map(function (item) {
return item[1];
});
var range = ordered.map(function (item) {
return item[0];
});
var scale = d3.scaleQuantize().domain(domain).range(range);
var svg = d3.select("svg"),
margin = { top: 20, right: 20, bottom: 30, left: 60 },
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom;
var x = d3.scaleBand().rangeRound([0, width]).padding(0.1).domain(ordered.map(function (d) {
return d[0];
}));
var y = d3.scaleLinear().rangeRound([height, 0]).domain([0, d3.max(ordered, function (d) {
return d[1];
})]);
var g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
g.append("g").attr("class", "axis axis--x").attr("transform", "translate(0," + height + ")").call(d3.axisBottom(x));
var yAxis = g.append("g").attr("class", "axis axis--y").call(d3.axisLeft(y).ticks(5)).append("text").attr("transform", "rotate(-90)").attr("y", 6).attr("dy", "0.71em").attr("text-anchor", "end").text("Frequency");
// text label for the x axis
g.append("text").attr("transform", "translate(" + width / 2 + " ," + (height + margin.top + 5) + ")").style("text-anchor", "middle").text("Opciones");
var bars = g.selectAll(".bar").data(ordered).enter().append("rect").attr("class", "bar").attr("x", function (d) {
return x(d[0]);
}).attr("y", function (d) {
return y(d[1]);
}).attr("width", x.bandwidth()).attr("height", function (d) {
return height - y(d[1]);
});
bars.on('mouseover', function (d) {
var label = d[0];
var value = d[1];
div.style("left", d3.event.pageX + 10 + "px");
div.style("top", d3.event.pageY - 25 + "px");
div.style("display", "inline-block");
div.html(label + "<br>" + value);
});
bars.on('mouseout', function (d) {
div.style("display", "none");
});
// text label for the y axis
g.append("text").attr("transform", "rotate(-90)").attr("y", 0 - margin.left).attr("x", 0 - height / 2).attr("dy", "1em").style("text-anchor", "middle").text("Cantidad");
</script>
<script id="jsbin-source-css" type="text/css"> .toolTip {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
position: absolute;
display: none;
width: auto;
height: auto;
background: none repeat scroll 0 0 white;
border: 0 none;
border-radius: 8px 8px 8px 8px;
box-shadow: -3px 3px 15px #888888;
color: black;
font: 12px sans-serif;
padding: 5px;
text-align: center;
}
text {
font: 10px sans-serif;
color: white;
}
text.value {
font-size: 120%;
fill: white;
}
.bar {
fill: lightskyblue;
}</script>
<script id="jsbin-source-javascript" type="text/javascript">const data = {
"No":530,
"Si":419
};
var div = d3.select("body").append("div").attr("class", "toolTip");
function addLabelX(svg, text){
svg.append("text") // text label for the x axis
.attr("x", 1000 )
.attr("y", 2000 )
.style("text-anchor", "middle")
.text(text);
}
function addLabelY(svg, text){
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0 - margin.left)
.attr("x",0 - (height / 2))
.attr("dy", "1em")
.style("text-anchor", "middle")
.text(text);
}
const ordered = _.chain(data)
.toPairs()
.sortBy(pair => pair[1])
.value();
const domain = ordered.map(item => item[1]);
const range = ordered.map(item => item[0]);
const scale = d3.scaleQuantize().domain(domain).range(range);
var svg = d3.select("svg"),
margin = {top: 20, right: 20, bottom: 30, left: 60},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom;
var x = d3.scaleBand()
.rangeRound([0, width])
.padding(0.1)
.domain(ordered.map(function(d) { return d[0]; }));
const y = d3.scaleLinear()
.rangeRound([height, 0])
.domain([0, d3.max(ordered, function(d) { return d[1]; })])
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x))
const yAxis = g.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y).ticks(5))
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("text-anchor", "end")
.text("Frequency");
// text label for the x axis
g.append("text")
.attr("transform",
"translate(" + (width/2) + " ," +
(height + margin.top + 5) + ")")
.style("text-anchor", "middle")
.text("Opciones");
const bars = g.selectAll(".bar")
.data(ordered)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d[0]); })
.attr("y", function(d) { return y(d[1]); })
.attr("width", x.bandwidth())
.attr("height", function(d) { return height - y(d[1]); });
bars.on('mouseover', function(d){
const label = d[0];
const value = d[1];
div.style("left", d3.event.pageX+10+"px");
div.style("top", d3.event.pageY-25+"px");
div.style("display", "inline-block");
div.html((label)+"<br>"+(value));
});
bars.on('mouseout', function(d){
div.style("display", "none");
});
// text label for the y axis
g.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0 - margin.left)
.attr("x",0 - (height / 2))
.attr("dy", "1em")
.style("text-anchor", "middle")
.text("Cantidad"); </script></body>
</html>
.toolTip {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
position: absolute;
display: none;
width: auto;
height: auto;
background: none repeat scroll 0 0 white;
border: 0 none;
border-radius: 8px 8px 8px 8px;
box-shadow: -3px 3px 15px #888888;
color: black;
font: 12px sans-serif;
padding: 5px;
text-align: center;
}
text {
font: 10px sans-serif;
color: white;
}
text.value {
font-size: 120%;
fill: white;
}
.bar {
fill: lightskyblue;
}
"use strict";
var data = {
"No": 530,
"Si": 419
};
var div = d3.select("body").append("div").attr("class", "toolTip");
function addLabelX(svg, text) {
svg.append("text") // text label for the x axis
.attr("x", 1000).attr("y", 2000).style("text-anchor", "middle").text(text);
}
function addLabelY(svg, text) {
svg.append("text").attr("transform", "rotate(-90)").attr("y", 0 - margin.left).attr("x", 0 - height / 2).attr("dy", "1em").style("text-anchor", "middle").text(text);
}
var ordered = _.chain(data).toPairs().sortBy(function (pair) {
return pair[1];
}).value();
var domain = ordered.map(function (item) {
return item[1];
});
var range = ordered.map(function (item) {
return item[0];
});
var scale = d3.scaleQuantize().domain(domain).range(range);
var svg = d3.select("svg"),
margin = { top: 20, right: 20, bottom: 30, left: 60 },
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom;
var x = d3.scaleBand().rangeRound([0, width]).padding(0.1).domain(ordered.map(function (d) {
return d[0];
}));
var y = d3.scaleLinear().rangeRound([height, 0]).domain([0, d3.max(ordered, function (d) {
return d[1];
})]);
var g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
g.append("g").attr("class", "axis axis--x").attr("transform", "translate(0," + height + ")").call(d3.axisBottom(x));
var yAxis = g.append("g").attr("class", "axis axis--y").call(d3.axisLeft(y).ticks(5)).append("text").attr("transform", "rotate(-90)").attr("y", 6).attr("dy", "0.71em").attr("text-anchor", "end").text("Frequency");
// text label for the x axis
g.append("text").attr("transform", "translate(" + width / 2 + " ," + (height + margin.top + 5) + ")").style("text-anchor", "middle").text("Opciones");
var bars = g.selectAll(".bar").data(ordered).enter().append("rect").attr("class", "bar").attr("x", function (d) {
return x(d[0]);
}).attr("y", function (d) {
return y(d[1]);
}).attr("width", x.bandwidth()).attr("height", function (d) {
return height - y(d[1]);
});
bars.on('mouseover', function (d) {
var label = d[0];
var value = d[1];
div.style("left", d3.event.pageX + 10 + "px");
div.style("top", d3.event.pageY - 25 + "px");
div.style("display", "inline-block");
div.html(label + "<br>" + value);
});
bars.on('mouseout', function (d) {
div.style("display", "none");
});
// text label for the y axis
g.append("text").attr("transform", "rotate(-90)").attr("y", 0 - margin.left).attr("x", 0 - height / 2).attr("dy", "1em").style("text-anchor", "middle").text("Cantidad");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment