Skip to content

Commit

Permalink
Apply dimension defaults in on('dimensions'), fix reorder(rowdata)
Browse files Browse the repository at this point in the history
  • Loading branch information
BroHammie committed Feb 16, 2016
1 parent dabf97d commit ca9a3e9
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 36 deletions.
40 changes: 24 additions & 16 deletions d3.parcoords.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ var side_effects = d3.dispatch.apply(this,d3.keys(__))
foregroundQueue.rate(d.value);
})
.on("dimensions", function(d) {
__.dimensions = pc.applyDimensionDefaults(d3.keys(d.value));
xscale.domain(pc.getOrderedDimensionKeys());
pc.sortDimensions();
if (flags.interactive){pc.render().updateAxes();}
Expand Down Expand Up @@ -885,27 +886,14 @@ pc.reorderable = function() {
// the lowest on the right. Visual values are determined by the data values in
// the given row.
pc.reorder = function(rowdata) {
var dims = __.dimensions.slice(0);
__.dimensions.sort(function(a, b) {
var pixelDifference = __.dimensions[a].yscale(rowdata[a]) - __.dimensions[b].yscale(rowdata[b]);

// Array.sort is not necessarily stable, this means that if pixelDifference is zero
// the ordering of dimensions might change unexpectedly. This is solved by sorting on
// variable name in that case.
if (pixelDifference === 0) {
return a.localeCompare(b);
} // else
return pixelDifference;
});
var firstDim = pc.getOrderedDimensionKeys()[0];

pc.sortDimensionsByRowData(rowdata);
// NOTE: this is relatively cheap given that:
// number of dimensions < number of data items
// Thus we check equality of order to prevent rerendering when this is the case.
var reordered = false;
dims.some(function(val, index) {
reordered = val !== __.dimensions[index];
return reordered;
});
reordered = firstDim !== pc.getOrderedDimensionKeys()[0];

if (reordered) {
xscale.domain(pc.getOrderedDimensionKeys());
Expand All @@ -926,6 +914,26 @@ pc.reorder = function(rowdata) {
}
}

pc.sortDimensionsByRowData = function(rowdata) {
var copy = __.dimensions;
var positionSortedKeys = d3.keys(__.dimensions).sort(function(a, b) {
var pixelDifference = __.dimensions[a].yscale(rowdata[a]) - __.dimensions[b].yscale(rowdata[b]);

// Array.sort is not necessarily stable, this means that if pixelDifference is zero
// the ordering of dimensions might change unexpectedly. This is solved by sorting on
// variable name in that case.
if (pixelDifference === 0) {
return a.localeCompare(b);
} // else
return pixelDifference;
});
__.dimensions = {};
positionSortedKeys.forEach(function(p, i){
__.dimensions[p] = copy[p];
__.dimensions[p].index = i;
});
}

pc.sortDimensions = function() {
var copy = __.dimensions;
var positionSortedKeys = d3.keys(__.dimensions).sort(function(a, b) {
Expand Down
4 changes: 2 additions & 2 deletions examples/axis-config.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@
.data(data)
.composite("darker")
.dimensions(dimensions)
.hideAxis(['name'])
//.hideAxis(['name'])
.render()
.shadows()
.reorderable()
.interactive()date
.interactive()
.brushMode("1D-axes"); // enable brushing
});

Expand Down
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ <h3>Reordering</h3>
d3.select("#reorder-dance").on("click", function() {
event.preventDefault();
function dance() {
pc2.dimensions( _.shuffle(pc2.dimensions()) )
pc2.dimensions( _.shuffle(d3.keys(pc2.dimensions())) )
.render()
.updateAxes();
};
Expand Down Expand Up @@ -487,7 +487,7 @@ <h3>Bundling</h3>
var select = d3.select("#bundleDimension").append("select").on("change", changeBundle);

var options = select.selectAll('option')
.data(pc0.dimensions().concat(pc0.hideAxis()));
.data(d3.keys(pc0.dimensions()));

options
.enter()
Expand Down
39 changes: 23 additions & 16 deletions src/axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,27 +226,14 @@ pc.reorderable = function() {
// the lowest on the right. Visual values are determined by the data values in
// the given row.
pc.reorder = function(rowdata) {
var dims = __.dimensions.slice(0);
__.dimensions.sort(function(a, b) {
var pixelDifference = __.dimensions[a].yscale(rowdata[a]) - __.dimensions[b].yscale(rowdata[b]);

// Array.sort is not necessarily stable, this means that if pixelDifference is zero
// the ordering of dimensions might change unexpectedly. This is solved by sorting on
// variable name in that case.
if (pixelDifference === 0) {
return a.localeCompare(b);
} // else
return pixelDifference;
});
var firstDim = pc.getOrderedDimensionKeys()[0];

pc.sortDimensionsByRowData(rowdata);
// NOTE: this is relatively cheap given that:
// number of dimensions < number of data items
// Thus we check equality of order to prevent rerendering when this is the case.
var reordered = false;
dims.some(function(val, index) {
reordered = val !== __.dimensions[index];
return reordered;
});
reordered = firstDim !== pc.getOrderedDimensionKeys()[0];

if (reordered) {
xscale.domain(pc.getOrderedDimensionKeys());
Expand All @@ -267,6 +254,26 @@ pc.reorder = function(rowdata) {
}
}

pc.sortDimensionsByRowData = function(rowdata) {
var copy = __.dimensions;
var positionSortedKeys = d3.keys(__.dimensions).sort(function(a, b) {
var pixelDifference = __.dimensions[a].yscale(rowdata[a]) - __.dimensions[b].yscale(rowdata[b]);

// Array.sort is not necessarily stable, this means that if pixelDifference is zero
// the ordering of dimensions might change unexpectedly. This is solved by sorting on
// variable name in that case.
if (pixelDifference === 0) {
return a.localeCompare(b);
} // else
return pixelDifference;
});
__.dimensions = {};
positionSortedKeys.forEach(function(p, i){
__.dimensions[p] = copy[p];
__.dimensions[p].index = i;
});
}

pc.sortDimensions = function() {
var copy = __.dimensions;
var positionSortedKeys = d3.keys(__.dimensions).sort(function(a, b) {
Expand Down
1 change: 1 addition & 0 deletions src/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var side_effects = d3.dispatch.apply(this,d3.keys(__))
foregroundQueue.rate(d.value);
})
.on("dimensions", function(d) {
__.dimensions = pc.applyDimensionDefaults(d3.keys(d.value));
xscale.domain(pc.getOrderedDimensionKeys());
pc.sortDimensions();
if (flags.interactive){pc.render().updateAxes();}
Expand Down

0 comments on commit ca9a3e9

Please sign in to comment.