D3.js how to get the minimum value of the zoom area when using nice ()

I have a scatter plot where I am trying to build a control point that should be displayed directly on the x axis, but I create the y axis like this:

// give ourselves some space yMin = yMin * 0.9; yMax = yMax * 1.1; // set up y var yValue = function (d) { return d.price; }, yScale = d3.scale.linear() .domain([yMin, yMax]) .range([height, 0]) .nice(), yAxis = d3.svg.axis() .scale(yScale) .orient("left"); 

The problem is that I want to build the anchor point directly on the x axis. If I do not use .nice (), I can easily build the anchor point like this:

  var reference = svg.append('g').attr("class", "grid-reference"); reference.append("circle").attr("id", "some-reference-value") .attr("class", "dot") .attr("r", 9) .attr("cx", xScale(someReferenceValue)) .attr("cy", yScale(yMin)) .style("fill", "grey") .style("opacity", 1); 

but I can't figure out how to do this when using .nice (). Is there any other way I could do this, for example, to somehow get the y-coordinate of the x-axis line?

I started digging through the d3 code to see how I can calculate a good value, and I understand the code, but it seems useless to calculate the value again, not to mention the unnecessary maintenance of the duplicate code, especially considering that I also have a point reference to me will need to build along the y axis, and I use the logarithmic scale for the x axis (the logarithmic function nice () is different from the linear function nice (), so this will mean extra duplicate code and unnecessary maintenance).

Ideas?

+6
source share
1 answer

You can simply request a scale for your domain after .nice() , and then get the minimum value:

 var yMin = yScale.domain()[0]; 
+11
source

Source: https://habr.com/ru/post/977560/


All Articles