@@ -19,6 +19,7 @@ import {
19
19
PartitionLayout ,
20
20
Settings ,
21
21
defaultPartitionValueFormatter ,
22
+ SeriesIdentifier ,
22
23
} from '@elastic/charts' ;
23
24
import { ShapeTreeNode } from '@elastic/charts/src/chart_types/partition_chart/layout/types/viewmodel_types' ;
24
25
import { mocks } from '@elastic/charts/src/mocks/hierarchical' ;
@@ -38,6 +39,8 @@ export const Example = () => {
38
39
{
39
40
treemap : PartitionLayout . treemap ,
40
41
sunburst : PartitionLayout . sunburst ,
42
+ mosaic : PartitionLayout . mosaic ,
43
+ waffle : PartitionLayout . waffle ,
41
44
} ,
42
45
PartitionLayout . sunburst ,
43
46
) ;
@@ -50,6 +53,26 @@ export const Example = () => {
50
53
} ) ;
51
54
const legendStrategy = select ( 'legendStrategy' , LegendStrategy , LegendStrategy . Key as LegendStrategy ) ;
52
55
const maxLines = number ( 'max legend label lines' , 1 , { min : 0 , step : 1 } ) ;
56
+
57
+ const legendSortStrategy = select (
58
+ 'Custom legend sorting' ,
59
+ { RegionsFirst : 'regionsFirst' , ProductsFirst : 'productsFirst' , DefaultSort : 'default' } ,
60
+ 'regionsFirst' ,
61
+ ) ;
62
+
63
+ const customLegendSort = ( a : SeriesIdentifier , b : SeriesIdentifier ) => {
64
+ if ( legendSortStrategy === 'regionsFirst' ) {
65
+ if ( a . key in regionLookup && b . key in regionLookup ) {
66
+ return a . key . localeCompare ( b . key ) ;
67
+ }
68
+ return a . key in regionLookup ? - 1 : b . key in regionLookup ? 1 : a . key . localeCompare ( b . key ) ;
69
+ }
70
+ if ( a . key in productLookup && b . key in productLookup ) {
71
+ return a . key . localeCompare ( b . key ) ;
72
+ }
73
+ return a . key in productLookup ? - 1 : b . key in productLookup ? 1 : a . key . localeCompare ( b . key ) ;
74
+ } ;
75
+
53
76
const partitionTheme : PartialTheme [ 'partition' ] = {
54
77
linkLabel : {
55
78
maxCount : 0 ,
@@ -72,14 +95,18 @@ export const Example = () => {
72
95
circlePadding : 4 ,
73
96
} ;
74
97
98
+ const isFlatLegendSupported =
99
+ partitionLayout === PartitionLayout . treemap || partitionLayout === PartitionLayout . sunburst ;
100
+
75
101
return (
76
102
< Chart >
77
103
< Settings
78
104
showLegend
79
105
showLegendExtra = { showLegendExtra }
80
- flatLegend = { flatLegend }
106
+ flatLegend = { isFlatLegendSupported ? flatLegend : true }
81
107
legendStrategy = { legendStrategy }
82
108
legendMaxDepth = { legendMaxDepth }
109
+ legendSort = { legendSortStrategy !== 'default' ? customLegendSort : undefined }
83
110
baseTheme = { useBaseTheme ( ) }
84
111
theme = { {
85
112
partition : partitionTheme ,
@@ -127,5 +154,8 @@ Example.parameters = {
127
154
markdown : `To flatten a hierarchical legend (like the rendered in a pie chart or a treemap when using a multi-layer configuration) you can
128
155
add the \`flatLegend\` prop into the \`<Settings baseTheme={useBaseTheme()} />\` component.
129
156
130
- To limit displayed hierarchy to a specific depth, you can use the \`legendMaxDepth\` prop. The first layer will have a depth of \`1\`.` ,
157
+ To limit displayed hierarchy to a specific depth, you can use the \`legendMaxDepth\` prop. The first layer will have a depth of \`1\`.
158
+
159
+ It is possible to provide a custom sorting logic for a legend when flattened, when not flattened the \`legendSort\` function is ignored.
160
+ ` ,
131
161
} ;
0 commit comments