D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
BenHeubl
Full window
Github gist
Personality Types
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> <link type="text/css" rel="stylesheet" href="style.css"/> <style type="text/css"> body { background: #FFFFF0 } h2 { font-family: futura; font-size: 28px; line-height: 40px; letter-spacing: -1px;color: #444; } p { font-family: futura; font-size: 14px; line-height: 20px; text-transform: uppercase; color: #444; } h1 { font-family: futura; font-size: 48px; line-height: 40px; letter-spacing: -1px; color: #444; margin: 0 0 0 0; padding: 0 0 0 0; font-weight: 100; } svg { font: 11px futura; fill-opacity: 0.5; } .foreground path { fill: none; stroke-width: 1.5px; } .foreground path.fade { stroke: grey; stroke-opacity: .05; } .legend { font-size: 16px; font-style: oblique; font: futura; } .legend line { stroke-width: 10px; } .EXPLORERS { stroke: #1d7e98; } .DIPLOMATS { stroke: #af341c; } .ANALYSTS { stroke: #d27a25; } .SENTINELS { stroke: #061e31; } .brush .extent { fill-opacity: 1; stroke: #fff; shape-rendering: crispEdges; } .axis line { fill: none; stroke: grey; stroke-opacity: 0.4; shape-rendering: crispEdges; } .axis path { fill: none; stroke: black; stroke-opacity: 0.1; shape-rendering: crispEdges; } .axis text { text-shadow: 0 1px 0 #fff; cursor: move; } </style> </head> <body> <h1>What if you could choose?</h1> <h2><i>16 Personality Types</i> in "Parallel coordinates"</h2> <script type="text/javascript" src="https://d3js.org/d3.v3.min.js"></script> <script type="text/javascript"> var types = ["EXPLORERS", "DIPLOMATS", "ANALYSTS", "SENTINELS"], traits = ["Personality Types", "Average Yearly Income", "Average Satisfaction Rating (1to5)", "Number of People Managed", "Percent of Stay Home Parents"]; //traits: // add = Personality Types //sepal length = AverageYearlyIncome //sepal width = AveragePeopleManaged //petal length = AverageSatisfactionRating1to5 //petal width = percentofStayHomeParents //types: // setosa=EXPLORERS // versicolor =DIPLOMATS // virginica =ANALYSTS // setosa2= SENTINELS var m = [40, 160, 250, 160], //Ben's scales //top, rigth, bottom, left w = 1000 - m[1] - m[3], h = 700 - m[0] - m[2]; var x = d3.scale.ordinal().domain(traits).rangePoints([0, w]), y = {}; var line = d3.svg.line(), axis = d3.svg.axis().orient("left").ticks(15), foreground; var svg = d3.select("body").append("svg:svg") .attr("width", w + m[1] + m[3]) .attr("height", h + m[0] + m[2]) .append("svg:g") .attr("transform", "translate(" + m[3] + "," + m[0] + ")"); d3.csv("datafinal.csv", function(flowers) { traits.forEach(function(d) { flowers.forEach(function(p) { p[d] = +p[d]; }); y[d] = d3.scale.linear() .domain(d3.extent(flowers, function(p) { return p[d]; })) .range([h, 0]); y[d].brush = d3.svg.brush() .y(y[d]) .on("brush", brush); }); var legend = svg.selectAll("g.legend") .data(types) .enter().append("svg:g") .attr("class", "legend") .attr("transform", function(d, i) { return "translate(0," + (i * 20 + 584) + ")"; }); legend.append("svg:line") .attr("class", String) .attr("x2", 8); legend.append("svg:text") .attr("x", 12) .attr("dy", ".31em") .text(function(d) { return "Type " + d; }); foreground = svg.append("svg:g") .attr("class", "foreground") .selectAll("path") .data(flowers) .enter().append("svg:path") .attr("d", path) .attr("class", function(d) { return d.types; }); var g = svg.selectAll(".trait") .data(traits) .enter().append("svg:g") .attr("class", "trait") .attr("transform", function(d) { return "translate(" + x(d) + ")"; }) .call(d3.behavior.drag() .origin(function(d) { return {x: x(d)}; }) .on("dragstart", dragstart) .on("drag", drag) .on("dragend", dragend)); g.append("svg:g") .attr("class", "axis") .each(function(d) { d3.select(this).call(axis.scale(y[d])); }) .append("svg:text") .attr("text-anchor", "middle") .attr("y", -9) .text(String); g.append("svg:g") .attr("class", "brush") .each(function(d) { d3.select(this).call(y[d].brush); }) .selectAll("rect") .attr("x", -8) .attr("width", 16); function dragstart(d) { i = traits.indexOf(d); } function drag(d) { x.range()[i] = d3.event.x; traits.sort(function(a, b) { return x(a) - x(b); }); g.attr("transform", function(d) { return "translate(" + x(d) + ")"; }); foreground.attr("d", path); } function dragend(d) { x.domain(traits).rangePoints([0, w]); var t = d3.transition().duration(1000); t.selectAll(".trait").attr("transform", function(d) { return "translate(" + x(d) + ")"; }); t.selectAll(".foreground path").attr("d", path); } }); //Ben's edits function path(d) { return line(traits.map(function(p) { return [x(p), y[p](d[p])]; })); } function brush() { var actives = traits.filter(function(p) { return !y[p].brush.empty(); }), extents = actives.map(function(p) { return y[p].brush.extent(); }); foreground.classed("fade", function(d) { return !actives.every(function(p, i) { return extents[i][0] <= d[p] && d[p] <= extents[i][1]; }); }); } </script> </body> </html>
Modified
http://d3js.org/d3.v3.min.js
to a secure url
https://d3js.org/d3.v3.min.js