Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ranges with one number and one string generate axis ticks with invalid translations #107

Closed
ajoslin103 opened this issue Jun 7, 2017 · 4 comments

Comments

@ajoslin103
Copy link

Not that I was doing this on purpose...

// dynamically determine the stylistic width of the graph, then strip the 'px' 
let width =String(this.graph.attr('width') || this.graph.style('width')).replace('px', '');
let height = String(this.graph.attr('height') || this.graph.style('height')).replace('px', '');

let xExtent = d3.extent(this.getTimeData()); // a series of millisecond times
let xScale = d3.scaleTime().domain(xExtent).range([0, width]);
let xAxis = d3.axisBottom(xScale);

d3.select('lines').append("svg").append("g").call(xAxis);

resulted in erroneous translations such as: transform="translate(20.8287963768115960.5,0)"
(notice the two decimal points in the x value)

the fix

let width = Number(String(this.graph.attr('width') || this.graph.style('width')).replace('px', ''));
let height = Number(String(this.graph.attr('height') || this.graph.style('height')).replace('px', ''));
@mbostock
Copy link
Member

mbostock commented Jun 7, 2017

This is the correct behavior for d3-scale, as a time scale makes no assumption that its range is numbers; however, it is the incorrect behavior for d3-axis, which should coerce the value returned by the scale to a number when setting the transform attribute.

@mbostock
Copy link
Member

mbostock commented Jun 7, 2017

(Of course, fixing this in d3-axis will mask the bug in your code; I recommend coercing your width and height to numbers rather than leaving them as strings.)

@ajoslin103
Copy link
Author

ajoslin103 commented Jun 7, 2017

wow, you're fast! yes, I have applied that fix!

is there a better way to get the size of the graph dynamically?

and: cool! you can just prefix with a '+' !!

@mbostock
Copy link
Member

mbostock commented Jun 8, 2017

Possibly element.getBoundingClientRect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants