如何在使用angularjs更新控制器中的新数据之前销毁Morris图表数据?

时间:2017-10-12 12:22:04

标签: angularjs morris.js

使用angularjs基于所选项目加载图表。如果我选择第一项我在控制器功能中获取详细信息正在显示它。当我选择下一项时,图形加载但首先选择项目详细信息不会破坏。如果我选​​择4项,则显示4次图表。如何清除莫里斯图表中的数据?

<!-- begin snippet: js hide: false console: true babel: false -->

     app.controller('MorrisCtrl', ['$rootScope', '$scope', '$state','loadhiscpuservice',  function ($rootScope, $scope, $state,loadhiscpuservice) {
   getgraphdetails();
$scope.getserverdetails = function (server) {
       // $state.go($state.current);
        $scope.serveridvalue = [];
        var serverid = [];
        $scope.serveridvalue = server;
        serverid = $scope.serveridvalue.serverID;

        var date = new Date();
        $scope.formattedDate = $filter('date')(date, "dd/MM/yyyy hh:mm:ss a", 'UTC');
        var date1 = $scope.formattedDate; 
        //chart1.destroy();      
        getgraphdetails(serverid, date1);      
    };
    function getgraphdetails(serverid, date1) {
        var details = { serverID: serverid, minutes: "180", date: "date1" };
        loadhiscpuservice.getCPUvalues(details).
              success(function (results) {                
                  alert(results);
                  var value = JSON.parse(results);
                  drawMorrisCharts1(results);
              })
              .error(function (error) {
                  $scope.result = 'Error Occured : ' + error;
              });
    };
     function drawMorrisCharts1(results2) {
            var points = [];
            points = results2;
            Morris.Area({
                element: 'area-chart2',
                data: points,
                xkey: 'lastUpdatedDateTime',
                ykeys: ['value'],
                labels: ['value'],
                parseTime: false,
                pointSize: 2,
                hideHover: 'auto',
                lineColors: [$rootScope.settings.color.themesecondary]
            });
        };
        })

    app.factory('loadhiscpuservice', function ($http) {
        return {
            //load d0ta service    
            getCPUvalues: function (details) {
                var request = $http({
                    method: 'POST',
                    url: 'http//abc link',              
                    data: { aaa: details.aaa, minutes: details.minutes, date: details.date }
                });
                return request;
            }
        };
    });
<!-- language: lang-css --> 'lib/jquery/charts/morris/raphael-2.0.2.min.js', 'lib/jquery/charts/morris/morris.js', 'app/controllers/morris.js' <!-- language: lang-html -->
    <div ng-controller="MorrisCtrl">
    <div class="orders-container">
            <div class="orders-header">
                <h6>Servers List</h6>
            </div>
            <ul ui-sref-active="active" class="orders-list" onscroll="true">
                <li class="order-item">
                    <div class="row">
                        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 item-left">
                            <div class="item-price">Servers: </div>
                        </div>
                        <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 item-right">
                            <div class="input-group">
                                <select ng-options="server.serverName for server in serverDetails1" class="form-control" ng-model="server" ng-change="getserverdetails(server)">
                                    <option value="">--Select Server--</option>
                                </select>
                                <span class="input-group-addon">
                                    <i class="fa fa-desktop"></i>
                                </span>
                            </div>
                        </div>
                    </div>
                </li>
            </ul>
        </div>
        <div class="row">
            <div class="col-xs-12 col-md-12 col-lg-12">
            <div id="area-chart2" class="chart chart-lg"></div>
          </div>
         </div>

2 个答案:

答案 0 :(得分:10)

我面临类似的问题..这就是我解决它的方式。 在绘制新图表之前,请使用.empty清除div。

$("#area-chart2").empty();
这就是全部。就像魔法一样。

希望这能解决您的问题

答案 1 :(得分:0)

只传递一个空的数据数组

drawMorrisCharts1([])