将数据重置回原始数据

时间:2017-08-07 21:01:27

标签: javascript html angularjs google-visualization

我正在研究angularjs和googlecharts。当用户点击图例时,图表上的数据被隐藏,当用户选择所有图例时,所有数据都被隐藏,我有一个清除按钮,点击该按钮时应该返回数据。现在我能够只获得最后隐藏的传奇价值。如果隐藏了多个图例数据,如何迭代并获取数据。有什么建议吗?

工作演示:https://plnkr.co/edit/kpgjpVA4Yp1KvNV7XkeZ?p=preview

当用户点击图例时,数据被隐藏。我想通过点击清除按钮获取显示数据,我想限制用户不要选择所有图例来隐藏数据,至少一行(数据)应该显示。如果用户选择3个图例并隐藏数据并选择第4个图例图标来隐藏数据,则应显示一条警告消息,说明"所有数据都可以隐藏"。

js code:

angular.module('myApp', ['googlechart'])
  .controller('myController', function($scope) {
    var chart1 = {};
    chart1.type = "LineChart";
    chart1.displayed = false;
    chart1.data = {
      "cols": [{
        id: "month",
        label: "Month",
        type: "string"
      }, {
        id: "laptop-id",
        label: "Laptop",
        type: "number"
      }, {
        id: "desktop-id",
        label: "Desktop",
        type: "number"
      }, {
        id: "server-id",
        label: "Server",
        type: "number"
      }, {
        id: "cost-id",
        label: "Shipping",
        type: "number"
      }],
      "rows": [{
        c: [{
          v: "January"
        }, {
          v: 19,
          f: "42 items"
        }, {
          v: 12,
          f: "Ony 12 items"
        }, {
          v: 7,
          f: "7 servers"
        }, {
          v: 4
        }]
      }, {
        c: [{
          v: "February"
        }, {
          v: 13
        }, {
          v: 1,
          f: "1 unit (Out of stock this month)"
        }, {
          v: 12
        }, {
          v: 2
        }]
      }, {
        c: [{
            v: "March"
          }, {
            v: 24
          }, {
            v: 5
          }, {
            v: 11
          }, {
            v: 6
          }

        ]
      }]
    }; 
    chart1.options = {
      "title": "Sales per month",
      "colors": ['#0000FF', '#009900', '#CC0000', '#DD9900'],
      "defaultColors": ['#0000FF', '#009900', '#CC0000', '#DD9900'],
      "isStacked": "true",
      "fill": 20,
      "displayExactValues": true,
      "vAxis": {
        "title": "Sales unit",
        "gridlines": {
          "count": 10
        }
      },
      "hAxis": {
        "title": "Date" 
      }
    }; 
    chart1.view = {
      columns: [0, 1, 2, 3, 4]
    };
    $scope.myChart = chart1;

    $scope.seriesSelected = function(selectedItem) {
      console.log(selectedItem);
      var col = selectedItem.column;
       //If there's no row value, user clicked the legend.
      if (selectedItem.row === null) {
        //If true, the chart series is currently displayed normally.  Hide it.
        console.log($scope.myChart.view.columns[col]);

        $scope.reset = function() {
         $scope.myChart.view.columns[col] = col;
          console.log($scope.myChart.view.columns[col]);
         $scope.myChart.options.colors[col - 1] = $scope.myChart.options.defaultColors[col - 1];
}

        if ($scope.myChart.view.columns[col] == col) {
          //Replace the integer value with this object initializer.
          $scope.myChart.view.columns[col] = {
            //Take the label value and type from the existing column.
            label: $scope.myChart.data.cols[col].label,
            type: $scope.myChart.data.cols[col].type,
            //makes the new column a calculated column based on a function that returns null, 
            //effectively hiding the series.
            calc: function() {
              return null;
            }
          };
          //Change the series color to grey to indicate that it is hidden.
          //Uses color[col-1] instead of colors[col] because the domain column (in my case the date values)
          //does not need a color value.
          $scope.myChart.options.colors[col - 1] = '#CCCCCC';
        }
        //series is currently hidden, bring it back.
        else {
          console.log("Ran this.");
          //Simply reassigning the integer column index value removes the calculated column.
          $scope.myChart.view.columns[col] = col;
          console.log($scope.myChart.view.columns[col]);
          //I had the original colors already backed up in another array.  If you want to do this in a more
          //dynamic way (say if the user could change colors for example), then you'd need to have them backed
          //up when you switch to grey.
          $scope.myChart.options.colors[col - 1] = $scope.myChart.options.defaultColors[col - 1];
        }
      }
       $scope.serverId = chart1.data.rows[selectedItem.row].c[3].v;
       alert("serverid : " + $scope.serverId);


    };
  });

HTML:

<div ng-controller="myController">
    <div google-chart chart="myChart" on-select="seriesSelected(selectedItem)"></div>
    <button class="btn btn-primary btn-large center" 
        type="reset" ng-click="reset()">Clear
</button>

1 个答案:

答案 0 :(得分:0)

this怎么样?这是one with an alert

首先,设置一个数组来保存隐藏的列:var hidden = [];

然后,你的重置功能:

$scope.reset = function() {
  for (var i = 0; i < hidden.length; i++) {
    var hiddenCol = hidden[i];
    $scope.myChart.view.columns[hiddenCol] = hiddenCol;
    $scope.myChart.options.colors[hiddenCol -1] = $scope.myChart.options.defaultColors[hiddenCol - 1];
  }

  //$scope.myChart.view.columns[col] = col;
  //console.log($scope.myChart.view.columns[col]);

}