https://github.com/d3/d3
https://observablehq.com/@d3/force-directed-graph
https://thecodingtutorials.blogspot.com/2012/07/introduction-to-d3.html
Some messing around with animation
-
<html>
-
-
<head>
-
<script src="https://d3js.org/d3.v5.min.js"></script>
-
-
<script type="text/javascript">
-
window.onload = function()
-
{
-
//Create the SVG graph.
-
var svg = d3.select("body").append("svg").attr("width", "100%").attr("height", "100%");
-
-
-
-
//Add data to the graph and call enter.
-
var dataEnter = svg.selectAll("rect").data([8, 22, 31, 36, 48, 17, 25]).enter();
-
-
-
-
//The height of the graph (without text).
-
var graphHeight = 450;
-
-
//The width of each bar.
-
var barWidth = 80;
-
-
//The distance between each bar.
-
var barSeparation = 10;
-
-
//The maximum value of the data.
-
var maxData = 50;
-
-
-
-
//The actual horizontal distance from drawing one bar rectangle to drawing the next.
-
var horizontalBarDistance = barWidth + barSeparation;
-
-
-
//The horizontal and vertical ofsvg.selectAll("rect")fsets of the text that displays each bar's value.
-
var textXOffset = horizontalBarDistance / 2 - 12;
-
var textYOffset = 20;
-
-
-
//The value to multiply each bar's value by to get its height.
-
var barHeightMultiplier = graphHeight / maxData;
-
-
//The actual Y position of every piece of text.
-
var textYPosition = graphHeight + textYOffset;
-
-
-
//Draw the bars.
-
dataEnter.append("rect").attr("x", function(d, i)
-
{
-
return i * horizontalBarDistance;
-
}).attr("y", function(d)
-
{
-
return graphHeight - d * barHeightMultiplier;
-
}).attr("width", function(d)
-
{
-
return barWidth;
-
}).attr("height", function(d)
-
{
-
return d * barHeightMultiplier;
-
}).attr("id", function(d, i)
-
{
-
return "dd" + i;
-
});
-
-
-
-
//Draw the text.
-
dataEnter.append("text").text(function(d)
-
{
-
return d;
-
}).attr("x", function(d, i)
-
{
-
return i * horizontalBarDistance + textXOffset;
-
}).attr("y", textYPosition);
-
-
var mySquare=svg.append("rect")
-
.attr("x",60)
-
.attr("y",60)
-
.attr("width",60)
-
.attr("height",60)
-
.attr("id",function(d, i)
-
{
-
console.log('d',i)
-
return "dd7";
-
});
-
-
mySquare
-
.transition()
-
.attr("x",320)
-
-
mySquare
-
.transition()
-
.attr("width",120); // will make it bigger
-
-
mySquare
-
.style("fill","blue") // if the fill is originally left blank and comes
-
// from a style sheet, it will start as black
-
.transition()
-
.style("fill","blue");
-
-
mySquare
-
.transition()
-
.style("opacity",0.3);
-
-
var right = [false,false,false,false,false,false,false,false,false,false]
-
-
var mof = function(cur, i) {
-
console.log(cur, i)
-
var d = 0
-
var w = 100
-
if (right[i]) {
-
d = 320 + (i * w)
-
} else {
-
d = 0 + (i * w)
-
}
-
right[i] = !right[i]
-
svg.select("#dd" + i).transition().attr("x",d);
-
// mySquare.transition().attr("x",d);
-
}
-
-
svg.selectAll("rect").on("mouseover", mof)
-
-
}
-
-
-
-
-
-
</script>
-
-
</head>
-
-
<body>
-
-
</body>
-
-
</html>
RJM Article Type
Work Notes