d3.js v5 enter update not inserting rectangle div





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I am using d3.js v5 version and trying to understand enter, update. I have two data set [1,2,3.5] and other [1,2,3,4,5] . Firstly I am calling first data which renders [1,2,3.5 ] in red color and then second data is called with blue color[1,2,3,4,5] .



On update why 3 is not getting inserted in blue?






              var scale = d3.scaleLinear().domain([1,5]).range([0,200]);


var svg = d3.select("body")
.append("svg")
.attr("width",250)
.attr("height",250);

function render(data,color){
// Bind data
var rects = svg.selectAll("rect").data(data);

//Enter

rects = rects.enter().append("rect");

//update
rects.attr("x", scale)
.attr("y",50)
.attr("width",20)
.attr("height",20)
.attr("fill",color);
};

render([1,2,3.5],"red");
render([1,2,3,4,5],"blue");

<script src="https://d3js.org/d3.v5.min.js"></script>












share|improve this question





























    1















    I am using d3.js v5 version and trying to understand enter, update. I have two data set [1,2,3.5] and other [1,2,3,4,5] . Firstly I am calling first data which renders [1,2,3.5 ] in red color and then second data is called with blue color[1,2,3,4,5] .



    On update why 3 is not getting inserted in blue?






                  var scale = d3.scaleLinear().domain([1,5]).range([0,200]);


    var svg = d3.select("body")
    .append("svg")
    .attr("width",250)
    .attr("height",250);

    function render(data,color){
    // Bind data
    var rects = svg.selectAll("rect").data(data);

    //Enter

    rects = rects.enter().append("rect");

    //update
    rects.attr("x", scale)
    .attr("y",50)
    .attr("width",20)
    .attr("height",20)
    .attr("fill",color);
    };

    render([1,2,3.5],"red");
    render([1,2,3,4,5],"blue");

    <script src="https://d3js.org/d3.v5.min.js"></script>












    share|improve this question

























      1












      1








      1








      I am using d3.js v5 version and trying to understand enter, update. I have two data set [1,2,3.5] and other [1,2,3,4,5] . Firstly I am calling first data which renders [1,2,3.5 ] in red color and then second data is called with blue color[1,2,3,4,5] .



      On update why 3 is not getting inserted in blue?






                    var scale = d3.scaleLinear().domain([1,5]).range([0,200]);


      var svg = d3.select("body")
      .append("svg")
      .attr("width",250)
      .attr("height",250);

      function render(data,color){
      // Bind data
      var rects = svg.selectAll("rect").data(data);

      //Enter

      rects = rects.enter().append("rect");

      //update
      rects.attr("x", scale)
      .attr("y",50)
      .attr("width",20)
      .attr("height",20)
      .attr("fill",color);
      };

      render([1,2,3.5],"red");
      render([1,2,3,4,5],"blue");

      <script src="https://d3js.org/d3.v5.min.js"></script>












      share|improve this question














      I am using d3.js v5 version and trying to understand enter, update. I have two data set [1,2,3.5] and other [1,2,3,4,5] . Firstly I am calling first data which renders [1,2,3.5 ] in red color and then second data is called with blue color[1,2,3,4,5] .



      On update why 3 is not getting inserted in blue?






                    var scale = d3.scaleLinear().domain([1,5]).range([0,200]);


      var svg = d3.select("body")
      .append("svg")
      .attr("width",250)
      .attr("height",250);

      function render(data,color){
      // Bind data
      var rects = svg.selectAll("rect").data(data);

      //Enter

      rects = rects.enter().append("rect");

      //update
      rects.attr("x", scale)
      .attr("y",50)
      .attr("width",20)
      .attr("height",20)
      .attr("fill",color);
      };

      render([1,2,3.5],"red");
      render([1,2,3,4,5],"blue");

      <script src="https://d3js.org/d3.v5.min.js"></script>








                    var scale = d3.scaleLinear().domain([1,5]).range([0,200]);


      var svg = d3.select("body")
      .append("svg")
      .attr("width",250)
      .attr("height",250);

      function render(data,color){
      // Bind data
      var rects = svg.selectAll("rect").data(data);

      //Enter

      rects = rects.enter().append("rect");

      //update
      rects.attr("x", scale)
      .attr("y",50)
      .attr("width",20)
      .attr("height",20)
      .attr("fill",color);
      };

      render([1,2,3.5],"red");
      render([1,2,3,4,5],"blue");

      <script src="https://d3js.org/d3.v5.min.js"></script>





                    var scale = d3.scaleLinear().domain([1,5]).range([0,200]);


      var svg = d3.select("body")
      .append("svg")
      .attr("width",250)
      .attr("height",250);

      function render(data,color){
      // Bind data
      var rects = svg.selectAll("rect").data(data);

      //Enter

      rects = rects.enter().append("rect");

      //update
      rects.attr("x", scale)
      .attr("y",50)
      .attr("width",20)
      .attr("height",20)
      .attr("fill",color);
      };

      render([1,2,3.5],"red");
      render([1,2,3,4,5],"blue");

      <script src="https://d3js.org/d3.v5.min.js"></script>






      javascript d3.js






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 10:36









      SumeetSumeet

      4,87022548




      4,87022548
























          1 Answer
          1






          active

          oldest

          votes


















          1














          This is because you are re-assigning rects to rects.enter(). Initially rects contains "updated" rectangles. What you want is to get both "enter" and "update", which can be done with merge.






          var scale = d3.scaleLinear().domain([1,5]).range([0,200]);

          var svg = d3.select("body")
          .append("svg")
          .attr("width",250)
          .attr("height",250);

          function render(data,color){
          // update
          var rects = svg.selectAll("rect").data(data, d => d);

          // enter
          rects
          .enter().append("rect")
          // enter + update
          .merge(rects)
          .attr("x", scale)
          .attr("y",50)
          .attr("width",20)
          .attr("height",20)
          .attr("fill",color);
          };

          render([1,2,3.5],"red");
          render([1,2,3,4,5],"blue");

          <script src="https://d3js.org/d3.v5.min.js"></script>





          Edit: By default, when you call .data(...) d3 joins by index, so 3.5 becomes 3. If you want to join by numerical value , so that 3, 4 and 5 get inserted, you can pass second parameter to .data (read more
          https://github.com/d3/d3-selection#selection_data)






          share|improve this answer


























          • in your answer 3.5 or 3 is not shown. It should have 6 rectangles after update [1,2,3,3.5,4,5]

            – Sumeet
            Nov 22 '18 at 11:28











          • 3.5 is not coming even after the merge?

            – Sumeet
            Nov 22 '18 at 11:29











          • I modified answer, as far as I understand, you need to pass second argument to .data()

            – Yaroslav Sergienko
            Nov 22 '18 at 11:36











          • Looks like d3.js v3 and v4 version has some difference in the way update and enter works?

            – Sumeet
            Nov 22 '18 at 11:41











          • Can't say for sure. Most thorough article on selections bost.ocks.org/mike/selection has been written when v3 was introduced, also key argument to .data() and .merge() existed already in v3. So, I do not think, that there were significant changes.

            – Yaroslav Sergienko
            Nov 22 '18 at 11:48














          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429011%2fd3-js-v5-enter-update-not-inserting-rectangle-div%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          This is because you are re-assigning rects to rects.enter(). Initially rects contains "updated" rectangles. What you want is to get both "enter" and "update", which can be done with merge.






          var scale = d3.scaleLinear().domain([1,5]).range([0,200]);

          var svg = d3.select("body")
          .append("svg")
          .attr("width",250)
          .attr("height",250);

          function render(data,color){
          // update
          var rects = svg.selectAll("rect").data(data, d => d);

          // enter
          rects
          .enter().append("rect")
          // enter + update
          .merge(rects)
          .attr("x", scale)
          .attr("y",50)
          .attr("width",20)
          .attr("height",20)
          .attr("fill",color);
          };

          render([1,2,3.5],"red");
          render([1,2,3,4,5],"blue");

          <script src="https://d3js.org/d3.v5.min.js"></script>





          Edit: By default, when you call .data(...) d3 joins by index, so 3.5 becomes 3. If you want to join by numerical value , so that 3, 4 and 5 get inserted, you can pass second parameter to .data (read more
          https://github.com/d3/d3-selection#selection_data)






          share|improve this answer


























          • in your answer 3.5 or 3 is not shown. It should have 6 rectangles after update [1,2,3,3.5,4,5]

            – Sumeet
            Nov 22 '18 at 11:28











          • 3.5 is not coming even after the merge?

            – Sumeet
            Nov 22 '18 at 11:29











          • I modified answer, as far as I understand, you need to pass second argument to .data()

            – Yaroslav Sergienko
            Nov 22 '18 at 11:36











          • Looks like d3.js v3 and v4 version has some difference in the way update and enter works?

            – Sumeet
            Nov 22 '18 at 11:41











          • Can't say for sure. Most thorough article on selections bost.ocks.org/mike/selection has been written when v3 was introduced, also key argument to .data() and .merge() existed already in v3. So, I do not think, that there were significant changes.

            – Yaroslav Sergienko
            Nov 22 '18 at 11:48


















          1














          This is because you are re-assigning rects to rects.enter(). Initially rects contains "updated" rectangles. What you want is to get both "enter" and "update", which can be done with merge.






          var scale = d3.scaleLinear().domain([1,5]).range([0,200]);

          var svg = d3.select("body")
          .append("svg")
          .attr("width",250)
          .attr("height",250);

          function render(data,color){
          // update
          var rects = svg.selectAll("rect").data(data, d => d);

          // enter
          rects
          .enter().append("rect")
          // enter + update
          .merge(rects)
          .attr("x", scale)
          .attr("y",50)
          .attr("width",20)
          .attr("height",20)
          .attr("fill",color);
          };

          render([1,2,3.5],"red");
          render([1,2,3,4,5],"blue");

          <script src="https://d3js.org/d3.v5.min.js"></script>





          Edit: By default, when you call .data(...) d3 joins by index, so 3.5 becomes 3. If you want to join by numerical value , so that 3, 4 and 5 get inserted, you can pass second parameter to .data (read more
          https://github.com/d3/d3-selection#selection_data)






          share|improve this answer


























          • in your answer 3.5 or 3 is not shown. It should have 6 rectangles after update [1,2,3,3.5,4,5]

            – Sumeet
            Nov 22 '18 at 11:28











          • 3.5 is not coming even after the merge?

            – Sumeet
            Nov 22 '18 at 11:29











          • I modified answer, as far as I understand, you need to pass second argument to .data()

            – Yaroslav Sergienko
            Nov 22 '18 at 11:36











          • Looks like d3.js v3 and v4 version has some difference in the way update and enter works?

            – Sumeet
            Nov 22 '18 at 11:41











          • Can't say for sure. Most thorough article on selections bost.ocks.org/mike/selection has been written when v3 was introduced, also key argument to .data() and .merge() existed already in v3. So, I do not think, that there were significant changes.

            – Yaroslav Sergienko
            Nov 22 '18 at 11:48
















          1












          1








          1







          This is because you are re-assigning rects to rects.enter(). Initially rects contains "updated" rectangles. What you want is to get both "enter" and "update", which can be done with merge.






          var scale = d3.scaleLinear().domain([1,5]).range([0,200]);

          var svg = d3.select("body")
          .append("svg")
          .attr("width",250)
          .attr("height",250);

          function render(data,color){
          // update
          var rects = svg.selectAll("rect").data(data, d => d);

          // enter
          rects
          .enter().append("rect")
          // enter + update
          .merge(rects)
          .attr("x", scale)
          .attr("y",50)
          .attr("width",20)
          .attr("height",20)
          .attr("fill",color);
          };

          render([1,2,3.5],"red");
          render([1,2,3,4,5],"blue");

          <script src="https://d3js.org/d3.v5.min.js"></script>





          Edit: By default, when you call .data(...) d3 joins by index, so 3.5 becomes 3. If you want to join by numerical value , so that 3, 4 and 5 get inserted, you can pass second parameter to .data (read more
          https://github.com/d3/d3-selection#selection_data)






          share|improve this answer















          This is because you are re-assigning rects to rects.enter(). Initially rects contains "updated" rectangles. What you want is to get both "enter" and "update", which can be done with merge.






          var scale = d3.scaleLinear().domain([1,5]).range([0,200]);

          var svg = d3.select("body")
          .append("svg")
          .attr("width",250)
          .attr("height",250);

          function render(data,color){
          // update
          var rects = svg.selectAll("rect").data(data, d => d);

          // enter
          rects
          .enter().append("rect")
          // enter + update
          .merge(rects)
          .attr("x", scale)
          .attr("y",50)
          .attr("width",20)
          .attr("height",20)
          .attr("fill",color);
          };

          render([1,2,3.5],"red");
          render([1,2,3,4,5],"blue");

          <script src="https://d3js.org/d3.v5.min.js"></script>





          Edit: By default, when you call .data(...) d3 joins by index, so 3.5 becomes 3. If you want to join by numerical value , so that 3, 4 and 5 get inserted, you can pass second parameter to .data (read more
          https://github.com/d3/d3-selection#selection_data)






          var scale = d3.scaleLinear().domain([1,5]).range([0,200]);

          var svg = d3.select("body")
          .append("svg")
          .attr("width",250)
          .attr("height",250);

          function render(data,color){
          // update
          var rects = svg.selectAll("rect").data(data, d => d);

          // enter
          rects
          .enter().append("rect")
          // enter + update
          .merge(rects)
          .attr("x", scale)
          .attr("y",50)
          .attr("width",20)
          .attr("height",20)
          .attr("fill",color);
          };

          render([1,2,3.5],"red");
          render([1,2,3,4,5],"blue");

          <script src="https://d3js.org/d3.v5.min.js"></script>





          var scale = d3.scaleLinear().domain([1,5]).range([0,200]);

          var svg = d3.select("body")
          .append("svg")
          .attr("width",250)
          .attr("height",250);

          function render(data,color){
          // update
          var rects = svg.selectAll("rect").data(data, d => d);

          // enter
          rects
          .enter().append("rect")
          // enter + update
          .merge(rects)
          .attr("x", scale)
          .attr("y",50)
          .attr("width",20)
          .attr("height",20)
          .attr("fill",color);
          };

          render([1,2,3.5],"red");
          render([1,2,3,4,5],"blue");

          <script src="https://d3js.org/d3.v5.min.js"></script>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 '18 at 11:35

























          answered Nov 22 '18 at 11:12









          Yaroslav SergienkoYaroslav Sergienko

          41026




          41026













          • in your answer 3.5 or 3 is not shown. It should have 6 rectangles after update [1,2,3,3.5,4,5]

            – Sumeet
            Nov 22 '18 at 11:28











          • 3.5 is not coming even after the merge?

            – Sumeet
            Nov 22 '18 at 11:29











          • I modified answer, as far as I understand, you need to pass second argument to .data()

            – Yaroslav Sergienko
            Nov 22 '18 at 11:36











          • Looks like d3.js v3 and v4 version has some difference in the way update and enter works?

            – Sumeet
            Nov 22 '18 at 11:41











          • Can't say for sure. Most thorough article on selections bost.ocks.org/mike/selection has been written when v3 was introduced, also key argument to .data() and .merge() existed already in v3. So, I do not think, that there were significant changes.

            – Yaroslav Sergienko
            Nov 22 '18 at 11:48





















          • in your answer 3.5 or 3 is not shown. It should have 6 rectangles after update [1,2,3,3.5,4,5]

            – Sumeet
            Nov 22 '18 at 11:28











          • 3.5 is not coming even after the merge?

            – Sumeet
            Nov 22 '18 at 11:29











          • I modified answer, as far as I understand, you need to pass second argument to .data()

            – Yaroslav Sergienko
            Nov 22 '18 at 11:36











          • Looks like d3.js v3 and v4 version has some difference in the way update and enter works?

            – Sumeet
            Nov 22 '18 at 11:41











          • Can't say for sure. Most thorough article on selections bost.ocks.org/mike/selection has been written when v3 was introduced, also key argument to .data() and .merge() existed already in v3. So, I do not think, that there were significant changes.

            – Yaroslav Sergienko
            Nov 22 '18 at 11:48



















          in your answer 3.5 or 3 is not shown. It should have 6 rectangles after update [1,2,3,3.5,4,5]

          – Sumeet
          Nov 22 '18 at 11:28





          in your answer 3.5 or 3 is not shown. It should have 6 rectangles after update [1,2,3,3.5,4,5]

          – Sumeet
          Nov 22 '18 at 11:28













          3.5 is not coming even after the merge?

          – Sumeet
          Nov 22 '18 at 11:29





          3.5 is not coming even after the merge?

          – Sumeet
          Nov 22 '18 at 11:29













          I modified answer, as far as I understand, you need to pass second argument to .data()

          – Yaroslav Sergienko
          Nov 22 '18 at 11:36





          I modified answer, as far as I understand, you need to pass second argument to .data()

          – Yaroslav Sergienko
          Nov 22 '18 at 11:36













          Looks like d3.js v3 and v4 version has some difference in the way update and enter works?

          – Sumeet
          Nov 22 '18 at 11:41





          Looks like d3.js v3 and v4 version has some difference in the way update and enter works?

          – Sumeet
          Nov 22 '18 at 11:41













          Can't say for sure. Most thorough article on selections bost.ocks.org/mike/selection has been written when v3 was introduced, also key argument to .data() and .merge() existed already in v3. So, I do not think, that there were significant changes.

          – Yaroslav Sergienko
          Nov 22 '18 at 11:48







          Can't say for sure. Most thorough article on selections bost.ocks.org/mike/selection has been written when v3 was introduced, also key argument to .data() and .merge() existed already in v3. So, I do not think, that there were significant changes.

          – Yaroslav Sergienko
          Nov 22 '18 at 11:48






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429011%2fd3-js-v5-enter-update-not-inserting-rectangle-div%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents