Skip to content
Advertisement

d3.js x-coord displaying incorrectly iOS

Using d3.js the x coordinates of the graph are displaying at x=0 or on the y axis. The x axis represents a date and time and the y axis is the temperature. But this is only on an ipad or iphone. On my own machine, Linux, it displays correctly.

The graphs and all file can be seen at, http://shanespi.no-ip.biz

The ipad/iphone display

enter image description here

While the correct graph is,

enter image description here

Here is the javascript,

JavaScript

Here is the ‘datahourly’ data,

JavaScript

I am using Chrome on Linux and Safari on the ipad and iphone. But I did install chrome on the iphone and the graph is still incorrect.

Are there svg issues with iOS?

EDIT: The main issue was that the time data was not parsed correctly,

This is the correct solution,

JavaScript

Advertisement

Answer

You can clearly see that your circles are getting the correct y (“cy”) value, the error lies in the x (“cx”) value.

The problem seems to be the use of new Date() in Safari with this pattern: yyyy-MM-dd.

In your code, given your data structure, you’ll end up having something like this in the line generator:

JavaScript

And the same for your circles:

JavaScript

Apparently, this is supported by Chrome and Firefox, but not by Safari. Curiously, the pattern (yyyy-MM-dd) is included in the ECMA standard, so this is probably a Safari specific issue.

According to this answer, it will work if you include a T (I didn’t test it):

JavaScript

Alternatively, remove the new Date() and parse the dates using D3 (d3.timeParse() in d3 v4.x and format.parse() in d3 v3.x).

EDIT: Summarising, you have two possible solutions:

Solution 1: Remove all new Date functions (both on the line generator and in all scales that use it) and parse the date using d3. You said that you’re using d3 v3.x, but your code is using d3 v4.x instead. Nevertheless, here is how to do it using d3 v3.x:

JavaScript
JavaScript

Note: I’m removing the last 3 digits because d3 can parse onli miliseconds, not microseconds.

Solution 2: Keep your new Date functions, but add a T as already discussed above:

JavaScript
Advertisement