d3.select(".chart") .selectAll() .data(data) .enter().append("div") .style("width", function(d) { return d * 10 + "px"; });
.chart
or the name of the div
tag), which is extremely inconvenient for our purposes. Fortunately, these methods have alternative signatures. These signatures exist for boring things like sample reuse. We will use them to rewrite our example as follows: function newDiv() { return document.createElement("div"); } var chart = { appendChild: function (child) { // append() newDiv() return document.getElementById("chartId") .appendChild(child); }, querySelectorAll: function () { // selectAll() return []; } } d3.select( chart ) .selectAll() .data(data) .enter().append( newDiv ) .style("width", function(d) { return d * 10 + "px"; });
div
explicitly, and 2) convinced D3 that our chart object is a duck . The result of our code has not changed at all. // D3- .append() .selectAll() THREE.Object3D.prototype.appendChild = function (c) { this.add(c); return c; }; THREE.Object3D.prototype.querySelectorAll = function () { return []; }; // - D3- .attr() THREE.Object3D.prototype.setAttribute = function (name, value) { var chain = name.split('.'); var object = this; for (var i = 0; i < chain.length - 1; i++) { object = object[chain[i]]; } object[chain[chain.length - 1]] = value; }
function newBar () { return new THREE.Mesh( geometry, material ); } chart3d = new THREE.Object3D(); // D3 3D d3.select( chart3d ) .selectAll() .data(data) .enter().append( newBar ) .attr("position.x", function(d, i) { return 30 * (i - 3); }) .attr("position.y", function(d, i) { return d; }) .attr("scale.y", function(d, i) { return d / 10; })
THREE.Object3D.prototype.getAttribute = function (name) { var chain = name.split('.'); var object = this; for (var i = 0; i < chain.length - 1; i++) { object = object[chain[i]]; } return object[chain[chain.length - 1]]; }
THREE.Object3D.prototype.querySelectorAll = function (selector) { var matches = []; var type = eval(selector); for (var i = 0; i < this.children.length; i++) { var child = this.children[i]; if (child instanceof type) { matches.push(child); } } return matches; }
var N = 9, v = 30, data = d3.range(9).map(next); function next () { return (v = ~~Math.max(10, Math.min(90, v + 20 * (Math.random() - .5)))); } setInterval(function () { data.shift(); data.push(next()); update(); }, 1500); function update () { // D3 3D var bars = d3.select( chart3d ) .selectAll("THREE.Mesh") .data(data); bars.enter().append( newBar ) .attr("position.x", function(d, i) { return 30 * (i - N/2); }); bars.transition() .duration(1000) .attr("position.y", function(d, i) { return d; }) .attr("scale.y", function(d, i) { return d / 10; }); }
THREE.Object3D.prototype.removeChild = function (c) { this.remove(c); }
THREE.Object3D.prototype.appendChild = function (c) { this.add(c); // parentNode c.parentNode = this; return c; }
var N = 9, t = 123, v = 30, data = d3.range(9).map(next); function next () { return { time: ++t, value: v = ~~Math.max(10, Math.min(90, v + 20 * (Math.random() - .5))) }; } function update () { // D3 , 3D var bars = d3.select( chart3d ) .selectAll("THREE.Mesh") .data(data, function(d) { return d.time; }); bars.transition() .duration(1000) .attr("position.x", function(d, i) { return 30 * (i - N / 2); }) bars.enter().append( newBar ) .attr("position.x", function(d, i) { return 30 * (i - N / 2 + 1); }) .attr("position.y", 0) .attr("scale.y", 1e-3) .transition() .duration(1000) .attr("position.x", function(d, i) { return 30 * (i - N / 2); }) .attr("position.y", function(d, i) { return d.value; }) .attr("scale.y", function(d, i) { return d.value / 10; }) bars.exit().transition() .duration(1000) .attr("position.x", function(d, i) { return 30 * (i - N / 2 - 1); }) .attr("position.y", 0) .attr("scale.y", 1e-3) .remove() }
Source: https://habr.com/ru/post/200584/
All Articles