?
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
Medium Priority
?
442 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 1

Accepted Solution

by:
jxbma earned 0 total points
ID: 39699268
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
ID: 39699272
It was a matter of confusing non-scaled and scaled coordinates/points.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Find out what you should include to make the best professional email signature for your organization.
This article discusses how to implement server side field validation and display customized error messages to the client.
In this tutorial viewers will learn how to style different bullet points for unordered lists in CSS. Begin with a normal unordered list; the default bullet point is a solid circle: In the CSS, create a defined class of unordered list by typing "ul.S…
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
Suggested Courses

752 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