Giter Club home page Giter Club logo

Comments (5)

1wheel avatar 1wheel commented on May 14, 2024

You can do this (w/ slightly different syntax) in 3 by modifying the selection prototype - see jetpack and starterkit

from d3-selection.

mgold avatar mgold commented on May 14, 2024

Okay, so that rolls .classed into .append but you still have to specify things twice for an enter selection. So those libraries don't really address the problem.

from d3-selection.

mgold avatar mgold commented on May 14, 2024

Hmm, I suppose you'd have a bit of a problem for union selectors like "div.a, span.b". I don't think this is a dealbreaker, though. You'd take the last one and most of the time the user would override it. I still would like Mike to weigh in.

from d3-selection.

mbostock avatar mbostock commented on May 14, 2024

I typically just select by class and not element:

selection.selectAll(".foo")
    .data(data)
  .enter().append("div")
    .class("foo", true);

Sometimes I’ll do things like select based on the presence of an attribute but then compute its value dynamically:

selection.selectAll("[foo]")
    .data(data)
  .enter().append("div")
    .attr("foo", function(d, i) { return ; });

So, yes, typically the selector used to create a selection implies certain things on entering elements, but not always enough to enforce those things automatically. It would be possible to capture the selector used to create a selection (when a selector is used—note that you can also pass a function to selection.select and selection.selectAll), and then modify enter.select / enter.append to try to enforce what’s implied by the selector.

But, it’s not trivial to enforce that the selector matches materialized elements in general. For example, you presumably wouldn’t be able to enforce a selector like ":nth-child(2)", and in an attribute existence selector (like "[foo]") you’d have to define a default value for that attribute, which might be pointless if you were going to subsequently give it an explicit value later anyway.

Given the difficulty of supporting this in the general case, and the undesirability of having it just work in a few special cases, I prefer having a little redundancy. Or just a little syntactic sugar for convenience as an optional feature, like in d3-jetpack.

from d3-selection.

mgold avatar mgold commented on May 14, 2024

Okay, thanks for the explanation.

from d3-selection.

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.