Skip to content

Instantly share code, notes, and snippets.

@f94f
Created August 29, 2017 20:30
Show Gist options
  • Save f94f/60bd376bbca1dc848c95e5cdfa50584f to your computer and use it in GitHub Desktop.
Save f94f/60bd376bbca1dc848c95e5cdfa50584f to your computer and use it in GitHub Desktop.
fresh block
license: mit
<!DOCTYPE html>
<html lang="es">
<head>
<title>Barchart to download</title>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v3.min.js"></script>
<style type="text/css">
.bar-pe {
fill: #0075ea;
}
.bar-to {
fill: #008e2d;
}
.bar-pa {
fill: #ffa500;
}
.bar-re {
fill: #FF0000;
}
/*tooltip*/
.show {
display: block;
}
.hidden {
display: none;
}
.tooltip {
background-color: #333;
padding: 4px 8px;
position: absolute;
color: #fff;
opacity: 0.8;
z-index: 100;
}
.title {
font: bold 14px "Helvetica Neue", Helvetica, Arial, sans-serif;
}
.axis {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
stroke-dasharray: 2px 2px;
}
.x.axis path {
display: none;
}
</style>
</head>
<body>
<div>
<button id='saveButton'>Export my D3 visualization to PNG</button>
<a href="#" id="download">Download</a>
</div>
</body>
<script type="text/javascript">
// Let's create a mock visualization
var puntos = [
{name: "Pendientes", value: 0.40},
{name: "Totales", value: 0.0667},
{name: "Parciales", value: 0.0667},
{name: "Rechazados", value: 0.0}
];
var margin = {top: 80, right: 180, bottom: 80, left: 180},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], 0.1, 0.3);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(8, "%");
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
//EL GRÁFICO
x.domain(puntos.map(function(d) { return d.name; }));
y.domain([0, d3.max(puntos, function(d) { return d.value; })]);
svg.append("text")
.attr("class", "title")
.attr("x", (150), (puntos[0].name))
.attr("y", -30)
.text("Porcentaje de las entregas según su estado");
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll(".tick text")
.call(wrap, x.rangeBand());
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
svg.selectAll(".bar")
.data(puntos)
.enter().append("rect")
.attr("class", function(d) {
if(d.name == "Pendientes") { return "bar-pe";}
else if(d.name == "Totales") { return "bar-to";}
else if(d.name == "Parciales") { return "bar-pa";}
else if(d.name == "Rechazados") { return "bar-re";}
})
.attr("x", function(d) { return x(d.name); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.value); })
.attr("height", function(d) { return height - y(d.value); })
function wrap(text, width) {
text.each(function() {
var text = d3.select(this),
words = text.text().split(/\s+/).reverse(),
word,
line = [],
lineNumber = 0,
lineHeight = 1.1, // ems
y = text.attr("y"),
dy = parseFloat(text.attr("dy")),
tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em");
while (word = words.pop()) {
line.push(word);
tspan.text(line.join(" "));
if (tspan.node().getComputedTextLength() > width) {
line.pop();
tspan.text(line.join(" "));
line = [word];
tspan = text.append("tspan").attr("x", 0).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word);
}
}
});
}
function type(d) {
d.value = +d.value;
return d;
}
d3.select("#download")
.on("mouseover", writeDownloadLink);
function writeDownloadLink(){
var html = d3.select("svg")
.attr("title", "svg_title")
.attr("version", 1.1)
.attr("xmlns", "http://www.w3.org/2000/svg")
.node().parentNode.innerHTML;
d3.select(this)
.attr("href-lang", "image/svg+xml")
.attr("href", "data:image/svg+xml;base64,\n" + btoa(html))
.on("mousedown", function(){
if(event.button != 2){
d3.select(this)
.attr("href", null)
.html("Use right click");
}
})
.on("mouseout", function(){
d3.select(this)
.html("Download");
});
};
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment