Skip to content

Instantly share code, notes, and snippets.

@ebendennis
Last active March 6, 2017 20:46
Show Gist options
  • Save ebendennis/debaae56a1e123d073a54d2c5a9f223a to your computer and use it in GitHub Desktop.
Save ebendennis/debaae56a1e123d073a54d2c5a9f223a to your computer and use it in GitHub Desktop.
Flow Comps
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
.trend {stroke-width: 4;stroke-opacity:.08;fill:none;}
.recPlan {stroke-width: 4;stroke-opacity:.75;stroke:#045275;fill:none;}
.tick line {stroke: rgba(0, 0, 0, 0.1);stroke-dasharray:2;}
</style>
</head>
<body>
<script>
var data = [{con: "Ex", name: "100yr", value: 4},
{con: "Alt1", name: "100yr", value: 3},
{con: "Alt2", name: "100yr", value: 2.2},
{con: "Alt3", name: "100yr", value: 2},
{con: "Alt4", name: "100yr", value: 1.8},
{con: "Rec", name: "100yr", value: 1.6},
{con: "Ex", name: "50yr", value: 3.8},
{con: "Alt1", name: "50yr", value: 3},
{con: "Alt2", name: "50yr", value: 2},
{con: "Alt3", name: "50yr", value: 2},
{con: "Alt4", name: "50yr", value: 1.5},
{con: "Rec", name: "50yr", value: 1.5},
{con: "Ex", name: "10yr", value: 2},
{con: "Alt1", name: "10yr", value: 2},
{con: "Alt2", name: "10yr", value: 1.5},
{con: "Alt3", name: "10yr", value: 1},
{con: "Alt4", name: "10yr", value: 1},
{con: "Rec", name: "10yr", value: 1.2},
{con: "Ex", name: "5yr", value: 1},
{con: "Alt1", name: "5yr", value: 1.5},
{con: "Alt2", name: "5yr", value: 1.2},
{con: "Alt3", name: "5yr", value: .8},
{con: "Alt4", name: "5yr", value: .5},
{con: "Rec", name: "5yr", value: .7},
{con: "Ex", name: "2yr", value: .5},
{con: "Alt1", name: "2yr", value: 1},
{con: "Alt2", name: "2yr", value: .8},
{con: "Alt3", name: "2yr", value: 0},
{con: "Alt4", name: "2yr", value: 0},
{con: "Rec", name: "2yr", value: .3}];
var margin = {top: 10, right: 40, bottom: 30, left: 50},
width = 360 - margin.left - margin.right,
height = 360 - margin.top - margin.bottom;
var x = d3.scalePoint()
.rangeRound([0, width]);
var y = d3.scaleLinear()
.range([height, 0]);
var z0 = d3.scaleOrdinal()
.range(['#00C853','#AA00FF','#0091EA','#FFAB00'])
.domain(data.map(function(d) { return d.con; }));
var z1 = d3.scaleOrdinal()
.range(["#263238",'#00C853','#AA00FF','#0091EA','#FFAB00','#045275'])
.domain(data.map(function(d) { return d.con; }));
var div = window.document.createElement('div');
var svg = d3.select("body")
.append("svg")
.attr("class", "xs")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
x.domain(data.map(function(d) { return d.name; }));
y.domain([0, 4]);
var nestData = d3.nest().key(function(d) { return d.con; }).entries(data);
var area = d3.area()
.x(function(d) { return x(d.name); })
.y0(function(d) { return y(d.value); })
.y1(function(d) { return y(0); });
var subjectPath = d3.line()
.x(function(d) {
return x(d.name);
})
.y(function(d) {
return y(d.value);
});
nestData.forEach(function(d) {
if (d.key != "Ex" && d.key != "Rec") {
svg.append("path")
.attr("class", "trend")
.attr("d", subjectPath(d.values));
} else if (d.key == "Ex") {
svg.append("path")
.style("fill", "#263238")
.style("fill-opacity", .1)
.style("stroke", "#607D8B")
.attr("d", area(d.values));
} else {
svg.append("path")
.attr("class", "recPlan")
.attr("d", subjectPath(d.values));
}
});
svg.selectAll(".trend")
.data(data)
.attr("stroke", function(d) { return z0(d.con); })
svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("r", function(d) {
if (d.con != "Ex" && d.con != "Rec") {return 0;}
else {return 3.5;};})
.attr("cx", function(d) { return x(d.name); })
.attr("cy", function(d) { return y(d.value); })
.attr("stroke", function(d) { if (d.con != "Ex"){ return z1(d.con); } else { return '#607D8B';}})
.style("fill", "none");
svg.append("text")
.attr("y", 10)
.attr("x", 265)
.attr("font-size", 10)
.attr("font-weight", "bold")
.attr("fill", "#00C853")
.style("text-anchor", "end")
.text("Alt 1");
svg.append("text")
.attr("y", 22)
.attr("x", 265)
.attr("font-size", 10)
.attr("font-weight", "bold")
.attr("fill", "#AA00FF")
.style("text-anchor", "end")
.text("Alt 2");
svg.append("text")
.attr("y", 34)
.attr("x", 265)
.attr("font-size", 10)
.attr("font-weight", "bold")
.attr("fill", "#0091EA")
.style("text-anchor", "end")
.text("Alt 3");
svg.append("text")
.attr("y", 46)
.attr("x", 265)
.attr("font-size", 10)
.attr("font-weight", "bold")
.attr("fill", "#FFAB00")
.style("text-anchor", "end")
.text("Alt 4");
// Add the X Axis
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x).tickSizeInner(-height).tickPadding(10).tickSizeOuter(0));
// Add the Y Axis
svg.append("g")
.call(d3.axisLeft(y).tickSizeInner(-width).ticks(4).tickPadding(5).tickSizeOuter(0))
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 3)
.attr("dy", ".71em")
.attr("font-size", 8)
.attr("fill", "#000")
.style("text-anchor", "end")
.text("ft");
function type(d) {
d.value = +d.value;
return d;
}
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment