Disable d3 brush resize
Asked Answered
H

8

6

Is it possible to have a brush that is a static width ie a brush that is not resizable? It would still need to be draggable. There doesn't seem to be anything indicating whether it's possible in the documentation.

Heddie answered 22/5, 2014 at 13:33 Comment(0)
S
4

There's no explicit option for that, but all you need to do is reset the domain in the brush event handler, for example

var brush = d3.svg.brush().on("brush", brushed);

function brushed() {
  brush.extent(desiredDomain);
}
Superinduce answered 22/5, 2014 at 13:59 Comment(0)
F
5

Resize can be prevented by hiding the resize handles via css

g.brush>.resize {
    display: none;
}
Folk answered 28/8, 2014 at 18:38 Comment(0)
S
4

There's no explicit option for that, but all you need to do is reset the domain in the brush event handler, for example

var brush = d3.svg.brush().on("brush", brushed);

function brushed() {
  brush.extent(desiredDomain);
}
Superinduce answered 22/5, 2014 at 13:59 Comment(0)
T
3

In D3.js v4.0 you can disable the pointer events in CSS for the SVG elements (handle rectangle).

.handle {
  pointer-events: none;
}
Trustworthy answered 23/9, 2016 at 15:20 Comment(0)
A
1

with latest version of d3 you can make the handleSize(0) and it will make the handle bar with width 0.

    `const brush = d3.brushX().extent([
            [margin.left, margin.top],
            [chartRight, chartBottom],
        ])
        .handleSize(0);`
Annalist answered 17/10, 2023 at 9:26 Comment(0)
W
0

Actually, manually resetting the extent does not seem to work well.

The solution I've found, which is hidden in the slider example, is to remove the resize element altogether:

// Create the brush normally.
var brush = d3.svg.brush()
    .x(xScale)
    .on("brush", brushed);

// Add it to a SVG group
var slider = context.append("g")
    .attr("class", "x brush")
    .call(brush);

// Remove the resize control
slider.selectAll(".resize").remove();
Weimer answered 17/4, 2016 at 20:23 Comment(0)
A
0

I encountered exactly the same problem. I was using D3 4.0 and need to implement a fixed size brush along the x axis. I used drag.filter() function and modified elements inside .brush to achieve this:

var brush = d3.brushX()
            .filter(function () {return d3.mouse(this)[0] > brushStart && d3.mouse(this)[0] < brushStart + brushWidth})
            .extent([[0, 0], [width, height]])
            .on("end", brushended)
            .on('brush', beingBrushed);
svg.append('g')
    .attr('class', 'brush')
    .call(brush)
    .call(brush.move, [0, brushWidth]); //initial position
d3.selectAll('.brush>.handle').remove();

function brushended() {
    var s = d3.event.selection;
    if (s!== null) {
        brushStart = s[0];
    }
}

This way the brush allows dragging but not resizing.

Anxious answered 13/7, 2016 at 7:28 Comment(0)
D
0

I just removed the .handle elements from graph with the brush:

contextGraph.selectAll('.handle').remove();
Day answered 16/10, 2018 at 12:20 Comment(0)
C
0

handleSize is the answer to read - 1 min fix, rest are workarounds

Coralline answered 14/8 at 18:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.