Fra Informations dataspurt.
xxxxxxxxxx
<meta charset="utf-8">
<style>
svg {
display: block;
margin-left: auto;
margin-right: auto;
}
.bar.positive {
fill: steelblue;
}
.bar.negative {
fill: brown;
}
rect.bar:hover {
fill: orange;
}
.axis text {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.d3-tip {
line-height: 1;
font-weight: bold;
padding: 12px;
background: rgba(0, 0, 0, 0.8);
color: #fff;
border-radius: 2px;
}
/* Creates a small triangle extender for the tooltip */
.d3-tip:after {
box-sizing: border-box;
display: inline;
font-size: 10px;
width: 100%;
line-height: 1;
color: rgba(0, 0, 0, 0.8);
content: "\25BC";
position: absolute;
text-align: center;
}
/* Style northward tooltips differently */
.d3-tip.n:after {
margin: -1px 0 0 0;
top: 100%;
left: 0;
}
</style>
<body>
<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="https://labratrevenge.com/d3-tip/javascripts/d3.tip.min.js"></script>
<script charset="utf-8">
// Config
var beforeValue = "2013",
afterValue = "2014";
var displayValue = "percent", // "percent", "absolute"
showLabels = true, // true, false
filterValue = 10; // 0-100
var margin = {top: 30, right: 10, bottom: 10, left: 10};
if (showLabels === true) { margin.left = 225; }
if (displayValue !== "percent") { margin.right = 35; }
width = 720 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width])
var y = d3.scale.ordinal()
.rangeRoundBands([0, height], .2);
var xAxis = d3.svg.axis()
.scale(x)
.tickFormat(function(d) {
return (displayValue === "percent") ? d+"%" : d;
})
.orient("top");
if (showLabels === true) {
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
}
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d) {
return "<strong>" + d.name + ":</strong> <span style='color:red'>" + Math.round(d.value) + ((displayValue === "percent") ? "%" : "") + "</span>";
})
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 + ")");
svg.call(tip);
d3.csv("data.csv", type, function(error, data) {
x.domain(d3.extent(data, function(d) { return d.value; })).nice();
y.domain(data.filter(function(d) { return d.value > filterValue || d.value < -filterValue; }).map(function(d) { return d.name; }));
svg.selectAll(".bar")
.data(data.filter(function(d) { return d.value > filterValue || d.value < -filterValue; }))
.enter().append("rect")
.attr("class", function(d) { return d.value < 0 ? "bar negative" : "bar positive"; })
.attr("x", function(d) { return x(Math.min(0, d.value)); })
.attr("y", function(d) { return y(d.name); })
.attr("width", function(d) { return Math.abs(x(d.value) - x(0)); })
.attr("height", y.rangeBand())
.on('mouseover', tip.show)
.on('mouseout', tip.hide);
svg.append("g")
.attr("class", "x axis")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.append("line")
.attr("x1", x(0))
.attr("x2", x(0))
.attr("y2", height);
if (showLabels === true) {
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr({
"transform": "rotate(-90)",
"y": 6,
"dy": ".71em"
})
.style("text-anchor", "end")
.text("");
}
});
function type(d) {
if (displayValue === "percent") {
d.value = (parseFloat(d[beforeValue]) - parseFloat(d[afterValue]));
d.value = d.value/parseFloat(d[beforeValue])*100;
} else {
d.value = (parseFloat(d[beforeValue]) - parseFloat(d[afterValue]));
}
return d;
}
</script>
Modified http://d3js.org/d3.v3.min.js to a secure url
Modified http://labratrevenge.com/d3-tip/javascripts/d3.tip.min.js to a secure url
https://d3js.org/d3.v3.min.js
https://labratrevenge.com/d3-tip/javascripts/d3.tip.min.js