Adding a subtle touch of glow to your d3.js visualizations

Part of the "SVGs beyond mere shapes" blog post series

Posted: June 6, 2016-Likes: 0-Comments: 0-Categories: D3.js, Data Visualization, Tutorial-Tags: D3.js, glow, Radar chart
You are here: ...
Home / Blog / D3.js / Adding a subtle touch of glow to your d3.js visualizations

Today it’s time for a short blog about a very subtle effect that can add a bit more pizzazz to your visualization. It’s definitely not something that will suit a business/no-nonsense chart. But then again, if you only make those kinds of charts, I feel that doing data visualization is definitely less enjoyable than if you try to have some fun with your creations every now and then ;) So let’s start by adding a bit of glow to our shapes!

Intro - Glow SVG Filter
SVGs beyond mere shapes blog series

This blog is part of the SVGs beyond mere shapes blog series. It’s based on the similar named talk. My goal with the talk was to inspire people to experiment with the norm, to create new ways of making a visual more effective or fun. Even for a subject as narrow as SVG filters and gradients, there are more things possible than you might think. From SVG gradients that can be based on data, dynamic, animated gradients and more, to SVG filters for creating glow, gooey, and fuzzy effects. You can find links to all of the other blogs in my kick-off post here.

Some people love radar charts, others hate them. I have to admit that I really like circular shaped graphics for their aesthetics, but I also understand that is is difficult to compare datapoints in most cases. Several months ago I made a redesign of a radar chart for D3. Although I was happy with it the result in the “normal” version of the image below, I remembered coming across the code for a glow filter and thought it might make this a bit more engaging. I applied the glow to the background axis circles and to the colored blobs to get the result shown on the right side.

Not a major difference, you might not even like it. It really is a matter of personal tastes. But I felt it made the chart just a bit more engaging to look at.

The difference with and without glow

Making it Glow

Adding some glow is exceptionally simple. If you want to, you can basically copy the code in the section below, paste it in your own code and refer to the filter’s id. You actually never have to change anything. But you can make the glow more spread out by increasing the value of the stdDeviation attribute (standard deviation).

//Container for the gradients
var defs = svg.append("defs");

//Filter for the outside glow
var filter = defs.append("filter")
var feMerge = filter.append("feMerge");

First, a defs element is appended to the SVG, which stands for definitions. It contains definitions of special elements such as gradients and filters. Next, append a filter element to the defs and give it a unique id to reference it later when you want to set the glow to specific SVG elements.
And then comes the magic part of combining specific filters in a specific order to get something you never expected. Luckily this one is fairly simple to understand. You apply a blur to the element (on which you set the filter) with feGaussianBlur. The result of this blur is named coloredBlur using the result attribute. Next, an feMerge is used to place the (sharp) original element, called the SourceGraphic, on top of the slightly blurred coloredBlur version. This together creates the appearance of a glow around the element. If you want a bit more info on feMerge, look here.

The result of the feMerge on the glow result

All that’s left to do is apply the filter to the element that should glow

//Apply to your element(s)
	.style("filter", "url(#glow)");

I found the filter code on a stackOverflow answer by Drew Noakes, I can’t even remember how I ended up there, except that I wasn’t looking directly for a glow. But nevertheless, I’m glad I came across it.

The glow works on rectangles, circles, paths. But as far as I can tell, it doesn’t seem to have an effect on lines, no matter how thick.

Below you can play around with the glow effect. Click anywhere around the three shapes to turn the glow filter on or off. Use the slider to adjust the spread of the glow (which adjusts the stdDeviation attribute of the feGaussianBlur)

stdDeviation (spread of glow): Click near the shapes to turn the glow on/off

One of the examples that I found to look more fun, more neon, when a glow is added was the Spirograph project I created recently :)

Spirograph with Glow

The Code

The code to the examples that have been used in the blog can be found here:

And that was it already! There really is nothing more to the glow effect. You just have to create the filter and apply it. Try it on some of your charts, you might be amazed to see which ones turn out to become a bit more interesting :)

SVGs beyond mere shapes

If you’re interested in seeing more examples of how SVG gradients or SVG filters can be used to make your data visualization more effective, engaging or fun, check out the other ±10 blogs that are available. You can find links to all of the blogs in my kick-off post here

Prev / Next Post

Leave a Reply