我创建了一个带有两个 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/