How to set intersection point of x and y axis

2020-04-15 11:09发布

问题:

I am using d3.js for scatter plot,I want to plot x and y axis such that they intersect at point(100,75).how to do this? I am using

svg.append("g")
    .attr("class", "axis")
    .attr("transform", "translate(0," + (padding+223) + ")")
    .call(xAxis2);

//Create Y2 axis
 svg.append("g")
   .attr("class", "axis")
   .attr("transform", "translate(" + (padding+200) + ",0)")
   .call(yAxis2);

But this will not change according to the scale,and I have used variable for scale. Please let me know if you need more information.

回答1:

You would need to offset the axes by the respective amount, which you can determine using the scales of the axes, i.e.

svg.append("g")
  .attr("class", "axis")
  .attr("transform", "translate(0," + yScale(75) + ")")
  .call(xAxis2);

and similarly for the y axis. You may have to tweak the offset slightly to account for other offsets, labels etc.