【问题标题】:Show numbers as times in xAxis of linechart in dc.js在 dc.js 的折线图的 xAxis 中将数字显示为时间
【发布时间】:2015-12-21 01:36:58
【问题描述】:

我有一个包含列 (arr_time_) 的 csv 文件,该列表示该特定交易发生的 15 分钟时间段。

此列中的值范围为0 to 28,每个代表从早上 6 点开始的 15 分钟时间段。所以0 表示6 am,1 表示6:15 am,等等。这就是我的折线图现在的样子:

这个折线图的 dc.js 代码是这样的:

timeChart
    .width(800)
    .height(230)
    .margins({top: 10, right: 50, bottom: 30, left: 30})
    .dimension(arrivalTimeDim)
    .group(arrivalPer15min, "Observed")
    // .ordinalColors(["orange"])
    // .stack(histArrivalPer15min, "historical")
    .transitionDuration(500)
    .renderArea(true)
    // .elasticX(true)
    .elasticY(true)
    .x(d3.scale.linear().domain([minDate, maxDate+.5]))//minDate=0, maxDate=28
    .renderHorizontalGridLines(true)
    // .legend(dc.legend().x(60).y(10).itemHeight(13).gap(5))
    .xAxisLabel("Time")
    .yAxis().ticks(4);
    // .xAxis().ticks(5);

我想更改此图表中的 xAXis,使其显示 6, 6:15, ...。我尝试过使用.rangexUnits,但可能是因为我做错了,它没有用。我该如何解决这个问题?

【问题讨论】:

  • 也许您可以将 d3.time.scale 用于您的 x 轴?
  • 我已经完成了这个 .x(d3.time.scale().domain([minDate, maxDate+.5])) .xUnits(d3.time.hour)

标签: d3.js dc.js


【解决方案1】:

为此,您需要在数据集中创建日期:

  var today = new Date(2015, 12, 25);//sample date for which the data is a part i am considering it to to be 25 Dec 2015 
  var hrs = 6;//since you want to start from 6 in the morning
  experiments.forEach(function(x) {
    x.Speed = +x.Speed;
    //making the Date data
    x.Date = new Date(today.getTime()+ hrs*60*60*1000)
    hrs +=0.25;//after 15 minutes data
  });

//this will calculate the max min for the date
var extent = d3.extent(experiments, function(d){return d.Date});

最后设置轴如下:

  chart
    .width(768)
    .height(480)
    .x(d3.time.scale().domain(extent))
    .xUnits(d3.time.minute)

工作代码示例here

【讨论】:

  • 最后一件事,你知道如何不显示“AM”或“PM”部分吗?它造成了太多的混乱,而且不可读
  • 执行此操作chart.xAxis().tickFormat(d3.time.format("%H:%M")); 更新了小提琴以获取更多格式信息阅读此bl.ocks.org/zanarmstrong/raw/ca0adb7e426c12c06a95
猜你喜欢
  • 1970-01-01
  • 2015-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多