Daniel Quathamer
2 years ago
10 changed files with 924 additions and 88 deletions
File diff suppressed because one or more lines are too long
@ -0,0 +1,556 @@
@@ -0,0 +1,556 @@
|
||||
|
||||
/* |
||||
myNodes[0] = { "node": 0, "name": "Africa" }; |
||||
myNodes[1] = { "node": 1, "name": "America" }; |
||||
myNodes[2] = { "node": 2, "name": "Europe" }; |
||||
|
||||
|
||||
myLinks[0] = { "source": 0, "target": 2, "value":1 }; |
||||
myLinks[1] = { "source": 1, "target": 2, "value":20 }; |
||||
|
||||
|
||||
let my_object1 = {};
|
||||
my_object1.name = "Africa"; |
||||
my_object1.node = 0; |
||||
myNodes.push(my_object1); |
||||
//let my_object2 = {};
|
||||
my_object1.name = "America"; |
||||
my_object1.node = 1; |
||||
myNodes.push(new Objectmy_object1); |
||||
let my_object3 = {};
|
||||
my_object3.name = "Europe"; |
||||
my_object3.node = 2; |
||||
myNodes.push(my_object3); |
||||
|
||||
let my_Linkobject1 = {};
|
||||
my_Linkobject1.source = 0; |
||||
my_Linkobject1.target = 2; |
||||
my_Linkobject1.value = 1; |
||||
myLinks.push(my_Linkobject1); |
||||
let my_Linkobject2 = {};
|
||||
my_Linkobject2.source = 1; |
||||
my_Linkobject2.target = 2; |
||||
my_Linkobject2.value = 20; |
||||
myLinks.push(my_Linkobject2); |
||||
*/ |
||||
/*var newNode = new myNode("0","Africa"); |
||||
myNodes.push(myNode); |
||||
var newNode = new myNode("1","America"); |
||||
myNodes.push(myNode); |
||||
var newNode = new myNode("2","Europe"); |
||||
myNodes.push(myNode); |
||||
|
||||
var newLink = new myLink("0","2",1); |
||||
myLinks.push(newLink); |
||||
var newLink = new myLink("1","2",2); |
||||
myLinks.push(newLink); |
||||
*/ |
||||
|
||||
|
||||
function makeSankey(targetDiv,data,metaData,chartElem) |
||||
{ |
||||
var measureCaption = getMeasureCaption(chartElem,metaData); //"Studierende";
|
||||
var captionEmptyTarget="Kein Master (eigene HS)"; |
||||
var margin = {top: 10, right: 10, bottom: 10, left: 10}, |
||||
width = 700 - margin.left - margin.right, |
||||
height = 300 - margin.top - margin.bottom; |
||||
|
||||
var formatNumber = d3.format(",.0f"), // zero decimal places
|
||||
format = function(d) { return measureCaption+": "+formatNumber(d); }, |
||||
color = d3.scale.category20(); |
||||
|
||||
// append the svg canvas to the page
|
||||
var clearCanvas=document.getElementById("chartDiv"); |
||||
while (clearCanvas.firstChild) { |
||||
clearCanvas.removeChild(clearCanvas.firstChild); |
||||
} |
||||
//targetDiv
|
||||
var svg = d3.select("#"+targetDiv).append("svg") |
||||
.attr("width", width + margin.left + margin.right) |
||||
.attr("height", height + margin.top + margin.bottom) |
||||
.append("g") |
||||
.attr("transform",
|
||||
"translate(" + margin.left + "," + margin.top + ")"); |
||||
|
||||
// Set the sankey diagram properties
|
||||
var sankey = d3.sankey() |
||||
.nodeWidth(36) |
||||
.nodePadding(40) |
||||
.size([width, height]); |
||||
|
||||
var path = sankey.link(); |
||||
|
||||
// load the data
|
||||
var sNodes=getSankeyNodes(data,captionEmptyTarget); //graph.nodes;
|
||||
var sLinks=getSankeyLinks(sNodes,data); //graph.links;
|
||||
sankey |
||||
.nodes(sNodes) |
||||
.links(sLinks) |
||||
.layout(32); |
||||
|
||||
// add in the links
|
||||
var link = svg.append("g").selectAll(".link") |
||||
.data(sLinks) |
||||
.enter().append("path") |
||||
.attr("class", "link") |
||||
.attr("d", path) |
||||
.style("stroke-width", function(d) { return Math.max(1, d.dy); }) |
||||
.sort(function(a, b) { return b.dy - a.dy; }); |
||||
|
||||
// add the link titles
|
||||
link.append("title") |
||||
.text(function(d) { |
||||
return d.source.name + " → " +
|
||||
d.target.name + "\n" + format(d.value); }); |
||||
|
||||
// add in the nodes
|
||||
var node = svg.append("g").selectAll(".node") |
||||
.data(sNodes) |
||||
.enter().append("g") |
||||
.attr("class", "node") |
||||
.attr("transform", function(d) {
|
||||
return "translate(" + d.x + "," + d.y + ")"; }) |
||||
.call(d3.behavior.drag() |
||||
.origin(function(d) { return d; }) |
||||
.on("dragstart", function() {
|
||||
this.parentNode.appendChild(this); }) |
||||
.on("drag", dragmove)); |
||||
|
||||
// add the rectangles for the nodes
|
||||
node.append("rect") |
||||
.attr("height", function(d) { return d.dy; }) |
||||
.attr("width", sankey.nodeWidth()) |
||||
.style("fill", function(d) {
|
||||
return d.color = color(d.name.replace(/ .*/, "")); }) |
||||
.style("stroke", function(d) {
|
||||
return d3.rgb(d.color).darker(2); }) |
||||
.append("title") |
||||
.text(function(d) {
|
||||
return d.name + "\n" + format(d.value); }); |
||||
|
||||
// add in the title for the nodes
|
||||
node.append("text") |
||||
.attr("x", -6) |
||||
.attr("y", function(d) { return d.dy / 2; }) |
||||
.attr("dy", ".35em") |
||||
.attr("text-anchor", "end") |
||||
.attr("transform", null) |
||||
.text(function(d) { return d.name; }) |
||||
.filter(function(d) { return d.x < width / 2; }) |
||||
.attr("x", 6 + sankey.nodeWidth()) |
||||
.attr("text-anchor", "start"); |
||||
|
||||
|
||||
|
||||
} |
||||
|
||||
|
||||
|
||||
function getSankeyNodes(data,captionEmptyTarget) |
||||
{ |
||||
var myNodes=[]; |
||||
|
||||
var distinctSource = []; |
||||
var zs=""; |
||||
for (var i = 0; i < data.length; i++) |
||||
{ |
||||
if(zs.indexOf("_" + data[i].dimension1 +"_")<0) |
||||
{ |
||||
distinctSource.push(data[i].dimension1); |
||||
zs+="_" + data[i].dimension1 +"_"; |
||||
} |
||||
} |
||||
for (var i = 0; i < data.length; i++) |
||||
{ |
||||
if(zs.indexOf("_" + data[i].dimension2 +"_")<0) |
||||
{ |
||||
distinctSource.push(data[i].dimension2); |
||||
zs+="_" + data[i].dimension2 +"_"; |
||||
} |
||||
} |
||||
for (var i = 0; i < distinctSource.length; i++) |
||||
{ |
||||
myNodes[i] = { "node": i, "name": distinctSource[i] }; |
||||
console.log("Abschluss: "+distinctSource[i]); |
||||
} |
||||
//Kein Abschluss:
|
||||
myNodes[i] = { "node": i, "name": captionEmptyTarget }; |
||||
return myNodes; |
||||
} |
||||
|
||||
function getSankeyLinks(myNodes,data) |
||||
{ |
||||
var myLinks=[]; |
||||
|
||||
var linkIndex=0; |
||||
for (var i = 0; i < data.length; i++) |
||||
{ |
||||
if(data[i].dimension2 !="") |
||||
{ |
||||
myLinks[linkIndex]={ "source": getSankeyNodeIndex(myNodes,data[i].dimension1), |
||||
"target": getSankeyNodeIndex(myNodes,data[i].dimension2), "value":data[i].measure1 }; |
||||
linkIndex++; |
||||
} |
||||
else |
||||
{ |
||||
myLinks[linkIndex]={ "source": getSankeyNodeIndex(myNodes,data[i].dimension1), |
||||
"target": myNodes.length-1, "value":data[i].measure1 }; |
||||
linkIndex++; |
||||
}
|
||||
} |
||||
return myLinks; |
||||
} |
||||
|
||||
function getSankeyNodeIndex(myNodes,name) |
||||
{ |
||||
var myIndex=0; |
||||
for (var i = 0; i < myNodes.length; i++) |
||||
{ |
||||
if(myNodes[i].name==name) |
||||
myIndex=i |
||||
} |
||||
return myIndex; |
||||
} |
||||
|
||||
function getMeasureCaption(chartElem,metaData) |
||||
{ |
||||
var measureCaption=""; |
||||
for(var k=0;k<chartElem.elementTypeProperties.length;k++) |
||||
{ |
||||
if(chartElem.elementTypeProperties[k].propertyValue!="") |
||||
{ |
||||
switch (chartElem.elementTypeProperties[k].vizTypePropertyUniquename) |
||||
{ |
||||
case "viz_measure1": |
||||
measure1=chartElem.elementTypeProperties[k].propertyValue; |
||||
measureCaption=getMetadataOfVizTypeProperty(metaData,measure1); |
||||
if(measureCaption=="") |
||||
measureCaption=measure1; |
||||
break; |
||||
default: |
||||
break; |
||||
|
||||
} |
||||
} |
||||
|
||||
} |
||||
return measureCaption; |
||||
} |
||||
function getMetadataOfVizTypeProperty(metaData,vizTypePropertyUniquename) |
||||
{ |
||||
var caption=""; |
||||
for(var k=0;k<metaData.length;k++) |
||||
{ |
||||
if(metaData[k].colname==vizTypePropertyUniquename) |
||||
{ |
||||
caption=metaData[k].colcaption; |
||||
} |
||||
|
||||
|
||||
} |
||||
return caption; |
||||
} |
||||
// the function for moving the nodes for sankey
|
||||
function dragmove(d) { |
||||
d3.select(this).attr("transform",
|
||||
"translate(" + d.x + "," + ( |
||||
d.y = Math.max(0, Math.min(height - d.dy, d3.event.y)) |
||||
) + ")"); |
||||
sankey.relayout(); |
||||
link.attr("d", path); |
||||
} |
||||
|
||||
|
||||
d3.sankey = function() { |
||||
var sankey = {}, |
||||
nodeWidth = 24, |
||||
nodePadding = 8, |
||||
size = [1, 1], |
||||
nodes = [], |
||||
links = []; |
||||
|
||||
sankey.nodeWidth = function(_) { |
||||
if (!arguments.length) return nodeWidth; |
||||
nodeWidth = +_; |
||||
return sankey; |
||||
}; |
||||
|
||||
sankey.nodePadding = function(_) { |
||||
if (!arguments.length) return nodePadding; |
||||
nodePadding = +_; |
||||
return sankey; |
||||
}; |
||||
|
||||
sankey.nodes = function(_) { |
||||
if (!arguments.length) return nodes; |
||||
nodes = _; |
||||
return sankey; |
||||
}; |
||||
|
||||
sankey.links = function(_) { |
||||
if (!arguments.length) return links; |
||||
links = _; |
||||
return sankey; |
||||
}; |
||||
|
||||
sankey.size = function(_) { |
||||
if (!arguments.length) return size; |
||||
size = _; |
||||
return sankey; |
||||
}; |
||||
|
||||
sankey.layout = function(iterations) { |
||||
computeNodeLinks(); |
||||
computeNodeValues(); |
||||
computeNodeBreadths(); |
||||
computeNodeDepths(iterations); |
||||
computeLinkDepths(); |
||||
return sankey; |
||||
}; |
||||
|
||||
sankey.relayout = function() { |
||||
computeLinkDepths(); |
||||
return sankey; |
||||
}; |
||||
|
||||
sankey.link = function() { |
||||
var curvature = .5; |
||||
|
||||
function link(d) { |
||||
var x0 = d.source.x + d.source.dx, |
||||
x1 = d.target.x, |
||||
xi = d3.interpolateNumber(x0, x1), |
||||
x2 = xi(curvature), |
||||
x3 = xi(1 - curvature), |
||||
y0 = d.source.y + d.sy + d.dy / 2, |
||||
y1 = d.target.y + d.ty + d.dy / 2; |
||||
return "M" + x0 + "," + y0 |
||||
+ "C" + x2 + "," + y0 |
||||
+ " " + x3 + "," + y1 |
||||
+ " " + x1 + "," + y1; |
||||
} |
||||
|
||||
link.curvature = function(_) { |
||||
if (!arguments.length) return curvature; |
||||
curvature = +_; |
||||
return link; |
||||
}; |
||||
|
||||
return link; |
||||
}; |
||||
|
||||
// Populate the sourceLinks and targetLinks for each node.
|
||||
// Also, if the source and target are not objects, assume they are indices.
|
||||
function computeNodeLinks() { |
||||
nodes.forEach(function(node) { |
||||
node.sourceLinks = []; |
||||
node.targetLinks = []; |
||||
}); |
||||
links.forEach(function(link) { |
||||
var source = link.source, |
||||
target = link.target; |
||||
console.log("source: "+source+" TARGET: "+target); |
||||
if (typeof source === "number" || typeof source === "string") source = link.source = nodes[link.source]; |
||||
if (typeof target === "number" || typeof target === "string") target = link.target = nodes[link.target]; |
||||
source.sourceLinks.push(link); |
||||
target.targetLinks.push(link); |
||||
}); |
||||
} |
||||
|
||||
// Compute the value (size) of each node by summing the associated links.
|
||||
function computeNodeValues() { |
||||
nodes.forEach(function(node) { |
||||
node.value = Math.max( |
||||
d3.sum(node.sourceLinks, value), |
||||
d3.sum(node.targetLinks, value) |
||||
); |
||||
}); |
||||
} |
||||
|
||||
// Iteratively assign the breadth (x-position) for each node.
|
||||
// Nodes are assigned the maximum breadth of incoming neighbors plus one;
|
||||
// nodes with no incoming links are assigned breadth zero, while
|
||||
// nodes with no outgoing links are assigned the maximum breadth.
|
||||
function computeNodeBreadths() { |
||||
var remainingNodes = nodes, |
||||
nextNodes, |
||||
x = 0; |
||||
|
||||
while (remainingNodes.length) { |
||||
nextNodes = []; |
||||
remainingNodes.forEach(function(node) { |
||||
node.x = x; |
||||
node.dx = nodeWidth; |
||||
node.sourceLinks.forEach(function(link) { |
||||
nextNodes.push(link.target); |
||||
}); |
||||
}); |
||||
remainingNodes = nextNodes; |
||||
++x; |
||||
} |
||||
|
||||
//
|
||||
moveSinksRight(x); |
||||
scaleNodeBreadths((size[0] - nodeWidth) / (x - 1)); |
||||
} |
||||
|
||||
function moveSourcesRight() { |
||||
nodes.forEach(function(node) { |
||||
if (!node.targetLinks.length) { |
||||
node.x = d3.min(node.sourceLinks, function(d) { return d.target.x; }) - 1; |
||||
} |
||||
}); |
||||
} |
||||
|
||||
function moveSinksRight(x) { |
||||
nodes.forEach(function(node) { |
||||
if (!node.sourceLinks.length) { |
||||
node.x = x - 1; |
||||
} |
||||
}); |
||||
} |
||||
|
||||
function scaleNodeBreadths(kx) { |
||||
nodes.forEach(function(node) { |
||||
node.x *= kx; |
||||
}); |
||||
} |
||||
|
||||
function computeNodeDepths(iterations) { |
||||
var nodesByBreadth = d3.nest() |
||||
.key(function(d) { return d.x; }) |
||||
.sortKeys(d3.ascending) |
||||
.entries(nodes) |
||||
.map(function(d) { return d.values; }); |
||||
|
||||
//
|
||||
initializeNodeDepth(); |
||||
resolveCollisions(); |
||||
for (var alpha = 1; iterations > 0; --iterations) { |
||||
relaxRightToLeft(alpha *= .99); |
||||
resolveCollisions(); |
||||
relaxLeftToRight(alpha); |
||||
resolveCollisions(); |
||||
} |
||||
|
||||
function initializeNodeDepth() { |
||||
var ky = d3.min(nodesByBreadth, function(nodes) { |
||||
return (size[1] - (nodes.length - 1) * nodePadding) / d3.sum(nodes, value); |
||||
}); |
||||
|
||||
nodesByBreadth.forEach(function(nodes) { |
||||
nodes.forEach(function(node, i) { |
||||
node.y = i; |
||||
node.dy = node.value * ky; |
||||
}); |
||||
}); |
||||
|
||||
links.forEach(function(link) { |
||||
link.dy = link.value * ky; |
||||
}); |
||||
} |
||||
|
||||
function relaxLeftToRight(alpha) { |
||||
nodesByBreadth.forEach(function(nodes, breadth) { |
||||
nodes.forEach(function(node) { |
||||
if (node.targetLinks.length) { |
||||
var y = d3.sum(node.targetLinks, weightedSource) / d3.sum(node.targetLinks, value); |
||||
node.y += (y - center(node)) * alpha; |
||||
} |
||||
}); |
||||
}); |
||||
|
||||
function weightedSource(link) { |
||||
return center(link.source) * link.value; |
||||
} |
||||
} |
||||
|
||||
function relaxRightToLeft(alpha) { |
||||
nodesByBreadth.slice().reverse().forEach(function(nodes) { |
||||
nodes.forEach(function(node) { |
||||
if (node.sourceLinks.length) { |
||||
var y = d3.sum(node.sourceLinks, weightedTarget) / d3.sum(node.sourceLinks, value); |
||||
node.y += (y - center(node)) * alpha; |
||||
} |
||||
}); |
||||
}); |
||||
|
||||
function weightedTarget(link) { |
||||
return center(link.target) * link.value; |
||||
} |
||||
} |
||||
|
||||
function resolveCollisions() { |
||||
nodesByBreadth.forEach(function(nodes) { |
||||
var node, |
||||
dy, |
||||
y0 = 0, |
||||
n = nodes.length, |
||||
i; |
||||
|
||||
// Push any overlapping nodes down.
|
||||
nodes.sort(ascendingDepth); |
||||
for (i = 0; i < n; ++i) { |
||||
node = nodes[i]; |
||||
dy = y0 - node.y; |
||||
if (dy > 0) node.y += dy; |
||||
y0 = node.y + node.dy + nodePadding; |
||||
} |
||||
|
||||
// If the bottommost node goes outside the bounds, push it back up.
|
||||
dy = y0 - nodePadding - size[1]; |
||||
if (dy > 0) { |
||||
y0 = node.y -= dy; |
||||
|
||||
// Push any overlapping nodes back up.
|
||||
for (i = n - 2; i >= 0; --i) { |
||||
node = nodes[i]; |
||||
dy = node.y + node.dy + nodePadding - y0; |
||||
if (dy > 0) node.y -= dy; |
||||
y0 = node.y; |
||||
} |
||||
} |
||||
}); |
||||
} |
||||
|
||||
function ascendingDepth(a, b) { |
||||
return a.y - b.y; |
||||
} |
||||
} |
||||
|
||||
function computeLinkDepths() { |
||||
nodes.forEach(function(node) { |
||||
node.sourceLinks.sort(ascendingTargetDepth); |
||||
node.targetLinks.sort(ascendingSourceDepth); |
||||
}); |
||||
nodes.forEach(function(node) { |
||||
var sy = 0, ty = 0; |
||||
node.sourceLinks.forEach(function(link) { |
||||
link.sy = sy; |
||||
sy += link.dy; |
||||
}); |
||||
node.targetLinks.forEach(function(link) { |
||||
link.ty = ty; |
||||
ty += link.dy; |
||||
}); |
||||
}); |
||||
|
||||
function ascendingSourceDepth(a, b) { |
||||
return a.source.y - b.source.y; |
||||
} |
||||
|
||||
function ascendingTargetDepth(a, b) { |
||||
return a.target.y - b.target.y; |
||||
} |
||||
} |
||||
|
||||
function center(node) { |
||||
return node.y + node.dy / 2; |
||||
} |
||||
|
||||
function value(link) { |
||||
return link.value; |
||||
} |
||||
|
||||
return sankey; |
||||
}; |
@ -0,0 +1,34 @@
@@ -0,0 +1,34 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<xsl:stylesheet version="1.0" |
||||
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:HtmlUtils="de.superx.util.HtmlUtils"> |
||||
<xsl:import href="xsl_functions.xsl" /> |
||||
<xsl:import href="resultset_html.xsl" /> |
||||
<xsl:import href="interLinks_html.xsl" /> |
||||
<xsl:import href="pageComponents_html.xsl" /> |
||||
<xsl:import href="pageComponents_html_final.xsl" /> |
||||
<xsl:import href="viz_html_chart.xsl" /> |
||||
<xsl:import href="menue_html_dojo.xsl" /> |
||||
<!--In diesem Stylesheet können Sie individuelle templates unterbringen, |
||||
die in ihrer Präzedenz das normale Stylesheet |
||||
pageComponents_html.xsl überragt. --> |
||||
<xsl:import href="pageComponents_html_final.xsl" /> |
||||
|
||||
<xsl:decimal-format name="German" grouping-separator="." NaN="" zero-digit ="0" decimal-separator="," /> |
||||
|
||||
<!-- wichtig für DOJO!--> |
||||
<xsl:output method="xml" media-type="text/html" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" |
||||
doctype-system="DTD/xhtml1-strict.dtd" cdata-section-elements="script style" indent="yes" encoding="UTF-8"/> |
||||
<xsl:variable name="defaultRenderer" select="'d3jsv3'" /> |
||||
<xsl:variable name="availableRendererPlot" select="'false'" /> |
||||
<xsl:variable name="availableRendererD3" select="'false'" /> |
||||
<xsl:variable name="availableRendererD3V3" select="'true'" /> |
||||
|
||||
<xsl:template match="/"> |
||||
<xsl:call-template name="table"/> |
||||
</xsl:template> |
||||
<xsl:template name="importVizJavascriptLibs"> |
||||
<script language="Javascript" type="text/javascript" src="../xml/js/viz/d3.v3.min.js" /> |
||||
<script language="Javascript" type="text/javascript" src="../xml/js/viz/viz_functions.js" /> |
||||
<script language="Javascript" type="text/javascript" src="../xml/js/viz/sankey_v3.js" /> |
||||
</xsl:template> |
||||
</xsl:stylesheet> |
Loading…
Reference in new issue