-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ML] Data vizualizer: add choropleth map for index and file (#99434)
* wip: add choropleth map to dataviz * add choropleth map to datavisualizer index and file based * fix translation * make function name more clear
- Loading branch information
1 parent
14f225f
commit d5a16f4
Showing
7 changed files
with
344 additions
and
13 deletions.
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
126
.../application/components/stats_table/components/field_data_expanded_row/choropleth_map.tsx
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,126 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React, { FC, useMemo } from 'react'; | ||
import { EuiFlexItem, EuiSpacer, EuiText, htmlIdGenerator } from '@elastic/eui'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { FormattedMessage } from '@kbn/i18n/react'; | ||
import { | ||
FIELD_ORIGIN, | ||
SOURCE_TYPES, | ||
STYLE_TYPE, | ||
COLOR_MAP_TYPE, | ||
} from '../../../../../../../maps/common/constants'; | ||
import { EMSTermJoinConfig } from '../../../../../../../maps/public'; | ||
import { FieldVisStats } from '../../types'; | ||
import { VectorLayerDescriptor } from '../../../../../../../maps/common/descriptor_types'; | ||
import { EmbeddedMapComponent } from '../../../embedded_map'; | ||
|
||
export const getChoroplethTopValuesLayer = ( | ||
fieldName: string, | ||
topValues: Array<{ key: any; doc_count: number }>, | ||
{ layerId, field }: EMSTermJoinConfig | ||
): VectorLayerDescriptor => { | ||
return { | ||
id: htmlIdGenerator()(), | ||
label: i18n.translate('xpack.fileDataVisualizer.choroplethMap.topValuesCount', { | ||
defaultMessage: 'Top values count for {fieldName}', | ||
values: { fieldName }, | ||
}), | ||
joins: [ | ||
{ | ||
// Left join is the id from the type of field (e.g. world_countries) | ||
leftField: field, | ||
right: { | ||
id: 'anomaly_count', | ||
type: SOURCE_TYPES.TABLE_SOURCE, | ||
__rows: topValues, | ||
__columns: [ | ||
{ | ||
name: 'key', | ||
type: 'string', | ||
}, | ||
{ | ||
name: 'doc_count', | ||
type: 'number', | ||
}, | ||
], | ||
// Right join/term is the field in the doc you’re trying to join it to (foreign key - e.g. US) | ||
term: 'key', | ||
}, | ||
}, | ||
], | ||
sourceDescriptor: { | ||
type: 'EMS_FILE', | ||
id: layerId, | ||
}, | ||
style: { | ||
type: 'VECTOR', | ||
// @ts-ignore missing style properties. Remove once 'VectorLayerDescriptor' type is updated | ||
properties: { | ||
icon: { type: STYLE_TYPE.STATIC, options: { value: 'marker' } }, | ||
fillColor: { | ||
type: STYLE_TYPE.DYNAMIC, | ||
options: { | ||
color: 'Blue to Red', | ||
colorCategory: 'palette_0', | ||
fieldMetaOptions: { isEnabled: true, sigma: 3 }, | ||
type: COLOR_MAP_TYPE.ORDINAL, | ||
field: { | ||
name: 'doc_count', | ||
origin: FIELD_ORIGIN.JOIN, | ||
}, | ||
useCustomColorRamp: false, | ||
}, | ||
}, | ||
lineColor: { | ||
type: STYLE_TYPE.DYNAMIC, | ||
options: { fieldMetaOptions: { isEnabled: true } }, | ||
}, | ||
lineWidth: { type: STYLE_TYPE.STATIC, options: { size: 1 } }, | ||
}, | ||
isTimeAware: true, | ||
}, | ||
type: 'VECTOR', | ||
}; | ||
}; | ||
|
||
interface Props { | ||
stats: FieldVisStats | undefined; | ||
suggestion: EMSTermJoinConfig; | ||
} | ||
|
||
export const ChoroplethMap: FC<Props> = ({ stats, suggestion }) => { | ||
const { fieldName, isTopValuesSampled, topValues, topValuesSamplerShardSize } = stats!; | ||
|
||
const layerList: VectorLayerDescriptor[] = useMemo( | ||
() => [getChoroplethTopValuesLayer(fieldName || '', topValues || [], suggestion)], | ||
[suggestion, fieldName, topValues] | ||
); | ||
|
||
return ( | ||
<EuiFlexItem data-test-subj={'fileDataVisualizerChoroplethMapTopValues'}> | ||
<div style={{ width: '100%', minHeight: 300 }}> | ||
<EmbeddedMapComponent layerList={layerList} /> | ||
</div> | ||
{isTopValuesSampled === true && ( | ||
<> | ||
<EuiSpacer size="xs" /> | ||
<EuiText size="xs" textAlign={'left'}> | ||
<FormattedMessage | ||
id="xpack.fileDataVisualizer.fieldDataCardExpandedRow.choroplethMapTopValues.calculatedFromSampleDescription" | ||
defaultMessage="Calculated from sample of {topValuesSamplerShardSize} documents per shard" | ||
values={{ | ||
topValuesSamplerShardSize, | ||
}} | ||
/> | ||
</EuiText> | ||
</> | ||
)} | ||
</EuiFlexItem> | ||
); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export const COMMON_EMS_LAYER_IDS = [ | ||
'world_countries', | ||
'administrative_regions_lvl2', | ||
'usa_zip_codes', | ||
'usa_states', | ||
]; |
126 changes: 126 additions & 0 deletions
126
...on/datavisualizer/index_based/components/field_data_row/choropleth_map/choropleth_map.tsx
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,126 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React, { FC, useMemo } from 'react'; | ||
import { EuiFlexItem, EuiSpacer, EuiText, htmlIdGenerator } from '@elastic/eui'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { FormattedMessage } from '@kbn/i18n/react'; | ||
import { | ||
FIELD_ORIGIN, | ||
SOURCE_TYPES, | ||
STYLE_TYPE, | ||
COLOR_MAP_TYPE, | ||
} from '../../../../../../../../maps/common/constants'; | ||
import { EMSTermJoinConfig } from '../../../../../../../../maps/public'; | ||
import { FieldVisStats } from '../../../../stats_table/types'; | ||
import { VectorLayerDescriptor } from '../../../../../../../../maps/common/descriptor_types'; | ||
import { MlEmbeddedMapComponent } from '../../../../../components/ml_embedded_map'; | ||
|
||
export const getChoroplethTopValuesLayer = ( | ||
fieldName: string, | ||
topValues: Array<{ key: any; doc_count: number }>, | ||
{ layerId, field }: EMSTermJoinConfig | ||
): VectorLayerDescriptor => { | ||
return { | ||
id: htmlIdGenerator()(), | ||
label: i18n.translate('xpack.ml.dataviz.choroplethMap.topValuesCount', { | ||
defaultMessage: 'Top values count for {fieldName}', | ||
values: { fieldName }, | ||
}), | ||
joins: [ | ||
{ | ||
// Left join is the id from the type of field (e.g. world_countries) | ||
leftField: field, | ||
right: { | ||
id: 'anomaly_count', | ||
type: SOURCE_TYPES.TABLE_SOURCE, | ||
__rows: topValues, | ||
__columns: [ | ||
{ | ||
name: 'key', | ||
type: 'string', | ||
}, | ||
{ | ||
name: 'doc_count', | ||
type: 'number', | ||
}, | ||
], | ||
// Right join/term is the field in the doc you’re trying to join it to (foreign key - e.g. US) | ||
term: 'key', | ||
}, | ||
}, | ||
], | ||
sourceDescriptor: { | ||
type: 'EMS_FILE', | ||
id: layerId, | ||
}, | ||
style: { | ||
type: 'VECTOR', | ||
// @ts-ignore missing style properties. Remove once 'VectorLayerDescriptor' type is updated | ||
properties: { | ||
icon: { type: STYLE_TYPE.STATIC, options: { value: 'marker' } }, | ||
fillColor: { | ||
type: STYLE_TYPE.DYNAMIC, | ||
options: { | ||
color: 'Blue to Red', | ||
colorCategory: 'palette_0', | ||
fieldMetaOptions: { isEnabled: true, sigma: 3 }, | ||
type: COLOR_MAP_TYPE.ORDINAL, | ||
field: { | ||
name: 'doc_count', | ||
origin: FIELD_ORIGIN.JOIN, | ||
}, | ||
useCustomColorRamp: false, | ||
}, | ||
}, | ||
lineColor: { | ||
type: STYLE_TYPE.DYNAMIC, | ||
options: { fieldMetaOptions: { isEnabled: true } }, | ||
}, | ||
lineWidth: { type: STYLE_TYPE.STATIC, options: { size: 1 } }, | ||
}, | ||
isTimeAware: true, | ||
}, | ||
type: 'VECTOR', | ||
}; | ||
}; | ||
|
||
interface Props { | ||
stats: FieldVisStats | undefined; | ||
suggestion: EMSTermJoinConfig; | ||
} | ||
|
||
export const ChoroplethMap: FC<Props> = ({ stats, suggestion }) => { | ||
const { fieldName, isTopValuesSampled, topValues, topValuesSamplerShardSize } = stats!; | ||
|
||
const layerList: VectorLayerDescriptor[] = useMemo( | ||
() => [getChoroplethTopValuesLayer(fieldName || '', topValues || [], suggestion)], | ||
[suggestion, stats] | ||
); | ||
|
||
return ( | ||
<EuiFlexItem data-test-subj={'mlChoroplethMapTopValues'}> | ||
<div style={{ width: '100%', minHeight: 300 }}> | ||
<MlEmbeddedMapComponent layerList={layerList} /> | ||
</div> | ||
{isTopValuesSampled === true && ( | ||
<> | ||
<EuiSpacer size="xs" /> | ||
<EuiText size="xs" textAlign={'left'}> | ||
<FormattedMessage | ||
id="xpack.ml.fieldDataCard.choroplethMapTopValues.calculatedFromSampleDescription" | ||
defaultMessage="Calculated from sample of {topValuesSamplerShardSize} documents per shard" | ||
values={{ | ||
topValuesSamplerShardSize, | ||
}} | ||
/> | ||
</EuiText> | ||
</> | ||
)} | ||
</EuiFlexItem> | ||
); | ||
}; |
8 changes: 8 additions & 0 deletions
8
.../application/datavisualizer/index_based/components/field_data_row/choropleth_map/index.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,8 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export { ChoroplethMap } from './choropleth_map'; |
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
Oops, something went wrong.