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

Milliseconds data detection and plotting #7

Open
danielkrizian opened this issue May 31, 2014 · 7 comments
Open

Milliseconds data detection and plotting #7

danielkrizian opened this issue May 31, 2014 · 7 comments
Milestone

Comments

@danielkrizian
Copy link
Owner

Detect the dataset resolution and automagically switch between the parser modes.

JavaScript millisecond parsing here (WIP) : danielkrizian/dygraphs#12

Other resources:
https://groups.google.com/forum/#!topic/dygraphs-users/qAKQndJvkRE
http://dygraphs.com/data.html

@TonyDIRL
Copy link

Hi,

Apologies for the delay. After guidance from the dygraph users group there is a working jfiddle of using millisecond plotting:
http://jsfiddle.net/kaliatech/DfWNz/3/

So to accomplish this in R, I think we'd first need to convert POSIXlt objects to milliseconds since epoch and enclose within "#! !#". As a reference here is an example of millisecond plotting in highcharts:

require(rCharts);require(xts)
npoints<-500
Data_xts=xts(1:npoints, as.POSIXlt(1366039619, tz="GMT", origin="1970-01-01") + round(runif(npoints,1,50)))
names(Data_xts)<-'RandomData'
DataDF=data.frame(Date=index(Data_xts),coredata(Data_xts$RandomData))
DataDF$Date=paste("#!", as.numeric(strptime((DataDF$Date), format="%Y-%m-%d %H:%M:%OS"))*1000, "!#")

HCGraph <- Highcharts$new()
HCGraph$yAxis(list(title = list(text = 'Data1') ) )
HCGraph$series(data = toJSONArray2(DataDF[,c('Date','RandomData')], json = F, names = F),enableMouseTracking=TRUE,shadow=FALSE,name = "Data1",type = "line")
HCGraph$xAxis(type = "datetime"); HCGraph$chart(zoomType = "x")
HCGraph$plotOptions(column=list(animation=FALSE),shadow=FALSE,line=list(marker=list(enabled=FALSE)));
HCGraph

Then based on the jfiddle we need to ass custom axisLabelFormatter and valueFormatter functions.

myChart$setOpts(axisLabelFormatter = "#! function (d, gran) {
                  return Dygraph.zeropad(d.getHours()) + ":" + Dygraph.zeropad(d.getMinutes()) + ":" + Dygraph.zeropad(d.getSeconds()) + "." + Dygraph.zeropad(d.getMilliseconds());
              } !#")
myChart$setOpts(valueFormatter = "#! function (ms) {
                  var d = new Date(ms);
                  return Dygraph.zeropad(d.getHours()) + ":" + Dygraph.zeropad(d.getMinutes()) + ":" + Dygraph.zeropad(d.getSeconds()) + "." + Dygraph.zeropad(d.getMilliseconds());
              }!#")

Unforuntately, I was unable to get this to work - could you take a look please.

Cheers

@danielkrizian
Copy link
Owner Author

After I edited your comment with adding ```r blocks around the code, I noticed that " in `":"` interfere with `"#! !#"`. Replace inside `"` quotes with equivalent `'`.

Thanks for providing example data and jsfiddle. Reproducible code of what you have tried so far with Dygraph would be also nice.

I was able to produce something like this:

npoints<-500
# second data:
# Data_xts=xts(1:npoints, as.POSIXlt(1366039619, tz="GMT", origin="1970-01-01") + round(runif(npoints,1,50)))
# millisecond data:
Data_xts=xts(1:npoints, as.POSIXlt(1366039619, tz="GMT", origin="1970-01-01") + rnorm(npoints, 500000, 250000)/1000000)
names(Data_xts)<-'RandomData'

myChart = dygraph(Data_xts)
myChart$setOpts(axes=list(
  x=list(
    ticker="#! Dygraph.dateTicker !#",
    axisLabelFormatter = "#! function (d, gran) {
                              return Dygraph.zeropad(d.getHours()) + ':' + 
                                     Dygraph.zeropad(d.getMinutes()) + ':' + 
                                     Dygraph.zeropad(d.getSeconds()) + '.' + 
                                     Dygraph.zeropad(d.getMilliseconds());
                             } !#",
    valueFormatter = "#! function (ms) {
                          var d = new Date(ms);
                          return Dygraph.zeropad(d.getHours()) + ':' + 
                          Dygraph.zeropad(d.getMinutes()) + ':' + 
                          Dygraph.zeropad(d.getSeconds()) + '.' + 
                          Dygraph.zeropad(d.getMilliseconds());
                         } !#"
    )
  )
)

myChart

Second data:
image
Millisecond data:
image

Note that time is off by exactly 1 hour, but that's problem in general with daylight saving offset I think and evident with daily frequency data too.
Let me know how does that work for your case. Once this use case is mature, I will implement automatic formatting based on data frequency detection in the Dygraph ReferenceClass.

@TonyDIRL
Copy link

Thanks for catching the double versus single quotes parsing error.
Apologies, I provided second rather than millisecond level data yesterday, updated as:

Data_xts=xts(1:npoints, as.POSIXlt(1366039619, tz="GMT", origin="1970-01-01") + rnorm(npoints, 500000, 250000)/1000000)

I confirm your suggested changes work with the exception of how the data is passed to dygraph.
Passing the data.frame directly:

 myChart = dygraph(DataDF)

Result in POSIXlt complaining about the format - so I assume it is still trying to obtain a date object. Where passing in the xts object directly worked with issue

 myChart = dygraph(Data_xts)

Out of interest how do you plan to automatically detect data frequency?

@danielkrizian
Copy link
Owner Author

Yes sorry, was typing that fast, I meant myChart = dygraph(Data_xts). Thanks for the millisecond update, edited my prev comment accordingly.

Out of interest how do you plan to automatically detect data frequency?

Don't know yet, focusing on other parts for the time being. Any suggestions, contributions welcome! Feel free to fork, develop and I will make sure to answer your specific questions, review and accept your pull request :)

@danielkrizian danielkrizian modified the milestones: Next Tasks, Blue Sky Jun 15, 2014
@TonyDIRL
Copy link

Sure, I'll fork and have a go.

@hallegue
Copy link

hallegue commented Feb 4, 2016

Hi I tried your code in order to display milliseconds in a dygraph plot but when I run this code:

myChart$setOpts(axes=list(
    x=list(
        ticker="#! Dygraph.dateTicker !#",
        axisLabelFormatter = "#! function (d, gran) {
        return Dygraph.zeropad(d.getHours()) + ':' + 
        Dygraph.zeropad(d.getMinutes()) + ':' + 
        Dygraph.zeropad(d.getSeconds()) + '.' + 
        Dygraph.zeropad(d.getMilliseconds());
        } !#",
        valueFormatter = "#! function (ms) {
        var d = new Date(ms);
        return Dygraph.zeropad(d.getHours()) + ':' + 
        Dygraph.zeropad(d.getMinutes()) + ':' + 
        Dygraph.zeropad(d.getSeconds()) + '.' + 
        Dygraph.zeropad(d.getMilliseconds());
        } !#"
    )
)
)

I got this error:

Error: attempt to apply non-function

Do you know what to fix it?

@danielkrizian
Copy link
Owner Author

@Haycen please note that the active development has moved to rstudio/dygraphs repository and they track the sub-second handling issue.

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

No branches or pull requests

3 participants