2014-02-05 86 views
1

我有一种情况,即在工作时间内以五分钟的时间间隔收集图表上显示的数据,而在晚上它保持空闲状态。当图表显示几天的数据时,在几天(夜间)之间显然有一条平坦的线条。类别轴 - 刻度格式

使用类别轴解决了晚上长时间稳定的问题。但是使用时间轴我能够格式化日期和使用时间间隔属性。

如何使用类别轴来做到这一点?我的意思是格式化刻度标签并达到使用时间间隔的类似行为?

这就是它的外观此刻正在使用的分类轴,如:

chart

我想目前只是一个刻度每天,正确的格式。

非常感谢您的帮助。

回答

4

您可以通过使用一个分组类别轴做到这一点,它会使用像这样的代码涉及您的日期有点操纵到一个单独的部分时间和日期部分:

// Create a d3 parser for this particular date format, this may not be relevant 
// if you already have actual dates 
var inFormat = d3.time.format("%Y-%m-%d %H:%M"); 

// Create a d3 parser for the day and time formats we are going to use 
var dayFormat = d3.time.format("%d %b"), 
    timeFormat = d3.time.format("%H:%M"); 

// Add some code to manipulate the date into 2 separate fields 
// because this is a category axis you need to handle formatting here 
// category axes use text content 
data.forEach(function (d) { 

    // Convert the date to an actual date, you may not need to do this if 
    // you already have date objects in your data 
    var inDate = inFormat.parse(d.Date); 

    // Add a new field for the time portion 
    d["Day"] = dayFormat(inDate); 
    d["Time"] = timeFormat(inDate); 

}, this); 

这是在上下文中做我认为你想要的:

http://jsfiddle.net/87GHM/1/

+0

谢谢你的回答。看起来很有希望。但是,当我更改数据时,id在您的示例中的工作方式不同。这是你的代码与我的数据:http://jsfiddle.net/ZU6jR/正如你可以看到之间的差距是存在的。 – Damian

+0

我发现如果日期之间的差距总是相等,并且每天的开始和结束时间都是相同的,那么图表会被正确绘制。 – Damian

+0

是的,您输入的数据不是5分钟的时间间隔。在这种方法中,时间被用作字符串类别,因此他们必须每天都有相同的值。对待它们的唯一方法就是使用timeAxis,但这不会让你减少夜晚。 –