tamc / Sankey

A javascript library for drawing sankey / flow diagrams
http://tamc.github.com/Sankey/
Other
253 stars 88 forks source link

Right margin #8

Open ghost opened 11 years ago

ghost commented 11 years ago

Hello. In a diagram with only two stacks, I've noticed that the 'x' position of the right boxes is being wrongly calculated.

I have a 800x400 area, with the default 100 margin on each side. I'm using stack 0 and stack 1 for my boxes. Thus, the result of the function 'Sankey.calculateXStep' would be (800 - 100 - 100) / 1 = 600. For the boxes in stack 1 to respect the right margin, their 'x' should be exactly the result above, as both margins have been accounted for in the calculation. However, they're being positioned on box.x = 100 + (1 * 600) = 700, as per 'Sankey.position_boxes_and_lines'. This should probably be changed to 'box.x = this.left_margin + (x > 0 ? (x * x_step) - this.box_width : 0);', or some equivalent expression, assuming no negative stack indexes are used (not sure if this expression would work for more than two stacks).

Following is a screenshot of the issue, where you can see the left margin being respected, as opposed to the right margin.

Kind regards.

Untitled

ghost commented 11 years ago

Actually, I think I've figured out how to deal with more than two stacks. The following seems to work for me.

// this.maximum_stack_x is saved when calculating x_step
box.x = this.left_margin + (x * x_step);
// just add the following (assuming the minimum stack.x is zero):
if (x === this.maximum_stack_x) { box.x -= this.box_width; }
else if (x > 0) { box.x -= this.box_width / 2; }