Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 999a698

Browse filesBrowse files
committed
implement sorting categories in cartesian axes by an asssociated value
1 parent d617a1b commit 999a698
Copy full SHA for 999a698

File tree

Expand file treeCollapse file tree

6 files changed

+109
-1
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+109
-1
lines changed

‎src/plots/cartesian/layout_attributes.js

Copy file name to clipboardExpand all lines: src/plots/cartesian/layout_attributes.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ module.exports = {
817817
categoryorder: {
818818
valType: 'enumerated',
819819
values: [
820-
'trace', 'category ascending', 'category descending', 'array'
820+
'trace', 'category ascending', 'category descending', 'array', 'value ascending', 'value descending'
821821
/* , 'value ascending', 'value descending'*/ // value ascending / descending to be implemented later
822822
],
823823
dflt: 'trace',

‎src/plots/plots.js

Copy file name to clipboardExpand all lines: src/plots/plots.js
+63Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2843,10 +2843,73 @@ plots.doCalcdata = function(gd, traces) {
28432843

28442844
doCrossTraceCalc(gd);
28452845

2846+
// Sort axis categories per value if specified
2847+
if(sortAxisCategoriesByValue(axList, gd)) {
2848+
// If a sort operation was performed, run calc() again
2849+
for(i = 0; i < fullData.length; i++) calci(i, true);
2850+
for(i = 0; i < fullData.length; i++) calci(i, false);
2851+
doCrossTraceCalc(gd);
2852+
}
2853+
28462854
Registry.getComponentMethod('fx', 'calc')(gd);
28472855
Registry.getComponentMethod('errorbars', 'calc')(gd);
28482856
};
28492857

2858+
function sortAxisCategoriesByValue(axList, gd) {
2859+
var sortByValue = false;
2860+
for(var i = 0; i < axList.length; i++) {
2861+
var ax = axList[i];
2862+
if(ax.type !== 'category') continue;
2863+
2864+
// Order by value
2865+
if(ax.categoryorder === 'value ascending' ||
2866+
ax.categoryorder === 'value descending') {
2867+
sortByValue = true;
2868+
// Store value associated with each category
2869+
var categoriesValue = [];
2870+
var j;
2871+
for(j = 0; j < ax._categories.length; j++) {
2872+
categoriesValue.push([ax._categories[j], 0]);
2873+
}
2874+
2875+
// Aggregate values across traces
2876+
for(j = 0; j < ax._traceIndices.length; j++) {
2877+
var traceIndex = ax._traceIndices[j];
2878+
var fullData = gd._fullData[traceIndex];
2879+
if(fullData.visible !== true) continue;
2880+
var cd = gd.calcdata[traceIndex];
2881+
var type = fullData.type;
2882+
for(var k = 0; k < cd.length; k++) {
2883+
if(type === 'scatter') {
2884+
categoriesValue[cd[k].x][1] += cd[k].y;
2885+
} else if(type === 'histogram') {
2886+
categoriesValue[cd[k].p][1] += cd[k].s;
2887+
}
2888+
}
2889+
}
2890+
2891+
// Sort by value
2892+
categoriesValue.sort(function(a, b) {
2893+
return a[1] - b[1];
2894+
});
2895+
2896+
// Set new category order
2897+
ax._initialCategories = categoriesValue.map(function(c) {
2898+
return c[0];
2899+
});
2900+
2901+
// Reverse if descending
2902+
if(ax.categoryorder === 'value descending') {
2903+
ax._initialCategories.reverse();
2904+
}
2905+
2906+
// Reinitialize axis
2907+
ax.clearCalc();
2908+
}
2909+
}
2910+
return sortByValue;
2911+
}
2912+
28502913
function setupAxisCategories(axList, fullData) {
28512914
for(var i = 0; i < axList.length; i++) {
28522915
var ax = axList[i];
Loading
Loading
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"data": [{
3+
"x": ["a", "b", "c", "a", "b", "d", "b", "c"],
4+
"type": "histogram"
5+
},
6+
{
7+
"x": ["d", "c", "b", "a", "e", "a", "b"],
8+
"type": "histogram"
9+
}],
10+
"layout": {
11+
"title": "xaxis.categoryorder: \"value ascending\"",
12+
"height": 300,
13+
"width": 400,
14+
"barmode": "stack",
15+
"xaxis": {
16+
"categoryorder": "value ascending"
17+
}
18+
}
19+
}
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"data": [
3+
{
4+
"x": ["a", "b", "c", "d"],
5+
"y": [4, 2, 3, 1],
6+
"type": "scatter",
7+
"mode": "markers"
8+
},
9+
{
10+
"x": ["a", "b", "c", "d", "c", "c"],
11+
"type": "histogram"
12+
}
13+
],
14+
"layout": {
15+
"title": "xaxis.categoryorder: \"value descending\"",
16+
"width": 400,
17+
"height": 400,
18+
"xaxis": {
19+
"domain": [
20+
0,
21+
1
22+
],
23+
"categoryorder": "value descending"
24+
}
25+
}
26+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.