带有双x轴(十进制)的谷歌条形图上的格式数字

时间:2017-10-12 16:43:08

标签: javascript charts google-visualization decimal material

如何使用双x轴格式化Google条形图上的数字值? 带有标签支持的顶轴应至少有四个小数位,如工具提示中显示的值。

我尝试的是this方法,但它似乎不起作用。

我的代码:

data.addColumn('string', 'RuleName');
        data.addColumn('number', 'Lift');
        data.addColumn('number', 'Support');


        for (var i = 0; i < chartsdata.length; i++) {
            data.addRow([rule, Lift,Support)]);
        }

        // format numbers in second column to 5 decimals
        var formatter = new google.visualization.NumberFormat({
            pattern: '#,##0.00000'
        }); // This does work, but only for the value in the tooltip.

        formatter.format(data, 2);

        // Passing in some options    
        var chart = new google.charts.Bar(document.getElementById('barChart'));
        var options = {

            title: "Association Rules by lift and support",
            bars: 'horizontal',
            series: {
                0: { axis: 'Lift', targetAxisIndex: 0, },
                1: { axis: 'Support', targetAxisIndex: 1}

            },

            axes: {
                x: {
                    Lift: { label: 'Lift', format: '0,000' //Doesn't work,  }, // Bottom x-axis.
                    Support: { side: 'top', label: 'Support' } // Top x-axis.
                }
            }, ..........

我还尝试了google doc中的这种方法:

series:{hAxes:{1:{title:'abc', format: '0,0000'}}

The top axes with the label support should have at least four decimal places

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

材料图表不支持多种选项 看 - &gt; Tracking Issue for Material Chart Feature Parity

虽然未列出format,但有几个选项不支持 - &gt; {hAxis,vAxis,hAxes.*,vAxes.*}

这可能是问题 注意:上述选项应该是独立的,不包括在series选项中 正如问题所示(我也尝试过......

您可以使用hAxis.format来更改两种x轴格式 但不要以为你只能换一个

请参阅以下工作代码段...

google.charts.load('current', {
  packages: ['bar']
}).then(function () {
  var data = new google.visualization.DataTable();
  data.addColumn('string', 'RuleName');
  data.addColumn('number', 'Lift');
  data.addColumn('number', 'Support');
  for (var i = 0; i < 10; i++) {
    data.addRow([i.toString(), i+2, i+3]);
  }

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

  var chart = new google.charts.Bar(document.getElementById('barChart'));
  var options = {
    chart: {
      title: 'Association Rules by lift and support'
    },
    bars: 'horizontal',
    series: {
      0: {axis: 'Lift'},
      1: {axis: 'Support'}
    },
    axes: {
      x: {
        Lift: {label: 'Lift'},
        Support: {side: 'top', label: 'Support'}
      }
    },
    hAxis: {
      format: '#,##0.00000'
    },
    height: 320
  };
  chart.draw(data, google.charts.Bar.convertOptions(options));
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="barChart"></div>