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?
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.
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.
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.
The d3. scaleLinear() method is used to create a visual scale point. This method is used to transform data values into visual variables.
You can simply query the scale for its domain after .nice()
and then get the minimum value of that:
var yMin = yScale.domain()[0];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With