gpt4 book ai didi

javascript - 如何在谷歌图表的工具提示中添加小数点和 Lacs?

转载 作者:行者123 更新时间:2023-11-30 16:02:02 25 4
gpt4 key购买 nike

我正在使用谷歌图表。我根据我的数据创建了一个图表。现在我想修改我的工具提示。有两件事我想修改。

每个包含单词 Lacs 的数据都保留一个小数点。假设我有像 31.221 这样的数据(来 self 的图片)。我希望它显示 31.2 Lacs。

我该怎么做?如果有人对此有任何想法,请与我分享。

enter image description here

我的代码是:

HTML:

<div id="chart_div" style="height: 400px; margin: 0 auto"></div>

脚本:

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {packages: ['corechart', 'line']});
google.charts.setOnLoadCallback(drawLogScales);

function drawLogScales() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'X');
data.addColumn('number', 'ELSS 13%');
data.addColumn('number', 'ULIP 9%');
data.addColumn('number', 'SSY 8.6%');
data.addColumn('number', 'PPF 8.1%');
data.addColumn('number', 'LIC 6%');
data.addColumn('number', 'NSC 5.95%');
data.addColumn('number', 'TD 5.95%');
data.addColumn('number', 'FD 5.5%');

data.addRows([
["0",0,0,0,0,0,0,0,0],["2016",1.695,1.635,1.629,1.6215,1.59,1.58925,1.58925,1.5825], ["2017", 3.61035, 3.41715,3.398094,3.3743415,3.2754,3.273060375,3.273060375, 3.2520375], ["2018", 5.7746955, 5.3596935,5.319330084,5.269163162, 5.061924, 5.057057467, 5.057057467, 5.013399563], ["2019", 8.220405915, 7.477065915,7.405792471,7.317465378, 6.95563944, 6.947202387, 6.947202387, 6.871636538], ["2020", 10.98405868, 9.785001847,9.671690624,9.531680073, 8.962977806, 8.949810929, 8.949810929, 8.832076548], ["2021", 14.10698631, 12.30065201,12.13245602,11.92524616, 11.09075647, 11.07157468, 11.07157468, 10.90034076], ["2022", 17.63589453, 15.04271069,14.80484723,14.5126911, 13.34620186, 13.31958337, 13.31958337, 13.0823595], ["2023", 21.62356082, 18.03155466,17.7070641,17.30971908, 15.73697398, 15.70134858, 15.70134858, 15.38438927], ["2024", 26.12962373, 21.28939458,20.85887161,20.33330632, 18.27119241, 18.22482882, 18.22482882, 17.81303068], ["2025", 31.22147481, 24.84044009,24.28173457,23.60180413, 20.95746396, 20.89845614, 20.89845614, 20.37524737], ["2026", 36.97526654, 28.7110797,27.99896374,27.13505027, 23.8049118, 23.73116428, 23.73116428, 23.07838598], ["2027", 43.47705119, 32.93007687,32.03587462,30.95448934, 26.8232065, 26.73241855, 26.73241855, 25.9301972], ["2028", 50.82406785, 37.52878379,36.41995984,35.08330298, 30.02259889, 29.91224746, 29.91224746, 28.93885805], ["2029", 59.12619667, 42.54137433,41.18107639,39.54655052, 33.41395483, 33.28127618, 33.28127618, 32.11299524], ["2030", 68.50760223, 48.00509802,46.35164896,44.37132111, 37.00879212, 36.85076211, 36.85076211, 35.46170998]
]);


var options = {
'title' : 'Accumulation of Rs. 150000 invested every year in 80C instruments',
colors: ['#F69688', '#A8423F', '#86A44A','#6E548D','#3D96AE','#DA8137','#403152','#CE8E8D'],
backgroundColor: 'transparent',
hAxis: {
title: 'Years',
logScale: true
},
vAxis: {
title: 'Accumulated amounts (in lakhs)',
logScale: false,
gridlines: { color: 'transparent' }
}

};

var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>

最佳答案

您可以使用NumberFormat 来格式化数据

// create formatter
var formatter = new google.visualization.NumberFormat({
pattern: '0.0',
suffix: ' Lacs'
});

// format number columns
for (var i = 1; i < data.getNumberOfColumns(); i++) {
formatter.format(data, i);
}

然后显示在工具提示中

看下面的例子...

google.charts.load('current', {
callback: drawLogScales,
packages: ['corechart']
});

function drawLogScales() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'X');
data.addColumn('number', 'ELSS 13%');
data.addColumn('number', 'ULIP 9%');
data.addColumn('number', 'SSY 8.6%');
data.addColumn('number', 'PPF 8.1%');
data.addColumn('number', 'LIC 6%');
data.addColumn('number', 'NSC 5.95%');
data.addColumn('number', 'TD 5.95%');
data.addColumn('number', 'FD 5.5%');

data.addRows([
["0",0,0,0,0,0,0,0,0],["2016",1.695,1.635,1.629,1.6215,1.59,1.58925,1.58925,1.5825], ["2017", 3.61035, 3.41715,3.398094,3.3743415,3.2754,3.273060375,3.273060375, 3.2520375], ["2018", 5.7746955, 5.3596935,5.319330084,5.269163162, 5.061924, 5.057057467, 5.057057467, 5.013399563], ["2019", 8.220405915, 7.477065915,7.405792471,7.317465378, 6.95563944, 6.947202387, 6.947202387, 6.871636538], ["2020", 10.98405868, 9.785001847,9.671690624,9.531680073, 8.962977806, 8.949810929, 8.949810929, 8.832076548], ["2021", 14.10698631, 12.30065201,12.13245602,11.92524616, 11.09075647, 11.07157468, 11.07157468, 10.90034076], ["2022", 17.63589453, 15.04271069,14.80484723,14.5126911, 13.34620186, 13.31958337, 13.31958337, 13.0823595], ["2023", 21.62356082, 18.03155466,17.7070641,17.30971908, 15.73697398, 15.70134858, 15.70134858, 15.38438927], ["2024", 26.12962373, 21.28939458,20.85887161,20.33330632, 18.27119241, 18.22482882, 18.22482882, 17.81303068], ["2025", 31.22147481, 24.84044009,24.28173457,23.60180413, 20.95746396, 20.89845614, 20.89845614, 20.37524737], ["2026", 36.97526654, 28.7110797,27.99896374,27.13505027, 23.8049118, 23.73116428, 23.73116428, 23.07838598], ["2027", 43.47705119, 32.93007687,32.03587462,30.95448934, 26.8232065, 26.73241855, 26.73241855, 25.9301972], ["2028", 50.82406785, 37.52878379,36.41995984,35.08330298, 30.02259889, 29.91224746, 29.91224746, 28.93885805], ["2029", 59.12619667, 42.54137433,41.18107639,39.54655052, 33.41395483, 33.28127618, 33.28127618, 32.11299524], ["2030", 68.50760223, 48.00509802,46.35164896,44.37132111, 37.00879212, 36.85076211, 36.85076211, 35.46170998]
]);

// create formatter
var formatter = new google.visualization.NumberFormat({
pattern: '0.0',
suffix: ' Lacs'
});

// format number columns
for (var i = 1; i < data.getNumberOfColumns(); i++) {
formatter.format(data, i);
}

var options = {
title : 'Accumulation of Rs. 150000 invested every year in 80C instruments',
titleTextStyle: {
fontSize: 20
},
colors: ['#F69688', '#A8423F', '#86A44A','#6E548D','#3D96AE','#DA8137','#403152','#CE8E8D'],
backgroundColor: 'transparent',
hAxis: {
title: 'Years',
logScale: true
},
vAxis: {
title: 'Accumulated amounts (in lakhs)',
logScale: false,
gridlines: { color: 'transparent' }
}

};

var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div" style="height: 400px; margin: 0 auto"></div>

关于javascript - 如何在谷歌图表的工具提示中添加小数点和 Lacs?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37584075/

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