Angular-nvd3 issue, failed to update chart when set data to newData

回眸只為那壹抹淺笑 提交于 2019-12-02 04:38:21

I believe this is a quirk of how nvd3 is using d3 data-binding.

The initial data array is bound to the DOM like a standard d3 visualization and holds a reference to it. When you modify the array in the directive, it triggers a redraw and since it's the same array (reference to it), the bound data is changed and the plot redraws normally.

When you change the array after the first draw, under the hood, the array that still data-binded is the initial array (the one with no data). The directive still triggers a re-draw but with no data.

To do what you want, instead of calling $scope.$apply(); get api into the $scope with:

<nvd3 options="options" data="data" api="api" config="{refreshDataOnly: true}"> </nvd3>

and then use:

$scope.api.updateWithData($scope.data);

Updated example.

Since the version 1.0.2 you might also want to set deepWatchData as true. There was a commit on 15/09/2015 and now this option by default is false which means that Angular's watch expression will not track changes in array with data.

No need in doing scope.api.refresh() with this settings. No need in rewriting of the whole array of data, with new portion of data you just add/push it to array. This will help to prevent flashing when reassigning new data.

Now instead of: <nvd3 options="options" data="data" class="with-3d-shadow with-transitions" config="{refreshDataOnly: true}"></nvd3>

should be: <nvd3 options="options" data="data" class="with-3d-shadow with-transitions" config="{refreshDataOnly: true, deepWatchData: true}"></nvd3>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!