Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
7df7e66f
Commit
7df7e66f
authored
6 years ago
by
Tom Robinson
Browse files
Options
Downloads
Patches
Plain Diff
Detect sort order of pivot table columns and sort them if necessary
parent
71ec586f
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
frontend/src/metabase/lib/data_grid.js
+73
-4
73 additions, 4 deletions
frontend/src/metabase/lib/data_grid.js
with
73 additions
and
4 deletions
frontend/src/metabase/lib/data_grid.js
+
73
−
4
View file @
7df7e66f
...
...
@@ -3,8 +3,11 @@ import { formatValue } from "metabase/lib/formatting";
import
_
from
"
underscore
"
;
export
function
pivot
(
data
,
normalCol
,
pivotCol
,
cellCol
)
{
const
pivotColValues
=
distinctValues
(
data
,
pivotCol
);
const
normalColValues
=
distinctValues
(
data
,
normalCol
);
const
{
pivotColValues
,
normalColValues
}
=
distinctValuesSorted
(
data
.
rows
,
pivotCol
,
normalCol
,
);
// make sure that the first element in the pivoted column list is null which makes room for the label of the other column
pivotColValues
.
unshift
(
data
.
cols
[
normalCol
].
display_name
);
...
...
@@ -57,6 +60,72 @@ export function pivot(data, normalCol, pivotCol, cellCol) {
};
}
export
function
distinctValues
(
data
,
colIndex
)
{
return
_
.
uniq
(
data
.
rows
.
map
(
row
=>
row
[
colIndex
]));
const
DEFAULT_COMPARE
=
(
a
,
b
)
=>
(
a
<
b
?
-
1
:
a
>
b
?
1
:
0
);
class
SortState
{
constructor
(
compare
=
DEFAULT_COMPARE
)
{
this
.
asc
=
true
;
this
.
desc
=
true
;
this
.
lastValue
=
undefined
;
this
.
isGrouped
=
false
;
this
.
groupAsc
=
true
;
this
.
groupDesc
=
true
;
this
.
lastGroupKey
=
undefined
;
this
.
compare
=
compare
;
}
update
(
value
,
groupKey
)
{
if
(
this
.
lastValue
!==
undefined
)
{
this
.
asc
=
this
.
asc
&&
value
>=
this
.
lastValue
;
this
.
desc
=
this
.
desc
&&
value
<=
this
.
lastValue
;
if
(
this
.
lastGroupKey
!==
undefined
&&
this
.
lastGroupKey
===
groupKey
)
{
this
.
groupAsc
=
this
.
groupAsc
&&
value
>=
this
.
lastValue
;
this
.
groupDesc
=
this
.
groupDesc
&&
value
<=
this
.
lastValue
;
this
.
isGrouped
=
true
;
}
}
this
.
lastValue
=
value
;
this
.
lastGroupKey
=
groupKey
;
}
sort
(
array
)
{
if
(
!
this
.
isGrouped
)
{
console
.
log
(
"
Not grouped
"
);
}
else
if
(
this
.
groupAsc
&&
this
.
groupDesc
)
{
console
.
warn
(
"
This shouldn't happen
"
);
}
else
if
(
this
.
groupAsc
&&
!
this
.
asc
)
{
console
.
log
(
"
Sorting ascending
"
);
array
.
sort
(
this
.
compare
);
}
else
if
(
this
.
groupDesc
&&
!
this
.
desc
)
{
console
.
log
(
"
Sorting descending
"
);
array
.
sort
((
a
,
b
)
=>
this
.
compare
(
b
,
a
));
}
}
}
export
function
distinctValuesSorted
(
rows
,
pivotColIdx
,
normalColIdx
)
{
const
normalSet
=
new
Set
();
const
pivotSet
=
new
Set
();
const
normalSortState
=
new
SortState
();
const
pivotSortState
=
new
SortState
();
for
(
const
row
of
rows
)
{
const
pivotValue
=
row
[
pivotColIdx
];
const
normalValue
=
row
[
normalColIdx
];
normalSet
.
add
(
normalValue
);
pivotSet
.
add
(
pivotValue
);
normalSortState
.
update
(
normalValue
,
pivotValue
);
pivotSortState
.
update
(
pivotValue
,
normalValue
);
}
const
normalColValues
=
Array
.
from
(
normalSet
);
const
pivotColValues
=
Array
.
from
(
pivotSet
);
normalSortState
.
sort
(
normalColValues
);
pivotSortState
.
sort
(
pivotColValues
);
return
{
normalColValues
,
pivotColValues
};
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment