-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(legend): display pie chart legend extra (#939)
- Loading branch information
Showing
15 changed files
with
269 additions
and
8 deletions.
There are no files selected for viewing
Binary file modified
BIN
+1.44 KB
(100%)
...ll-stories-interactions-sunburst-slice-clicks-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+1.4 KB
(100%)
...ctions-test-ts-interactions-tooltips-should-show-tooltip-on-sunburst-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+117 KB
...ies-extra-values-should-display-flat-legend-extra-values-on-sunburst-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+133 KB
...ries-extra-values-should-display-flat-legend-extra-values-on-treemap-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+117 KB
...s-extra-values-should-display-nested-legend-extra-values-on-sunburst-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+133 KB
...es-extra-values-should-display-nested-legend-extra-values-on-treemap-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...t_types/partition_chart/state/selectors/__snapshots__/get_legend_items_extra.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Partition - Legend item extra values should return all extra values in nested legend 1`] = `Object {}`; | ||
|
||
exports[`Partition - Legend item extra values should return extra values in nested legend within max depth of 1 1`] = `Object {}`; | ||
|
||
exports[`Partition - Legend item extra values should return extra values in nested legend within max depth of 2 1`] = `Object {}`; |
137 changes: 137 additions & 0 deletions
137
src/chart_types/partition_chart/state/selectors/get_legend_items_extra.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { Store } from 'redux'; | ||
|
||
import { MockSeriesSpec, MockGlobalSpec } from '../../../../mocks/specs'; | ||
import { MockStore } from '../../../../mocks/store'; | ||
import { GlobalChartState } from '../../../../state/chart_state'; | ||
import { PrimitiveValue } from '../../layout/utils/group_by_rollup'; | ||
import { getLegendItemsExtra } from './get_legend_items_extra'; | ||
|
||
describe('Partition - Legend item extra values', () => { | ||
type TestDatum = [string, string, string, number]; | ||
const spec = MockSeriesSpec.sunburst({ | ||
data: [ | ||
['aaa', 'aa', '1', 1], | ||
['aaa', 'aa', '1', 2], | ||
['aaa', 'aa', '3', 1], | ||
['aaa', 'bb', '4', 1], | ||
['aaa', 'bb', '5', 1], | ||
['aaa', 'bb', '6', 1], | ||
['bbb', 'aa', '7', 1], | ||
['bbb', 'aa', '8', 1], | ||
['bbb', 'bb', '9', 1], | ||
['bbb', 'bb', '10', 1], | ||
['bbb', 'cc', '11', 1], | ||
['bbb', 'cc', '12', 1], | ||
], | ||
valueAccessor: (d: TestDatum) => d[3], | ||
layers: [ | ||
{ | ||
groupByRollup: (datum: TestDatum) => datum[0], | ||
nodeLabel: (d: PrimitiveValue) => String(d), | ||
}, | ||
{ | ||
groupByRollup: (datum: TestDatum) => datum[1], | ||
nodeLabel: (d: PrimitiveValue) => String(d), | ||
}, | ||
{ | ||
groupByRollup: (datum: TestDatum) => datum[2], | ||
nodeLabel: (d: PrimitiveValue) => String(d), | ||
}, | ||
], | ||
}); | ||
let store: Store<GlobalChartState>; | ||
|
||
beforeEach(() => { | ||
store = MockStore.default(); | ||
}); | ||
|
||
it('should return all extra values in nested legend', () => { | ||
MockStore.addSpecs([spec], store); | ||
|
||
const extraValues = getLegendItemsExtra(store.getState()); | ||
expect([...extraValues.keys()]).toEqual([ | ||
'0', | ||
'0__0', | ||
'0__0__0', | ||
'0__0__0__0', | ||
'0__0__0__1', | ||
'0__0__1', | ||
'0__0__1__0', | ||
'0__0__1__1', | ||
'0__0__1__2', | ||
'0__1', | ||
'0__1__0', | ||
'0__1__0__0', | ||
'0__1__0__1', | ||
'0__1__1', | ||
'0__1__1__0', | ||
'0__1__1__1', | ||
'0__1__2', | ||
'0__1__2__0', | ||
'0__1__2__1', | ||
]); | ||
expect(extraValues.values()).toMatchSnapshot(); | ||
}); | ||
|
||
it('should return extra values in nested legend within max depth of 1', () => { | ||
const settings = MockGlobalSpec.settings({ legendMaxDepth: 1 }); | ||
MockStore.addSpecs([settings, spec], store); | ||
|
||
const extraValues = getLegendItemsExtra(store.getState()); | ||
expect([...extraValues.keys()]).toEqual(['0', '0__0', '0__1']); | ||
expect(extraValues.values()).toMatchSnapshot(); | ||
}); | ||
|
||
it('should return extra values in nested legend within max depth of 2', () => { | ||
const settings = MockGlobalSpec.settings({ legendMaxDepth: 2 }); | ||
MockStore.addSpecs([settings, spec], store); | ||
|
||
const extraValues = getLegendItemsExtra(store.getState()); | ||
expect([...extraValues.keys()]).toEqual([ | ||
'0', | ||
'0__0', | ||
'0__0__0', | ||
'0__0__1', | ||
'0__1', | ||
'0__1__0', | ||
'0__1__1', | ||
'0__1__2', | ||
]); | ||
expect(extraValues.values()).toMatchSnapshot(); | ||
}); | ||
|
||
it('filters all extraValues is depth is 0', () => { | ||
const settings = MockGlobalSpec.settings({ legendMaxDepth: 0 }); | ||
MockStore.addSpecs([settings, spec], store); | ||
|
||
const extraValues = getLegendItemsExtra(store.getState()); | ||
expect([...extraValues.keys()]).toEqual([]); | ||
}); | ||
|
||
it('filters all extraValues is depth is NaN', () => { | ||
const settings = MockGlobalSpec.settings({ legendMaxDepth: NaN }); | ||
MockStore.addSpecs([settings, spec], store); | ||
|
||
const extraValues = getLegendItemsExtra(store.getState()); | ||
expect([...extraValues.keys()]).toEqual([]); | ||
}); | ||
}); |
81 changes: 81 additions & 0 deletions
81
src/chart_types/partition_chart/state/selectors/get_legend_items_extra.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import createCachedSelector from 're-reselect'; | ||
|
||
import { LegendItemExtraValues } from '../../../../common/legend'; | ||
import { SeriesKey } from '../../../../common/series_id'; | ||
import { SettingsSpec } from '../../../../specs'; | ||
import { getChartIdSelector } from '../../../../state/selectors/get_chart_id'; | ||
import { getSettingsSpecSelector } from '../../../../state/selectors/get_settings_specs'; | ||
import { HierarchyOfArrays, CHILDREN_KEY } from '../../layout/utils/group_by_rollup'; | ||
import { PartitionSpec } from '../../specs'; | ||
import { getPieSpec } from './pie_spec'; | ||
import { getTree } from './tree'; | ||
|
||
/** @internal */ | ||
export const getLegendItemsExtra = createCachedSelector( | ||
[getPieSpec, getSettingsSpecSelector, getTree], | ||
(pieSpec, { legendMaxDepth }, tree): Map<SeriesKey, LegendItemExtraValues> => { | ||
const legendExtraValues = new Map<SeriesKey, LegendItemExtraValues>(); | ||
|
||
return pieSpec && isValidLegendMaxDepth(legendMaxDepth) | ||
? getExtraValueMap(pieSpec, tree, legendMaxDepth) | ||
: legendExtraValues; | ||
}, | ||
)(getChartIdSelector); | ||
|
||
/** | ||
* Check if the legendMaxDepth from settings is a valid number (NaN or <=0) | ||
* | ||
* @param legendMaxDepth - SettingsSpec['legendMaxDepth'] | ||
*/ | ||
function isValidLegendMaxDepth(legendMaxDepth: SettingsSpec['legendMaxDepth']): boolean { | ||
return typeof legendMaxDepth === 'number' && !Number.isNaN(legendMaxDepth) && legendMaxDepth > 0; | ||
} | ||
|
||
/** | ||
* Creates flat extra value map from nested key path | ||
*/ | ||
function getExtraValueMap( | ||
{ layers, valueFormatter }: Pick<PartitionSpec, 'layers' | 'valueFormatter'>, | ||
tree: HierarchyOfArrays, | ||
maxDepth: number, | ||
depth: number = 0, | ||
keys: Map<SeriesKey, LegendItemExtraValues> = new Map(), | ||
): Map<SeriesKey, LegendItemExtraValues> { | ||
for (let i = 0; i < tree.length; i++) { | ||
const branch = tree[i]; | ||
const [key, arrayNode] = branch; | ||
const { value, path, [CHILDREN_KEY]: children } = arrayNode; | ||
|
||
if (key != null) { | ||
const values: LegendItemExtraValues = new Map(); | ||
const formattedValue = valueFormatter ? valueFormatter(value) : value; | ||
|
||
values.set(key, formattedValue); | ||
keys.set(path.map(({ index }) => index).join('__'), values); | ||
} | ||
|
||
if (depth < maxDepth) { | ||
getExtraValueMap({ layers, valueFormatter }, children, maxDepth, depth + 1, keys); | ||
} | ||
} | ||
return keys; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters