javascript 谷歌图表 hAxis.gridlines.count 没有按预期工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25552824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 04:39:57  来源:igfitidea点击:

Google Chart hAxis.gridlines.count does not work as expected

javascriptgoogle-visualizationbar-chart

提问by Dominik G

I have a google Chart with percentage values from 0 to 100% (i.e. 0 to 1) When I set hAxis.gridlines.count to 10 expecting to get gridlines at 0, 10, 20, ... 90, 100I get gridlines at 0, 12, 24, 36, ..., 96, 108

我有一个百分比值从 0 到 100%(即 0 到 1)的谷歌图表当我将 hAxis.gridlines.count 设置为 10 期望0, 10, 20, ... 90, 1000, 12, 24, 36, ..., 96, 108

I could not find any solution by now.

我现在找不到任何解决方案。

Here is my Code:

这是我的代码:

function getData() {

    // Create the data table.
    var data = new google.visualization.arrayToDataTable([
      ['Category', 'Fulfillment (%)'],
      ['A', .75],
      ['B', .50],
      ['C', .50],
      ['D', .30],
      ['E', 0]
    ]);

    // Set chart options
    var options = {
        'title': 'My Title',
        'height': 300,
        'backgroundColor': 'none',
        'hAxis': {
            'maxValue': 1,
            format: '#%',
            gridlines: {
                count: 10
            }
        },
        'colors': ['#F5821E'],
        legend: { position: "none" }
    };

    var formatter = new google.visualization.NumberFormat({ pattern: '#%' });
    formatter.format(data, 1);

    // Instantiate and draw our chart, passing in some options.
    var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
    chart.draw(data, options);
}

回答by Nit

Reading the documentation, you can see that hAxis.gridlines.countspecifies how manylines to draw, not where to draw them. What you're looking for is probably hAxis.ticksinstead.

阅读文档,您可以看到hAxis.gridlines.count指定要绘制多少条线,而不是在哪里绘制它们。你正在寻找的可能是hAxis.ticks相反的。

回答by user3241682

Use count: 11and not 10. This will change the labels of the grid lines.

使用count: 11而不是10。这将更改网格线的标签。

回答by alexeevyci

I had the same problem (bug) and I realized that if you set a height for a chart (column chart) then number of horizontal grid will be 2. I you want to use propriety hAxis.gridlines.count than you should to remove height propriety.

我遇到了同样的问题(错误),我意识到如果您为图表(柱状图)设置高度,那么水平网格的数量将为 2。我想使用适当的 hAxis.gridlines.count 来删除高度礼。