【问题标题】:How to update D3 chart when data is updated数据更新时如何更新 D3 图表
【发布时间】:2017-06-19 08:28:12
【问题描述】:

我正在使用 D3 图表在屏幕上显示数据。数据从 WebApi 更新。数据更新后,我想刷新图表中的数据。

这里是代码

 $scope.config = {
        title: 'Products',
        tooltips: true,
        labels: false,
        mouseover: function () { },
        mouseout: function () { },
        click: function () { },
        legend: {
            display: true,
            //could be 'left, right'
            position: 'right'
        }
    };


$http.put(WebApiURL.GetWebApiURL() + 'api/ReportPerformance', data)
     .then(function (response) {
         $scope.PerformanceData = response.data;
         $scope.LoadingData = "";
         $scope.chartdata = [];
         var tempchartdata = [];
         angular.forEach($scope.PerformanceData, function (value) {
             tempchartdata.push(value.ReportTimeInSeconds);
         });
         $scope.chartdata = tempchartdata;
     }), function errorCallback(x, y, z) {
         $scope.LoadingData = "Error while generating report:= " + x.ExceptionMessage;
     };
    }

此网络电话正在更新数据$scope.chartdata

html中的图表定义...

<div data-ac-chart="'bar'"
     data-ac-data="chartdata"
     data-ac-config="chartConfig"
     style="height: 500px; width: 500px;"
     class="chart">
</div>

但是当数据来自 webApi 时,它正在更新 $scope.chartdata 但它没有反映在屏幕上。

添加了 plunker...http://plnkr.co/edit/a0mVRL0Jo9ah8Om8ONTc

【问题讨论】:

  • 你能提供小提琴吗?
  • 您需要将绘制/更新图表的代码放在$http.put 回调中,或者添加$scope.$watch 并将其放在那里。你能展示实际制作图表的代码吗?
  • 这是绘制图表的实际代码。你能给我举一些例子来提供你的建议吗?
  • “这是绘制图表的实际代码”...你在说什么?该代码中没有一行 D3。
  • 我就放弃了……

标签: javascript angularjs d3.js


【解决方案1】:

尝试应用方法,我认为它现在已更新,因为 $digest 在您的代码中不起作用。把你的代码包装起来

$scope.$apply(function () {
            //your code
        });




 $scope.$apply(function () {
            $scope.dataAcData
        });

go through http://jimhoskins.com/2012/12/17/angularjs-and-apply.html link, Hope it will help you

【讨论】:

  • 更多详情请访问
【解决方案2】:

您应该注意指令中的$scope.dataAcData 并更新

中的 D3
scope.$watch('dataAcData',function(){
  //update your d3 here
})

【讨论】:

  • 我没有为图表创建任何指令。我只是直接使用我的问题中提到的 D3 提供的任何内容
  • 它应该有一个指令,或者你将你的 $scope.chartdata 绑定到哪里?你的 d3 代码在哪里?
  • 你确定你没有使用angular-charts吗?因为我认为这些data-ac-data,data-ac-config 用于angular-charts 指令。关于你的 plunker,我没有看到任何关于 d3 的代码,它没有在你的网络上绘制任何东西,你一定错过了什么
猜你喜欢
  • 2020-11-24
  • 1970-01-01
  • 1970-01-01
  • 2012-01-22
  • 2018-08-03
  • 1970-01-01
  • 2016-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多