D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
silkwaffle
Full window
Github gist
test movement block
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <style> .active { stroke: #000; stroke-width: 2px; } .rect { pointer-events: all; stroke: none; stroke-width: 40px; } </style> <body> <script src="//d3js.org/d3.v4.min.js"></script> <script> var margin = {top: 10, right: 10, bottom: 10, left: 30}, width = 700 - margin.left - margin.right, height = 500 - margin.top - margin.bottom, rech = 40, recw = 250; var whatever = [ {x : 50, y : 10}, ]; //dataset var dataset = [ {x: 1, y:5}, {x: 2, y:15}, {x: 10, y:25}, {x: 45, y: 10}, {x: 50, y: 15}, {x: 3, y:100}, {x: 80, y: 55}]; //scale + axis var x = d3.scaleBand() .range([0, width]) .padding(0.1); var y = d3.scaleLinear().range([height,0]); xRange = d3.scaleLinear().range([margin.left, width]).domain([d3.min(dataset, function(d) {return d.x;}), d3.max(dataset, function (d){return d.x})]); yRange = d3.scaleLinear().range([height, margin.bottom]).domain([d3.min(dataset, function(d) {return d.y;}), d3.max(dataset, function (d){return d.y})]); //create svg var svg = d3.select("body").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom); //create 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", "0%"); linearGradient.append("stop") .attr("offset", "0%") .attr("stop-color", "#da75ff"); linearGradient.append("stop") .attr("offset", "100%") .attr("stop-color", "#0d0089"); x.domain(dataset.map(function(d){return d.x;})); y.domain([0,d3.max(dataset, function(d) {return d.y;})]); var chart1 = svg.append("g") .data(dataset) //x-axis chart1.append("g") .attr("transform","translate(0," + (height - margin.bottom) + ")") .call(d3.axisBottom(xRange)); chart1.append("text") .attr("transform","translate(" + (width/2) + " ," + (height + margin.bottom+10)+ ")") .style("text-anchor","middle") .text("x"); //y-axis chart1.append("g") .attr("transform", "translate(20,0)") .call(d3.axisLeft(yRange)); chart1.selectAll(".bar") .data(dataset) .enter().append("rect") .attr("class", "bar") .attr("x", function(d){return xRange(d.x);}) .attr("width", x.bandwidth()) .attr("y", function(d){return yRange(d.y);}) //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", "url(#linear-gradient)"); group.append("text") .attr("x", function(d) { return d.x; }) .attr("y", function(d) { return d.y + (rech/2); }) .style("fill", "url(#linear-gradient)") .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