D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
MelanieSalignat
Full window
Github gist
Data Mining
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <style> .links line { stroke: #999; stroke-opacity: 0.6; } .nodes circle { stroke: #fff; stroke-width: 1.5px; } </style> <svg width="960" height="600"></svg> <script src="https://d3js.org/d3.v4.min.js"></script> <script> var svg = d3.select("svg"), width = +svg.attr("width"), height = +svg.attr("height"); var color = d3.scaleOrdinal(d3.schemeCategory20); var tooltip = d3.select('body').append('div') .attr('class', 'hidden tooltip'); var simulation = d3.forceSimulation() .force("link", d3.forceLink().id(function(d) { return d.artist; })) .force("charge", d3.forceManyBody().distanceMax(200)) .force("center", d3.forceCenter(width / 2, height / 2)); /* d3.csv("recommandations.csv", function(data){ // console.log("data " + data[2].Antecedent) var links = []; var nodes = new Set(); for (var i=0; i< data.length; i++){ // spliter Consequence avant de parcourir les données for (var j=0; j<10; j++){ links.push({source: data[i].Antecedent, target: data[i].Consequence[j], value: data[i].Value[j]}) nodes.add(data[i].Consequence[j]); } nodes.add(data[i].Antecedent); } var nodesArray = Array.from(nodes).map(function(e){return {film: e}}); for (i=0; i<nodesArray.length; i++){ recommandations=[]; for (j=0; j<links.length;j++){ if (nodesArray[i].film == links[j].source){ recommandations.push(links[j].target) } } nodesArray[i].rec = recommandations ; } var link = svg.append("g") .attr("class", "links") .selectAll("line") .data(links) .enter().append("line") .attr("stroke", "grey"); var node = svg.append("g") .attr("class", "nodes") .selectAll("circle") .data(nodesArray) .enter().append("circle") .attr("r", 7) .attr("fill", "grey") .style("opacity", 0.5) .call(d3.drag() .on("start", dragstarted) .on("drag", dragged) .on("end", dragended)) .on("mouseover", function(d) { // d = nodesArray[this] var mouse = d3.mouse(svg.node()).map(function(d) { return parseInt(d); }); d3.select(this) .style("fill", "red") .attr("r", 20) .style("opacity", 1) for (j=0; j<d.rec.length; i++){ for (k=0; k<nodesArray.length; k++){ if (d.rec[j] == nodesArray[k].artist){ d3.select(nodesArray[k]) .style("fill", "red") .attr("r", 15) .style("opacity", 1) } } } /*tooltip.classed('hidden', false) .attr("y", this.y) .attr("x", this.x) .attr('style', 'left:' + (mouse[0] + 15) + 'px; top:' + (mouse[1] - 5) + 'px') .style("text-anchor", "start") .style("font-size","25px") .style("font-weight", "normal") .html("Artiste : " + d.artist + "<br>Tags : " ) }) .on("mouseout", function(d){ d3.select(this).style("fill","grey").attr("r", 7).style("opacity",0.5) tooltip.classed('hidden', true); }) ; node.append("title") .text(function(d) { return d.film; }); simulation .nodes(nodesArray) .on("tick", ticked); simulation.force("link") .links(links); function ticked() { link .attr("x1", function(d) { return d.source.x; }) .attr("y1", function(d) { return d.source.y; }) .attr("x2", function(d) { return d.target.x; }) .attr("y2", function(d) { return d.target.y; }); node .attr("cx", function(d) { return d.x; }) .attr("cy", function(d) { return d.y; }); } }) */ ; function dragstarted(d) { if (!d3.event.active) simulation.alphaTarget(0.3).restart(); d.fx = d.x; d.fy = d.y; } function dragged(d) { d.fx = d3.event.x; d.fy = d3.event.y; } function dragended(d) { if (!d3.event.active) simulation.alphaTarget(0); d.fx = null; d.fy = null; } </script>
https://d3js.org/d3.v4.min.js