Parcourir la source

fixed sankey label and title functions for nodes and edges

Piotr Labudda il y a 7 ans
Parent
commit
35d3a4d0b4
2 fichiers modifiés avec 159 ajouts et 62 suppressions
  1. 23 15
      theme/assets/js/graph/sankey-init-widget.js
  2. 136 47
      tools/Bocian.php.view.js

+ 23 - 15
theme/assets/js/graph/sankey-init-widget.js

@@ -186,10 +186,8 @@ var change = function(d) {
 			d3.select(this).style('opacity', highopacity);
 		}).on("mouseout", function(d) {
 			d3.select(this).style('opacity', lowopacity);
-		}).append("title") //link
-		.text(function(i) {
-			return i.source.name + " → " + i.target.name + "\n" + sankeyNumberFormat(i.value)
-		});
+		}).append("title")
+		.text(sankeyEdgeTitle);
 	var c = svg.append("g") //node
 		.selectAll(".node").data(d.nodes).enter().append("g").attr("class", "node").attr("transform", function(i) {
 			return "translate(" + i.x + "," + i.y + ")"
@@ -221,21 +219,13 @@ var change = function(d) {
 				if (d3.select(this).attr("display") == "none") return "inline"
 				else return "none"
 			});
-		}).append("title").text(function(i) {
-			return i.name + "\n" + sankeyNumberFormat(i.value)
-
-		});
+		}).append("title").text(sankeyNodeTitle);
 	c.append("text") //node
 		.attr("x", -6).attr("y", function(i) {
 			return i.dy / 2
 		}).attr("dy", ".35em").attr("text-anchor", "end").attr("font-size","16px")
-		.text(function(i) {
-		if (labeltextformat<1){
-				return i.name;
-			} else {
-				return "";
-			}
-		}).filter(function(i) {
+		.text(sankeyNodeLabel)
+		.filter(function(i) {
 			return i.x < width / 2
 		}).attr("x", 6 + sankey.nodeWidth()).attr("text-anchor", "start")
 	if (showlinkcount>0) c.append("text") //node
@@ -294,10 +284,28 @@ var change = function(d) {
 };
 // draw();
 
+function sankeyEdgeTitle(i) {
+	return i.source.name + " → " + i.target.name + "\n" + sankeyNumberFormat(i.value)
+}
+function sankeyNodeTitle(i) {
+	if (i.label) return i.label + "\n" + sankeyNumberFormat(i.value)
+	return i.name + "\n" + sankeyNumberFormat(i.value)
+}
+
+function sankeyNodeLabel(i) {
+	if (labeltextformat < 1) {
+		if (i.label) return (i.label.length > 20) ? i.label.substr(0, 20) + '...' : i.label
+		return i.name;
+	} else {
+		return "";
+	}
+}
+
 	var renderGraph = (htmlNode, data, opts) => {
 		var opts = opts || {}
 		if (!htmlNode) throw "Missing html node";
 		if (!data) throw "Missing data";
+		// console.log('renderGraph data', data)
 
 		htmlNode.append("rect").attr("x",0).attr("y",0).attr("width","100%").attr("height","100%").attr("fill","white")
 		width = (opts.width) ? opts.width - margin.left - margin.right : 1020

+ 136 - 47
tools/Bocian.php.view.js

@@ -1809,6 +1809,30 @@ function graphFetchData(node, nameSection) {
 					'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
 					'default_db__x3A__BI_audit_KRS:BI_audit_KRS'
 				].join('/'),
+				[
+					'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
+					'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row',
+					'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
+					'default_db__x3A__BI_audit_KRS:BI_audit_KRS/ID'
+				].join('/'),
+				[
+					'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
+					'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row',
+					'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
+					'default_db__x3A__BI_audit_KRS:BI_audit_KRS/nazwa'
+				].join('/'),
+				[
+					'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
+					'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row',
+					'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
+					'default_db__x3A__BI_audit_KRS:BI_audit_KRS/krs'
+				].join('/'),
+				[
+					'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
+					'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row',
+					'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
+					'default_db__x3A__BI_audit_KRS:BI_audit_KRS/S_miejscowosc'
+				].join('/'),
 				[
 					'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
 					'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row',
@@ -1823,12 +1847,12 @@ function graphFetchData(node, nameSection) {
 				].join('/'),
 				[
 					'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
-					'ID',
+					'ID'
 				].join('/'),
 				[
 					'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
 					'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row',
-					'ID',
+					'ID'
 				].join('/'),
 				[
 					'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
@@ -2006,7 +2030,6 @@ function parseResponseRec(_todoGraphData, json, typeName, parentNodeId, level) {
 	var level = level || 0
 	var parentNodeId = parentNodeId || null
 	if(DBG)console.log('DBG::parseResponseRec', {json:json, typeName:typeName, parentNodeId:parentNodeId, isString: p5Utils__isString(json), isArray: p5Utils__isArray(json), isObject: p5Utils__isObject(json)});
-	console.log('DBG::parseResponseRec', {json:json, typeName:typeName, parentNodeId:parentNodeId, isString: p5Utils__isString(json), isArray: p5Utils__isArray(json), isObject: p5Utils__isObject(json)});
 	if (p5Utils__isArray(json)) {
 		// TODO: create named group
 		var isXlinkList = (json.length > 0 && p5Utils__isString(json[0]))
@@ -2029,7 +2052,13 @@ function parseResponseRec(_todoGraphData, json, typeName, parentNodeId, level) {
 		})
 		var nodeId = nodeObject.id
 
-		if ("default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object" !== typeName
+		if ("default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row" === typeName) {
+			parseResponseRec__row(_todoGraphData, json, typeName, parentNodeId, level)
+		}
+		else if ("default_db__x3A__BI_audit_KRS:BI_audit_KRS" === typeName) {
+			parseResponseRec__krs(_todoGraphData, json, typeName, parentNodeId, level)
+		}
+		else if ("default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object" !== typeName
 			&& "default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row" !== typeName
 		) {
 			_todoGraphData[level].nodes.push(nodeObject)
@@ -2043,45 +2072,6 @@ function parseResponseRec(_todoGraphData, json, typeName, parentNodeId, level) {
 				var value = json[fieldName]
 				parseResponseRec(_todoGraphData, value, fieldName, nodeId, level + 1)
 			})
-		} else if ("default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row" === typeName) {
-			// 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row',
-			// 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
-			// 'default_db__x3A__BI_audit_KRS:BI_audit_KRS' --> Kontrahenci
-			var rowObjectChildrens = []
-			var rowTN = 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row'
-			var rowObjTN = 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object'
-			// { row: row_obj: [ { ID, kontr: [ "http...kontr.ID" ] }, { ID, kontr: [ "http...krs.ID" ] } ], ... }
-			var rowJson = json
-			if (rowJson[rowObjTN] && rowJson[rowObjTN].length > 1) {
-				var pathItems = []
-				rowJson[rowObjTN].forEach(function (rowObjJson) {
-					Object.keys(rowObjJson)
-						.filter(function (rowObjFld) { return (-1 !== rowObjFld.indexOf(':')); })
-						.filter(function (rowObjRefFld) { return (rowObjJson[rowObjRefFld] && rowObjJson[rowObjRefFld].length); })
-						.forEach(function (rowObjRefFld) {
-							pathItems.push([ rowObjRefFld, rowObjJson[rowObjRefFld][0] ])
-						})
-				})
-			}
-			// console.log('pathItems:', pathItems);
-			var lastParentFid = null
-			pathItems.reverse().forEach(function (pathItem) {
-				var nodeObject = dataMakeXlinkNode({
-					xlink: pathItem[1],
-					typeName: pathItem[0]
-				})
-				// _todoGraphData[todoLevel].nodes.push(nodeObject) // already added below
-				if (lastParentFid) _todoGraphData[level].edges.push(dataMakeEdge(lastParentFid, nodeObject))
-				lastParentFid = nodeObject.id
-			})
-
-			Object.keys(json).filter(function (fieldName) {
-				return (fieldName.indexOf(':') > -1)
-			})
-			.forEach(function (fieldName) {
-				var value = json[fieldName]
-				parseResponseRec(_todoGraphData, value, fieldName, parentNodeId, level + 1)
-			})
 		} else {
 			Object.keys(json).filter(function (fieldName) {
 				return (fieldName.indexOf(':') > -1)
@@ -2150,6 +2140,80 @@ function parseResponseP5Link(_todoGraphData, json, typeName, parentNodeId, level
 	}
 }
 
+function parseResponseRec__krs(_todoGraphData, json, typeName, parentNodeId, level) {
+	var nodeObject = dataMakeNode({
+		typeName: typeName,
+		primaryKey: (json['ID']) ? json['ID'] : null, // TODO: get primaryKey from object
+		row: json
+	})
+	var nodeId = nodeObject.id
+
+	_todoGraphData[level].nodes.push(nodeObject)
+	if (parentNodeId) {
+		_todoGraphData[level].edges.push(dataMakeEdge(parentNodeId, nodeObject))
+	}
+	Object.keys(json).filter(function (fieldName) {
+		return (fieldName.indexOf(':') > -1)
+	})
+	.forEach(function (fieldName) {
+		var value = json[fieldName]
+		parseResponseRec(_todoGraphData, value, fieldName, nodeId, level + 1)
+	})
+}
+function parseResponseRec__row(_todoGraphData, json, typeName, parentNodeId, level) {
+	var nodeObject = dataMakeNode({
+		typeName: typeName,
+		primaryKey: (json['ID']) ? json['ID'] : null, // TODO: get primaryKey from object
+	})
+	var nodeId = nodeObject.id
+
+	// 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row',
+	// 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
+	// 'default_db__x3A__BI_audit_KRS:BI_audit_KRS' --> Kontrahenci
+	var rowObjectChildrens = []
+	var rowTN = 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row'
+	var rowObjTN = 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object'
+	// { row: row_obj: [ { ID, kontr: [ "http...kontr.ID" ] }, { ID, kontr: [ "http...krs.ID" ] } ], ... }
+	var rowJson = json
+	if (rowJson[rowObjTN] && rowJson[rowObjTN].length > 1) {
+		var pathItems = []
+		rowJson[rowObjTN].forEach(function (rowObjJson) {
+			Object.keys(rowObjJson)
+				.filter(function (rowObjFld) { return (-1 !== rowObjFld.indexOf(':')); })
+				.filter(function (rowObjRefFld) { return (rowObjJson[rowObjRefFld] && rowObjJson[rowObjRefFld].length); })
+				.forEach(function (rowObjRefFld) {
+					pathItems.push([ rowObjRefFld, rowObjJson[rowObjRefFld][0] ])
+				})
+		})
+	}
+	// console.log('node('+nodeId+') pathItems:', pathItems);
+	var lastParentFid = null
+	pathItems.reverse().forEach(function (pathItem) {
+		var nodeObject = ('string' === typeof pathItem[1])
+		? dataMakeXlinkNode({
+				xlink: pathItem[1],
+				typeName: pathItem[0]
+			})
+		:	 dataMakeNode({
+				typeName: pathItem[0],
+				primaryKey: (pathItem[1]['ID']) ? pathItem[1]['ID'] : null,
+				row: pathItem[1]
+			})
+		;
+		// _todoGraphData[todoLevel].nodes.push(nodeObject) // already added below
+		if (lastParentFid) _todoGraphData[level].edges.push(dataMakeEdge(lastParentFid, nodeObject))
+		lastParentFid = nodeObject.id
+	})
+
+	Object.keys(json).filter(function (fieldName) {
+		return (fieldName.indexOf(':') > -1)
+	})
+	.forEach(function (fieldName) {
+		var value = json[fieldName]
+		parseResponseRec(_todoGraphData, value, fieldName, parentNodeId, level + 1)
+	})
+}
+
 function isP5LinkObject(json) {
 	if ( !('type' in json) || !json['type'] ) return false;
 	if ( !('value' in json) || !json['value'] ) return false;
@@ -2163,15 +2227,16 @@ function isP5LinkObject(json) {
 function dataMakeNode(params) {
 	var objectName = params.typeName.substr(params.typeName.indexOf(':') + 1)
 	var nodeId = objectName + '.' + params.primaryKey // TODO: primaryKey?
-	return {
+	var graphNode = {
 		id: nodeId,
-		label: makeShortLabel(nodeId), // TODO: get from schema assert @label attribute
 		name: nodeId,
 		group: objectName,
 		_loaded: true,
 		typeName: params.typeName,
 		primaryKey: params.primaryKey // TODO: _primaryKey
 	}
+	if (params.row) graphNode.row = params.row
+	return graphAddNodeLabel(graphNode)
 }
 function dataMakeEdge(parentNodeId, nodeObject) {
 	return {
@@ -2184,15 +2249,15 @@ function dataMakeXlinkNode(params) {
 	var objectName = params.typeName.substr(params.typeName.indexOf(':') + 1)
 	var nodeId = params.xlink.substr(params.xlink.indexOf('#') + 1)
 	var primaryKey = nodeId.substr(nodeId.lastIndexOf('.') + 1)
-	return {
+	var graphNode = {
 		id: nodeId,
-		label: makeShortLabel(nodeId) + ' (+)',
 		name: nodeId,
 		group: objectName,
 		_loaded: false,
 		typeName: params.typeName,
 		primaryKey: primaryKey
 	}
+	return graphAddNodeLabel(graphNode)
 }
 
 function dataMakeFetchMoreNode(params) {
@@ -2228,11 +2293,35 @@ function dataMakeFetchMoreEdge(parentNodeId, fetchMoreNode) {
 }
 function makeShortLabel(label) { // TODO: shorter name
 	// BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object.1234567
+
 	if (label.length < 30) return label;
 	var exLabel = label.split('.')
 	if (exLabel.length !== 2) return label;
 	return exLabel[0].substr(0, 24) + '...' + exLabel[0].substr(-10) + '.' + exLabel[1];
 }
+function graphAddNodeLabel(graphNode) {
+	// var graphNode = {
+	// 	id: nodeId,
+	// 	name: nodeId,
+	// 	group: objectName,
+	// 	_loaded: true,
+	// 	typeName: params.typeName,
+	// 	primaryKey: params.primaryKey // TODO: _primaryKey
+	// }
+	var label = graphNode.id // TODO: get from schema assert @label attribute
+	label = makeShortLabel(graphNode.id)
+	// <xs:assert test="@default_db__x3A__BI_audit_KRS:label = concat(nazwa, ' ', krs, ' ', S_miejscowosc)" id="I_audit_KRS___d6e76977-1">
+	switch (graphNode.typeName) {
+		case 'default_db__x3A__BI_audit_KRS:BI_audit_KRS': {
+			if (graphNode.row) label = graphNode.row.nazwa + ' ' + graphNode.row.krs + ' ' + graphNode.row.S_miejscowosc;
+			label = label + ' (krs)'
+		} break;
+	}
+	return Object.assign(graphNode, {
+		label: label,
+	})
+}
+
 
 
 /**