2017-08-16 28 views
0

私はangularjs google chartで作業しています。私はカラムの形でデータを表示するために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}, 
+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ためplunkを見つけてください、

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> 

私はあなたのデモを変更します。

関連する問題