【问题标题】:unable to hide the data as expected based on the selection无法根据选择隐藏数据
【发布时间】:2017-08-04 16:19:14
【问题描述】:

当用户单击隐藏笔记本电脑堆叠条单选按钮时,应隐藏笔记本电脑(蓝色)显示的堆叠条,单击所有单选按钮时应显示所有条。但是目前,当我单击隐藏笔记本电脑堆叠条时,未显示运输数据,甚至在单击隐藏笔记本电脑单选按钮后的所有单选按钮时,运输的图例名称也显示错误。 请找demohere

js代码:

angular.module('myApp', ['googlechart'])
  .controller('myController', function($scope) {
    var chart1 = {};
    var variableCol = {
       id: "laptop-id",
        label: "Laptop",
        type: "number"
       };
    chart1.type = "ColumnChart";
    chart1.displayed = false;
     var valueSelected;
                $scope.newValue = function(value) {
                    console.log(value);
                    console.log(chart1.data.cols.length);
                    valueSelected = value;
                    if(value == 'few' && chart1.data.cols.length == 5) {
                      alert("Laptop data should not be shown" );
                      chart1.data.cols.pop();  
                    } else {
                      chart1.data.cols.push(variableCol);
                    }

                }
                //if the ALL radio button is selected all the stacked bars should be shown
                //if SDL radio button is selected, show only server,desktop,laptop but onmouse over show the shipping details tooo
    chart1.data = {
      "cols": [{
        id: "month",
        label: "Month",
        type: "string"
      },variableCol,
      {
        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",
      "isStacked": "true",
      focusTarget: 'category',
      "fill": 20,
      "displayExactValues": true,
      colors: ['blue', 'green', 'pink', 'brown'],
      "vAxis": {
        "title": "Sales unit",
        "gridlines": {
          "count": 10
        }
      },
      "hAxis": {
        "title": "Date"
      }
    };
    $scope.myChart = chart1;
  }).value('googleChartApiConfig', {
    version: '1.1',
    optionalSettings: {
      packages: ['bar'],
      language: 'en'
    }
  });

【问题讨论】:

    标签: javascript html angularjs


    【解决方案1】:

    您正在调用array.pop,它会从数组中删除最后一个元素。笔记本电脑元素位于位置 1。这是一个关于如何搜索 variableCol 索引,然后拼接删除该元素的数组的 sn-p。这是最安全的方法,因为您将确保找到该特定列。

    到目前为止,这些解决方案并未解决数据问题

    不考虑颜色

    if(value == 'few' && chart1.data.cols.length == 5) {
      //alert("Laptop data should not be shown" );
      var idx = chart1.data.cols.indexOf(variableCol);
      chart1.data.cols.splice(idx, 1);
      console.log("var col at " + idx);
    } 
    

    Here's a working plnkr

    笔记本电脑保持蓝色

    This version 将使笔记本电脑保持蓝色(但位置确实发生了变化)。

     if (value == 'few' && chart1.data.cols.length == 5) {
      //alert("Laptop data should not be shown" );
    
      var colIdx = chart1.data.cols.indexOf(variableCol);
      chart1.data.cols.splice(colIdx, 1);
    
      var colorIdx = chart1.options.colors.indexOf("blue");
      chart1.options.colors.splice(colorIdx, 1);
    
    } else {
      chart1.data.cols.push(variableCol);
      chart1.options.colors.push("blue");
    }
    

    【讨论】:

    • 谢谢布赖恩,它仍然没有删除第一个元素(笔记本电脑)。即使你分享的 plnkr 也没有按预期工作。@布赖恩
    • 刚刚在干净的窗口中再次打开它,它正在工作。也许你的问题不清楚?我的工作感觉是反复来回更改单选按钮会在图表中添加/删除笔记本电脑数据。
    • 堆叠栏中的蓝色代表笔记本电脑数据,当单击不显示笔记本电脑数据按钮时,它只是删除运输堆叠颜色,如果您注意到蓝色映射到桌面鼠标现在..我认为有些事情搞砸了非常糟糕。它不起作用。请比较鼠标悬停的工具提示和导航按钮时的图例,你可以知道区别..
    • 我尝试修复颜色,但不得不暂时远离它。蓝色确实存在,但价值观也是一个问题。必须遵循相同的模式来找到正确的索引。您基本上必须遍历月份数据,提取笔记本电脑数据,然后将其推回。我不知道图表,也许有人知道更简单的方法来操作它们,这似乎代码太多了。
    • 快速搜索给了我这个:angular-google-chart.github.io/angular-google-chart/docs/latest/…。你为什么要重新发明?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多