rickshaw icon indicating copy to clipboard operation
rickshaw copied to clipboard

Timezone is not adjustable for Time based axis

Open edasque opened this issue 12 years ago • 27 comments

While very elegant, the existing system doesn't seem to allow displaying the x axis unix timestamps using whatever time zone is appropriate (browser time zone for example).

It ends up using (for hour formatting) something like this:

new Date(1350425400*1000).toUTCString().match(/(\d+:\d+):/)[1]

no matter what

edasque avatar Oct 16 '12 22:10 edasque

:thumbsup:

See also https://github.com/shutterstock/rickshaw/blob/master/src/js/Rickshaw.Fixtures.Time.js#L5 -- this tzOffset variable isn't being used anywhere.

metanation avatar Oct 17 '12 04:10 metanation

+1

abarre avatar Oct 22 '12 08:10 abarre

Yes, it would be nice if this were more flexible.

dchester avatar Oct 23 '12 12:10 dchester

We actually ran into this problem recently, and we forked the repo to include time zone offset. You can check out the code here: https://github.com/alphaboost/rickshaw/blob/master/src/js/Rickshaw.Graph.Axis.Time.js

Basically, we added an argument to the Rickshaw.Graph.Axis.Time.js, which should be the time zone offset from UTC (i.e. -7 for PDT) * the number of seconds in an hour (3600, since Rickshaw uses seconds instead of milliseconds).

The code isn't quite ready for a pull request on Rickshaw, but it did the trick for us. When we have time to polish it up we'll drop it on a pull.

ryancurtin avatar Oct 23 '12 13:10 ryancurtin

That's great, I'll use that in the mean time but would love to it to be pulled into Rickshaw after cleanup

edasque avatar Oct 23 '12 13:10 edasque

Cool, I'll get this on a pull by the end of the week.

ryancurtin avatar Oct 23 '12 13:10 ryancurtin

Any update on the pull request?

edasque avatar Oct 30 '12 15:10 edasque

+1 as well as formatting the hover timestamp

skyepn avatar Nov 03 '12 01:11 skyepn

It seems you can accomplish this by overriding the xFormatter function like so. If you want time as well as date, use toString() instead of toDateString() or format it however you like

        var hoverDetail = new Rickshaw.Graph.HoverDetail( {
            graph: graph,
            formatter: function(series, x, y) {
                var date = '<span class="date">' + new Date(x * 1000).toDateString() + '</span>';
                var swatch = '<span class="detail_swatch" style="background-color: ' + series.color + '"></span>';
                var content = swatch + series.name + ": " + numberWithCommas(parseInt(y)); // + '<br>' + date;
                return content;
            },
            xFormatter: function(x) {
                return new Date(x * 1000).toDateString();
            },
        } );

skyepn avatar Nov 03 '12 02:11 skyepn

Sorry about the delay...I haven't been able to get online with the east coast hurricane. I'll get a pull request up once I'm able to get online.

On Friday, November 2, 2012, Skye Nott wrote:

It seems you can accomplish this by overriding the xFormatter function like so. If you want time as well as date, use toString() instead of toDateString() or format it however you like

    var hoverDetail = new Rickshaw.Graph.HoverDetail( {
        graph: graph,
        formatter: function(series, x, y) {
            var date = '<span class="date">' + new Date(x * 1000).toDateString() + '</span>';
            var swatch = '<span class="detail_swatch" style="background-color: ' + series.color + '"></span>';
            var content = swatch + series.name + ": " + numberWithCommas(parseInt(y)); // + '<br>' + date;
            return content;
        },
        xFormatter: function(x) {
            return new Date(x * 1000).toDateString();
        },
    } );

— Reply to this email directly or view it on GitHubhttps://github.com/shutterstock/rickshaw/issues/140#issuecomment-10035466.

ryancurtin avatar Nov 03 '12 17:11 ryancurtin

I think that the time axis should be drop in favor of d3.time.scale as it's mentioned in #163.

If we do that, we could close this issue.

abarre avatar Dec 24 '12 17:12 abarre

+1 as well as formatting the hover timestamp

funvit avatar Jan 22 '13 11:01 funvit

This (or #163) seems like a popular request for our Giraffe users... Any ideas about the complexity of supporting this in Rickshaw? We'd be happy to try to contribute some code to make this happen, but might need some help getting started.

gingerlime avatar Mar 25 '13 08:03 gingerlime

I fix it Here: https://github.com/idning/giraffe try it

idning avatar Apr 30 '13 17:04 idning

Hey @idning - I had a quick look, but it's hard to see which changes were made to Rickshaw / D3 to make this work. As far as I could see, those were the only changes - and not specifically inside Giraffe?

I think it would be great if you could make a pull request to Rickshaw, so this might hopefully get merged-in and supported by the Rickshaw library.

gingerlime avatar Apr 30 '13 20:04 gingerlime

I made a Pull Request #239 out of necessities. My goal was more narrow to show local time, instead of make timezone adjustable.

I didn't go the d3.time.scale route, because I am not familiar with d3 code base and thought it would take longer.

I post it here because it might help other, before the real fix.

thomasyip avatar May 01 '13 01:05 thomasyip

You can also just make your own unit formatter and send it to timeUnit. I'm using moment in this example:

# Create the formatter
unit_formatter = name: '2 hour', seconds: 3600 * 2, formatter: (d) -> moment(d).format('h:mm a')

# Pass to the time axis
xAxis = new Rickshaw.Graph.Axis.Time
    graph: graph,
    ticksTreatment: ticksTreatment,
    timeUnit: unit_formatter
xAxis.render()

rbriski avatar Jul 17 '13 23:07 rbriski

This looks like a great suggestion/workaround @rbriski, thanks! I'll try to see if I can use it for giraffe, to resolve kenhub/giraffe#15 and kenhub/giraffe#21

gingerlime avatar Jul 19 '13 05:07 gingerlime

+1 for handling timezones

pierre-b avatar Feb 07 '15 18:02 pierre-b

The trick actually is hacking the ticks like @ryancurtin did, and use a custom time unit formatter as @rbriski said too.

The first hack is used to sync the X axis ticks with your data points, and the second one is to avoid having the wrong date displayed on the ticks.

Anyway, the ticks offsets will be often wrong because they are calculated with seconds span and not from a real calendar (i.e: month = 86400 * 30.5), so I think this Time Axis has to be rewritten from scratch.

Will post one I have time to write it.

pierre-b avatar Feb 07 '15 20:02 pierre-b

Hi guys,

Took time this morning to write a new component wich uses d3.time to compute ticks offsets and allow you to customize the labels for displaying proper timezone formats.

Here is the code: https://gist.github.com/pierre-b/f9be76e3237c12951262

Feel free to merge it with the official code base if you want to.

pierre-b avatar Feb 09 '15 10:02 pierre-b

@pierre-b , thanks for putting in the time (in multiple senses of the phrase.) The link to your code is giving a 404 at the moment, however

UPDATE: I've since placed in @ryancurtin's suggested edits, and got things to work. Thanks!

RashidClark avatar Dec 23 '15 18:12 RashidClark

Sorry @RashidClark since there was no much activity here I thought the Rickshaw project was dead and I deleted my code to switch to another graph lib (a commercial one), however thank you for Rickshaw I was really happy to use it by the time ;)

pierre-b avatar Dec 28 '15 12:12 pierre-b

So there's still no official, merged solution for this, despite multiple attempts at solutions and pull requests? Is anyone even still managing this project?

IronSean avatar Mar 14 '16 17:03 IronSean

+1 (even if this is like 5 month late, still it's really worth it to have timezone offsetting)

ren90 avatar Aug 11 '16 08:08 ren90

The Rickshaw documentation is really bad, so you have to read the source code to make sense of the library which is agony for new comers. Maybe we all should make a public documentation for this amazing library.

Anyway, here's a simple solution.

For the X-Axis use - Rickshaw.Fixtures.Time.Local():

var x_axis = new Rickshaw.Graph.Axis.Time( {
            graph: graph,
            timeFixture: new Rickshaw.Fixtures.Time.Local()
        } );

Now you might have face inconsistency with Rickshaw.Graph.HoverDetail. So for that, update the xFormatter as follows:

var hoverDetail = new Rickshaw.Graph.HoverDetail( {
        graph: graph,
        xFormatter: function(x) {
                return new Date( x * 1000 ).toLocaleString();
                    }
        });

This is because Rickshaw by default uses toUTCString(). You can further modify the date format to your liking.

Side note: Rickshaw uses seconds instead of milliseconds, therefore the (x * 1000)

ksheetijsaini avatar Oct 17 '16 18:10 ksheetijsaini

+1 for handling timezones

chriscummings avatar Oct 26 '16 02:10 chriscummings