From 621aed87ce3a8f9751c3183630f4a59d65dc4806 Mon Sep 17 00:00:00 2001 From: Alexey Velikiy Date: Wed, 1 Mar 2017 23:56:49 +0100 Subject: [PATCH] basic bubbles --- .../bubbles-nested-routes/bubbles.vue | 24 +++--- .../bubbles-nested-routes/ph2.js | 73 ++++++++++++++++--- static/{ => data}/earthquakes.csv | 0 3 files changed, 75 insertions(+), 22 deletions(-) rename static/{ => data}/earthquakes.csv (100%) diff --git a/src/d3-components/bubbles-nested-routes/bubbles.vue b/src/d3-components/bubbles-nested-routes/bubbles.vue index f4c1f12..1cac9b9 100644 --- a/src/d3-components/bubbles-nested-routes/bubbles.vue +++ b/src/d3-components/bubbles-nested-routes/bubbles.vue @@ -7,7 +7,7 @@ Links: --> diff --git a/src/d3-components/bubbles-nested-routes/ph2.js b/src/d3-components/bubbles-nested-routes/ph2.js index 89ac4d8..5ef9887 100644 --- a/src/d3-components/bubbles-nested-routes/ph2.js +++ b/src/d3-components/bubbles-nested-routes/ph2.js @@ -1,23 +1,76 @@ -const d3 = require('d3'); +// http://vallandingham.me/bubble_charts_with_d3v4.html +const d3 = require('d3'); // TODO: do it d3-way so I can use selection.call() - - export default class PH2 { - constructor(elem, layoutName) { - elem - .append('rect') - .attr('x', 10) - .attr('y', 10) - .attr('width', 10) - .attr('height', 10) + constructor(elem, data, layoutName) { + this.svg = elem; + this.setData(data); + this._init(); + this.setLayout(layoutName); } setLayout(layoutName) { + // TODO: something special + this.simulation.alpha(1).restart(); + } + + setData(data) { + var range = data.length; + this.data = { + nodes:data.map(d => ({ + label: "l"+d ,r:+d.Magnitude + })), + links:d3.range(0, range).map(function(){ return {source:~~d3.randomUniform(range)(), target:~~d3.randomUniform(range)()} }) + }; + } + _init() { + this.simulation = d3.forceSimulation() + .force("link", d3.forceLink().id(d => d.index)) + .force("collide", d3.forceCollide(d => d.r + 5).iterations(16) ) + .force("center", d3.forceCenter(0, 0)) + + var link = this.svg.append("g") + .attr("class", "links") + .selectAll("line") + .data(this.data.links) + .enter() + .append("line") + .attr("stroke", "black") + + var node = this.svg.append("g") + .attr("class", "nodes") + .selectAll("circle") + .data(this.data.nodes) + .enter() + .append("circle") + .attr("r", d => d.r ) + + var ticked = function() { + link + .attr("x1", d => d.source.x ) + .attr("y1", d => d.source.y ) + .attr("x2", d => d.target.x ) + .attr("y2", d => d.target.y ); + + node + .attr("cx", d => d.x ) + .attr("cy", d => d.y ); + } + + this.simulation + .nodes(this.data.nodes) + .on("tick", ticked); + + this.simulation.force("link") + .links(this.data.links); } + _initNodeYearPos() { + // TODO: fill + } } diff --git a/static/earthquakes.csv b/static/data/earthquakes.csv similarity index 100% rename from static/earthquakes.csv rename to static/data/earthquakes.csv