-
Notifications
You must be signed in to change notification settings - Fork 41
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
Add treemap visualization #1709
Open
kuzmadom
wants to merge
11
commits into
main
Choose a base branch
from
CHARTS-10538
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1138f8b
Add treemap visualization
kuzmadom 94fd5f2
fix type
kuzmadom 5029da7
fix lint
kuzmadom cdaa28a
tooltip + export
kuzmadom 284025a
Md in labels
kuzmadom f9a6343
add chart to Ql
kuzmadom 6c21edb
fix
kuzmadom 22c3372
Merge branch 'main' into CHARTS-10538
kuzmadom 4cfc209
fix unit test
kuzmadom c3d692e
fix ql test
kuzmadom 8f57bd9
Merge branch 'main' into CHARTS-10538
kuzmadom 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
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
262 changes: 262 additions & 0 deletions
262
src/server/modes/charts/plugins/datalens/preparers/treemap/d3.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,262 @@ | ||
import type { | ||
ChartKitWidgetData, | ||
TreemapSeries, | ||
TreemapSeriesData, | ||
} from '@gravity-ui/chartkit/build/types/widget-data'; | ||
import escape from 'lodash/escape'; | ||
|
||
import type { | ||
ColumnExportSettings, | ||
SeriesExportSettings, | ||
WrappedMarkdown, | ||
} from '../../../../../../../shared'; | ||
import { | ||
Feature, | ||
MINIMUM_FRACTION_DIGITS, | ||
PlaceholderId, | ||
isDateField, | ||
isMarkdownField, | ||
} from '../../../../../../../shared'; | ||
import {wrapMarkdownValue} from '../../../../../../../shared/utils/markdown'; | ||
import { | ||
mapAndColorizeHashTableByGradient, | ||
mapAndColorizeHashTableByPalette, | ||
} from '../../utils/color-helpers'; | ||
import {getExportColumnSettings} from '../../utils/export-helpers'; | ||
import { | ||
chartKitFormatNumberWrapper, | ||
findIndexInOrder, | ||
formatDate, | ||
isGradientMode, | ||
isNumericalDataType, | ||
} from '../../utils/misc-helpers'; | ||
import type {PrepareFunctionArgs} from '../types'; | ||
|
||
type TreemapItemName = string | null | WrappedMarkdown; | ||
type ExtendedTreemapSeriesData = Omit<TreemapSeriesData, 'name'> & { | ||
name: TreemapItemName | TreemapItemName[]; | ||
drillDownFilterValue: unknown; | ||
label?: string; | ||
}; | ||
|
||
type ExtendedTreemapSeries = TreemapSeries & { | ||
custom?: { | ||
exportSettings?: SeriesExportSettings; | ||
}; | ||
}; | ||
|
||
export function prepareD3Treemap({ | ||
placeholders, | ||
resultData, | ||
colors, | ||
colorsConfig, | ||
idToTitle, | ||
idToDataType, | ||
features, | ||
ChartEditor, | ||
}: PrepareFunctionArgs): Partial<ChartKitWidgetData> { | ||
const dimensions = placeholders.find((p) => p.id === PlaceholderId.Dimensions)?.items ?? []; | ||
const dTypes = dimensions.map((item) => item.data_type); | ||
const useMarkdown = dimensions?.some(isMarkdownField); | ||
|
||
const measures = placeholders.find((p) => p.id === PlaceholderId.Measures)?.items ?? []; | ||
|
||
const color = colors[0]; | ||
const colorFieldDataType = color ? idToDataType[color.guid] : null; | ||
|
||
const gradientMode = | ||
color && | ||
colorFieldDataType && | ||
isGradientMode({colorField: color, colorFieldDataType, colorsConfig}); | ||
|
||
const {data, order} = resultData; | ||
|
||
let treemap: ExtendedTreemapSeriesData[] = []; | ||
const treemapIds: string[] = []; | ||
const hashTable: Record<string, {value: string | null; label: string}> = {}; | ||
const valuesForColorData: Record<string, number> & {colorGuid?: string} = {}; | ||
const isFloat = measures[0] && measures[0].data_type === 'float'; | ||
const shouldEscapeUserValue = features[Feature.EscapeUserHtmlInDefaultHcTooltip]; | ||
let colorData: Record<string, {backgroundColor: string}> = {}; | ||
|
||
if (color) { | ||
// We make the property non-enumerable so that it does not participate in the formation of the palette | ||
Object.defineProperty(valuesForColorData, 'colorGuid', { | ||
enumerable: false, | ||
value: color.guid, | ||
}); | ||
} | ||
|
||
data.forEach((values) => { | ||
let colorByDimension: string | null; | ||
if (color && color.type === 'DIMENSION') { | ||
const actualTitle = idToTitle[color.guid]; | ||
const i = findIndexInOrder(order, color, actualTitle); | ||
const colorValue = values[i]; | ||
|
||
colorByDimension = colorValue; | ||
} | ||
|
||
const dPath: (string | null)[] = []; | ||
let lastDimensionItem: ExtendedTreemapSeriesData | undefined; | ||
dimensions.forEach((item, level) => { | ||
if (item.type === 'PSEUDO') { | ||
return; | ||
} | ||
|
||
const actualTitle = idToTitle[item.guid]; | ||
|
||
const i = findIndexInOrder(order, item, actualTitle); | ||
|
||
const rawValue = values[i]; | ||
let value: string | null; | ||
|
||
if (isDateField({data_type: dTypes[level]})) { | ||
value = formatDate({ | ||
valueType: dTypes[level], | ||
value: rawValue, | ||
format: item.format, | ||
}); | ||
} else if (isNumericalDataType(dTypes[level]) && item.formatting) { | ||
value = chartKitFormatNumberWrapper(rawValue as unknown as number, { | ||
lang: 'ru', | ||
...item.formatting, | ||
}); | ||
} else { | ||
value = rawValue && shouldEscapeUserValue ? escape(rawValue as string) : rawValue; | ||
} | ||
|
||
const treemapId = | ||
dPath.length >= 1 ? `id_${dPath[0]}/${value}` : `id_${dPath.join()}${value}`; | ||
|
||
const name = isMarkdownField(item) ? wrapMarkdownValue(value as string) : value; | ||
|
||
const treemapItem: ExtendedTreemapSeriesData = { | ||
id: treemapId, | ||
name, | ||
drillDownFilterValue: value, | ||
}; | ||
|
||
if (dPath.length) { | ||
treemapItem.parentId = `id_${dPath.join('/')}`; | ||
} | ||
|
||
dPath.push(value); | ||
|
||
treemapItem.id = `id_${dPath.join('/')}`; | ||
|
||
if (level === dimensions.length - 1) { | ||
lastDimensionItem = treemapItem; | ||
} else if (!treemapIds.includes(treemapItem.id)) { | ||
treemap.push(treemapItem); | ||
treemapIds.push(treemapItem.id); | ||
} | ||
}); | ||
|
||
const key = `id_${dPath.join('/')}`; | ||
measures.forEach((measureItem) => { | ||
const actualTitle = idToTitle[measureItem.guid]; | ||
const i = findIndexInOrder(order, measureItem, actualTitle); | ||
const value = values[i]; | ||
const label = chartKitFormatNumberWrapper(Number(value), { | ||
lang: 'ru', | ||
...(measureItem.formatting ?? {precision: isFloat ? MINIMUM_FRACTION_DIGITS : 0}), | ||
}); | ||
|
||
hashTable[key] = {value: value, label}; | ||
|
||
if (color) { | ||
if (gradientMode) { | ||
const colorTitle = idToTitle[color.guid]; | ||
const i = findIndexInOrder(order, color, colorTitle); | ||
const colorValue = values[i]; | ||
|
||
valuesForColorData[key] = colorValue as unknown as number; | ||
} else { | ||
valuesForColorData[key] = colorByDimension as unknown as number; | ||
} | ||
} | ||
}); | ||
|
||
if (lastDimensionItem) { | ||
lastDimensionItem.value = Number(hashTable[key]?.value); | ||
lastDimensionItem.label = hashTable[key]?.label; | ||
let name: any[] = dPath; | ||
if (useMarkdown) { | ||
name = dPath.map((item) => (item ? wrapMarkdownValue(item) : item)); | ||
} | ||
lastDimensionItem.name = name as any; | ||
|
||
treemap.push(lastDimensionItem); | ||
} | ||
}); | ||
|
||
if (color) { | ||
if (gradientMode) { | ||
colorData = mapAndColorizeHashTableByGradient( | ||
valuesForColorData, | ||
colorsConfig, | ||
).colorData; | ||
} else { | ||
colorData = mapAndColorizeHashTableByPalette(valuesForColorData, colorsConfig); | ||
} | ||
|
||
treemap = treemap.map((obj) => { | ||
const item = {...obj}; | ||
|
||
const colorDataValue = obj.id ? colorData[obj.id] : null; | ||
if (colorDataValue) { | ||
item.color = colorDataValue.backgroundColor; | ||
} | ||
|
||
return item; | ||
}); | ||
} | ||
|
||
const dimensionsSize = dimensions.length; | ||
const maxPadding = 5; | ||
const levels: TreemapSeries['levels'] = new Array(dimensionsSize) | ||
.fill(null) | ||
.map((_, index) => ({ | ||
index: index + 1, | ||
padding: Math.min(maxPadding, (dimensionsSize - index) * 2 - 1), | ||
})); | ||
|
||
if (useMarkdown) { | ||
ChartEditor.updateConfig({useMarkdown: true}); | ||
} | ||
|
||
const exportSettingsCols = dimensions.map<ColumnExportSettings>((field, index) => { | ||
return getExportColumnSettings({path: `name.${index}`, field}); | ||
}); | ||
exportSettingsCols.push(getExportColumnSettings({path: `value`, field: measures[0]})); | ||
|
||
const series: ExtendedTreemapSeries = { | ||
type: 'treemap', | ||
name: '', | ||
layoutAlgorithm: 'squarify' as TreemapSeries['layoutAlgorithm'], | ||
dataLabels: { | ||
enabled: true, | ||
html: useMarkdown, | ||
style: { | ||
fontColor: 'var(--g-color-text-complementary)', | ||
}, | ||
}, | ||
levels, | ||
data: treemap as TreemapSeriesData[], | ||
custom: { | ||
exportSettings: { | ||
columns: exportSettingsCols, | ||
}, | ||
}, | ||
}; | ||
|
||
return { | ||
series: { | ||
data: [series], | ||
}, | ||
legend: { | ||
enabled: false, | ||
}, | ||
}; | ||
} |
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
2 changes: 2 additions & 0 deletions
2
src/server/modes/charts/plugins/datalens/preparers/treemap/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,2 @@ | ||
export * from './d3'; | ||
export * from './highcharts'; |
Oops, something went wrong.
Oops, something went wrong.
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.
Almost a complete copy of the data preparation for highcharts chart except for the difference in data format and things that didn't make sense