Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

d3.js how to get minimum value of scale's domain when using nice()

I have a scatter plot where I am trying to plot a reference point that should appear directly on the x-axis, but I am creating the y-axis like so:

   // 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 issue is that I want to plot a reference point directly on the x-axis. If I don't use .nice(), I can easily plot the reference point like so:

        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 seem to figure out how to do this when using .nice(). Is there another way I could go about this such as somehow getting the y-coordinate of the x-axis line?

I started digging through the d3 code to see how I could calculate the nice value and I understand the code, but it seems wasteful to calculate the value again not to mention the unnecessary maintenance of duplicate code - especially considering I also have reference points I will need to plot along the y-axis and I am using a logarithmic scale for my x-axis (the logarithmic nice() function is different from the linear nice() function so this would mean additional duplicate code and unnecessary maintenance).

Ideas?

like image 979
Jordan Avatar asked Oct 31 '14 18:10

Jordan


People also ask

What will be the output of scale 300?

It means scaling factor is 0.5 and the data will be represented in pixels as: data value * 0.5. So, now if our input value is 300, the output value would be 150.

Which is valid scale in D3 JS?

D3 creates a function myScale which accepts input between 0 and 10 (the domain) and maps it to output between 0 and 600 (the range). Scales are mainly used for transforming data values to visual variables such as position, length and colour.

What is domain and range in D3?

The domain is the complete set of values, so in this case that is all of your temperatures, from 33 to 64. The range is the set of resulting values of a function, in this case the resulting values of scaling your temperatures from 0 to 600.

What is D3 scale linear?

The d3. scaleLinear() method is used to create a visual scale point. This method is used to transform data values into visual variables.


1 Answers

You can simply query the scale for its domain after .nice() and then get the minimum value of that:

var yMin = yScale.domain()[0];
like image 179
Lars Kotthoff Avatar answered Oct 06 '22 18:10

Lars Kotthoff