Giter Site home page Giter Site logo

Comments (7)

Jrubzjeknf avatar Jrubzjeknf commented on July 19, 2024 7

@yrokhlin's solution didn't work for me, so based on his solution I modify the calculated path.

      .attr('d', d => {
        // Prevents exact horizontal sankey links from disappearing.
        // See for reference https://github.com/d3/d3-sankey/issues/28
        const path = sankeyLinkHorizontal()(d);
        const match = path.match(/,([^C]+)C/);

        if (match.length !== 2) {
          return path;
        }

        const replacementValue = +match[1] + 0.01;
        const fixedPath = path.replace(match[1], '' + replacementValue);

        return fixedPath;
      })

This turns M16,352.50000000000005C249.66666666666666,etc.. into M16,352.51000000000005C249.66666666666666,etc...

from d3-sankey.

yrokhlin avatar yrokhlin commented on July 19, 2024 6

For any future googlers such as myself that may stumble upon this problem, I found a neat little hack that will let you get around this issue.

.attr('d', d3.sankeyLinkHorizontal())
.attr('d', (d, i, items) => {
    const path = d3.select(items[i]).attr('d');
        return path + ' l0,0.001';
    })

This takes your d attribute after you set it with d3.sankeyLinkHorizontal() and adds a small string at the end which basically just picks up the last point of the path and slightly jitters it on the y axis. This makes it so d.y1 - d.y0 === 0 will never be true and the jitter is visually imperceivable.

I think this is a better solution than creating a rect especially if you are doing any sort of highlighting.

I hope this helps someone else out who is stuck.

from d3-sankey.

mbostock avatar mbostock commented on July 19, 2024 3

This is an unfortunate limitation of SVG’s design; the default gradientUnits = objectBoundingBox, and when the path is horizontal, the bounding box is degenerate (height = zero) because the bounding box does not consider the stroke.

The “easiest” fix is to gradientUnits = userSpaceOnUse, but then you have to compute the coordinates of the gradient relative to the SVG container rather than using the local coordinates of the path.

Or, you could compute the outline of the path yourself by computing the offset Bézier—not trivial! Bezier.js might help with that.

Or, you could special-case horizontal lines and draw a rect instead of a line, or some such.

In the future, please use Stack Overflow tag d3.js to ask for help. Although I make an effort to assist everyone that asks, I am not always available to provide help promptly or directly. Stack Overflow provides a better collaborative forum for self-help: tens of thousands of D3-related questions have already been asked there, and some answered questions may be relevant to you.

If you have a question about D3’s behavior and want to discuss it with other users, also consider the d3-js Google Group or joining the d3-js Slack.

Thank you! 🤗

from d3-sankey.

wiesson avatar wiesson commented on July 19, 2024 2

Thank you very much for your detailed answer! I did a quick fix, with userSpaceOnUse, but I will maybe just draw a rect to avoid this problem at all.

"Quickfix" with wrong colors:

if(d.y1 - d.y0 === 0) {
    linearGradient.attr('gradientUnits', "userSpaceOnUse")
}

from d3-sankey.

mbostock avatar mbostock commented on July 19, 2024

That fix probably isn’t sufficient because you’ll need to specify different coordinates for the gradient when you switch to userSpaceOnUse.

from d3-sankey.

wiesson avatar wiesson commented on July 19, 2024

Yes I know - but the color is at least visible! 😅 (despite being wrong / incorrect).
// edit: I added a rect if d.y1 - d.y0 === 0 - works perfectly.

from d3-sankey.

ivanjermakov avatar ivanjermakov commented on July 19, 2024

For me issue was that I had spaces in gradient id so it was not picking up correctly.

from d3-sankey.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.