Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8993447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:11:52+00:00 2026-06-15T23:11:52+00:00

Working in d3, I’ve got a function call that correctly draws a bar chart,

  • 0

Working in d3, I’ve got a function call that correctly draws a bar chart, but it doesn’t update it correctly. Here’s the important part of my code:

  # Drawing Elements
  @svg = d3.select(@s).append('svg').attr('width',@w).attr('height',@h)
  @rects = @svg.selectAll('rect')
  @labels = @svg.selectAll('text')

  drawRects: (data) ->
    rects = @rects.data(data)
    rects.enter().append('rect')
    rects.exit().remove()
    rects.attr('x', (d,i) => this.xScale(i) )
      .attr('y', (d) => @h - this.yScale(d) )
      .attr('width', @w / @data.length - @pad)
      .attr('height', (d) => this.yScale(d) )
      .attr('fill','#FFC05C')


  drawLabels: (data) ->
    labels = @labels.data(data)
    labels.enter().append('text')
    labels.exit().remove()
    labels.text( (d) -> d )
      .attr('text-anchor','middle')
      .attr('x', (d,i) => this.xScale(i) + ((@w / @data.length - @pad)/2) )
      .attr('y', (d) => @h - this.yScale(d) + 15 )
      .attr('fill','white')

  update: (data) ->
    this.drawRects(data)
    this.drawLabels(data)

When I initially set the graph I create some data (in this case an array of 20 numbers) and call the draw methods:

data = [1..20]
this.drawRects(data)
this.drawLabels(data)

The graphs are drawn correctly.

However, if these methods are called a second time, to update the data, the new data is placed in the DOM, but the old data is left behind as well, so it’s just making a whole new set of elements.

What am I doing wrong here that’s keeping .exit() from removing the old DOM elements?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:11:53+00:00Added an answer on June 15, 2026 at 11:11 pm

    You need to reselect the rect and text elements and assign them the new data:

    drawRects: (data) ->
        rects = @svg.selectAll('rect').data(data)
        #...
    
    drawLabels: (data) ->
        labels = @svg.selectAll('text').data(data)
        #...
    

    In your code you always assign the new data to the old (empty) @rect and (empty) @labels collections. And the data will be treated as “entered” for these empty collections and thus new elements will be created and nothing will be removed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working with an undisclosed API, I found a function that can set the number
Working through Pro ASP.NET MVC book and I got the following code snippet that
working on xcode I realized that if I create a non-void method, I call
Working with Reporting Services 2008 r2. So here's my issue: We have 5 reports
Working example: http://alpha.jsfiddle.net/gTpWv/ Both of the methods work separately, but once regexp for smilies
Working with a Lucene index, I have a standard document format that looks something
Working on Progress 9.1E on a Windows box. We've got a standard 4GL GUI
Working on an image gallery, but it keeps crashing on me (you control it
Working with SSRS and a SQL 2000 source, I have a stored procedure that
Working against jQuery 1.3.2 I'm looking for best practices that implement the typical '

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.