Skip to content

Commit

Permalink
Merge pull request #279 from Git-me-outta-here/brushextents
Browse files Browse the repository at this point in the history
brushExtents Example fixes
  • Loading branch information
Git-me-outta-here committed Feb 24, 2016
2 parents 2f5a83c + 795f118 commit a022b81
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 137 deletions.
158 changes: 91 additions & 67 deletions d3.parcoords.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ function getset(obj,state,events) {
};

function extend(target, source) {
for (key in source) {
for (var key in source) {
target[key] = source[key];
}
return target;
Expand Down Expand Up @@ -457,7 +457,8 @@ pc.renderBrushed.queue = function() {
} else {
brushedQueue([]); // This is needed to clear the currently brushed items
}
};function compute_cluster_centroids(d) {
};
function compute_cluster_centroids(d) {

var clusterCentroids = d3.map();
var clusterCounts = d3.map();
Expand Down Expand Up @@ -1042,28 +1043,28 @@ pc.brushMode = function(mode) {
// brush mode: 1D-Axes

(function() {
var brushes = {};
var brushes = {};

function is_brushed(p) {
return !brushes[p].empty();
}
function is_brushed(p) {
return !brushes[p].empty();
}

// data within extents
function selected() {
var actives = d3.keys(__.dimensions).filter(is_brushed),
extents = actives.map(function(p) { return brushes[p].extent(); });

// We don't want to return the full data set when there are no axes brushed.
// Actually, when there are no axes brushed, by definition, no items are
// selected. So, let's avoid the filtering and just return false.
//if (actives.length === 0) return false;
// We don't want to return the full data set when there are no axes brushed.
// Actually, when there are no axes brushed, by definition, no items are
// selected. So, let's avoid the filtering and just return false.
//if (actives.length === 0) return false;

// Resolves broken examples for now. They expect to get the full dataset back from empty brushes
if (actives.length === 0) return __.data;
// Resolves broken examples for now. They expect to get the full dataset back from empty brushes
if (actives.length === 0) return __.data;

// test if within range
var within = {
"date": function(d,p,dimension) {
// test if within range
var within = {
"date": function(d,p,dimension) {
if (typeof __.dimensions[p].yscale.rangePoints === "function") { // if it is ordinal
return extents[dimension][0] <= __.dimensions[p].yscale(d[p]) && __.dimensions[p].yscale(d[p]) <= extents[dimension][1]
} else {
Expand Down Expand Up @@ -1135,7 +1136,10 @@ pc.brushMode = function(mode) {
brush.extent(extents[d]);

//redraw the brush
brush(brushSelections[d]);
brushSelections[d]
.transition()
.duration(0)
.call(brush);

//fire some events
brush.event(brushSelections[d]);
Expand All @@ -1144,6 +1148,8 @@ pc.brushMode = function(mode) {

//redraw the chart
pc.renderBrushed();

return pc;
}
}

Expand All @@ -1157,60 +1163,78 @@ pc.brushMode = function(mode) {
d3.event.sourceEvent.stopPropagation();
}
})
.on("brush", function() {
brushUpdated(selected());
})
.on("brushend", function() {
events.brushend.call(pc, __.brushed);
});

brushes[axis] = brush;
return brush;
};
function brushReset(dimension) {
__.brushed = false;
if (g) {
g.selectAll('.brush')
.each(function(d) {
d3.select(this).call(
brushes[d].clear()
);
});
pc.renderBrushed();
}
return this;
};

function install() {
if (!g) pc.createAxes();
.on("brush", function() {
brushUpdated(selected());
})
.on("brushend", function() {
events.brushend.call(pc, __.brushed);
});

// Add and store a brush for each axis.
g.append("svg:g")
.attr("class", "brush")
.each(function(d) {
d3.select(this).call(brushFor(d));
})
.selectAll("rect")
.style("visibility", null)
.attr("x", -15)
.attr("width", 30);
brushes[axis] = brush;
return brush;
};

function brushReset(dimension) {
if (dimension===undefined) {
__.brushed = false;
if (g) {
g.selectAll('.brush')
.each(function(d) {
d3.select(this)
.transition()
.duration(0)
.call(brushes[d].clear());
});
pc.renderBrushed();
}
}
else {
if (g) {
g.selectAll('.brush')
.each(function(d) {
if (d!=dimension) return;
d3.select(this)
.transition()
.duration(0)
.call(brushes[d].clear());
brushes[d].event(d3.select(this));
});
pc.renderBrushed();
}
}
return this;
};

pc.brushExtents = brushExtents;
pc.brushReset = brushReset;
return pc;
};
function install() {
if (!g) pc.createAxes();

brush.modes["1D-axes"] = {
install: install,
uninstall: function() {
g.selectAll(".brush").remove();
brushes = {};
delete pc.brushExtents;
delete pc.brushReset;
},
selected: selected,
brushState: brushExtents
}
// Add and store a brush for each axis.
g.append("svg:g")
.attr("class", "brush")
.each(function(d) {
d3.select(this).call(brushFor(d));
})
.selectAll("rect")
.style("visibility", null)
.attr("x", -15)
.attr("width", 30);

pc.brushExtents = brushExtents;
pc.brushReset = brushReset;
return pc;
};

brush.modes["1D-axes"] = {
install: install,
uninstall: function() {
g.selectAll(".brush").remove();
brushes = {};
delete pc.brushExtents;
delete pc.brushReset;
},
selected: selected,
brushState: brushExtents
}
})();
// brush mode: 2D-strums
// bl.ocks.org/syntagmatic/5441022
Expand Down
14 changes: 11 additions & 3 deletions examples/setterForBrushes.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<p>Demonstrates a parallel coordinates plot a pre-set 1D-axes brush.</p>
</body>
<script>
var parcoords = d3.parcoords({nullValueSeparator: "bottom"})("#example")
var pc = d3.parcoords({nullValueSeparator: "bottom"})("#example")
.data([
[0,-0,0,0,0,1],
[1,-1,1,2,1,1],
Expand All @@ -20,7 +20,15 @@
.render()
.shadows()
.createAxes()
.reorderable()
.brushMode("1D-axes")
.brushExtents({"0": [2,3]});

.brushExtents({"2": [3,4]});

pc.brushReset();
pc.brushExtents({"3": [3,6]});

pc.brushExtents({"2": [3,12]});



</script>
Loading

0 comments on commit a022b81

Please sign in to comment.