D3: add filter as background to SVG element

smcs

In D3 I'd like to apply a background colour to my axis labels. It could be done with rect but using SVG filters looks like a nicer option since they adapt to the size of the labels. I've come this far (fiddle) using

var filterDef = svg.append("defs");
var filter = filterDef.append("filter")
        .attr("id", "textBackground")
        .attr("x", 0)
        .attr("y", 0)
        .attr("width", 1)
        .attr("height", 1)
        .append("feFlood")
        .attr("flood-color", "green")
        .attr("result","txtBackground")
var filterMerge = filter.append("feMerge");
filterMerge.append("feMergeNode")
        .attr("in", "txtBackground");
filterMerge.append("feMergeNode")
        .attr("in", "SourceGraphic");            
d3.selectAll(".xAxis>.tick>text")
    .style("filter", "url(#textBackground)");

but the filters are added in front of the text elements, even though I've closely followed this example.

Robert Longson

You're nearly there, you just need to get the filter hierarchy right. The filter elements need to be siblings.

var svg = d3.select("body").append("svg");
var w = 500, h = 200;
var padding = 50;
svg.attr("width", w)
   .attr("height", h);
var xScale = d3.time.scale()
    .domain([0, 1000])
    .range([padding, w-padding]);
var xAxis = d3.svg.axis()
    .scale(xScale)
    .ticks(5);
svg.append("g")
		.attr("class","xAxis")
    .call(xAxis);
var filterDef = svg.append("defs");
var filter = filterDef.append("filter")
            .attr("id", "textBackground")
            .attr("x", 0)
            .attr("y", 0)
            .attr("width", 1)
            .attr("height", 1);
   filter         
            .append("feFlood")
            .attr("flood-color", "green")
            .attr("result","txtBackground")
var filterMerge = filter.append("feMerge");
filterMerge.append("feMergeNode")
            .attr("in", "txtBackground");
filterMerge.append("feMergeNode")
            .attr("in", "SourceGraphic");            
d3.selectAll(".xAxis>.tick>text")
		.style("filter", "url(#textBackground)");
    
    
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

D3 - Add background fill to rect

From Dev

SVG element background color

From Dev

Add background to svg polyline

From Dev

Background image to SVG choropleth map using D3

From Dev

Using D3 to fill an svg with a background image

From Dev

SVG filter using d3 js not working in IE

From Dev

Add two different svg element in g element of svg using d3.js from a dataset

From Dev

Parse SVG and add it to a svg element

From Dev

D3 multi-line tooltip for SVG element

From Dev

D3 append (insert) existing SVG string (or element) to a DIV

From Dev

D3: How to select every thing under svg element

From Dev

d3 doesn't append namespace attributes to svg element

From Dev

D3 events firing on a hidden svg element

From Dev

Bind Bootstrap tooltip to dynamically created SVG element with d3

From Dev

D3 - Positioning tooltip on SVG element not working

From Dev

Get class of selected svg element in d3

From Dev

d3 mouseover on element conflicts with svg mousemove

From Dev

Enlarge svg group element on mouseenter (D3)

From Dev

d3 vs. svg When Dealing with Element

From Dev

d3 - rendering an html table on top of an svg element

From Dev

d3 Trying to select text within a svg text element

From Dev

D3 geometric zoom when there is no SVG element under the cursor

From Dev

D3: change data based on svg element manipulation

From Dev

Add borders to SVG path d3 javascript

From Dev

SVG d3 | can I add inline style dynamically?

From Dev

Add drop-shadow to svg image in D3

From Dev

How to add a background color to d3 text elements?

From Dev

How to add a background image to a plot created using D3

From Dev

How to add a background image to a plot created using D3

Related Related

HotTag

Archive