Skip to content

Commit

Permalink
add titles to node and sizing
Browse files Browse the repository at this point in the history
  • Loading branch information
shaankhosla committed Oct 3, 2023
1 parent 444612f commit 55437b9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,52 @@
import { Sankey, Tooltip } from 'recharts';
import { Sankey, Tooltip, Rectangle, Layer } from 'recharts';

import Container from '../Container';
type SankeyProps = {
style;
data;
};

function SankeyNode({ x, y, width, height, index, payload, containerWidth }) {
const isOut = x + width + 6 > containerWidth;
let payloadValue = Math.round(payload.value / 1000).toString();
if (payload.value < 1000) {
payloadValue = '<1k';
} else {
payloadValue = payloadValue + 'k';
}
return (
<Layer key={`CustomNode${index}`}>
<Rectangle
x={x}
y={y}
width={width}
height={height}
fill="#5192ca"
fillOpacity="1"
/>
<text
textAnchor={isOut ? 'end' : 'start'}
x={isOut ? x - 6 : x + width + 6}
y={y + height / 2}
fontSize="14"
stroke="#333"
>
{payload.name}
</text>
<text
textAnchor={isOut ? 'end' : 'start'}
x={isOut ? x - 6 : x + width + 6}
y={y + height / 2 + 13}
fontSize="12"
stroke="#333"
strokeOpacity="0.5"
>
{payloadValue}
</text>
</Layer>
);
}

function SankeyGraph({ style, data }: SankeyProps) {
return (
<Container
Expand All @@ -20,11 +61,13 @@ function SankeyGraph({ style, data }: SankeyProps) {
width={width}
height={height}
data={data}
node={<SankeyNode />}

Check failure on line 64 in packages/desktop-client/src/components/reports/graphs/SankeyGraph.tsx

View workflow job for this annotation

GitHub Actions / typecheck

Type '{}' is missing the following properties from type '{ x: any; y: any; width: any; height: any; index: any; payload: any; containerWidth: any; }': x, y, width, height, and 3 more.
nodePadding={23}
margin={{
left: 0,
right: 0,
top: 0,
bottom: 0,
left: 100,
right: 100,
top: 25,
bottom: 25,
}}
>
<Tooltip formatter={value => Math.round(value)} />

Check failure on line 73 in packages/desktop-client/src/components/reports/graphs/SankeyGraph.tsx

View workflow job for this annotation

GitHub Actions / typecheck

Argument of type 'ValueType' is not assignable to parameter of type 'number'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,28 +109,25 @@ function transformToSankeyData(categoryData, incomeData) {
const data = { nodes: [], links: [] };
const nodeNames = new Set();

// Add the Income node first.
data.nodes.push({ name: 'Income' });
nodeNames.add('Income');
// Add the Budget node first.
data.nodes.push({ name: 'Budget' });
nodeNames.add('Budget');

// Handle the income sources and link them to the Income node.
// Handle the income sources and link them to the Budget node.
Object.entries(incomeData).forEach(([sourceName, value]) => {
if (!nodeNames.has(sourceName) && integerToAmount(value) > 0) {
data.nodes.push({ name: sourceName });
nodeNames.add(sourceName);
data.links.push({
source: sourceName,
target: 'Income',
target: 'Budget',
value: integerToAmount(value),
});
}
});

for (let mainCategory of categoryData) {
if (!nodeNames.has(mainCategory.name)) {
data.nodes.push({ name: mainCategory.name });
nodeNames.add(mainCategory.name);

if (!nodeNames.has(mainCategory.name) && mainCategory.balances.length > 0) {
let mainCategorySum = 0;
for (let subCategory of mainCategory.balances) {
if (!nodeNames.has(subCategory.subcategory) && subCategory.value > 0) {
Expand All @@ -147,8 +144,10 @@ function transformToSankeyData(categoryData, incomeData) {
}
}
if (mainCategorySum > 0) {
data.nodes.push({ name: mainCategory.name });
nodeNames.add(mainCategory.name);
data.links.push({
source: 'Income',
source: 'Budget',
target: mainCategory.name,
value: integerToAmount(mainCategorySum),
});
Expand Down

0 comments on commit 55437b9

Please sign in to comment.