Skip to content
Advertisement

My rectangle don’t have the good coordinates without a zoom event in d3

I’m doing a zoom in a chart. But i have a problem. My rectangles need a zoom event to display at the correct position, if they do not have this event, they are displayed at wrong coordinates.

I have done a jsfiddle https://jsfiddle.net/Natko/mr1250un/90/

The problem is in this part of the code (i think)

JavaScript

All the attributes are working except the x and y coordinates. But when i’m updating the coordinates with my zoom event, they are displayed in the right place.

JavaScript

And here is the this.x and this.y

JavaScript

Anyone have an idea to fix my problem ?

Advertisement

Answer

Your code has multiple errors and here are the main points:

  1. You add a <g> element and set its transform (I saw it in the fiddle, not in the question’s code sample) according to x and y, it’s OK.

  2. You add a <rect> under each <g> and assign the same id to all. It will not work, id should be unique. You don’t need the attribute at all, omit it.

  3. You cannot use x and y attributes for a <g> element. You should use transform: g.attr('transform', 'translate(x, y)')

  4. In ‘change’, you try to set the <rect> coordinates within its <g> instead of assign new transform to the <g> element itself. Keep the <rect> as is.

  5. You can calculate transform with a simple formula: transformedX = originalX * e.transform.k + e.transform.x, where e is a D3 zoom event.

Advertisement