<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>d3 on Tom Roth</title><link>https://tomroth.dev/tags/d3/</link><description>Recent content in d3 on Tom Roth</description><generator>Hugo -- gohugo.io</generator><language>en</language><lastBuildDate>Wed, 24 May 2017 00:00:00 +0000</lastBuildDate><atom:link href="https://tomroth.dev/tags/d3/index.xml" rel="self" type="application/rss+xml"/><item><title>Simulating the multi-armed bandit problem</title><link>https://tomroth.dev/bandit/</link><pubDate>Wed, 24 May 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/bandit/</guid><description>Machine learning can be split up into many subfields. One of these subfields growing in relevance is reinforcement learning.
One of the core problems in reinforcement learning is the multi-armed bandit problem. This problem has been well studied and is commonly used to explore the tradeoff between exploration and exploitation integral to reinforcement learning.
To illustrate this tradeoff and to visualise different ways of solving the multi-armed bandit problem, I created a simulation using the JavaScript library d3.</description></item><item><title>Force directed graphs: testing ground</title><link>https://tomroth.dev/fdg-testing/</link><pubDate>Mon, 22 May 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/fdg-testing/</guid><description>I&amp;rsquo;ve written a decent amount of material about force directed graphs. This included posts for each of the forces that the d3-force module comes shipped with.
I found that when I read about these forces, text-based explanations never really solidified the concepts for me. To really get a sense of how the forces worked, I had to build lots of force graphs and tweak the parameters manually to see what each of them did.</description></item><item><title>Force directed graph: zoomable</title><link>https://tomroth.dev/fdg-zoom/</link><pubDate>Thu, 20 Apr 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/fdg-zoom/</guid><description>This example builds off the d3-zoom articles to add zoom functionality onto our force directed graph. If you&amp;rsquo;ve got questions on how zoom works or what this code is really doing, check those articles out!
Once you know what&amp;rsquo;s going on, adding zoom to force directed graph is really simple.
Most of the code in the above example (click on the picture to see it) I&amp;rsquo;ve covered in other articles, so I won&amp;rsquo;t go over it again.</description></item><item><title>How to combine drag and zoom in d3 version 4</title><link>https://tomroth.dev/drag-zoom/</link><pubDate>Tue, 18 Apr 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/drag-zoom/</guid><description>Combining drag and zoom should be an easy task, and it is. But only if you do it right.
In this article I&amp;rsquo;ll present two ways to apply drag and zoom to your d3 visualisations. One way is easy and simple, and one way is complicated and difficult, and yet both examples hold key insights.
The easy way Link to heading Mike Bostock has written a simple example where he combines drag and zoom.</description></item><item><title>Integrating Javascript and d3 into R notebooks</title><link>https://tomroth.dev/r-d3/</link><pubDate>Thu, 13 Apr 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/r-d3/</guid><description>Recently the team over at RStudio released R Notebooks.
R Notebooks are really interesting. Here&amp;rsquo;s why:
You can write code in several different languages in one document (including python, bash, dot, ruby, javascript, sql) You can write in Markdown You can write in HTML You can see your code as it runs Writing reproducible code is easier It&amp;rsquo;s easy to share code results with others It&amp;rsquo;s the intertwining of languages bit that interests me the most.</description></item><item><title>Zoom in d3 version 4: selective zooming</title><link>https://tomroth.dev/zoom-selective/</link><pubDate>Fri, 07 Apr 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/zoom-selective/</guid><description>Not everything has to enable zoom. You can selectively pick and choose which objects you&amp;rsquo;d like to enable zoom on.
Consider this tale of two circles.
We create two circles which we call circle1 and circle2. Circle1 is the circle on the left and circle2 is the circle on the right.
Here&amp;rsquo;s the interesting bit. We define our zoom handler and set what happens when a zoom action is detected.</description></item><item><title>Zoom in d3 version 4: minimal example</title><link>https://tomroth.dev/zoom-minimal/</link><pubDate>Mon, 03 Apr 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/zoom-minimal/</guid><description>In this post we&amp;rsquo;ll make a minimal example to demonstrate how zoom works.
I&amp;rsquo;ll use a similar setup to this other example that was used to demonstrate drag - create circles on the screen at random and then see if we can zoom in on them.
Nice and simple.
Setup Link to heading First we&amp;rsquo;ll set up a simple scaffold for the example:
&amp;lt;!DOCTYPE html&amp;gt; &amp;lt;meta charset=&amp;#34;utf-8&amp;#34;&amp;gt; &amp;lt;svg width=&amp;#34;960&amp;#34; height=&amp;#34;600&amp;#34;&amp;gt;&amp;lt;/svg&amp;gt; &amp;lt;script src=&amp;#34;https://d3js.</description></item><item><title>Zoom in d3 version 4: understanding SVG transforms</title><link>https://tomroth.dev/zoom-svg/</link><pubDate>Wed, 29 Mar 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/zoom-svg/</guid><description>Zooming in d3 version 4 isn&amp;rsquo;t straightforward to pick up. Far from it.
Just like force graphs, zoom in d3 has been separated into its own module. Understanding the API documents can be a little&amp;hellip;confusing.
Things start to make more sense once you realise that zoom is intricately linked to SVG transforms, and that zooming and panning can just be thought of as changing the transforms on SVG elements.
So step one to understand zooming is to understand how the SVG transform works.</description></item><item><title>Force directed graph: popping</title><link>https://tomroth.dev/fdg-popping/</link><pubDate>Sat, 18 Mar 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/fdg-popping/</guid><description>This graph exploits the unstable effects of setting the forceCollide strength parameter outside of [0,1].
//High strength value causes unstable effects var collide_force = d3.forceCollide(radius) .strength(10); The result is a kind of &amp;ldquo;popping effect&amp;rdquo; as the nodes violently push each other away.
Here&amp;rsquo;s another example.
This graph takes out the link force, sets the strength parameter for forceCollide to an unstable value, and implements an attractive force between particles.</description></item><item><title>Force directed graph: bounding box</title><link>https://tomroth.dev/fdg-bounding-box/</link><pubDate>Fri, 17 Mar 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/fdg-bounding-box/</guid><description>In this example we&amp;rsquo;ll keep the force directed graph constrained inside a box. The nodes won&amp;rsquo;t be able to go outside.
The basic gist of it is that you impose a constraint that takes effect on every tick of the simulation. You can either put this code inside the tick function, or you can define a custom force to do the work for you.
Let&amp;rsquo;s go through both methods.
Method 1: use the tick function Link to heading The first method is to put the constraints inside the tick function.</description></item><item><title>Force directed graph: custom forces</title><link>https://tomroth.dev/fdg-custom/</link><pubDate>Fri, 17 Mar 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/fdg-custom/</guid><description>There’s two types of forces in the d3-force module – preshipped forces and custom forces.
Preshipped forces have already been written for you. Custom forces you write yourself.
Here we&amp;rsquo;ll cover custom forces.
What is a custom force? Link to heading A custom force is simply a function that modifies the positions and velocities of nodes. See the d3 API.
You write custom forces to achieve effects that aren&amp;rsquo;t possible by using the preshipped forces.</description></item><item><title>Force directed graph: growing and shrinking nodes</title><link>https://tomroth.dev/fdg-grow/</link><pubDate>Fri, 17 Mar 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/fdg-grow/</guid><description>This example was written as a demonstration of custom forces with d3&amp;rsquo;s force directed graph.
The custom forces change the size of the nodes over time. The blue &amp;ldquo;male&amp;rdquo; nodes gradually grow larger and the pink &amp;ldquo;female&amp;rdquo; nodes gradually grow smaller.
Implementing the custom force is simple. On each tick of the simulation, we loop through every node and check to see if its sex attribute is M or F.</description></item><item><title>Force directed graph: splitting nodes</title><link>https://tomroth.dev/fdg-split/</link><pubDate>Fri, 17 Mar 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/fdg-split/</guid><description>This is a somewhat silly example of adding custom forces to a forceSimulation. All of the blue male nodes are pulled to the right side and all the pink female nodes are pulled to the left side.
There&amp;rsquo;s also a bounding box effect to stop the nodes receding into the void.
The custom force splitting_force() causes this effect. What&amp;rsquo;s a custom force? Click here for a refresher.
// bring all male nodes to right side, all female nodes to left side function splitting_force() { for (var i = 0, n = nodes_data.</description></item><item><title>Force directed graph: drifting</title><link>https://tomroth.dev/fdg-drifting/</link><pubDate>Thu, 16 Mar 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/fdg-drifting/</guid><description>This force graph drifts while the simulation is running.
What happens is that every tick adds 1 to the x and y position of the centering force forceCenter, so the graph slowly drifts off the screen.
Here&amp;rsquo;s the key bit of code:
function tickActions() { //update circle positions each tick of the simulation node .attr(&amp;#34;cx&amp;#34;, function(d) { return d.x; }) .attr(&amp;#34;cy&amp;#34;, function(d) { return d.y; }); //update link positions //simply tells one end of the line to follow one node around //and the other end of the line to follow the other node around link .</description></item><item><title>Force directed graph: squashed</title><link>https://tomroth.dev/fdg-squashed/</link><pubDate>Thu, 16 Mar 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/fdg-squashed/</guid><description>This example uses a combination of the positioning forces forceX and forceCenter to create a &amp;ldquo;squashed&amp;rdquo; aesthetic.
The centering force forceCenter keeps the graph in the centre of the svg element, while forceX drags all the nodes towards x-position 400 on the svg element.
var force_X = d3.forceX(400) .strength(0.2); var center_force = d3.forceCenter(width/2, height/2); The result is a tall and thin graph. Doing the same with forceY instead of forceX leads to a short and fat graph instead.</description></item><item><title>Force directed graph: collision forces</title><link>https://tomroth.dev/fdg-collision/</link><pubDate>Wed, 15 Mar 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/fdg-collision/</guid><description>There’s two types of forces in the d3-force module – preshipped forces and custom forces.
Preshipped forces have already been written for you. Custom forces you write yourself.
Here we&amp;rsquo;ll cover the preshipped force that handles collisions between nodes - forceCollide.
Using forceCollide will prevent your nodes from rolling right over each other. Its purpose is to stop nodes from overlapping.
forceCollide is designed for those many situations where you crave some order in your nodes.</description></item><item><title>Force directed graph: radially expanding</title><link>https://tomroth.dev/fdg-expand/</link><pubDate>Wed, 15 Mar 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/fdg-expand/</guid><description>This is an example of the effect you can generate by playing with the forceLink parameters and the forceManyBody parameters.
This graph radially expands. Try dragging it around - nodes will stay where you put them but the rest will expand outwards.
This effect can be created in two steps:
Set the forceLink strength to 0. This removes the link force while maintaining the lines between nodes. Set the charge to a small negative number.</description></item><item><title>Force directed graph: charge forces</title><link>https://tomroth.dev/fdg-charge/</link><pubDate>Sun, 12 Mar 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/fdg-charge/</guid><description>There’s two types of forces in the d3-force module. These are preshipped forces and custom forces.
Preshipped forces have already been written for you. Custom forces you write yourself.
Here we cover a preshipped force - forceManyBody(). This is perhaps the defining force of the force-directed graph.
You use this force to simulate the charged-particles model, where every node repels (or attracts) every other node until we reach a stable state.</description></item><item><title>Force directed graph: link forces</title><link>https://tomroth.dev/fdg-link/</link><pubDate>Sun, 12 Mar 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/fdg-link/</guid><description>There’s two types of forces in the d3-force module – preshipped forces and custom forces.
Preshipped forces have already been written for you. Custom forces you write yourself.
In this post we cover the force that links nodes together - forceLink(). This is the force that pushes linked nodes together or pulls them apart.
Parameters Link to heading Like the other forces, there&amp;rsquo;s a number of parameters you can tweak to customise your force directed graph.</description></item><item><title>Force directed graph: positioning forces</title><link>https://tomroth.dev/fdg-position/</link><pubDate>Sun, 12 Mar 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/fdg-position/</guid><description>There’s two types of forces in the d3-force module – preshipped forces and custom forces.
Preshipped forces have already been written for you. Custom forces you write yourself.
Here we&amp;rsquo;ll cover the three preshipped positioning forces:
forceCenter forceX forceY Using positioning forces is easy. There&amp;rsquo;s not too many parameters to play with and they&amp;rsquo;re easy to understand!
forceCenter Link to heading forceCenter applies a force to all nodes that’ll bring their centre of mass towards a given point.</description></item><item><title>Force directed graph: using node and link attributes</title><link>https://tomroth.dev/fdg-attributes/</link><pubDate>Wed, 08 Mar 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/fdg-attributes/</guid><description>We&amp;rsquo;ve slowly been adding more functionality onto our graph. It&amp;rsquo;s not quite useable yet, but not far off.
Today&amp;rsquo;s topic: incorporating node attributes and link attributes into our graph.
It&amp;rsquo;ll really bring our graph to life!
Attributes Link to heading When you specify data for nodes and links you can put in whatever fields you like.
Let&amp;rsquo;s look at an example.
Here&amp;rsquo;s some data. I&amp;rsquo;ve taken some of the characters from the fantastic online serial Twig and mapped out some relationships between them.</description></item><item><title>Force directed graph: dragable</title><link>https://tomroth.dev/fdg-dragable/</link><pubDate>Mon, 06 Mar 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/fdg-dragable/</guid><description>This is the second of two posts explaining the basics of drag and drop. You can find the first post here.
Lets take the minimal working example as a template.
We&amp;rsquo;re going to make the same graph dragable so that we can interact with it. Click on the picture above to see what this looks like!
You also need to know how drag and drop works before diving into this article.</description></item><item><title>Force directed graph: sticky nodes</title><link>https://tomroth.dev/fdg-sticky/</link><pubDate>Mon, 06 Mar 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/fdg-sticky/</guid><description>Sticky nodes are where nodes are &amp;ldquo;stuck&amp;rdquo; once you&amp;rsquo;ve finished dragging them. They won&amp;rsquo;t move again once the mouse has been released - unless you drag them again, of course. Click on the image above for spoilers!
It&amp;rsquo;s super easy to make your nodes sticky.
Start with the example here.
Scroll down to the drag_end function. It looks like
function drag_end(d) { if (!d3.event.active) simulation.alphaTarget(0); d.fx = null; d.fy = null; } Currently it is telling the force directed graph to release the nodes when the mouse is released.</description></item><item><title>Get drag and drop working with d3</title><link>https://tomroth.dev/drag-example/</link><pubDate>Tue, 28 Feb 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/drag-example/</guid><description>This is the first of two posts explaining the basics of drag and drop.
Want the second post? Here it is!.
In this post we’ll start to become familiar with the module d3-drag.
This module is a useful and flexible one that has many applications. Spending some time understanding the basics of this module can pay large benefits.
The best way to learn is through examples - so we’re going to create a simple example to see what exactly is happening.</description></item><item><title>Force directed graph: minimal working example</title><link>https://tomroth.dev/fdg-minimal/</link><pubDate>Sat, 25 Feb 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/fdg-minimal/</guid><description>The goal of this post is to take what we covered earlier and make a simple working example of a force-directed graph.
We&amp;rsquo;ll start with just getting the nodes working, adding the links afterwards to complete the example. Find what we&amp;rsquo;ll be building here.
So open up a text file in your favourite editor. Fire up a local server. Find yourself a cool drink or two.
Ready? Let’s begin.
Nodes Link to heading First thing we’ll do is set up a basic skeleton for our force directed graph.</description></item><item><title>Force directed graph: basics</title><link>https://tomroth.dev/fdg-basics/</link><pubDate>Sat, 18 Feb 2017 00:00:00 +0000</pubDate><guid>https://tomroth.dev/fdg-basics/</guid><description>To create a D3 force directed graph you really only need three things:
Nodes A simulation Forces to act on the simulation There’s also some things that are optional, but real useful:
Links Node attributes Great. Let&amp;rsquo;s learn more.
Nodes Link to heading What are nodes? They&amp;rsquo;re an array of objects.
Nodes can be anything. Here&amp;rsquo;s three examples:
people in a friendship group telegraph poles in a distributed grid articles on a Wikipedia page Nodes are whatever data objects that you want to visualise.</description></item><item><title>Simple examples with transition chaining</title><link>https://tomroth.dev/transitions/</link><pubDate>Thu, 02 Jun 2016 00:00:00 +0000</pubDate><guid>https://tomroth.dev/transitions/</guid><description>There are a few explanations and tutorials on the web about how to perform chained transitions on the same element using d3. Also existing are lots of demonstration: see this and this, for example.
I couldn&amp;rsquo;t find any dead-simple tutorial out there, however. This example will be just about as simple as it gets.
Let&amp;rsquo;s create a circle attached to a SVG element and use this as a running example. You can just do this in the console for a browser with d3 activated.</description></item><item><title>The story of d3 and the Start and End event listeners.</title><link>https://tomroth.dev/event-listeners/</link><pubDate>Sun, 29 May 2016 00:00:00 +0000</pubDate><guid>https://tomroth.dev/event-listeners/</guid><description>One of the cool things about d3 is that we can &amp;ldquo;listen&amp;rdquo; for transition events.
These listeners can be very useful things.
I&amp;rsquo;ll go through two of these in this blog post: start, and end. The use of these is best explained through examples.
Let&amp;rsquo;s start with the start listener (ha!).
Start listeners Link to heading One use of a start listener is to trigger a function to run at the start of a transition.</description></item><item><title>Dancing madness: fun with mouse events and d3</title><link>https://tomroth.dev/dancing-madness/</link><pubDate>Sun, 22 May 2016 00:00:00 +0000</pubDate><guid>https://tomroth.dev/dancing-madness/</guid><description>There’s something oddly satisfying about making things appear through a mouse click. It’s like the webpage has recognised your supremacy over it and is bowing to accommodate your wishes. You are its master. You have power.
Are you a person who likes to feel powerful?
Read on. We’re going to create something truly amazing.
Our task: to click inside a rectangle, and have dancing people appear. Why dancing people, you ask?</description></item><item><title>Graphs 'n stuff: simple line chart with d3</title><link>https://tomroth.dev/linechart/</link><pubDate>Sun, 08 May 2016 00:00:00 +0000</pubDate><guid>https://tomroth.dev/linechart/</guid><description>Tennis: a sport almost designed for statistical analysis. You have only two primary variables (players) up against each other - whereas for something like soccer, you have 22 players who can impact the play. Much simpler - yet infinitely more complicated at the same time.
With this in mind (and with the aid of the mountains of tennis data available) I decided to trial a simple betting strategy. Simply put, it compares calculated probabilities for the outcome of tennis matches against the odds done by bookmakers.</description></item><item><title>Graphs 'n stuff: simple bar chart with d3</title><link>https://tomroth.dev/barchart/</link><pubDate>Sun, 01 May 2016 00:00:00 +0000</pubDate><guid>https://tomroth.dev/barchart/</guid><description>This next introductory example is a simple bar chart with a rollover tooltip. This bar chart uses the d3-tip tooltip capabilities to enhance on the tooltip shown in our scatterplot.
It&amp;rsquo;s a graph of the number of internet users over time, also displaying the percentage of the world&amp;rsquo;s population as rollover information. The code is here.
I&amp;rsquo;ll be abandoning the Wp-D3 plugin I&amp;rsquo;ve been using for the moment (it&amp;rsquo;s just too fiddly for my liking) and instead link directly to the code with the graph.</description></item><item><title>Graphs 'n stuff: simple scatterplot with d3</title><link>https://tomroth.dev/scatter/</link><pubDate>Sun, 17 Apr 2016 00:00:00 +0000</pubDate><guid>https://tomroth.dev/scatter/</guid><description>Recently I&amp;rsquo;ve been learning how to use d3.js (D3), often touted as an incredibly powerful cool for creating graphics. I thought it&amp;rsquo;d be helpful to any other novices out there to write up my D3 examples as I do them. I come from a statistical background and while I have plenty of experience coding in R, JavaScript is a whole new challenge.
I&amp;rsquo;ll be writing up short explanations on any particular aspect that confuses me with a particular visualisation.</description></item></channel></rss>