最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to graph dates on X axis in Rickshaw - Stack Overflow

programmeradmin0浏览0评论

I have a set of data for dates. What value should I provide the X axis values? How do I make Rickshaw display the X data values as dates?

I looked around the docs and examples and cannot find anything.

I have a set of data for dates. What value should I provide the X axis values? How do I make Rickshaw display the X data values as dates?

I looked around the docs and examples and cannot find anything.

Share Improve this question edited Sep 30, 2013 at 18:39 Krystian Cybulski asked Sep 30, 2013 at 13:36 Krystian CybulskiKrystian Cybulski 11.1k14 gold badges71 silver badges99 bronze badges 7
  • Did you see this example? – Lars Kotthoff Commented Sep 30, 2013 at 18:09
  • Yes. I have looked at the examples. I have now figured out that my axis were not showing nicely because I did not include the css. But I still have not figured out how to provide the date data and render it as dates. The examples generate random data and I do not know which format it is in. – Krystian Cybulski Commented Sep 30, 2013 at 18:38
  • I guess the easiest thing to do would be to print the variable that holds the data to the debug console so you can have a look at it. – Lars Kotthoff Commented Sep 30, 2013 at 18:44
  • Have you looked at "axes and tick marks" section in rickshaw documentation? code.shutterstock.com/rickshaw – Phuoc Do Commented Oct 1, 2013 at 5:52
  • You may be able to use d3 format function, like this: var xAxis = d3.svg.axis().scale(x).orient("bottom").tickFormat(d3.time.format("%m-%d")); – Phuoc Do Commented Oct 1, 2013 at 5:53
 |  Show 2 more comments

4 Answers 4

Reset to default 13

I've just started using Rickshaw and was in the exact situation.

But, before I go any further, Rickshaw documentation is virtually nonexistent which is very upsetting because the performance of Rickshaw compared to other JS graphing libraries is outstanding.

The best way to find examples is to dig into the source code and example code on their github page try to make sense of things (not the way documentation should be).

That being said, let's try and build a strong base of questions/answers here on StackOverflow!

So, back to the question :) It looks like you've already found your own solution to the question, but I'll provide my solution as well.

Rather than using Rickshaw.Graph.Axis.Time, I've used Rickshaw.Graph.Axis.X and set the tickFormat accordingly.

var data = [ { x: TIME_SINCE_EPOCH_IN_SECONDS, y: VALUE }, 
             { x: NEXT_TIME_SINCE_EPOCH_IN_SECONDS, y: NEXT_VALUE } ]

var xAxis = new Rickshaw.Graph.Axis.X({
  graph: graph,
  tickFormat: function(x){
                return new Date(x * 1000).toLocaleTimeString();
              }
})
xAxis.render();

toLocaleTimeString() can be any of the Javascript date functions, such as toLocaleString(), toLocaleDateString(), toTimeString(), or toUTCString(). Obviously, because the tickFormat takes a function as an argument one can supply their own formatter.

Koliber, I'd be interested to understand your answer if you could provide more detail as well.

Additional to Lars' reply, I found by default Rickshaw is calling

 .toUTCString(x.value*1000) //(just ctrl+F to find where =) ). 

In my case, I saw different time label on X between Graphite and Rickshaw for this reason, and it works beautifully once I changed it to

 .toLocaleString(x.value*1000). 

Plus, you may need modify this in two places : Rickshaw.Graph.Axis.Time and the ...HoverDetails

I have finally figured out that the X axis values should be epoch time values. Then, using the code from the examples I was able to show a proper time scale.

I still have a problem because I would like to show the tick marks on weeks on the X axis. However, setting timeUnit to 'week' causes JavaScript errors. It works with other time units though.

None of this worked for me. What worked with angularjs was:

'x' : d3.time.format.iso.parse(date).getTime(), 'y' : 10

发布评论

评论列表(0)

  1. 暂无评论