Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"@gravity-ui/date-utils": "^2.5.4",
"@gravity-ui/i18n": "^1.6.0",
"d3": "^7.9.0",
"d3-sankey": "^0.12.3",
"lodash": "^4.17.21",
"tslib": "^2.6.2"
},
Expand Down Expand Up @@ -93,6 +94,7 @@
"@testing-library/react": "^16.0.0",
"@testing-library/user-event": "^14.5.2",
"@types/d3": "^7.4.3",
"@types/d3-sankey": "^0.12.4",
"@types/d3-selection": "^3.0.11",
"@types/jest": "^29.5.12",
"@types/lodash": "^4.17.12",
Expand Down
30 changes: 30 additions & 0 deletions src/__stories__/Sankey/Sankey.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type {Meta, StoryObj} from '@storybook/react';

import {ChartStory} from '../ChartStory';
import {sankeyPlaygroundData} from '../__data__';

const meta: Meta<typeof ChartStory> = {
title: 'Sankey',
component: ChartStory,
};

export default meta;

type Story = StoryObj<typeof ChartStory>;

export const SankeyPlayground = {
name: 'Playground',
args: {
data: sankeyPlaygroundData,
wrapperProps: {
styles: {
height: 560,
},
},
},
argTypes: {
data: {
control: 'object',
},
},
} satisfies Story;
20 changes: 13 additions & 7 deletions src/__stories__/Showcase.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ import {
lineTwoYAxisData,
pieBasicData,
pieDonutData,
sankeyPlaygroundData,
scatterBasicData,
scatterTwoYAxisData,
treemapPlaygroundData,
} from './__data__';

const ShowcaseStory = () => {
Expand Down Expand Up @@ -171,15 +173,19 @@ const ShowcaseStory = () => {
<ChartStory data={scatterTwoYAxisData} />
</Col>
</Row>
{/* <Row space={1}>
<Text variant="header-2">Combined charts</Text>
<Row space={1}>
<Text variant="header-2">Other</Text>
</Row>
<Row space={3} style={{minHeight: 280}}>
<Col s={12}>
<Text variant="subheader-1">Line + Bar-X</Text>
<LineAndBarXCombinedChart />
<Row space={3}>
<Col s={12} m={6} l={6}>
<Text variant="subheader-1">Treemap</Text>
<ChartStory data={treemapPlaygroundData} />
</Col>
</Row> */}
<Col s={12} m={6} l={6}>
<Text variant="subheader-1">Sankey</Text>
<ChartStory data={sankeyPlaygroundData} />
</Col>
</Row>
</Container>
</div>
);
Expand Down
1 change: 1 addition & 0 deletions src/__stories__/__data__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './pie';
export * from './scatter';
export * from './treemap';
export * from './waterfall';
export * from './sankey';
1 change: 1 addition & 0 deletions src/__stories__/__data__/sankey/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './playground';
46 changes: 46 additions & 0 deletions src/__stories__/__data__/sankey/playground.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {groups, sort} from 'd3';

import type {ChartData} from '../../../types';
import nintendoGames from '../nintendoGames';

function prepareData(): ChartData {
const gamesWithScore = nintendoGames.filter((game) => Math.round(game.user_score ?? 0));
const gamesByPlatform = groups(gamesWithScore, (item) => item.platform);
const data = gamesByPlatform.map(([platform, games]) => {
const links = sort(
groups(games, (game) => Math.round(game.user_score ?? 0)).map(([score, items]) => ({
name: String(score),
value: items.length,
})),
(d) => d.name,
);

return {
name: platform,
links,
};
});

const scores = sort(
Array.from(new Set(gamesWithScore.map((game) => Math.round(game.user_score ?? 0)))),
(d) => d,
).map((d) => ({name: String(d), links: []}));

return {
title: {
text: 'Average user score by platform',
},
tooltip: {enabled: true},
series: {
data: [
{
type: 'sankey',
data: [...data, ...scores],
name: 'Series 1',
},
],
},
};
}

export const sankeyPlaygroundData = prepareData();
4 changes: 4 additions & 0 deletions src/components/ChartInner/useChartInnerHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export function useChartInnerHandlers(props: Props) {
const closest = getClosestPoints({
position: [x, y],
shapesData,
boundsHeight,
boundsWidth,
});
dispatcher.call(EventType.HOVER_SHAPE, event.target, closest, [pointerX, pointerY]);
dispatcher.call(
Expand Down Expand Up @@ -128,6 +130,8 @@ export function useChartInnerHandlers(props: Props) {
const items = getClosestPoints({
position: [x, y],
shapesData,
boundsHeight,
boundsWidth,
});
const selected = items?.find((item) => item.closest);
if (!selected) {
Expand Down
26 changes: 23 additions & 3 deletions src/components/Tooltip/DefaultContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import type {
ChartXAxis,
ChartYAxis,
TooltipDataChunk,
TooltipDataChunkSankey,
TreemapSeriesData,
WaterfallSeriesData,
} from '../../types';
import {block, getDataCategoryValue, getWaterfallPointSubtotal} from '../../utils';

const b = block('d3-tooltip');
const b = block('tooltip');

type Props = {
hovered: TooltipDataChunk[];
Expand Down Expand Up @@ -55,7 +56,9 @@ const getXRowData = (data: ChartSeriesData, xAxis?: ChartXAxis) => getRowData('x
const getYRowData = (data: ChartSeriesData, yAxis?: ChartYAxis) => getRowData('y', data, yAxis);

const getMeasureValue = (data: TooltipDataChunk[], xAxis?: ChartXAxis, yAxis?: ChartYAxis) => {
if (data.every((item) => ['pie', 'treemap', 'waterfall'].includes(item.series.type))) {
if (
data.every((item) => ['pie', 'treemap', 'waterfall', 'sankey'].includes(item.series.type))
) {
return null;
}

Expand All @@ -72,7 +75,8 @@ export const DefaultContent = ({hovered, xAxis, yAxis}: Props) => {
return (
<React.Fragment>
{measureValue && <div>{measureValue}</div>}
{hovered.map(({data, series, closest}, i) => {
{hovered.map((seriesItem, i) => {
const {data, series, closest} = seriesItem;
const id = `${get(series, 'id')}_${i}`;
const color = get(series, 'color');

Expand Down Expand Up @@ -144,6 +148,22 @@ export const DefaultContent = ({hovered, xAxis, yAxis}: Props) => {
</div>
);
}
case 'sankey': {
const {target, data: source} = seriesItem as TooltipDataChunkSankey;
const value = source.links.find((d) => d.name === target?.name)?.value;

return (
<div key={id} className={b('content-row')}>
<div
className={b('color')}
style={{backgroundColor: source.color}}
/>
<div style={{display: 'flex', gap: 8, verticalAlign: 'center'}}>
{source.name} <span>→</span> {target?.name}: {value}
</div>
</div>
);
}
default: {
return null;
}
Expand Down
1 change: 1 addition & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const SeriesType = {
Scatter: 'scatter',
Treemap: 'treemap',
Waterfall: 'waterfall',
Sankey: 'sankey',
} as const;

export enum DashStyle {
Expand Down
50 changes: 50 additions & 0 deletions src/hooks/useSeries/prepare-sankey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type {ScaleOrdinal} from 'd3';
import get from 'lodash/get';

import type {ChartSeriesOptions, SankeySeries} from '../../types';
import {getUniqId} from '../../utils';

import {DEFAULT_DATALABELS_STYLE} from './constants';
import type {PreparedLegend, PreparedSankeySeries} from './types';
import {prepareLegendSymbol} from './utils';

type PrepareSankeySeriesArgs = {
colorScale: ScaleOrdinal<string, string>;
legend: PreparedLegend;
series: SankeySeries[];
seriesOptions?: ChartSeriesOptions;
};

export function prepareSankeySeries(args: PrepareSankeySeriesArgs) {
const {colorScale, legend, series} = args;

return series.map<PreparedSankeySeries>((s) => {
const id = getUniqId();
const name = s.name || '';
const color = colorScale(name);

const preparedSeries: PreparedSankeySeries = {
color,
data: s.data.map((d) => ({
name: d.name,
color: d.color ?? colorScale(d.name),
links: d.links,
})),
dataLabels: {
enabled: get(s, 'dataLabels.enabled', true),
style: Object.assign({}, DEFAULT_DATALABELS_STYLE, s.dataLabels?.style),
},
id,
type: s.type,
name,
visible: get(s, 'visible', true),
legend: {
enabled: get(s, 'legend.enabled', legend.enabled),
symbol: prepareLegendSymbol(s),
},
cursor: get(s, 'cursor', null),
};

return preparedSeries;
});
}
10 changes: 10 additions & 0 deletions src/hooks/useSeries/prepareSeries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
ChartSeriesOptions,
LineSeries,
PieSeries,
SankeySeries,
ScatterSeries,
TreemapSeries,
WaterfallSeries,
Expand All @@ -19,6 +20,7 @@ import {prepareBarXSeries} from './prepare-bar-x';
import {prepareBarYSeries} from './prepare-bar-y';
import {prepareLineSeries} from './prepare-line';
import {preparePieSeries} from './prepare-pie';
import {prepareSankeySeries} from './prepare-sankey';
import {prepareScatterSeries} from './prepare-scatter';
import {prepareTreemap} from './prepare-treemap';
import {prepareWaterfallSeries} from './prepare-waterfall';
Expand Down Expand Up @@ -87,6 +89,14 @@ export function prepareSeries(args: {
colorScale,
});
}
case 'sankey': {
return prepareSankeySeries({
series: series as SankeySeries[],
seriesOptions,
colorScale,
legend,
});
}
default: {
throw new ChartError({
message: `Series type "${type}" does not support data preparation for series that do not support the presence of axes`,
Expand Down
Loading
Loading