Skip to content
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

Improve/refactor alluvial code #235

Merged
merged 2 commits into from
Aug 31, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export const MultipleLevelsWithColorDerivationFromIncomingLinks: Story = {
}),
args: {
...DATASETS.MultipleLevelsWithColorDerivationFromIncomingLinks,
options: { gradient: true },
title: 'Color derivation from incoming links',
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ export function useAlluvialGraph(

const links: ComputedRef<
Array<D3SankeyLink<SankeyNodeProps, SankeyLinkProps>>
> = computed(() => {
return data.value?.[0].values
> = computed(() =>
data.value?.[0].values
.map((source) =>
source.targets?.map(
({ node: target, value, color, curveFunction }) => ({
id: `${source.value}:${target}`,
color,
curveFunction,
source: source.value ?? source.label,
Expand All @@ -58,8 +59,8 @@ export function useAlluvialGraph(
)
)
.filter(Boolean)
.flat();
});
.flat()
);

const layout = computed(() => {
return sankey<SankeyNodeProps, SankeyLinkProps>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ import { SankeyNode } from 'd3-sankey';

import { AlluvialDiagramOptions } from '@/composables/options';

import {
generateLinkId,
getAlluvialNodeId,
isSankeyNode,
updateNode,
} from '../helpers';
import { getAlluvialNodeId, isSankeyNode, updateNode } from '../helpers';

import {
GetHighlightedElementsFunction,
Expand All @@ -29,7 +24,10 @@ function getSourceNodes(
if (node.targetLinks) {
node.targetLinks.forEach(({ source, value }) => {
group[(source as SankeyNode<SankeyNodeProps, unknown>).id] = value;
getSourceNodes(source as SankeyNode<SankeyNodeProps, unknown>, group);
getSourceNodes(
source as SankeyNode<SankeyNodeProps, SankeyLinkProps>,
group
);
});
}

Expand All @@ -45,7 +43,10 @@ function getTargetNodes(
if (node.sourceLinks) {
node.sourceLinks.forEach(({ target, value }) => {
group[(target as SankeyNode<SankeyNodeProps, unknown>).id] = value;
getTargetNodes(target as SankeyNode<SankeyNodeProps, unknown>, group);
getTargetNodes(
target as SankeyNode<SankeyNodeProps, SankeyLinkProps>,
group
);
});
}

Expand All @@ -58,7 +59,7 @@ function getLinkIdsFromNodes(
): Array<string> {
return graph.links.reduce((array, link) => {
if (link.source.id in nodes && link.target.id in nodes) {
array.push(generateLinkId(link));
array.push(link.id);
govind-srinidhi marked this conversation as resolved.
Show resolved Hide resolved
}
return array;
}, []);
Expand Down Expand Up @@ -106,7 +107,7 @@ const getClosestHighlightedElements: GetHighlightedElementsFunction = (
},
{ [getAlluvialNodeId(element)]: element.value }
),
links: links.map((l) => generateLinkId(l)),
links: links.map(({ id }) => id),
};
}

Expand All @@ -118,7 +119,7 @@ const getClosestHighlightedElements: GetHighlightedElementsFunction = (
element.source as SankeyNode<SankeyNodeProps, SankeyLinkProps>
).value,
},
links: [generateLinkId(element)],
links: [element.id],
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,16 @@ export function getAlluvialNodeId(
return node.id ?? node.label;
}

export function generateLinkId(
link: SankeyLink<SankeyNodeProps, SankeyLinkProps>
) {
return link.source.id + ':' + link.target.id;
}

export function getNodeById(id: number | string, graph: SankeyGraph) {
return graph.nodes.find((node) => node.id === id);
}

export function getLinkById(id: string, graph: SankeyGraph) {
return graph.links.find((link) => generateLinkId(link) === id);
return graph.links.find((link) => link.id === id);
}

export function isSankeyNode(
element:
| SankeyNode<SankeyNodeProps, unknown>
| SankeyLink<SankeyNodeProps, unknown>
element: SankeyNode<SankeyNodeProps, unknown> | SankeyLink
): element is SankeyNode<SankeyNodeProps, unknown> {
return Boolean(
(element as SankeyNode<unknown, unknown>).sourceLinks &&
Expand Down Expand Up @@ -126,7 +118,7 @@ export function getLinkPathAttributes(
links: Array<SankeyLink>
): Array<LinkPath> {
return links.map((link) => ({
id: generateLinkId(link),
id: link.id,
color:
link.color ||
(link.source as SankeyNode<SankeyNodeProps, SankeyLinkProps>)?.color ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ import { Color } from '@/utils/constants';
import { warn, Warnings } from '@/utils/warnings';
import { GHOST_STROKE_WIDTH_OFFSET, NODE_HEADER_PADDING } from './constants';
import {
generateLinkId,
getLabelSizes,
getLinkById,
getLinkPathAttributes,
Expand Down Expand Up @@ -272,11 +271,20 @@ const gradients = computed(() => {
if (!options.value.gradient || !graph.value) return;

return graph.value.links.reduce((acc, link) => {
acc[generateLinkId(link)] = {
source: link.source.color || link.source.fallbackColor,
target: link.target.color || link.target.fallbackColor,
x1: link.source.x1,
x2: link.target.x0,
const { source, target } = link;

const sourceColor = source.color || source.fallbackColor;

// If `deriveColorFromIncomingLinks`, use the source's color for the gradient
const targetColor = target.deriveColorFromIncomingLinks
? sourceColor
: target.color || target.fallbackColor;

acc[link.id] = {
source: sourceColor,
target: targetColor,
x1: source.x1,
x2: target.x0,
};
return acc;
}, {}) as Record<
Expand Down Expand Up @@ -328,8 +336,8 @@ const nodesDerivingColorFromIncomingLinks = computed(() => {
return subNodeDetails;
});

function getLinkStroke(link: SankeyLink<SankeyNodeProps, SankeyLinkProps>) {
return options.value.gradient && `url('#${chartID}_${generateLinkId(link)}')`;
function getLinkStroke(link: SankeyLink) {
return options.value.gradient && `url('#${chartID}_${link.id}')`;
}

function isNodeFaded(id: string | number) {
Expand Down
5 changes: 3 additions & 2 deletions packages/lib/src/composables/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { mergeDeep } from '@/utils/helpers';
import {
GetHighlightedElementsFunction,
SankeyLink,
SankeyLinkProps,
SankeyNodeProps,
} from '@/types/alluvial';
import { ColorPalette } from '@/types/dataset';
Expand Down Expand Up @@ -79,8 +80,8 @@ export interface AlluvialDiagramOptions extends ChartOptions {
) => number | undefined | null;
nodeWidth?: number;
linkSort?: (
a: SankeyLink<SankeyNodeProps, unknown>,
b: SankeyLink<SankeyNodeProps, unknown>
a: SankeyLink<SankeyNodeProps, SankeyLinkProps>,
b: SankeyLink<SankeyNodeProps, SankeyLinkProps>
) => number | undefined | null;
switchText?: boolean;
valueFormat?: Format;
Expand Down
1 change: 1 addition & 0 deletions packages/lib/src/docs/storybook-data/alluvial-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ const DATASETS = {
{
label: 'E',
value: 'E',
color: 'royalblue',
targets: [
{ node: 'G', value: 10 },
{ node: 'H', value: 5, color: 'darkteal' },
Expand Down
1 change: 1 addition & 0 deletions packages/lib/src/types/alluvial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface SankeyNodeProps extends SankeyExtraProperties {

export interface SankeyLinkProps extends SankeyExtraProperties {
color?: Color;
id: string;
x0?: number | undefined;
x1?: number | undefined;
curveFunction?: CurveFactory;
Expand Down
Loading