Built with blockbuilder.org
Last active
May 30, 2016 19:17
-
-
Save wwymak/e4cbafda76988ca707100e3e0ce2d127 to your computer and use it in GitHub Desktop.
experimenting with d3v4 force params
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const bikeFeedUrl = 'https://gbfs.citibikenyc.com/gbfs/en/station_status.json'; | |
| const colorScale1 = d3.scaleCategory20(); | |
| var width = 700; | |
| var height = 600; | |
| var foci = [{id: 0, x: width * 0.5, y:height * 0.5} ]; | |
| let radius = Math.min(width, height) * 0.4; | |
| d3.range(10).map(d => { | |
| foci.push({ | |
| id: d + 1, | |
| x: radius * Math.sin(Math.PI * d/ 5) + width * 0.5, | |
| y: radius * Math.cos(Math.PI * d/ 5) + height * 0.5 | |
| }) | |
| }); | |
| var canvas = document.getElementById("canvas"), | |
| context = canvas.getContext("2d"), | |
| tau = 2 * Math.PI; | |
| context.canvas.width = width; | |
| context.canvas.height = height; | |
| d3.json(bikeFeedUrl, (err, data) => { | |
| let bikeData = data.data.stations; | |
| let updatedTime = data['last_updated']; | |
| let testSet = bikeData.slice(0, 10); | |
| let nodeArr = testSet.map((d, i) => { | |
| let numBikes = d.num_bikes_available; | |
| let focalInfo = foci.filter(j => j.id == i + 1)[0]; | |
| if(!focalInfo){ | |
| console.log( i) | |
| return | |
| } | |
| let nodes = d3.range(numBikes).map(e => { | |
| return { | |
| group: d.station_id, | |
| x: focalInfo.x + (Math.random() - 0.5) * 10, | |
| y: focalInfo.y + (Math.random() - 0.5) * 10, | |
| radius: 5, | |
| color: colorScale1(i), | |
| id: `${e.toString()}_${d.station_id}`, | |
| forceCenter: [focalInfo.x, focalInfo.y] | |
| } | |
| }); | |
| return nodes; | |
| }); | |
| var nodes = [].concat.apply([], nodeArr); | |
| var simulation = d3.forceSimulation(nodes) | |
| .drag(0.02) | |
| .alphaDecay(0) | |
| .force("x", d3.forceX().x(d => d.forceCenter[0]).strength(0.1)) | |
| .force("y", d3.forceY().y(d => d.forceCenter[1]).strength(0.1)) | |
| .force("collide", d3.forceCollide().radius(d => d.radius + 1.5).iterations(1)); | |
| simulation.on('tick', ticked); | |
| d3.select("#nAlphaDecay").on('input', function () { | |
| d3.select("#nAlphaDecay-value").text(this.value); | |
| simulation.alphaDecay(this.value); | |
| reheatSimulation(simulation); | |
| }); | |
| d3.select("#nDrag").on('input', function () { | |
| simulation.drag(this.value); | |
| reheatSimulation(simulation); | |
| d3.select("#nDrag-value").text(this.value) | |
| }); | |
| d3.select("#nPostionStrength").on('input', function () { | |
| simulation | |
| .force("x", d3.forceX().x(d => d.forceCenter[0]).strength(this.value)) | |
| .force("y", d3.forceY().y(d => d.forceCenter[1]).strength(this.value)); | |
| reheatSimulation(simulation); | |
| d3.select("#nPostionStrength-value").text(this.value) | |
| }); | |
| function ticked() { | |
| context.clearRect(0, 0, width, height); | |
| context.save(); | |
| nodes.forEach(d => { | |
| context.beginPath(); | |
| context.moveTo(d.x + d.radius, d.y); | |
| context.arc(d.x, d.y, d.radius, 0, tau); | |
| context.fillStyle = d.color; | |
| context.fill(); | |
| context.strokeStyle = d.color; | |
| context.stroke(); | |
| }); | |
| context.restore(); | |
| } | |
| function reheatSimulation(simulation) { | |
| simulation.alphaTarget(0).alpha(1); | |
| simulation.restart(); | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title>Title</title> | |
| <script src="https://d3js.org/d3.v4.0.0-alpha.44.min.js"></script> | |
| </head> | |
| <body> | |
| <canvas id="canvas"></canvas> | |
| <p> | |
| <label for="nAlphaDecay" | |
| style="display: inline-block; width: 240px; text-align: right"> | |
| alphaDecay = <span id="nAlphaDecay-value">0</span> | |
| </label> | |
| <input type="range" min="0" max="0.1" step="0.001" value="0" id="nAlphaDecay"> | |
| </p> | |
| <p> | |
| <label for="nDrag" | |
| style="display: inline-block; width: 240px; text-align: right"> | |
| drag = <span id="nDrag-value">0.02</span> | |
| </label> | |
| <input type="range" min="0" max="0.1" step="0.001" value="0.02" id="nDrag"> | |
| </p> | |
| <p> | |
| <label for="nPostionStrength" | |
| style="display: inline-block; width: 240px; text-align: right"> | |
| positioning strength = <span id="nPostionStrength-value">0.1</span> | |
| </label> | |
| <input type="range" min="0" max="0.5" step="0.001" value="0.1" id="nPostionStrength"> | |
| </p> | |
| <script src="bikeVis.js"></script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment