Question

I have dual scaled line chart using d3, coffescript is below

# bottom axis
xScale = d3.time.scale().range([0,width]).domain(d3.extent(data, (d) -> d.date))
xAxis = d3.svg.axis().scale(xScale).orient("bottom").ticks(5)
# left axis
yLeftMax = d3.max(data, (d) -> d.price)
yLeftScale = d3.scale.linear().domain([0, yLeftMax]).range([height, 0])
yLeftAxis = d3.svg.axis().scale(yLeftScale).orient("left")
# right axis
yRightMax = d3.max(data, (d) -> d.yoy)
yRightMin = d3.min(data, (d) -> d.yoy)
yRightAbsMax = Math.max(Math.abs(yRightMin), Math.abs(yRightMax))
yRightScale = d3.scale.linear().domain([-yRightAbsMax, yRightAbsMax]).range([height, 0])
yRightAxis = d3.svg.axis().scale(yRightScale).orient("right")
myZoom = ->
  canvas.select("._x._axis").call xAxis
  #canvas.select(".axisLeft").call yLeftAxis  // can't zoom left axis here
  canvas.select(".axisRight").call yRightAxis // Zoom right axis
  canvas.select(".line1").attr("d", line1(data)) // Zoom left scaled line
  canvas.select(".line2").attr("d", line2(data)) // Zoom right scaled line
zoom = d3.behavior.zoom()
 .x(xScale) // set xScale for zoom 
 #.y(yLeftScale) // can't set left yScale for zoom here
 .y(yRightScale) // set right yScale for zoom  
 .scaleExtent([1,20]) # 20x times zoom
 .on("zoom", myZoom)
...

The problem is, when I'm using zoom, either left or right y-axis is zoomed, i.e. I can't get zoom working for both y-scales.

Was it helpful?

Solution

In your myZoom function, you can explicitly set the domain of the yLeftScale:

myZoom = ->
  scale = d3.event.scale
  translate = d3.event.translate

  # Haven't verified these calculations. Is it scale first or translate first?
  # See `alternate answer` below for a better approach
  yLeftScale.domain([0 + translate[1], yLeftMax / scale + translate[1])
  canvas.select("._x._axis").call xAxis
  canvas.select(".axisLeft").call yLeftAxis  # can't zoom left axis here
  canvas.select(".axisRight").call yRightAxis # Zoom right axis
  canvas.select(".line1").attr("d", line1(data)) # Zoom left scaled line
  canvas.select(".line2").attr("d", line2(data)) # Zoom right scaled line

And alternate solution is having two separate zoom behaviors and controlling the other zoom behavior instead of manually updating the domains. I find this to be more robust though a bit more verbose.

zoomLeft = d3.behavior.zoom()
 .x(xScale) # set xScale for zoom 
 .y(yLeftScale) # can't set left yScale for zoom here
 .scaleExtent([1,20]) # 20x times zoom

and in myZoom, set the zoomLeft's scale and translate:

myZoom = ->
  zoomLeft.scale(zoom.scale()).translate(zoom.translate())

  canvas.select("._x._axis").call xAxis
  canvas.select(".axisLeft").call yLeftAxis  # can't zoom left axis here
  canvas.select(".axisRight").call yRightAxis # Zoom right axis
  canvas.select(".line1").attr("d", line1(data)) # Zoom left scaled line
  canvas.select(".line2").attr("d", line2(data)) # Zoom right scaled line
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top