@@ -19,6 +19,7 @@ import {
1919 PartitionLayout ,
2020 Settings ,
2121 defaultPartitionValueFormatter ,
22+ SeriesIdentifier ,
2223} from '@elastic/charts' ;
2324import { ShapeTreeNode } from '@elastic/charts/src/chart_types/partition_chart/layout/types/viewmodel_types' ;
2425import { mocks } from '@elastic/charts/src/mocks/hierarchical' ;
@@ -38,6 +39,8 @@ export const Example = () => {
3839 {
3940 treemap : PartitionLayout . treemap ,
4041 sunburst : PartitionLayout . sunburst ,
42+ mosaic : PartitionLayout . mosaic ,
43+ waffle : PartitionLayout . waffle ,
4144 } ,
4245 PartitionLayout . sunburst ,
4346 ) ;
@@ -50,6 +53,26 @@ export const Example = () => {
5053 } ) ;
5154 const legendStrategy = select ( 'legendStrategy' , LegendStrategy , LegendStrategy . Key as LegendStrategy ) ;
5255 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+
5376 const partitionTheme : PartialTheme [ 'partition' ] = {
5477 linkLabel : {
5578 maxCount : 0 ,
@@ -72,14 +95,18 @@ export const Example = () => {
7295 circlePadding : 4 ,
7396 } ;
7497
98+ const isFlatLegendSupported =
99+ partitionLayout === PartitionLayout . treemap || partitionLayout === PartitionLayout . sunburst ;
100+
75101 return (
76102 < Chart >
77103 < Settings
78104 showLegend
79105 showLegendExtra = { showLegendExtra }
80- flatLegend = { flatLegend }
106+ flatLegend = { isFlatLegendSupported ? flatLegend : true }
81107 legendStrategy = { legendStrategy }
82108 legendMaxDepth = { legendMaxDepth }
109+ legendSort = { legendSortStrategy !== 'default' ? customLegendSort : undefined }
83110 baseTheme = { useBaseTheme ( ) }
84111 theme = { {
85112 partition : partitionTheme ,
@@ -127,5 +154,8 @@ Example.parameters = {
127154 markdown : `To flatten a hierarchical legend (like the rendered in a pie chart or a treemap when using a multi-layer configuration) you can
128155add the \`flatLegend\` prop into the \`<Settings baseTheme={useBaseTheme()} />\` component.
129156
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+ ` ,
131161} ;
0 commit comments