11import { NodeCoordinates , getEmptyGraphDataset , toNumber , toScalar } from "@gephi/gephi-lite-sdk" ;
22import Graph , { MultiGraph } from "graphology" ;
33import { Attributes } from "graphology-types" ;
4- import { flatMap , forEach , isNil , isNumber , keys , mapValues , omit , sortBy , uniq , values } from "lodash" ;
4+ import { flatMap , forEach , isNil , isNumber , keyBy , keys , mapValues , omit , sortBy , uniq , values } from "lodash" ;
55
66import { ItemType , Scalar } from "../types" ;
77import { inferFieldType } from "./fieldModel" ;
@@ -21,7 +21,7 @@ import {
2121export { datasetToString , getEmptyGraphDataset , parseDataset } from "@gephi/gephi-lite-sdk" ;
2222
2323export function getRandomNodeCoordinate ( ) : number {
24- return Math . random ( ) * 100 ;
24+ return Math . random ( ) * 1000 ;
2525}
2626
2727export function cleanNode ( _node : string , attributes : Attributes ) : { data : ItemData ; position : NodeCoordinates } {
@@ -48,7 +48,10 @@ export function cleanEdge(_edge: string, attributes: Attributes): { data: ItemDa
4848 * This function takes any graphology instance (like returned by any graphology
4949 * importer basically), and returns a properly shaped graph dataset:
5050 */
51- export function initializeGraphDataset ( graph : Graph ) : GraphDataset {
51+ export function initializeGraphDataset (
52+ graph : Graph ,
53+ { nodeFields, edgeFields } : { nodeFields ?: FieldModel < "nodes" > [ ] ; edgeFields ?: FieldModel < "edges" > [ ] } = { } ,
54+ ) : GraphDataset {
5255 const dataset = getEmptyGraphDataset ( ) ;
5356
5457 // setting graph meta data
@@ -86,20 +89,26 @@ export function initializeGraphDataset(graph: Graph): GraphDataset {
8689 } ) ;
8790
8891 // Infer model:
92+ const nodeFieldsDict = keyBy ( nodeFields , "id" ) ;
93+ const edgeFieldsDict = keyBy ( edgeFields , "id" ) ;
94+
8995 forEach ( nodeAttributeValues , ( values , key ) => {
90- dataset . nodeFields . push ( {
91- id : key ,
92- itemType : "nodes" ,
93- ...inferFieldType ( key , values , graph . order ) ,
94- } ) ;
96+ dataset . nodeFields . push (
97+ nodeFieldsDict [ key ] || {
98+ id : key ,
99+ itemType : "nodes" ,
100+ ...inferFieldType ( key , values , graph . order ) ,
101+ } ,
102+ ) ;
95103 } ) ;
96-
97104 forEach ( edgeAttributeValues , ( values , key ) => {
98- dataset . edgeFields . push ( {
99- id : key ,
100- itemType : "edges" ,
101- ...inferFieldType ( key , values , graph . size ) ,
102- } ) ;
105+ dataset . edgeFields . push (
106+ edgeFieldsDict [ key ] || {
107+ id : key ,
108+ itemType : "edges" ,
109+ ...inferFieldType ( key , values , graph . size ) ,
110+ } ,
111+ ) ;
103112 } ) ;
104113
105114 const labelsOrder = [ "label" , "size" , "weight" , "color" ] ;
@@ -253,23 +262,3 @@ export function uniqFieldValuesAsStrings(items: Record<string, ItemData>, field:
253262 } ) ,
254263 ) as string [ ] ;
255264}
256-
257- // /**
258- // * Generate the original graph from the graphDataset.
259- // */
260- // export function graphDatasetToGraphData(graphDataset: GraphDataset): DataGraph {
261- // const graph = graphDataset.fullGraph.copy();
262- // Object.entries(graphDataset.nodeData).map(([key, value]) => {
263- // graph.updateNodeAttributes(key, (attrs) => ({ ...attrs, ...value }));
264- // });
265- // Object.entries(graphDataset.edgeData).map(([key, value]) => {
266- // graph.updateEdgeAttributes(key, (attrs) => ({ ...attrs, ...value }));
267- // });
268- // Object.entries(graphDataset.nodeRenderingData).map(([key, value]) => {
269- // graph.updateNodeAttributes(key, (attrs) => ({ ...attrs, ...value }));
270- // });
271- // Object.entries(graphDataset.edgeRenderingData).map(([key, value]) => {
272- // graph.updateEdgeAttributes(key, (attrs) => ({ ...attrs, ...value }));
273- // });
274- // return graph;
275- // }
0 commit comments