Built with blockbuilder.org
forked from silkwaffle's block: test movement block
forked from silkwaffle's block: test movement block
xxxxxxxxxx
<meta charset="utf-8">
<style> /* set the CSS */
.bar { fill: steelblue; }
</style>
<body>
<!-- load the d3.js library -->
<script src="//d3js.org/d3.v4.min.js"></script>
<script>
// set the dimensions and margins of the graph
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 600 - margin.top - margin.bottom;
// set the ranges
var x = d3.scaleBand()
.range([0, width])
.padding(0.1);
var y = d3.scaleLinear()
.range([height, 0]);
// append the svg object to the body of the page
// append a 'group' element to 'svg'
// moves the 'group' element to the top left margin
var svg = d3.select("body").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 + ")");
//gradient?
var defs = svg.append("defs");
var linearGradient = defs.append("linearGradient")
.attr("id", "linear-gradient")
.attr("x1", "0%")
.attr("y1", "0%")
.attr("x2", "100%")
.attr("y2", "100%");
linearGradient.append("stop")
.attr("offset", "0%")
.attr("stop-color", "#da75ff");
linearGradient.append("stop")
.attr("offset", "100%")
.attr("stop-color", "#0d0089");
// get the data
d3.csv("test.csv", function(error, data) {
if (error) throw error;
// format the data
data.forEach(function(d) {
d.amount = +d.amount;
});
// Scale the range of the data in the domains
x.domain(data.map(function(d) { return d.name; }));
y.domain([0, d3.max(data, function(d) { return d.amount; })]);
// append the rectangles for the bar chart
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.style("fill", "url(#linear-gradient)")
.attr("x", function(d) { return x(d.name); })
.attr("width", x.bandwidth())
.attr("y", function(d) { return y(d.amount); })
.attr("height", function(d) { return height - y(d.amount); });
// add the x Axis
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// add the y Axis
svg.append("g")
.call(d3.axisLeft(y));
});
var rech = 50
var recw = 100
var whatever = [{x: width - margin.right - recw, y:0}];
//this is the box element
var group = svg.append("g")
.data(whatever)
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged));
group.append("rect")
.attr("x", function(d) { return d.x; })
.attr("y", function(d) { return d.y; })
.attr("height", rech)
.attr("width", recw)
.style("fill", "purple");
group.append("text")
.attr("x", function(d) { return d.x; })
.attr("y", function(d) { return d.y + rech/2; })
.style("fill", "white")
.text("Close");
//drag functions
function dragstarted(d) {
d3.select(this).raise();
}
function dragged(d) {
d3.select(this).select("rect")
.attr("x", d.x = Math.max(0, Math.min(width, d3.event.x)))
.attr("y", d.y = Math.max(0, Math.min(height, d3.event.y)))
d3.select(this).select("text")
.attr("x", d.x = Math.max(0, Math.min(width, d3.event.x)))
.attr("y", d.y = Math.max(0, Math.min(height, d3.event.y + rech/2)));;
}
</script>
</body>
https://d3js.org/d3.v4.min.js