gpt4 book ai didi

javascript - 在Google Piechart中显示不同的后缀

转载 作者:行者123 更新时间:2023-12-03 08:04:36 25 4
gpt4 key购买 nike

我有一个饼图,显示上行链路/下行链路总带宽结果。

现在,它们的后缀是 GB。

enter image description here

我努力尝试显示不同的后缀。例如,

  • 下载GB
  • 上行链路以 KB 为单位。
<小时/>

我有

<script>

google.setOnLoadCallback(drawChart);
function drawChart() {

console.log(color['downlink']);

var data = google.visualization.arrayToDataTable([
['Task', 'Bandwith'],
['Downlink', ({{$t_down_bytes}})],
['Uplink', ({{$t_up_bytes}})]
]);

var options = {
legend: 'buttom',
pieSliceText: 'value', // text | none
title: 'Total Bandwith Usage',
colors: [color['downlink'], color['uplink']],
width:'100%',
height: 400,
slices: {
1: {offset: 0.1}
},

};

var formatter = new google.visualization.NumberFormat({
fractionDigits:2,
suffix: ' GB'
});

formatter.format(data, 1);

var chart = new google.visualization.PieChart(document.getElementById('private'));
chart.draw(data, options);
}

</script>

有人可以解释一下这一点。

对此的任何提示/建议将不胜感激!

最佳答案

一种方法就是自己做:( Correct way to convert size in bytes to KB, MB, GB in Javascript 可能有帮助)

    var data = google.visualization.arrayToDataTable([
['Task', 'Bandwith'],
['Downlink', {v:6.4672328, f:"6.46 GB"}],
['Uplink', {v:9.40213213, f:"9.40 KB"}]
]);

请注意,v 是 Google 用于绘制的“真实”值,f 是它将显示的格式化值

如果您想保留 Google 格式化程序,另一种方法是在 formatter.format(data, 1); 之后添加此行

data.setFormattedValue(1,1,data.getFormattedValue(1,1).replace("GB","KB"))

设置第 1 行第 1 列的 formattedValue

更新考虑到您想要混合使用两者:

var data = google.visualization.arrayToDataTable([
['Task', 'Bandwith'],
['Downlink', $t_down_bytes],
['Uplink', $t_up_bytes],
]);

var formatter = new google.visualization.NumberFormat({
fractionDigits:2
});

formatter.format(data, 1);

data.setFormattedValue(0,1,data.getFormattedValue(0,1) + ' {{$t_down_bytes_suffix}}')

data.setFormattedValue(1,1,data.getFormattedValue(1,1) + ' {{$t_up_bytes_suffix}}')

有关 setFormattedValuegetFormattedValue 的更多信息,请查看

Google Datatable Documentation

关于javascript - 在Google Piechart中显示不同的后缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34440855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com