{% extends "base.html" %}
{% block content %}
<div id=p4treemap class=draw>
<h1>Perforce Server Tree Map</h1>
{% from "_formhelpers.html" import render_field %}
<form class="form form-horizontal" action="{{ url_for('treeMap') }}" method="post" role="form">
{{ form.csrf_token }}
{{ render_field(form.p4port, size=60) }}
{{ render_field(form.p4user) }}
{{ render_field(form.root, size=60) }}
{{ render_field(form.path, size=60) }}
<p>{{ form.submit() }}</p>
</form>
{% if treedata %}
<h2>Tree Map: '{{ something }}'</h2>
<svg width="960" height="570"></svg>
{% endif %}
</div>
<footer>
{% if treedata %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.11.0/d3.min.js"></script>
<script>
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var sep = "/";
var format = d3.format(",d");
var color = d3.scaleSequential(d3.interpolatePlasma)
.domain([-8, 8]);
var stratify = d3.stratify()
.parentId(function(d) { return d.id.substring(0, d.id.lastIndexOf(sep)); });
var treemap = d3.treemap()
.size([width, height])
.paddingOuter(3)
.paddingTop(19)
.paddingInner(1)
.round(true);
d3.tsv("{{ treedata|safe }}", function(error, data) {
if (error) throw error;
var root = stratify(data)
.sum(function(d) { return d.value; })
.sort(function(a, b) { return b.height - a.height || b.value - a.value; });
treemap(root);
var cell = svg
.selectAll(".node")
.data(root.descendants())
.enter().append("g")
.attr("transform", function(d) { return "translate(" + d.x0 + "," + d.y0 + ")"; })
.attr("class", "node")
.each(function(d) { d.node = this; })
.on("mouseover", hovered(true))
.on("mouseout", hovered(false));
cell.append("rect")
.attr("id", function(d) { return "rect-" + d.id; })
.attr("width", function(d) { return d.x1 - d.x0; })
.attr("height", function(d) { return d.y1 - d.y0; })
.style("fill", function(d) { return color(d.depth); });
cell.append("clipPath")
.attr("id", function(d) { return "clip-" + d.id; })
.append("use")
.attr("xlink:href", function(d) { return "#rect-" + d.id + ""; });
var label = cell.append("text")
.attr("clip-path", function(d) { return "url(#clip-" + d.id + ")"; });
label
.filter(function(d) { return d.children; })
.selectAll("tspan")
.data(function(d) { return d.id.substring(d.id.lastIndexOf(sep) + 1).split(/(?=[A-Z][^A-Z])/g).concat("\xa0" + format(d.value)); })
.enter().append("tspan")
.attr("x", function(d, i) { return i ? null : 4; })
.attr("y", 13)
.text(function(d) { return d; });
label
.filter(function(d) { return !d.children; })
.selectAll("tspan")
.data(function(d) { return d.id.substring(d.id.lastIndexOf(sep) + 1).split(/(?=[A-Z][^A-Z])/g).concat(format(d.value)); })
.enter().append("tspan")
.attr("x", 4)
.attr("y", function(d, i) { return 13 + i * 10; })
.text(function(d) { return d; });
cell.append("title")
.text(function(d) { return d.id + "\n" + format(d.value); });
});
function hovered(hover) {
return function(d) {
d3.selectAll(d.ancestors().map(function(d) { return d.node; }))
.classed("node--hover", hover)
.select("rect")
.attr("width", function(d) { return d.x1 - d.x0 - hover; })
.attr("height", function(d) { return d.y1 - d.y0 - hover; });
};
}
</script>
{% endif %}
</footer>
{% endblock %}