柱形图:如何在水平轴上显示所有标签

时间:2013-07-19 09:30:07

标签: google-visualization

我一直试图在我的图表的水平轴上显示所有标签,但我无法做到这一点!

我尝试使用 hAxis.showTextEvery = 1 但不起作用

(见https://developers.google.com/chart/interactive/docs/gallery/columnchart)。

enter image description here

基本上,我还想显示上图中目前缺少的数字“5”,“7”和“9”。

这里是JavaScript代码,非常感谢。

<script type="text/javascript">
 google.setOnLoadCallback(drawChart1);
 function drawChart1(){
     var data = new google.visualization.DataTable(
     {
        "cols":[
            {"id":"","label":"ratings","type":"number"},
            {"id":"","label":"# of movies","type":"number"}],
            "rows":[
                {"c":[{"v":9},{"v":26}]},
                {"c":[{"v":8},{"v":64}]},
                {"c":[{"v":10},{"v":5}]},
                {"c":[{"v":7},{"v":50}]},
                {"c":[{"v":6},{"v":38}]},
                {"c":[{"v":5},{"v":10}]},
                {"c":[{"v":2},{"v":1}]},
                {"c":[{"v":4},{"v":1}]}
            ]});

     var options = {
        "title":"Rating distribution",
        "vAxis":{"title":"# of movies","minValue":0},
        "hAxis":{"title":"Ratings","maxValue":10},"legend":"none","is3D":true,"width":800,"height":400,"colors":["red"]
     };
     var chart = new google.visualization.ColumnChart(document.getElementById('chart_movies_per_rating'));chart.draw(data, options);
 }
 </script> 

更新 这是我开发的解决方案,遵循以下答案(再次感谢!)。 http://jsfiddle.net/mdt86/x8dafm9u/104/

<script type="text/javascript">
google.setOnLoadCallback(drawChart1); 
function drawChart1(){ 
var data = new google.visualization.DataTable( 
     {"cols": 
        [{"id":"","label":"ratings","type":"string"},
        {"id":"","label":"# of movies","type":"number"}], 
             "rows":
             [{"c":[{"v":"0"},{"v":0}]},
             {"c":[{"v":" 1"},{"v":0}]},
             {"c":[{"v":" 2"},{"v":1}]},
             {"c":[{"v":" 3"},{"v":0}]},
             {"c":[{"v":" 4"},{"v":1}]},
             {"c":[{"v":" 5"},{"v":10}]},
             {"c":[{"v":" 6"},{"v":38}]},
             {"c":[{"v":" 7"},{"v":50}]},
             {"c":[{"v":" 8"},{"v":64}]},
             {"c":[{"v":" 9"},{"v":26}]},
             {"c":[{"v":" 10"},{"v":5}]}
             ]
        }
 ); 

 var options = 
    {"title":"Rating distribution",
    "vAxis":{"title":"# of movies","minValue":0},
    "hAxis":{"title":"Ratings","maxValue":10},
    "legend":"none",
    "is3D":true,
    "width":800,
    "height":400,
    "colors":["CC0000"]};
    var chart = new google.visualization.ColumnChart(document.getElementById('chart_movies_per_rating'));
    chart.draw(data, options);
 } 
    </script>

2 个答案:

答案 0 :(得分:11)

你的问题与ColumnChart中的连续与离散的细微差别有关。基本上,hAxis上的标签有连续值,showTextEvery仅适用于离散值。要解决这个问题,我会做以下事情:

  1. 将所有缺失的评级都插入图表中(即,如果评级为'3'时没有值,则插入零)。
  2. 在图表中订购评级。 (Google图表可以为您排序,但订购它们可能更容易。)
  3. 将评分更改为{"id":"","label":"ratings","type":"string"},
  4. 在选项
  5. 中使用showTextEvery:1

    下面是一些演示此内容的代码:

    var data = new google.visualization.DataTable(
    {
      "cols":[
        {"id":"","label":"ratings","type":"string"},
        {"id":"","label":"# of movies","type":"number"}],
      "rows":[
        {"c":[{"v":'10'},{"v":5}]},
        {"c":[{"v":'9'}, {"v":26}]},
        {"c":[{"v":'8'}, {"v":64}]},
        {"c":[{"v":'7'}, {"v":50}]},
        {"c":[{"v":'6'}, {"v":38}]},
        {"c":[{"v":'5'}, {"v":10}]},
        {"c":[{"v":'4'}, {"v":1}]},
        {"c":[{"v":'3'}, {"v":0}]},
        {"c":[{"v":'2'}, {"v":1}]},
        {"c":[{"v":'1'}, {"v":0}]},
      ]});
    
    var options = {
      "title":"Rating distribution",
      "vAxis":{"title":"# of movies","minValue":0},
      "hAxis":{"title":"Ratings",showTextEvery:1},
      "legend":"none",
      "width":800,"height":400,"colors":["red"]
    };
    

答案 1 :(得分:6)

除了Jeremy的解决方案,另一种方法是在hAxis上继续使用连续值,但是指定所需的网格线数量,这应该与所需的标签数量相同。如果你想要10个标签,1到10,这应该有效:

hAxis: { gridlines: { count: 10 } }