gpt4 book ai didi

google-visualization - 柱状图 : how to show all labels on horizontal axis

转载 作者:行者123 更新时间:2023-12-03 22:17:01 28 4
gpt4 key购买 nike

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

我尝试使用 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>

最佳答案

您的问题与 ColumnChart 中连续与离散的细微差别有关。 .基本上,您的 hAxis 上的标签具有连续值。 ,以及 showTextEvery仅适用于离散的。为了解决这个问题,我将执行以下操作:

  • 将所有缺失的评分插入图表中(即,如果评分“3”处没有值,则插入零)。
  • 对图表中的评级进行排序。 (Google 图表可以为您排序,但订购它们可能更容易。)
  • 将评级更改为 {"id":"","label":"ratings","type":"string"},
  • 在选项
  • 中使用 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"]
    };

    关于google-visualization - 柱状图 : how to show all labels on horizontal axis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17742651/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com