2017-08-16 83 views
0

我正在处理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]; 
     } 
     } 
    }; 
    }); 

我试图通过添加下面的代码在我的js代码上面chart1.options,但它并没有奏效:

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

不能通过设置宽度来使用css来做同样的事情吗? –

+0

我已经使用过,但是当酒吧的数量更多时,它会减少酒吧的宽度。我想保持现在显示的酒吧的大小,所以我认为我们需要以不同的方式处理它。 – user8373379

+0

你想要这个吗? https://plnkr.co/edit/GKhoyoC4zbrAzvfSa55U?p=preview – Baptiste

回答

0

在你的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样式,请reffer说。

更新

,你必须根据自己requirement.The上述结构设置图表属性

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

具有根据你的need.You可以看到一个例子here即要被操纵。你正在接受的链接。请找到相同https://plnkr.co/edit/o8iccmrB13NdDAKCxb7Z?p=preview的普拉克(如果你不能观看,请在版本部分中看到)

更新 如果您使用的异步调用数据,那么,里面

MyDataService.getChartDataFromService().then(
      function (response) { 
//other codes 
.......//set options here 

})) 
+0

感谢您的帮助,我在使用代码时注意到的事情,即使酒吧很少或没有酒吧,也会显示垂直和水平滚动条。我不想显示垂直滚动条,但使用属性style =“height:500px; overflow-x:auto”它显示垂直和水平滚动条。我们禁用垂直滚动条并仅在数据更多时才动态显示水平滚动条显示? – user8373379

+0

overflow-x:auto“表示仅当内容溢出时滚动条才会出现。您可以使用overflow-y:hidden来隐藏垂直滚动条 –

+0

请参阅我的更新回答。你必须配置图表,并使条宽保持不变,否则图表会自动响应并自动更改条形的宽度。请参阅示例 –

0

设置选项在你的div标签你需要设置滚动溢出。

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

我在演示中进行了更改。