Merge branch 'master' of git.scode.ovh:timo/graphviz-builder
This commit is contained in:
@ -6,15 +6,18 @@ export class Edge {
|
||||
public target: string;
|
||||
public style?: EdgeStyles;
|
||||
public color?: Color;
|
||||
private _directional: boolean;
|
||||
|
||||
public constructor (origin: string, target: string) {
|
||||
public constructor (origin: string, target: string, directional: boolean) {
|
||||
this.origin = origin;
|
||||
this.target = target;
|
||||
this._directional = directional;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
public toString (): string {
|
||||
const attributes = [];
|
||||
|
||||
if (this.style)
|
||||
attributes.push ({ name: 'style', value: this.style.toString () });
|
||||
if (this.color)
|
||||
@ -23,7 +26,9 @@ export class Edge {
|
||||
const attr_string = ` [${attributes.map ((v) => `${v.name}="${v.value}"`)
|
||||
.join (',')}]`;
|
||||
|
||||
return `${this.origin} -> ${this.target}${attributes.length > 0
|
||||
return `${this.origin} -${
|
||||
this._directional ? '>' : '-'
|
||||
} ${this.target}${attributes.length > 0
|
||||
? attr_string
|
||||
: ''}`;
|
||||
}
|
||||
|
@ -25,6 +25,13 @@ export class Element {
|
||||
}
|
||||
|
||||
public constructor (name: string, parent = '') {
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
const regex = /^[a-z_][a-z_0-9]+$/iu;
|
||||
|
||||
if (!regex.test (name))
|
||||
throw new Error ('invalid name specified');
|
||||
>>>>>>> f553396eee29e1e0820624345e5d23dc3471a4a4
|
||||
this.name = name;
|
||||
this.parent_name = parent;
|
||||
}
|
||||
|
35
lib/Graph.ts
35
lib/Graph.ts
@ -3,6 +3,7 @@ import { Edge } from './Edge';
|
||||
import { Node } from './Node';
|
||||
import { GraphStyles, NodeStyles } from './Styles';
|
||||
import { Color } from './Color';
|
||||
import { GraphLayouts } from './GraphLayouts';
|
||||
|
||||
interface NodeOptions {
|
||||
name: string;
|
||||
@ -18,19 +19,35 @@ export class Graph extends Element {
|
||||
public edges: Array<Edge> = [];
|
||||
public style?: GraphStyles;
|
||||
public color?: Color;
|
||||
public directional = true;
|
||||
public overlap?: boolean | string;
|
||||
public splines?: boolean | string;
|
||||
public layout?: GraphLayouts;
|
||||
|
||||
private get attributes (): Array<{name: string; value: string}> {
|
||||
const attributes = [];
|
||||
|
||||
if (typeof this.color !== 'undefined')
|
||||
attributes.push ({ name: 'color', value: this.color.toString () });
|
||||
if (typeof this.style !== 'undefined')
|
||||
attributes.push ({ name: 'style', value: this.style.toString () });
|
||||
if (typeof this.overlap !== 'undefined')
|
||||
attributes.push ({ name: 'overlap', value: this.overlap.toString () });
|
||||
if (typeof this.splines !== 'undefined')
|
||||
attributes.push ({ name: 'splines', value: this.splines.toString () });
|
||||
if (typeof this.layout !== 'undefined')
|
||||
attributes.push ({ name: 'layout', value: this.layout.toString () });
|
||||
|
||||
return attributes;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
public toString (): string {
|
||||
const header = this.parent
|
||||
? `subgraph cluster_${this.full_name}`
|
||||
: `digraph ${this.full_name}`;
|
||||
const attributes = [];
|
||||
if (this.color)
|
||||
attributes.push ({ name: 'color', value: this.color.toString () });
|
||||
if (this.style)
|
||||
attributes.push ({ name: 'style', value: this.style.toString () });
|
||||
: `${this.directional ? 'di' : ''}graph ${this.full_name}`;
|
||||
|
||||
let attrs = `\n${attributes.map ((v) => `${v.name} = ${v.value}`)
|
||||
let attrs = `\n${this.attributes.map ((v) => `${v.name} = ${v.value}`)
|
||||
.join ('\n')}\n`;
|
||||
let children = `\n${this.children.map ((c) => c.toString ())
|
||||
.join ('\n')}\n`;
|
||||
@ -75,6 +92,8 @@ export class Graph extends Element {
|
||||
public add_graph (constructor: ((g: Graph) => void) | string): string {
|
||||
const graph = new Graph ('unnamed', this.full_name);
|
||||
|
||||
graph.directional = this.directional;
|
||||
|
||||
if (typeof constructor === 'string')
|
||||
graph.name = constructor;
|
||||
else
|
||||
@ -85,6 +104,6 @@ export class Graph extends Element {
|
||||
}
|
||||
|
||||
public add_edge (origin: string, target: string): void {
|
||||
this.edges.push (new Edge (origin, target));
|
||||
this.edges.push (new Edge (origin, target, this.directional));
|
||||
}
|
||||
}
|
||||
|
10
lib/GraphLayouts.ts
Normal file
10
lib/GraphLayouts.ts
Normal file
@ -0,0 +1,10 @@
|
||||
export enum GraphLayouts {
|
||||
neato = 'neato',
|
||||
dot = 'dot',
|
||||
circo = 'circo',
|
||||
fdp = 'fdp',
|
||||
sfdp = 'sfdp',
|
||||
osage = 'osage',
|
||||
twopi = 'twopi',
|
||||
patchwork = 'patchwork'
|
||||
}
|
@ -20,6 +20,7 @@ export class Node extends Element {
|
||||
|
||||
const mapped_columns = this.table_contents
|
||||
.map ((val) => `<td>${val.join ('</td><td>')}</td>`);
|
||||
|
||||
return `<table>\n <tr>${
|
||||
mapped_columns.join ('</tr>\n <tr>')
|
||||
}</tr>\n</table>`;
|
||||
@ -28,6 +29,7 @@ export class Node extends Element {
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
public toString (): string {
|
||||
const attributes = [];
|
||||
|
||||
if (this.label || this.is_table) {
|
||||
attributes.push ({
|
||||
name: 'label',
|
||||
@ -51,6 +53,7 @@ export class Node extends Element {
|
||||
'"',
|
||||
'"'
|
||||
];
|
||||
|
||||
return `${v.name}=${d[0]}${v.value}${d[1]}`;
|
||||
})
|
||||
.join (', ');
|
||||
|
@ -4,3 +4,4 @@ export { Element } from './Element';
|
||||
export { Edge } from './Edge';
|
||||
export { Color } from './Color';
|
||||
export { EdgeStyles, NodeStyles, GraphStyles } from './Styles';
|
||||
export { GraphLayouts } from './GraphLayouts';
|
||||
|
Reference in New Issue
Block a user