-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Sort transform #1609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Sort transform #1609
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0be666f
first cut sort transform
etpinard 36f7acd
Merge branch 'master' into sort-transform
etpinard 1a21ad9
mv getTargetArray to lib/
etpinard 823375c
mv getDataToCoordFunc in axes.js
etpinard 2dd1333
make Lib.getTargetArray API more robust
etpinard 9c70ee9
do not slice array just to read its values in sort loop
etpinard 4f91957
:lock: behavior target arrays are shorter/longer than other arrays
etpinard 4e8efe4
add test for sort transform side effects on eventData pointNumber
etpinard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Copyright 2012-2017, Plotly, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
module.exports = require('../src/transforms/sort'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/** | ||
* Copyright 2012-2017, Plotly, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var Lib = require('../lib'); | ||
var PlotSchema = require('../plot_api/plot_schema'); | ||
var Axes = require('../plots/cartesian/axes'); | ||
|
||
exports.moduleType = 'transform'; | ||
|
||
exports.name = 'sort'; | ||
|
||
exports.attributes = { | ||
enabled: { | ||
valType: 'boolean', | ||
dflt: true, | ||
description: [ | ||
'Determines whether this sort transform is enabled or disabled.' | ||
].join(' ') | ||
}, | ||
target: { | ||
valType: 'string', | ||
strict: true, | ||
noBlank: true, | ||
arrayOk: true, | ||
dflt: 'x', | ||
description: [ | ||
'Sets the target by which the sort transform is applied.', | ||
|
||
'If a string, *target* is assumed to be a reference to a data array', | ||
'in the parent trace object.', | ||
'To sort about nested variables, use *.* to access them.', | ||
'For example, set `target` to *marker.size* to sort', | ||
'about the marker size array.', | ||
|
||
'If an array, *target* is then the data array by which', | ||
'the sort transform is applied.' | ||
].join(' ') | ||
}, | ||
order: { | ||
valType: 'enumerated', | ||
values: ['ascending', 'descending'], | ||
dflt: 'ascending', | ||
description: [ | ||
'Sets the sort transform order.' | ||
].join(' ') | ||
} | ||
}; | ||
|
||
exports.supplyDefaults = function(transformIn) { | ||
var transformOut = {}; | ||
|
||
function coerce(attr, dflt) { | ||
return Lib.coerce(transformIn, transformOut, exports.attributes, attr, dflt); | ||
} | ||
|
||
var enabled = coerce('enabled'); | ||
|
||
if(enabled) { | ||
coerce('target'); | ||
coerce('order'); | ||
} | ||
|
||
return transformOut; | ||
}; | ||
|
||
exports.calcTransform = function(gd, trace, opts) { | ||
if(!opts.enabled) return; | ||
|
||
var targetArray = Lib.getTargetArray(trace, opts); | ||
if(!targetArray) return; | ||
|
||
var target = opts.target; | ||
var len = targetArray.length; | ||
var arrayAttrs = PlotSchema.findArrayAttributes(trace); | ||
var d2c = Axes.getDataToCoordFunc(gd, trace, target, targetArray); | ||
var indices = getIndices(opts, targetArray, d2c); | ||
|
||
for(var i = 0; i < arrayAttrs.length; i++) { | ||
var np = Lib.nestedProperty(trace, arrayAttrs[i]); | ||
var arrayOld = np.get(); | ||
var arrayNew = new Array(len); | ||
|
||
for(var j = 0; j < len; j++) { | ||
arrayNew[j] = arrayOld[indices[j]]; | ||
} | ||
|
||
np.set(arrayNew); | ||
} | ||
}; | ||
|
||
function getIndices(opts, targetArray, d2c) { | ||
var len = targetArray.length; | ||
var indices = new Array(len); | ||
|
||
var sortedArray = targetArray | ||
.slice() | ||
.sort(getSortFunc(opts, d2c)); | ||
|
||
for(var i = 0; i < len; i++) { | ||
var vTarget = targetArray[i]; | ||
|
||
for(var j = 0; j < len; j++) { | ||
var vSorted = sortedArray[j]; | ||
|
||
if(vTarget === vSorted) { | ||
indices[j] = i; | ||
|
||
// clear sortedArray item to get correct | ||
// index of duplicate items (if any) | ||
sortedArray[j] = null; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return indices; | ||
} | ||
|
||
function getSortFunc(opts, d2c) { | ||
switch(opts.order) { | ||
case 'ascending': | ||
return function(a, b) { return d2c(a) - d2c(b); }; | ||
case 'descending': | ||
return function(a, b) { return d2c(b) - d2c(a); }; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I chose ascending and descending to be familiar with axis
categoryorder
.