xxxxxxxxxx
<meta charset="utf-8">
<style>
text {
font: 10px sans-serif;
}
g .youyou {
font: 20px sans-serif;
}
tspan:last-child {
font-size: 9px;
fill-opacity: 0.8;
}
.node rect {
shape-rendering: crispEdges;
}
.node--hover rect {
stroke: #000;
}
</style>
<svg width="995" height="400"></svg>
<script src="//d3js.org/d3.v4.0.0-alpha.35.min.js"></script>
<script>
//https://www.w3schools.com/tags/ref_colornames.asp
var svg = d3.select("svg"),
width = +svg.attr("width")/2,
height = +svg.attr("height");
var svg1 = d3.select("body").append("svg").attr("width", 960).attr("height", 1060);
var container1 = svg.append('g').attr("id","sommaire");
var container2 = svg1.append('g').attr("id","arbre");
var textActif=false;
var texte = container1.append("text")
.text("Taille occupées par les différents types de fichier")
.attr('x',500)
.attr('y',50)
.attr("class","youyou")
.attr("font-size", "30px")
.attr("font-family","Times New Roman")
.attr("fill", "#2F4F4F");
var format = d3.format(",d");
var color = d3.scaleInferno()
.domain([-4, 4]);
var stratify = d3.stratify()
.parentId(function(d) { return d.id.substring(0, d.id.lastIndexOf(".")); });
var treemap = d3.treemap()
.size([width, height])
.paddingOuter(3)
.paddingTop(19)
.paddingInner(1)
.round(true);
/////////////////////////////////////////////////////
/* var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height"),
g = svg*/
//container2.append("g1").attr("transform", "translate(" + (960 / 2 + 40) + "," + (1000) + ")");
g1= svg1.append("g").attr("transform", "translate(" + (960 / 2 + 40) + "," + (400) + ")");
var stratify1 = d3.stratify()
.parentId(function(d) { return d.id.substring(0, d.id.lastIndexOf(".")); });
var tree = d3.tree()
.size([350, 300])
.separation(function(a, b) { return (a.parent == b.parent ? 1 : 2) / a.depth; });
///////////////////////////////////////////////////
d3.csv("test.csv", function(error, data) {
if (error) throw error;
var root = tree(stratify1(data));
var link = g1.selectAll(".link")
.data(root.descendants().slice(1))
.enter().append("path")
.attr("class", "link")
.attr("d", function(d) {
return "M" + project(d.x, d.y)
+ "C" + project(d.x, (d.y + d.parent.y) / 2)
+ " " + project(d.parent.x, (d.y + d.parent.y) / 2)
+ " " + project(d.parent.x, d.parent.y);
});
var node = g1.selectAll(".node")
.data(root.descendants())
.enter().append("g")
.attr("class", function(d) { return "node" + (d.children ? " node--internal" : " node--leaf"); })
.attr("transform", function(d) { return "translate(" + project(d.x, d.y) + ")"; });
node.append("circle")
.attr("r", 2.5);
node.append("text")
.attr("dy", ".31em")
.attr("x", function(d) { return d.x < 180 === !d.children ? 6 : -6; })
.style("text-anchor", function(d) { return d.x < 180 === !d.children ? "start" : "end"; })
.attr("transform", function(d) { return "rotate(" + (d.x < 180 ? d.x - 90 : d.x + 90) + ")"; })
.text(function(d) { return d.id.substring(d.id.lastIndexOf(".") + 1); });
});
function project(x, y) {
var angle = (x - 90) / 180 * Math.PI, radius = y;
return [radius * Math.cos(angle), radius * Math.sin(angle)];
}
d3.csv("test.csv", 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))
.on("click", function(d){
})
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 color2(d); });
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(".") + 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(".") + 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; });
var dossiers=d.id.split(".");
var dossier="Le lien relatif:"+"\n";
for (i = 0; i < dossiers.length; i++) {
dossier+="/"+dossiers[i];
}
svg.selectAll("#myText").remove();
container1.append("text")
.text(dossier)
.attr('x',500)
.attr('y',124)
.attr("id", "myText")
.attr("font-size", "10px")
.attr("font-family","Times New Roman")
.attr("fill", "#2F4F4F");
};
}
function color2(d) {
//console.log(d.id);
if (d.id.split("(")[1]=="pdf)")
return "#7FFFD4";
else if (d.id.split("(")[1]=="txt)")
return "#DC143C";
else if (d.id.split("(")[1]=="JPEG)")
return "#FFF8DC";
else if (d.id.split("(")[1]=="JPG)")
return "#FAFAD2";
else return color(d.depth);
}
function afficherArbre(d) {
}
</script>
https://d3js.org/d3.v4.0.0-alpha.35.min.js