|
<!DOCTYPE html> |
|
<html lang="en"> |
|
<head> |
|
<meta charset="utf-8"> |
|
<!-- |
|
|
|
This gist is based on those three gists: |
|
http://bl.ocks.org/mbostock/1341021 |
|
http://bl.ocks.org/jasondavies/1341281 |
|
http://bl.ocks.org/syntagmatic/4020926 |
|
|
|
--> |
|
<title>Parallel Coordinates Ordinal Axis</title> |
|
<style> |
|
svg { |
|
font: 10px sans-serif; |
|
} |
|
|
|
.background path { |
|
fill: none; |
|
stroke: #ddd; |
|
shape-rendering: crispEdges; |
|
} |
|
|
|
.foreground path { |
|
fill: none; |
|
stroke: steelblue; |
|
} |
|
|
|
.brush .extent { |
|
fill-opacity: .3; |
|
stroke: #fff; |
|
shape-rendering: crispEdges; |
|
} |
|
|
|
.axis line, |
|
.axis path { |
|
fill: none; |
|
stroke: #000; |
|
shape-rendering: crispEdges; |
|
} |
|
|
|
.axis text { |
|
text-shadow: 0 1px 0 #fff, 1px 0 0 #fff, 0 -1px 0 #fff, -1px 0 0 #fff; |
|
cursor: move; |
|
} |
|
|
|
.axis .axis-label { |
|
font-size: 11px; |
|
font-weight: bold; |
|
text-transform: uppercase; |
|
} |
|
</style> |
|
</head> |
|
<body> |
|
<script src="http://d3js.org/d3.v3.min.js"></script> |
|
<script> |
|
|
|
var margin = {top: 50, right: 50, bottom: 50, left: 50}, |
|
width = 960 - margin.left - margin.right, |
|
height = 500 - margin.top - margin.bottom; |
|
|
|
var dimensions = [ |
|
{ |
|
name: "column1", |
|
scale: d3.scale.linear().range([height, 0]), |
|
type: "number" |
|
}, |
|
{ |
|
name: "column2", |
|
scale: d3.scale.ordinal().rangePoints([0, height]), |
|
type: "string" |
|
}, |
|
{ |
|
name: "column3", |
|
scale: d3.scale.linear().range([height, 0]), |
|
type: "number" |
|
}, |
|
{ |
|
name: "column4", |
|
scale: d3.scale.ordinal().rangePoints([0, height]), |
|
type: "string" |
|
}, |
|
{ |
|
name: "column5", |
|
scale: d3.scale.linear().range([height, 0]), |
|
type: "number" |
|
}, |
|
{ |
|
name: "column6", |
|
scale: d3.scale.ordinal().rangePoints([0, height]), |
|
type: "string" |
|
}, |
|
{ |
|
name: "column7", |
|
scale: d3.scale.ordinal().rangePoints([0, height]), |
|
type: "string" |
|
} |
|
]; |
|
|
|
var x = d3.scale.ordinal().domain(dimensions.map(function(d) { return d.name; })).rangePoints([0, width]), |
|
y = {}, |
|
dragging = {}; |
|
|
|
var line = d3.svg.line(), |
|
axis = d3.svg.axis().orient("left"), |
|
background, |
|
foreground; |
|
|
|
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 + ")"); |
|
|
|
d3.csv("data.csv", function(error, data) { |
|
|
|
//Create the dimensions depending on attribute "type" (number|string) |
|
//The x-scale calculates the position by attribute dimensions[x].name |
|
dimensions.forEach(function(dimension) { |
|
dimension.scale.domain(dimension.type === "number" |
|
? d3.extent(data, function(d) { return +d[dimension.name]; }) |
|
: data.map(function(d) { return d[dimension.name]; }).sort()); |
|
}); |
|
|
|
// Add grey background lines for context. |
|
background = svg.append("g") |
|
.attr("class", "background") |
|
.selectAll("path") |
|
.data(data) |
|
.enter().append("path") |
|
.attr("d", path); |
|
|
|
// Add blue foreground lines for focus. |
|
foreground = svg.append("g") |
|
.attr("class", "foreground") |
|
.selectAll("path") |
|
.data(data) |
|
.enter().append("path") |
|
.attr("d", path); |
|
|
|
// Add a group element for each dimension. |
|
var g = svg.selectAll(".dimension") |
|
.data(dimensions) |
|
.enter().append("g") |
|
.attr("class", "dimension") |
|
.attr("transform", function(d) { return "translate(" + x(d.name) + ")"; }) |
|
.call(d3.behavior.drag() |
|
.origin(function(d) { return {x: x(d.name)}; }) |
|
.on("dragstart", function(d) { |
|
dragging[d.name] = x(d.name); |
|
background.attr("visibility", "hidden"); |
|
}) |
|
.on("drag", function(d) { |
|
dragging[d.name] = Math.min(width, Math.max(0, d3.event.x)); |
|
foreground.attr("d", path); |
|
dimensions.sort(function(a, b) { return position(a) - position(b); }); |
|
x.domain(dimensions.map(function(d) { return d.name; })); |
|
g.attr("transform", function(d) { return "translate(" + position(d) + ")"; }) |
|
}) |
|
.on("dragend", function(d) { |
|
delete dragging[d.name]; |
|
transition(d3.select(this)).attr("transform", "translate(" + x(d.name) + ")"); |
|
transition(foreground).attr("d", path); |
|
background |
|
.attr("d", path) |
|
.transition() |
|
.delay(500) |
|
.duration(0) |
|
.attr("visibility", null); |
|
}) |
|
); |
|
|
|
// Add an axis and title. |
|
g.append("g") |
|
.attr("class", "axis") |
|
.each(function(d) { d3.select(this).call(axis.scale(d.scale)); }) |
|
.append("text") |
|
.style("text-anchor", "middle") |
|
.attr("class", "axis-label") |
|
.attr("y", -19) |
|
.text(function(d) { return d.name; }); |
|
|
|
// Add and store a brush for each axis. |
|
g.append("g") |
|
.attr("class", "brush") |
|
.each(function(d) { |
|
d3.select(this).call(d.scale.brush = d3.svg.brush().y(d.scale).on("brushstart", brushstart).on("brush", brush)); |
|
}) |
|
.selectAll("rect") |
|
.attr("x", -8) |
|
.attr("width", 16); |
|
}); |
|
|
|
function position(d) { |
|
var v = dragging[d.name]; |
|
return v == null ? x(d.name) : v; |
|
} |
|
|
|
function transition(g) { |
|
return g.transition().duration(500); |
|
} |
|
|
|
// Returns the path for a given data point. |
|
function path(d) { |
|
//return line(dimensions.map(function(p) { return [position(p), y[p](d[p])]; })); |
|
return line(dimensions.map(function(dimension) { |
|
var v = dragging[dimension.name]; |
|
var tx = v == null ? x(dimension.name) : v; |
|
return [tx, dimension.scale(d[dimension.name])]; |
|
})); |
|
} |
|
|
|
function brushstart() { |
|
d3.event.sourceEvent.stopPropagation(); |
|
} |
|
|
|
// Handles a brush event, toggling the display of foreground lines. |
|
function brush() { |
|
var actives = dimensions.filter(function(p) { return !p.scale.brush.empty(); }), |
|
extents = actives.map(function(p) { return p.scale.brush.extent(); }); |
|
|
|
foreground.style("display", function(d) { |
|
return actives.every(function(p, i) { |
|
if(p.type==="number"){ |
|
return extents[i][0] <= parseFloat(d[p.name]) && parseFloat(d[p.name]) <= extents[i][1]; |
|
}else{ |
|
return extents[i][0] <= p.scale(d[p.name]) && p.scale(d[p.name]) <= extents[i][1]; |
|
} |
|
}) ? null : "none"; |
|
}); |
|
} |
|
</script> |
|
</body> |
|
</html> |
Hello,
Can you please share something similar in d3 v4 ? Like the one in this link , I am looking for brushing in the "name" column.
http://plnkr.co/edit/TiM6ZsvMTTBhh8ZdMoFQ?p=preview
Thanks and Regards
Abhijeet