javascript - d3v4 时间线在底部添加日期轴?

标签 javascript d3.js

我重构了 d3v4 的时间线图表。我不确定如何将日期添加到底部以指示时间线。

http://jsfiddle.net/0ht35rpb/179/

//scales
var x =  d3.scaleTime()
        .range([0, w])              
        .domain([timeBegin, timeEnd]);

它基于这个旧的时间线代码 - 但同样没有标记轴。 http://bl.ocks.org/bunkat/2338034

此示例调用轴..

https://github.com/jiahuang/d3-timeline

   var axis = g.append("g")
        .attr("class", "axis")
        .attr("transform", "translate(" + 0 + "," + yPosition + ")")
        .call(xAxis);
    };

这是一个合理的解决方案吗?

  g.append("g")
    .attr("class", "axis")
    .attr("transform", "translate(" + 0 + "," + (margin.top + (itemHeight + itemMargin) * maxStack) + ")")
    .attr(timeAxisTickFormat.stroke, timeAxisTickFormat.spacing)
    .call(xAxis.tickFormat("").tickSize(-(margin.top + (itemHeight + itemMargin) * (maxStack - 1) + 3), 0, 0));

最佳答案

在D3中,我们通常将axis称为指代轴生成器的变量,而不是指包含它的组。除此之外,这确实是调用轴的最常见方法:

var gX = chart.append("g")
    .attr("class", "axis")
    .attr("transform", "translate(" + 140 + "," + 450 + ")")
    .call(d3.axisBottom(x));

这是您更新的 fiddle :http://jsfiddle.net/5yqkz1z5/

PS:相应地更改翻译函数中的魔数(Magic Number)。

关于javascript - d3v4 时间线在底部添加日期轴?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46348318/

相关文章:

javascript - 使用 Three.js 从本地机器加载 OBJ 模型?

javascript - 如何将 html 表格转换为 csv 字符串

javascript - d3 将刻度日期变换到 x 位置?

javascript - 如何获取d3.js中json文件中具有特定值的总行数

javascript - 如何在 D3.js 中创建 CSV 条目的唯一列表?

javascript - 如何在 JavaScript 中使用 CSS

Javascript 检查数组中的现有对象

javascript - 打开抽屉时在 React Native 中隐藏 StatusBar

javascript - 我无法获得以下代码的输出

javascript - 如何使d3 js TreeView 垂直自下而上