-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(legend): display pie chart legend extra #939
Merged
nickofthyme
merged 12 commits into
elastic:master
from
shahzad31:pie-charts-legend-extra
Jan 20, 2021
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8807b24
feat: add legend extra values to partition charts
nickofthyme e08f89f
test: add vrt screenshots
nickofthyme 355e6a0
test: update broken tests
nickofthyme 0895356
Merge branch 'master' into pie-charts-legend-extra
nickofthyme ec75e37
Merge branch 'master' into pie-charts-legend-extra
nickofthyme ea40ae2
chore: update screenshots
nickofthyme 22fc63d
Merge branch 'master' into pie-charts-legend-extra
nickofthyme 69c7dc8
fix: merge issues and code simplifications
nickofthyme 13811eb
fix: replace value with index in path join
nickofthyme a2972a0
fix: remove number type from LegendItemChildId
nickofthyme dac6d17
fix: naming, limit recursive depth and add tests
nickofthyme 29c0cd5
fix: broken key lookup for xy charts
nickofthyme File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
86 changes: 86 additions & 0 deletions
86
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,86 @@ | ||
/* | ||
* 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 '../../../../commons/legend'; | ||
import { SeriesKey } from '../../../../commons/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>(); | ||
|
||
if (!pieSpec) { | ||
return legendExtraValues; | ||
} | ||
if (isInvalidLegendMaxDepth(legendMaxDepth)) { | ||
return legendExtraValues; | ||
} | ||
|
||
return getExtraValueMap(pieSpec, tree); | ||
}, | ||
)(getChartIdSelector); | ||
|
||
/** | ||
* Check if the legendMaxDepth from settings is not a valid number (NaN or <=0) | ||
* | ||
* @param legendMaxDepth - SettingsSpec['legendMaxDepth'] | ||
*/ | ||
function isInvalidLegendMaxDepth(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, | ||
keys: Map<SeriesKey, LegendItemExtraValues> = new Map(), | ||
): Map<SeriesKey, LegendItemExtraValues> { | ||
if (tree.length === 0) { | ||
return keys; | ||
} | ||
|
||
monfera marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.join('__'), values); | ||
} | ||
|
||
getExtraValueMap({ layers, valueFormatter }, children, 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I'd prefer positive predicates in general, eg.
isValidLegendMaxDepth
over!isInvalidLegendMaxDepth
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
69c7dc8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good changes here, would still prefer a positive predicate over a negative one, though totally not a blocker.
I've a question here: this extracts, and only keys on the concatenated
value
ie. series keys; won't this approach need the structure information downstream of this? The legend strategies PR discontinued the 1:1 relation between data label (textual name) and identifying node(s);veven for just highlighting the value, at least for non-single layer charts such as partition charts. I'll look at it in more detail tomorrow, just wanted to check if/how this info won't be needed, ie. why eg. not key on the concatentated{index, value}
tuples rather than on just thevalue
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just assume not mess with existing naming (i.e.
isInvalidLegendMaxDepth
) because there's likely a lot of this around. I typically use positive or negative such that I don't have to!
it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes you are right about the
value
that should beindex
instead. See 13811ebThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Fair enough not to add possibly sweeping changes to this PR. We could maybe brainstorm about whether we should prefer positive predicates at all, not just the general avoidance of
non
orun
but also some legit words likeinvalid
; there's no specific line we can draw between negative and positive words and it might be something noone else cares about, it's a miniscule stylistic thingThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re the
value
vsindex
change, the current test suite is insensitive to whether it yields the desired thing, it'd be neat to make it fail if the info is insufficient, I guess it depends on mock structure to trigger a differenceThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See dac6d17