Fixed majority of ts errors for sankey

This commit is contained in:
Nikolay Rozhkov
2023-06-27 16:12:41 +03:00
parent bbba643288
commit d4d7ce5a4b

View File

@@ -1,4 +1,3 @@
// @ts-nocheck
import { Diagram } from '../../Diagram.js'; import { Diagram } from '../../Diagram.js';
import * as configApi from '../../config.js'; import * as configApi from '../../config.js';
@@ -15,6 +14,7 @@ import {
sankeyRight as d3SankeyRight, sankeyRight as d3SankeyRight,
sankeyCenter as d3SankeyCenter, sankeyCenter as d3SankeyCenter,
sankeyJustify as d3SankeyJustify, sankeyJustify as d3SankeyJustify,
SankeyNode as d3SankeyNode,
} from 'd3-sankey'; } from 'd3-sankey';
import { configureSvgSize } from '../../setupGraphViewbox.js'; import { configureSvgSize } from '../../setupGraphViewbox.js';
import { Uid } from '../../rendering-util/uid.js'; import { Uid } from '../../rendering-util/uid.js';
@@ -41,11 +41,11 @@ export const draw = function (text: string, id: string, _version: string, diagOb
if (securityLevel === 'sandbox') { if (securityLevel === 'sandbox') {
sandboxElement = d3select('#i' + id); sandboxElement = d3select('#i' + id);
} }
let root = d3select('body'); const root =
securityLevel === 'sandbox'
if (securityLevel === 'sandbox' && sandboxElement) { ? d3select(sandboxElement.nodes()[0].contentDocument.body)
root = d3select(sandboxElement.nodes()[0].contentDocument.body); : d3select('body');
} // @ts-ignore TODO root.select is not callable
const svg = securityLevel === 'sandbox' ? root.select(`[id="${id}"]`) : d3select(`[id="${id}"]`); const svg = securityLevel === 'sandbox' ? root.select(`[id="${id}"]`) : d3select(`[id="${id}"]`);
// Establish svg dimensions and get width and height // Establish svg dimensions and get width and height
@@ -53,9 +53,12 @@ export const draw = function (text: string, id: string, _version: string, diagOb
const width = conf?.width || 800; const width = conf?.width || 800;
const height = conf?.height || 400; const height = conf?.height || 400;
const useMaxWidth = conf?.useMaxWidth || false; const useMaxWidth = conf?.useMaxWidth || false;
const nodeAlignment = conf?.nodeAlignment || SankeyNodeAlignment.justify;
// FIX: using max width prevents height from being set, is it intended? // FIX: using max width prevents height from being set, is it intended?
// to add height directly one can use `svg.attr('height', height)` // to add height directly one can use `svg.attr('height', height)`
//
// @ts-ignore TODO: svg type vs selection mismatch
configureSvgSize(svg, height, width, useMaxWidth); configureSvgSize(svg, height, width, useMaxWidth);
// Prepare data for construction based on diagObj.db // Prepare data for construction based on diagObj.db
@@ -66,17 +69,21 @@ export const draw = function (text: string, id: string, _version: string, diagOb
// "links": [ { "source": "Alice", "target": "Bob", "value": 23 }, { "source": "Bob", "target": "Carol", "value": 43 } ] // "links": [ { "source": "Alice", "target": "Bob", "value": 23 }, { "source": "Bob", "target": "Carol", "value": 43 } ]
// } // }
// //
// @ts-ignore TODO: db should be coerced to sankey DB type
const graph = diagObj.db.getGraph(); const graph = diagObj.db.getGraph();
// Map config options to alignment functions
const alignmentsMap: Map< const alignmentsMap: Map<
SankeyNodeAlignment, SankeyNodeAlignment,
(node: SankeyNode<{}, {}>, n: number) => number (node: d3SankeyNode<{}, {}>, n: number) => number
> = new Map([ > = new Map([
[SankeyNodeAlignment.left, d3SankeyLeft], [SankeyNodeAlignment.left, d3SankeyLeft],
[SankeyNodeAlignment.right, d3SankeyRight], [SankeyNodeAlignment.right, d3SankeyRight],
[SankeyNodeAlignment.center, d3SankeyCenter], [SankeyNodeAlignment.center, d3SankeyCenter],
[SankeyNodeAlignment.justify, d3SankeyJustify], [SankeyNodeAlignment.justify, d3SankeyJustify],
]); ])
const nodeAlignment = alignmentsMap.get(conf?.nodeAlignment || SankeyNodeAlignment.justify); // We need fallback because typescript thinks that `get` can result in undefined
const nodeAlign = alignmentsMap.get(nodeAlignment) || d3SankeyJustify;
// Construct and configure a Sankey generator // Construct and configure a Sankey generator
// That will be a function that calculates nodes and links dimensions // That will be a function that calculates nodes and links dimensions
@@ -86,7 +93,7 @@ export const draw = function (text: string, id: string, _version: string, diagOb
.nodeId((d: any) => d.id) // we use 'id' property to identify node .nodeId((d: any) => d.id) // we use 'id' property to identify node
.nodeWidth(nodeWidth) .nodeWidth(nodeWidth)
.nodePadding(10) .nodePadding(10)
.nodeAlign(nodeAlignment) .nodeAlign(nodeAlign)
.extent([ .extent([
[0, 0], [0, 0],
[width, height], [width, height],
@@ -152,32 +159,32 @@ export const draw = function (text: string, id: string, _version: string, diagOb
if (linkColor === SankeyLinkColor.gradient) { if (linkColor === SankeyLinkColor.gradient) {
const gradient = link const gradient = link
.append('linearGradient') .append('linearGradient')
.attr('id', (d) => (d.uid = Uid.next('linearGradient-')).id) .attr('id', (d: any) => (d.uid = Uid.next('linearGradient-')).id)
.attr('gradientUnits', 'userSpaceOnUse') .attr('gradientUnits', 'userSpaceOnUse')
.attr('x1', (d) => d.source.x1) .attr('x1', (d: any) => d.source.x1)
.attr('x2', (d) => d.target.x0); .attr('x2', (d: any) => d.target.x0);
gradient gradient
.append('stop') .append('stop')
.attr('offset', '0%') .attr('offset', '0%')
.attr('stop-color', (d) => colorScheme(d.source.id)); .attr('stop-color', (d: any) => colorScheme(d.source.id));
gradient gradient
.append('stop') .append('stop')
.attr('offset', '100%') .attr('offset', '100%')
.attr('stop-color', (d) => colorScheme(d.target.id)); .attr('stop-color', (d: any) => colorScheme(d.target.id));
} }
let coloring: any; let coloring: any;
switch (linkColor) { switch (linkColor) {
case SankeyLinkColor.gradient: case SankeyLinkColor.gradient:
coloring = (d) => d.uid; coloring = (d: any) => d.uid;
break; break;
case SankeyLinkColor.source: case SankeyLinkColor.source:
coloring = (d) => d.source.id; coloring = (d: any) => d.source.id;
break; break;
case SankeyLinkColor.target: case SankeyLinkColor.target:
coloring = (d) => d.target.id; coloring = (d: any) => d.target.id;
break; break;
default: default:
coloring = linkColor; coloring = linkColor;