【问题标题】:Google chart: increase margin between x axis labels and x-axis谷歌图表:增加 x 轴标签和 x 轴之间的边距
【发布时间】:2019-06-17 20:37:02
【问题描述】:

我正在使用 angular-google-chart (https://github.com/FERNman/angular-google-charts) 一个 google-chart 的角度包装器来显示柱形图。我想增加 x 轴标签和 x 轴之间的边距。 在下图中,红色部分表示我要增加差距的地方

按照文档,我添加了以下代码:

options: {
      ...

      hAxis: {
        textStyle: {
          fontSize: 10,
          fontStyle: "Arial",
          marginTop: '10',
          color: '#808080'
        },
   ...

颜色、字体大小和字体样式正常,但无法获取边距。有任何想法吗?提前致谢。

【问题讨论】:

    标签: javascript angular google-visualization


    【解决方案1】:

    使用chartArea.bottom增加x轴空间

    options: {
          ...
          chartArea: {
            bottom: 60
          },
    
          hAxis: {
            textStyle: {
              fontSize: 10,
              fontStyle: "Arial",
              marginTop: '10',
              color: '#808080'
            },
       ...  
    

    编辑

    虽然你可以使用bottom来增加x轴的高度,
    标签仍然与 x 轴的顶部对齐。

    但我们可以在图表的'ready' 事件中手动将它们向下移动,
    通过增加'y' 属性,
    请参阅以下工作 sn-p...

    google.charts.load('current', {
      packages: ['controls', 'corechart']
    }).then(function () {
      var dataTable = new google.visualization.DataTable();
      dataTable.addColumn('timeofday', 'Time of Day');
      dataTable.addColumn('number', 'Motivation Level');
      dataTable.addRows([
        [[8, 0, 0], 46],
        [[9, 0, 0], 46],
        [[10, 0, 0], 34],
        [[11, 0, 0], 4],
        [[12, 0, 0], 5],
        [[13, 0, 0], 6],
        [[14, 0, 0], 7],
        [[15, 0, 0], 8],
        [[16, 0, 0], 9],
        [[17, 0, 0], 10],
      ]);
    
      var options = {
        chartArea: {
          height: '100%',
          width: '100%',
          top: 24,
          left: 60,
          right: 16,
          bottom: 100
        },
        height: '100%',
        width: '100%',
    
        hAxis: {
          textStyle: {
            fontSize: 10,
            fontStyle: "Arial",
            marginTop: '10',
            color: '#808080'
          }
        }
      };
    
      var container = document.getElementById('chart_div');
      var chart = new google.visualization.ColumnChart(container);
      google.visualization.events.addListener(chart, 'ready', function () {
        var labels = container.getElementsByTagName('text');
        Array.prototype.forEach.call(labels, function(label) {
          if (label.getAttribute('text-anchor') === 'middle') {
            label.setAttribute('y', parseFloat(label.getAttribute('y')) + 20);
          }
        });
      });
      chart.draw(dataTable, options);
    });
    <script src="https://www.gstatic.com/charts/loader.js"></script>
    <div id="chart_div"></div>

    注意:bottom 是在 release 43, on Oct. 2, 2015 期间添加的

    【讨论】:

    • 感谢您的回复...设置“底部:60”不起作用...在我的尝试中,如果我将底部属性设置为小于 25,则 X 轴标签消失.. .所以我猜底部属性设置X轴标签的高度
    • 是的,这是唯一可以增加空间的选项,60 只是一个例子……
    • 我的意思是设置底部属性小于 25 使 x-labels 消失...设置更多(在任何值)只是在常规位置显示 X-labels...所以我想它表示 X-labels 的高度。奇怪的是,官方文档中没有 chartArea.bottom 属性
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-02
    • 2014-04-22
    • 1970-01-01
    相关资源
    最近更新 更多