Skip to content

Commit

Permalink
feat: Add lit-html enabled graph
Browse files Browse the repository at this point in the history
Signed-off-by: Gordon Smith <GordonJSmith@gmail.com>
  • Loading branch information
GordonSmith committed Jan 28, 2025
1 parent 81d2379 commit 8e7b6f2
Show file tree
Hide file tree
Showing 62 changed files with 1,185 additions and 362 deletions.
27 changes: 24 additions & 3 deletions package-lock.json

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

5 changes: 4 additions & 1 deletion packages/comms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
"module": "./dist/index.browser.js",
"exports": {
".": {
"types": "./types/index.browser.d.ts",
"types": {
"node": "./types/index.node.d.ts",
"default": "./types/index.browser.d.ts"
},
"node": "./dist/index.node.js",
"import": "./dist/index.browser.js",
"require": "./dist/index.browser.umd.cjs"
Expand Down
42 changes: 42 additions & 0 deletions packages/graph/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
}

#placeholder,
#placeholder1,
#placeholder2 {
width: 100%;
height: 500px;
Expand All @@ -29,6 +30,11 @@
</head>

<body onresize="doResize()">
<h1>ESM Quick Test</h1>
<div id="placeholder1"></div>
<script type="module">
import "./tests/index.ts";
</script>
<h1>ESM Quick Test</h1>
<div id="placeholder2"></div>
<script type="module">
Expand Down Expand Up @@ -120,6 +126,42 @@ <h1>ESM Quick Test</h1>
.render()
;
</script>
<div id="placeholder"></div>
<script type="module">
import { SankeyGraph } from "./src/index.ts";

import "@hpcc-js/common/font-awesome/css/font-awesome.min.css";

new SankeyGraph()
.target("placeholder")
.vertexColumns(["category", "id", "label"])
.vertices([
[0, 0, "Year 1"],
[0, 1, "Year 2"],
[0, 2, "Year 3"],
[0, 3, "Year 4"],
[1, 4, "Math"],
[1, 5, "English"],
[1, 6, "Geometry"],
[1, 7, "Science"],
])
.edgeColumns(["source", "target", "weight"])
.edges([
[0, 4, 1],
[1, 4, 1],
[2, 4, 1],
[3, 4, 1],
[0, 5, 1],
[1, 5, 1],
[2, 6, 1],
[3, 6, 1],
[1, 7, 1],
[2, 7, 1],
[3, 7, 1],
])
.render()
;
</script>
<script>
function doResize() {
window.__widget?.resize()?.render();
Expand Down
3 changes: 2 additions & 1 deletion packages/graph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
"d3-shape": "^1",
"d3-tile": "^1",
"d3-transition": "^1",
"dagre": "0.8.5"
"dagre": "0.8.5",
"lit-html": "3.2.1"
},
"peerDependencies": {},
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/graph/src/__tests__/test5.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Vertex4, CentroidVertex4 } from "@hpcc-js/react";
import { Graph2 } from "../graph2/graph";
import { GraphReact } from "../react/graphReact.ts";

const VERTEX_ARR = [{
id: 0,
Expand Down
33 changes: 33 additions & 0 deletions packages/graph/src/common/graphT.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
.graph_GraphT .graphVertex {
cursor: pointer;
}

.graph_GraphT .allowDragging .graphVertex {
cursor: pointer;
}

.graph_GraphT .allowDragging .graphVertex.grabbed {
cursor: grabbing;
}

.graph_GraphT .graphEdge {
stroke: darkgray;
fill: none;
}

.graph_GraphT .graphEdge>text {
stroke: none;
fill: black;
}

.graph_GraphT .graphEdge.hide-text>text {
display: none;
}

.graph_GraphT g.selected rect {
stroke: navy !important;
}

.graph_GraphT g.selected circle {
stroke: navy !important;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { EdgePlaceholder, SubgraphPlaceholder, VertexPlaceholder, isEdgePlacehol
import { Engine, graphviz as gvWorker } from "./layouts/graphvizWorker.ts";
import { Tree, RadialTree, Dendrogram, RadialDendrogram } from "./layouts/tree.ts";

import "../../src/graph2/graph.css";
import "./graphT.css";

let scriptDir = (globalThis?.document?.currentScript as HTMLScriptElement)?.src ?? "./dummy.js";
scriptDir = scriptDir.substring(0, scriptDir.replace(/[?#].*/, "").lastIndexOf("/") + 1);
Expand Down Expand Up @@ -121,6 +121,8 @@ export class GraphT<SG extends SubgraphBaseProps, V extends VertexBaseProps, E e
];
this._iconBar.buttons(buttons.concat(this._iconBar.buttons()));

this.selectionGlowColor("navy");

this._dragHandler
.on("start", function (d) {
if (context.allowDragging()) {
Expand Down Expand Up @@ -1036,6 +1038,9 @@ export class GraphT<SG extends SubgraphBaseProps, V extends VertexBaseProps, E e
super.update(domNode, element);

this._renderElement.classed("allowDragging", this.allowDragging());
if (this._centroidFilter) {
this._centroidFilter.update(this.selectionGlowColor());
}

this.updateCategories();
this.updateAnnotations();
Expand Down
3 changes: 3 additions & 0 deletions packages/graph/src/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "../react/dataGraph.ts";
export * from "./graphT.ts";
export * from "./sankeyGraph.ts";
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface EdgeBaseProps<V extends VertexBaseProps = VertexBaseProps> exte
strokeDasharray?: string;
strokeWidth?: number;
stroke?: string;
path?: string;
fontFamily?: string;
}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Palette, SVGWidget, Utility, select as d3Select } from "@hpcc-js/common";
import { compare2 } from "@hpcc-js/util";
import { sankey as d3Sankey, sankeyLinkHorizontal as d3SankeyLinkHorizontal } from "d3-sankey";
import { AnnotationColumn, toJsonObj } from "./dataGraph.ts";
import { AnnotationColumn, toJsonObj } from "../react/dataGraph.ts";

import "../../src/graph2/sankeyGraph.css";
import "./sankeyGraph.css";
import { EdgeBaseProps, VertexBaseProps } from "./graphT.ts";

export class SankeyGraph extends SVGWidget {
Expand Down
34 changes: 0 additions & 34 deletions packages/graph/src/graph2/graph.css

This file was deleted.

7 changes: 0 additions & 7 deletions packages/graph/src/graph2/index.ts

This file was deleted.

9 changes: 9 additions & 0 deletions packages/graph/src/html/GraphHtml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { GraphHtmlT, SubgraphBaseProps, EdgeBaseProps } from "./graphHtmlT.ts";
import { HtmlVertex, HtmlVertexProps } from "./vertex.ts";

export class GraphHtml extends GraphHtmlT<SubgraphBaseProps, HtmlVertexProps, EdgeBaseProps> {
constructor() {
super(undefined, HtmlVertex);
}
}
GraphHtml.prototype._class += " graph_GraphHtml";
70 changes: 70 additions & 0 deletions packages/graph/src/html/annotation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { svg } from "lit-html";
import { Palette, Utility } from "@hpcc-js/common";
import { extend } from "./component.ts";
import { TextBox } from "./textBox.ts";

export interface HtmlAnnotationProps {
text: string;
fontFamily?: string;
fontSize?: number;
fill?: string;
stroke?: string;
}

export const HtmlAnnotation = ({
text,
fontFamily = "FontAwesome",
fontSize = 8,
fill = "gray",
stroke = "darkgray"
}: HtmlAnnotationProps) => {
const renderChar = fontFamily === "FontAwesome" ? Utility.faChar(text) : text;
const textBoxTpl = TextBox({
text: {
text: renderChar,
fill: Palette.textColor(fill),
fontFamily,
fontSize,
dominantBaseline: fontFamily === "FontAwesome" ? "ideographic" : undefined
},
padding: 3,
fill,
stroke
});
return extend(svg`\
<g data-click="annotation" data-click-data=${JSON.stringify({ text, fill, fontFamily, fontSize, stroke })}>
${textBoxTpl}
</g>`, textBoxTpl.extent.width, textBoxTpl.extent.height);
};

export interface AnnotationsProps {
annotations: HtmlAnnotationProps[];
padding?: number;
}

export const Annotations = ({
annotations,
padding = 3,
}: AnnotationsProps) => {
let xOffset = 0;
const items = annotations.map(annotationProp => {
const annotation = HtmlAnnotation(annotationProp);
const itemSvg = extend(svg`\
<g transform="translate(${xOffset + annotation.extent.width / 2} 0)">
${annotation}
</g>`, annotation.extent.width, annotation.extent.height);
xOffset += annotation.extent.width + padding;
return itemSvg;
});
const { width, height } = items.reduce((acc, item) => {
return {
width: acc.width + item.extent.width,
height: Math.max(acc.height, item.extent.height)
};
}, { width: (items.length - 1) * padding, height: 0 });
return extend(svg`\
<g transform="translate(${-width / 2} 0)">
${items}
</g>`, width, height);
};

14 changes: 14 additions & 0 deletions packages/graph/src/html/component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { HTMLTemplateResult, SVGTemplateResult } from "lit-html";

export type TemplateResult = HTMLTemplateResult | SVGTemplateResult;
export type TemplateResultEx = TemplateResult & {
extent?: { width: number, height: number };
};
export function extend(result: TemplateResult, width: number, height: number): TemplateResultEx {
return {
...result,
extent: { width, height }
};
}
export type Component<T> = (props: T) => TemplateResult;

Loading

0 comments on commit 8e7b6f2

Please sign in to comment.