ngClick导致D3图表重绘

Han*_*pio 5 d3.js angularjs nvd3.js angular-nvd3

每当我点击其中一个点时,它就会导致图表更新.只有在我ngClick<nvd3>元素有一个指令时才会发生这种情况(即使监听器函数什么都不做).如果我删除了ngClick,一切都很好.

我正在使用一个非常基本的nvd3散点图angular-nvd3.

什么可能导致这种奇怪的互动?

(此录制的帧率很低,因此很难看到,但每次点击都会重新绘制图表,包括顶部的图表)

单击图表上的点

这是一个再现问题的plunker:

http://plnkr.co/edit/F0ZslBaisoHWIp0VcI8o

谢谢!

更新我把它缩小到angular-nvd3内的手表.不知何故,ngClick的存在导致'数据'发生变化.此行触发刷新:https://github.com/krispo/angular-nvd3/blob/master/dist/angular-nvd3.js#L328 我的代码中没有任何内容可以更改"数据"(也验证了对象是在指令之外的同一个实例),而我在angular-nvd3.js中看不到任何可以改变数据的东西......嗯......

更新 这是我的点击事件监听器(它是空的):

controller.handleChartClick = function(event) {
};
Run Code Online (Sandbox Code Playgroud)

和HTML:

<div class="col-sm-10">
    <nvd3 ng-click="observationsCharts.handleChartClick($event)"
              options="observationsCharts.scatterPlotChartOptions"
              data="observationsCharts.scatterPlotChartData"></nvd3>
</div>
Run Code Online (Sandbox Code Playgroud)

Sta*_*rns 2

您是正确的,以下手表导致了该问题:

// Watching on data changing
 scope.$watch('data', function (newData, oldData) {
  if (newData !== oldData && scope.chart) {

    if (!scope._config.disabled && scope._config.autorefresh) {
        scope._config.refreshDataOnly && scope.chart.update ? scope.chart.update() : scope.api.refresh(); // if wanted to refresh data only, use chart.update method, otherwise use full refresh.
                            }
                        }
                    }, scope._config.deepWatchData);
Run Code Online (Sandbox Code Playgroud)

问题是,如果你在这个函数中添加一个日志来查看新旧数据是什么,数据实际上是在变化的。

当您单击其中一个点时,nvd3 正在向数据添加颜色属性。

旧数据:

[{"key":"Battery Voltage","values":[{"x":1439419440000,"y":90,"series":0},{"x":1439419440000,"y":43,"series":0},{"x":1439419440000,"y":345,"series":0},{"x":1439167620000,"y":73,"series":0},{"x":1439167620000,"y":42,"series":0},{"x":1439167620000,"y":36,"series":0},{"x":1440010740000,"y":32,"series":0},{"x":1439419440000,"y":62,"series":0},{"x":1439167620000,"y":73,"series":0},{"x":1439167620000,"y":42,"series":0},{"x":1439167620000,"y":36,"series":0}]}] 
Run Code Online (Sandbox Code Playgroud)

新数据:

[{"key":"Battery Voltage","values":[{"x":1439419440000,"y":90,"series":0,"color":"#1f77b4"},{"x":1439419440000,"y":43,"series":0,"color":"#1f77b4"},{"x":1439419440000,"y":345,"series":0},{"x":1439167620000,"y":73,"series":0},{"x":1439167620000,"y":42,"series":0},{"x":1439167620000,"y":36,"series":0},{"x":1440010740000,"y":32,"series":0},{"x":1439419440000,"y":62,"series":0,"color":"#1f77b4"},{"x":1439167620000,"y":73,"series":0},{"x":1439167620000,"y":42,"series":0},{"x":1439167620000,"y":36,"series":0}],"color":"#1f77b4","value":90}] 
Run Code Online (Sandbox Code Playgroud)

每次单击新点时,颜色属性都会添加到对象中,因此监视功能实际上运行正确。

我建议在https://github.com/krispo/angular-nvd3上打开一个问题

**编辑 - 只是为了澄清,添加 ng-click 时发生的原因是因为这会强制 Angular 摘要周期运行,从而触发手表。