以角度nvd3折线图显示当前时间

Show current time in angular nvd3 line chart

本文关键字:时间 显示 折线图 nvd3      更新时间:2023-09-26

我想将当前时间的垂直"Now"标记添加到使用angular-nvd3-directives实现的工作时间序列折线图中。有点像这样:https://i.stack.imgur.com/X95T2.jpg

以前,我使用纯d3.js构建了这个图表,并使其发挥了作用。因此,我试图将我现有的解决方案移植到这样的指令中:

myApp.directive( 'nowMarker', function () {
  return {
      restrict: 'A',
      link: function (scope, element, attr) {
          var line = d3.svg.line()
            .interpolate("basis")
            // ####### this cannot work #######
            .x(function (d) { return x(d.t); })
            .y(function (d) { return y(d.value); });
            // ################################
          element.children()[0].append('svg:path')
            .attr("class", "NOW")
            .attr('d', line(scope.nowData))
            .attr('stroke', '#14022B')
            .attr('stroke-width', '1px')
            .attr('stroke-dasharray', ('5,5'))
            .attr('fill', 'none');
        }
    };
});

这就是我被卡住的地方。我不知道如何访问angularjs-nvd3-直线图的x和y刻度。

HTML是:

<nvd3-line-chart nowMarker ...>
</nvd3-line-chart>

在我的图表控制器中,我有:

var now = new Date();
$scope.nowData = [
        {name: "NOW", t: now, value: 0}, 
        {name: "NOW", t: now, value: 100}
    ];

没有错误消息,只是什么都没发生。

非常感谢您的帮助。谢谢Bennet

如果其他人发现这很有用:

最后,我在图表数据中添加了一个"NOW"系列,并为其指定了特定的颜色。

var tNow = new Date();
var sNow = {
    key: "NOW",
    values: [
        [tNow, 0],
        [tNow, max]
    ],
    color: '#FFFFFF'
};