-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(D3 plugin): add treemap chart (#408)
* feat(D3 plugin): add treemap chart * fix: typo fixes * fix: review fixes * fix: fix jsdoc * fix: review fixes 2
- Loading branch information
Showing
23 changed files
with
635 additions
and
20 deletions.
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
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,69 @@ | ||
import React from 'react'; | ||
import {StoryObj} from '@storybook/react'; | ||
import {Button} from '@gravity-ui/uikit'; | ||
import {settings} from '../../../../libs'; | ||
import {ChartKit} from '../../../../components/ChartKit'; | ||
import type {ChartKitRef} from '../../../../types'; | ||
import type {ChartKitWidgetData} from '../../../../types/widget-data'; | ||
import {D3Plugin} from '../..'; | ||
|
||
const prepareData = (): ChartKitWidgetData => { | ||
return { | ||
series: { | ||
data: [ | ||
{ | ||
type: 'treemap', | ||
name: 'Example', | ||
dataLabels: { | ||
enabled: true, | ||
}, | ||
layoutAlgorithm: 'binary', | ||
levels: [{index: 1}, {index: 2}, {index: 3}], | ||
data: [ | ||
{name: 'One', value: 15}, | ||
{name: 'Two', value: 10}, | ||
{name: 'Three', value: 15}, | ||
{name: 'Four'}, | ||
{name: 'Four-1', value: 5, parentId: 'Four'}, | ||
{name: 'Four-2', parentId: 'Four'}, | ||
{name: 'Four-3', value: 4, parentId: 'Four'}, | ||
{name: 'Four-2-1', value: 5, parentId: 'Four-2'}, | ||
{name: 'Four-2-2', value: 7, parentId: 'Four-2'}, | ||
{name: 'Four-2-3', value: 10, parentId: 'Four-2'}, | ||
], | ||
}, | ||
], | ||
}, | ||
}; | ||
}; | ||
|
||
const ChartStory = ({data}: {data: ChartKitWidgetData}) => { | ||
const [shown, setShown] = React.useState(false); | ||
const chartkitRef = React.useRef<ChartKitRef>(); | ||
|
||
if (!shown) { | ||
settings.set({plugins: [D3Plugin]}); | ||
return <Button onClick={() => setShown(true)}>Show chart</Button>; | ||
} | ||
|
||
return ( | ||
<div style={{height: '300px', width: '100%'}}> | ||
<ChartKit ref={chartkitRef} type="d3" data={data} /> | ||
</div> | ||
); | ||
}; | ||
|
||
export const TreemapPlayground: StoryObj<typeof ChartStory> = { | ||
name: 'Playground', | ||
args: {data: prepareData()}, | ||
argTypes: { | ||
data: { | ||
control: 'object', | ||
}, | ||
}, | ||
}; | ||
|
||
export default { | ||
title: 'Plugins/D3/Treemap', | ||
component: ChartStory, | ||
}; |
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
48 changes: 48 additions & 0 deletions
48
src/plugins/d3/renderer/hooks/useSeries/prepare-treemap.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,48 @@ | ||
import type {ScaleOrdinal} from 'd3'; | ||
import get from 'lodash/get'; | ||
|
||
import {LayoutAlgorithm} from '../../../../../constants'; | ||
import type {ChartKitWidgetSeriesOptions, TreemapSeries} from '../../../../../types'; | ||
import {getRandomCKId} from '../../../../../utils'; | ||
|
||
import {DEFAULT_DATALABELS_PADDING, DEFAULT_DATALABELS_STYLE} from './constants'; | ||
import type {PreparedLegend, PreparedTreemapSeries} from './types'; | ||
import {prepareLegendSymbol} from './utils'; | ||
|
||
type PrepareTreemapSeriesArgs = { | ||
colorScale: ScaleOrdinal<string, string>; | ||
legend: PreparedLegend; | ||
series: TreemapSeries[]; | ||
seriesOptions?: ChartKitWidgetSeriesOptions; | ||
}; | ||
|
||
export function prepareTreemap(args: PrepareTreemapSeriesArgs) { | ||
const {colorScale, legend, series} = args; | ||
|
||
return series.map<PreparedTreemapSeries>((s) => { | ||
const id = getRandomCKId(); | ||
const name = s.name || ''; | ||
const color = s.color || colorScale(name); | ||
|
||
return { | ||
color, | ||
data: s.data, | ||
dataLabels: { | ||
enabled: get(s, 'dataLabels.enabled', true), | ||
style: Object.assign({}, DEFAULT_DATALABELS_STYLE, s.dataLabels?.style), | ||
padding: get(s, 'dataLabels.padding', DEFAULT_DATALABELS_PADDING), | ||
allowOverlap: get(s, 'dataLabels.allowOverlap', false), | ||
}, | ||
id, | ||
type: s.type, | ||
name, | ||
visible: get(s, 'visible', true), | ||
legend: { | ||
enabled: get(s, 'legend.enabled', legend.enabled), | ||
symbol: prepareLegendSymbol(s), | ||
}, | ||
levels: s.levels, | ||
layoutAlgorithm: get(s, 'layoutAlgorithm', LayoutAlgorithm.Binary), | ||
}; | ||
}); | ||
} |
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
Oops, something went wrong.