forked from plotly/plotly.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathordered_categories.js
33 lines (25 loc) · 967 Bytes
/
ordered_categories.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* Copyright 2012-2016, 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 d3 = require('d3');
/**
* TODO add documentation
*/
module.exports = function orderedCategories(axisLetter, categorymode, categorylist, data) {
return categorymode === 'array' ?
// just return a copy of the specified array ...
categorylist.slice() :
// ... or take the union of all encountered tick keys and sort them as specified
// (could be simplified with lodash-fp or ramda)
[].concat.apply([], data.map(function(d) {return d[axisLetter];}))
.filter(function(element, index, array) {return index === array.indexOf(element);})
.sort(({
'category ascending': d3.ascending,
'category descending': d3.descending
})[categorymode]);
};