javascript - 谷歌图表双 Axis ,设置格式

标签 javascript format google-visualization axis

我创建了一个带有两个 x Axis 的 Google 条形图。 我的问题是我似乎无法在底部 Axis 上获得正确的格式,该格式应以百分比显示。

我尝试过使用以下内容

axes:{{},{format:'#%'}}}

一般来说,在有意义的地方插入 format:'#%' ,但似乎没有任何效果。

有人对此有想法吗?

在此处查看完整代码:https://jsfiddle.net/laursen92/azr4kfn0/1/

      google.charts.load('current', {
    'packages': ['bar']
  });
  google.charts.setOnLoadCallback(drawStuff);

  function drawStuff() {
    var data = new google.visualization.arrayToDataTable([
      ['Slave number', 'Voltage', '%'],
      ['Slave 1', 12.15, 0.40],
      ['Slave 2', 12.18, 0.50],
      ['Slave 3', 11.80, 0.60],
      ['Slave 4', 13.12, 0.70],
    ]);
    var formatter = new google.visualization.NumberFormat({
      pattern: '##0.00'
    });
    var formatter2 = new google.visualization.NumberFormat({
      pattern: '##0%'
    });
    formatter.format(data, 1);
    formatter2.format(data, 2);
    var options = {
      width: 800,

      chart: {
        title: 'Status of slaves',
        subtitle: 'Overview of the voltage and SOC of connected slaves. '
      },

      bars: 'horizontal', // Required for Material Bar Charts.
      series: {
        0: {
          axis: 'voltage'
        }, // Bind series 0 to an axis named 'voltage'.
        1: {
          axis: 'percent'
        } // Bind series 1 to an axis named 'soc'.
      },
      axes: {
        x: {
          voltage: {
            side: 'top',
            label: 'Voltage'
          }, // Top x-axis.
          percent: {
            label: 'Percent',
          } // Bottom x-axis.
        }
      },

    };

    var chart = new google.charts.Bar(document.getElementById('dual_x_div'));
    chart.draw(data, options);
  };

最佳答案

以下是您的 fidde 更新:https://jsfiddle.net/Balrog30/azr4kfn0/4/

我使用了我编写的一个小代码笔来对 Material 图表选项的更改进行逆向工程,这些更改大部分仍然没有记录。您可以在这里找到:http://codepen.io/nbering/pen/Kddvge

该选项的格式如下:

var options = {
  axes: {
    x: {
      yourAxis: {
        format: {
          pattern: "#%"
        }
      }
    }
 };

-- 或者--

options.axes.x.yourAxis.format.pattern = "#%";

关于javascript - 谷歌图表双 Axis ,设置格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38724577/

相关文章:

javascript - Axios:基本身份验证不适用于 GET 请求

c - C 是如何将信息存储到文件中的呢?

安卓图片加载速度

javascript - 谷歌图表 : Cannot read property 'datefield' of undefined

javascript - 函数内的jquery函数

javascript - 如果您尝试编辑,光标会跳到字段末尾

javascript - 使用正则表达式解析 'min:sec' 字符串

r - 在 R 字符串中的每 k 个位置插入字符

javascript - Google 图表饼图不完整

javascript - Google Charts 计算并显示过滤表的总计