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 9111713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:34:16+00:00 2026-06-17T03:34:16+00:00

I have a button which draws a line and some dots on that line

  • 0

I have a button which draws a line and some dots on that line on a graph. I have a second button which removes the line and the dots. I’m trying to use the same button for both functions.

Im having some difficulty with it. Has anyone seen anything like this before?

Here are my two functions. Thanks in advance!

 //Draws line and dots
 d3.select(".button1").on("click", function(){
  var path = svg.append("path")   // Add the valueline path. 
          .attr("class", "line")
          .attr("d", valueline(data))
          .attr("stroke", "steelblue")
          .attr("stroke-width", "5")
          .attr("fill", "black");


svg.selectAll("dot")
    .data(data)
.enter().append("circle")
    .attr("class", "firstDots")
    .attr("r", 5)
    .attr("cx", function(d) { return x(d.date); })
    .attr("cy", function(d) { return y(d.close); })

      var totalLength = path.node().getTotalLength();


  path
    .attr("stroke-dasharray", totalLength + "30" * 30)
    .attr("stroke-dashoffset", totalLength)
    .transition()
      .duration(2000)
      .ease("linear")
      .attr("stroke-dashoffset", 0);

     //Removes line and dots
     d3.select(".button2").on("click", function(){
      path.remove();

  var removeDot = svg.selectAll(".firstDots")
                .remove();

     });

});

At first I tried passing the class of the buttons back and forth on each click event, it works for drawing and removing. But only once, so I am not able to draw the line and second time.

  • 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-17T03:34:17+00:00Added an answer on June 17, 2026 at 3:34 am

    You could move the path variable outside the event-handler:

    <!DOCTYPE html>
    <html>
    <head>
        <script src='http://d3js.org/d3.v3.min.js'></script>
    </head>
    <body>
        <button>Toggle path</button>
        <script>
        var svg = d3.select('body')
            .append('svg')
            .attr('width', 500)
            .attr('height', 250);   
    
        var path;
    
        d3.select('button').on('click', function() {
    
            if ( path ) {
                path.remove();
                // Remove dots
                path = null;
            } else {
                path = svg.append('path')
                    .attr('class', 'line')
                    .attr('d', 'M100,150 Q200,25 300,150 T500,150')
                    .attr('stroke', 'steelblue')
                    .attr('stroke-width', '5')
                    .attr('fill', 'black');
                // Add dots etc.
            }        
        });
        </script>
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which takes a touch on one button and draws
I have code which will draw a graph that scales if the user attempts
I have a basic line graph with two lines. When I click a button,
I have an HTML canvas on which a user draws some shapes. Now suppose
I have button which post values of form to url but during post i
I have a button which on Click loads another activity, but before it loads
I have a button which creates form elements on click . My addelement function
I have a button which needs to get created dynamically but the problem is
I have a button which is in update panel. When I click on button
I have a button which opens a context menu with a list of various

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.