Skip to content

Instantly share code, notes, and snippets.

@f94f
Last active August 29, 2017 23:16
Show Gist options
  • Save f94f/9927af35ba1ef86d146fbc52c0ec27a6 to your computer and use it in GitHub Desktop.
Save f94f/9927af35ba1ef86d146fbc52c0ec27a6 to your computer and use it in GitHub Desktop.
nuevo intento
license: mit
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.bar-pe {
fill: #0075ea;
}
.bar-to {
fill: #008e2d;
}
.bar-pa {
fill: #ffa500;
}
.bar-re {
fill: #FF0000;
}
.bar-pe:hover {
fill: #56aafe;
}
.bar-to:hover {
fill: #00BC3C;
}
.bar-pa:hover {
fill: #ffbb3d;
}
.bar-re:hover {
fill: #ff1e1e;
}
.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>
<body>
<a href="#" id="download">Download</a>
<div id="chart"></div>
<script src="//cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"></script>
<script src="//bl.ocks.org/shimizu/raw/0b526eab82263c8443108c33e454d221/nChart.js"></script>
<script src="createDownloader.js"></script>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
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("#chart").append("svg:svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
/*.style("background-color", "WhiteSmoke")*/
.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("fill", function(d) {
if(d.name == "Pendientes") { return "#0075ea";}
else if(d.name == "Totales") { return "#008e2d";}
else if(d.name == "Parciales") { return "#ffa500";}
else if(d.name == "Rechazados") { return "#FF0000";}
})
.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>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment