Solved

Why is the circle position in a D3 chart not proportionally tracking with mouse during a Drag operation?

Posted on 2013-11-25
2
412 Views
Last Modified: 2013-12-05
I'm relatively new to the world of D3, charting and visualizations.
I have a bubble chart with connected lines (see attached image).
I have the x/y scales set up so that the points plot and display correctly.

I am running into issues when I attempt to drag a circle (bubble) to
change it's height (y value) within the chart. I get the drag event and am able to
move the selected circle shape.

The issue is that the movement/new placement of the circle shape within the chart is
not proportional to the mouse movements. I'm pretty sure that it has something to do with scaling the d3.event.dy value so it matches the chart's y axis scale.

Here is my on drag handling code snippet:
var drag = d3.behavior.drag()
        .origin(function() { 
                var t = d3.select(this);
                return {x: t.attr("x"), y: t.attr("y")};
            })
        .on("dragstart", function(d,i) {
			
              // Set the style of the dragged node
              d3.select(this)
                  .style("fill", "red")
                  .attr("stroke", "orange")
                  .attr("stroke-width", 4)                
                  .attr("r", function(d){ return d.radius * 2 });
 
        })
        .on("drag", function(d,i) {

            // We are only tracking vertical movement, so we are only interested in dY changes.
                   
             d3.select(this)
			
               .attr("cx", function(d, i) {
                    xlocation=xScale(d.x_axis);
                    ylocation=yScale(d.y_axis);
                    return xScale(d.x_axis);
                })
			   
                .attr("cy", function(d, i) {
                    d.y_axis -= d3.event.dy;
                    return yScale(d.y_axis);
                });
      })

Open in new window



Thanks in advance for taking your advice.
JohnB
Chart-Snapshot.PNG
0
Comment
Question by:jxbma
  • 2
2 Comments
 
LVL 1

Accepted Solution

by:
jxbma earned 0 total points
Comment Utility
The issue was tat I was confusing scaled points/cords with non-scaled points/cords for the cy. If I make the following changes, it all works now.
			   
                .attr("cy", function(d, i) {
		    
                    var y1 = yScale(d.y_axis) + d3.event.dy;
                    d.y_axis = yScale.invert(y1);
                    
                    return y1;
                });

Open in new window

0
 
LVL 1

Author Closing Comment

by:jxbma
Comment Utility
It was a matter of confusing non-scaled and scaled coordinates/points.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
CSS is a visual language used to classify objects and define rules about how they should be displayed. CSS skills aren’t restricted to developers anymore, there is a big benefit to having a basic understanding of the language, regardless of your occ…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now