【问题标题】:unable to add horizontal scrollbar to my chart无法向我的图表添加水平滚动条
【发布时间】:2017-08-19 03:55:26
【问题描述】:

我正在研究 angularjs 谷歌图表。我正在使用 angularjs google 柱形图以列的形式显示数据,问题是当我有更多数据来显示条形宽度正在减小时。如果要显示更多数据,如示例here 所示,我想显示带有水平滚动条的图表。 我尝试实现相同但无法显示滚动条,任何建议都会有所帮助。

我的示例演示here

js代码:

angular.module('myApp', ['googlechart'])
  .controller('myController', function($scope) {
    var chart1 = {};
    chart1.type = "ColumnChart";
    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
          }

        ]
      }, {
        c: [{
            v: "April"
          }, {
            v: 24
          }, {
            v: 5
          }, {
            v: 11
          }, {
            v: 6
          }

        ]
      }, {
        c: [{
            v: "May"
          }, {
            v: 18
          }, {
            v:11
          }, {
            v: 7
          }, {
            v:2
          }

        ]
      }, {
        c: [{
            v: "June"
          }, {
            v: 21
          }, {
            v: 5
          }, {
            v: 8
          }, {
            v: 6
          }

        ]
      }, {
        c: [{
            v: "July"
          }, {
            v: 24
          }, {
            v: 5
          }, {
            v: 9
          }, {
            v: 9
          }

        ]
      }, {
        c: [{
            v: "August"
          }, {
            v: 14
          }, {
            v: 1
          }, {
            v: 11
          }, {
            v: 5
          }

        ]
      }, {
        c: [{
            v: "September"
          }, {
            v: 4
          }, {
            v: 2
          }, {
            v: 51
          }, {
            v: 6
          }

        ]
      }, {
        c: [{
            v: "October"
          }, {
            v: 34
          }, {
            v: 4
          }, {
            v: 0
          }, {
            v: 1
          }

        ]
      }]
    };
    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]);
        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];
        }
      }
    };
  });

我尝试在chart1.options 上面的 js 代码中添加以下代码,但没有成功:

width: chart1.data.getNumberOfRows() * 130,
bar: {groupWidth: 90},

【问题讨论】:

  • 你不能通过设置宽度来使用 css 来做同样的事情吗?
  • 我使用过,但是当条数更多时,它会减小条形宽度。我想保持与现在显示的条形大小相同,所以我认为我们需要以不同的方式处理它。跨度>

标签: javascript html angularjs


【解决方案1】:

在您的 html 中,您可以像这样设置宽度并设置溢出时要执行的操作

<div ng-controller="myController">
    <div google-chart chart="myChart" style="width:400px;height:500px;overflow-x:scroll"></div>
    <br><br>
     <div google-chart chart="myChart"></div>
  </div>

或者你也可以这样做

<div ng-controller="myController">
<div google-chart chart="myChart" style="height:500px;overflow-x:auto"></div>
<br><br>
 <div google-chart chart="myChart"></div>

在您附加的小提琴中,他们使用了 css 样式,请参考。

更新

您必须将图表属性设置为

          "width": chart1.data.rows.length *65,
          "bar": {groupWidth: 20},

根据您的要求。以上配置必须根据您的需要进行操作。您可以看到一个示例here 即。您引用的链接。请找到相同https://plnkr.co/edit/o8iccmrB13NdDAKCxb7Z?p=preview的plunk(如果您无法查看,请在版本部分查看)

更新 如果您正在使用来自异步调用的数据,请在

中设置选项
MyDataService.getChartDataFromService().then(
            function (response) {
//other codes
.......//set options here

}))

【讨论】:

  • 感谢您的帮助,我在使用您的代码时注意到的事情,即使栏很少或没有,也会显示垂直和水平滚动条。我不想显示垂直滚动条,但使用属性 style="height:500px;overflow-x:auto" 它正在显示垂直和水平滚动条。我们可以禁用垂直滚动条并仅在数据更多时动态显示水平滚动条显示?
  • overflow-x:auto" 表示只有内容溢出时才会出现滚动条。可以使用overflow-y:hidden来隐藏垂直滚动条
  • 请查看我更新的答案。您必须配置图表并使条形宽度保持不变,否则图表将响应并自动更改条形的宽度。请参阅您附加的小提琴例子
  • 嗯,这就是我无法使用您提到的添加图表属性的代码在上面的帖子中提到的显示的地方。如果您在我提到的同一件事上看到我的帖子。你能用你更新的代码更新plnkr.co/edit/sEszNxDtQDDOwaAUd5Sj?p=preview吗?
  • 我重新创建了 plunk。请找到我更新的答案。而不是宽度:chart1.data.getNumberOfRows() * 130,使用宽度:chart1.data.rows.length* 130,它会做技巧..不要忘记在html中指定,overflow-x:auto
【解决方案2】:

在您的 div 标签中,您需要为溢出设置滚动。

<div google-chart chart="myChart" style="overflow:scroll"></div>

我在您的演示中进行了更改。

【讨论】:

    猜你喜欢
    • 2011-07-28
    • 2020-11-23
    • 1970-01-01
    • 1970-01-01
    • 2019-02-20
    • 2016-10-20
    • 2018-10-20
    • 1970-01-01
    • 2017-09-11
    相关资源
    最近更新 更多