Modifying scales with behavior.zoom + manual domain control

1,312 views
Skip to first unread message

Davin

unread,
Jun 29, 2011, 5:09:21 AM6/29/11
to d3...@googlegroups.com
I tried combining two different controls to modify the domain of the scales on a chart I'm building.  One control is just like the zoom-pan example where you can grab the chart and pan or zoom.  d3.event.transform modifies the scale domains in this case.  Another control manually modifies the domain by calling scale.domain([.., ..]).  

The weird thing is that if I go back and forth between using the manual control and using the zoom-pan controls, the scale domains get out of sync.  It seems that d3.scale.linear is caching an older version of the scale (perhaps scale.__domain) which gets restored by d3.event.transform before that function does it's transform.  If I stick to only using the manual control or only using the zoom-pan controls there is no problem.

Is there a proper way I should be applying changes to the scale domain so that they get picked up by behavior.zoom?  

Thanks!

Mike Bostock

unread,
Jun 29, 2011, 11:10:38 AM6/29/11
to d3...@googlegroups.com
> The weird thing is that if I go back and forth between using the manual
> control and using the zoom-pan controls, the scale domains get out of sync.
>  It seems that d3.scale.linear is caching an older version of the scale

Correct, except it's d3.behavior.zoom that's caching the original
domain, rather than d3.scale.linear. The zoom behavior needs to know
what the original domain of the scale is, such that it can apply the
pan & zoom relative to the original domain.

The zoom behavior is still a work-in-progress, and if you have
suggestions on how the API could be designed to be more usable, I'm
all ears. :)

Mike

Davin

unread,
Jun 29, 2011, 12:24:54 PM6/29/11
to d3...@googlegroups.com
I'm curious why behavior.zoom needs to cache the original scale when the scale is passed into the d3.event.transform function as an argument.  Shouldn't the passed in argument supercede the cached scale?  I'm just thinking that behavior.zoom shouldn't assume that it is the only one modifying the scales...  Perhaps it can make caching the scale optional?

Davin

Mike Bostock

unread,
Jun 29, 2011, 12:49:31 PM6/29/11
to d3...@googlegroups.com
It needs to know the original domain. For example, say the behavior
changes the zoom level from 2 to 2.1. The passed in scale's domain is
already zoomed in by 2; so, without the original domain, the zoom
behavior would increase the effective zoom level to 2 * 2.1 = 4.2.

The transform methods on the zoom event are optional; you can always
modify the scale's domain yourself, using the event's scale and
translate attributes, rather than using the built-in methods which
cache the scale's domain. Then you have more control over the
behavior.

Mike

ma...@m00s3jaw.net

unread,
May 23, 2013, 10:24:57 AM5/23/13
to d3...@googlegroups.com, mbos...@cs.stanford.edu
Do you have an example that does this?

Mike Bostock

unread,
May 23, 2013, 12:08:28 PM5/23/13
to d3...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages