如何设置工具提示以显示与 Google 可视化折线图中的轴匹配的百分比?

发布于 2024-11-24 06:13:13 字数 564 浏览 0 评论 0原文

可以使用以下代码将工具提示设置为显示百分比:

var formatter = new google.visualization.NumberFormat({
      fractionDigits: 2,
      suffix: '%'
    });
formatter.format(data, 1); // Apply formatter to first column.

NumberFormat 有没有办法将每个元素乘以 100?否则工具提示将显示为 0.50%。

我使用的是 vAxis.format = "format:'#%' " 它确实乘以 100。因此 0.5 在垂直轴上显示为 50%。

根据文档(icu-project.org/apiref),这可以通过将 % 用单引号括起来来覆盖,但这不起作用。

最终结果是工具提示与轴不匹配。最好的方法是什么?

The tooltips can be set to display percentages using the following code:

var formatter = new google.visualization.NumberFormat({
      fractionDigits: 2,
      suffix: '%'
    });
formatter.format(data, 1); // Apply formatter to first column.

Is there a way for NumberFormat to multiply each element by 100? Otherwise the tooltip appears as .50%.

I am using vAxis.format = "format:'#%' " which does multiply by 100. So .5 is displayed as 50% on the vertical axis.

According to the documentation(icu-project.org/apiref), this can be overwritten by enclosing the % in single quotes, but this did not work.

The net result is that the tooltips do not match the axis. What is the best way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无法回应 2024-12-01 06:13:13

我通过完全按照您的方式指定格式化程序来实现此目的:

var chartData = google.visualization.arrayToDataTable(tableData);
var formatter = new google.visualization.NumberFormat({
    fractionDigits: 2,
    suffix: '%'
});
formatter.format(chartData, 1);

最后一次调用中的 1 表示第二列,其中我有浮点值。

然后,我在图表选项中指定轴的格式,转义百分号,如文档和其他人指出的那样:

var chartOptions = {
    vAxis: { format: '#\'%\'' }
};

然后绘制图表:

var chart = new google.visualization.ColumnChart(document.getElementById('chart'));
chart.draw(chartData, chartOptions);

这将呈现一个左侧轴,其值如 10%20% 等等。工具提示看起来与默认提示类似,但百分比如 10.10%20.20% 等。

如果您还想在左侧轴中显示两个小数位,请在图表选项中使用此格式:

vAxis: { format: '#.00\'%\'' }

I got this working by specifying a formatter exactly as you do:

var chartData = google.visualization.arrayToDataTable(tableData);
var formatter = new google.visualization.NumberFormat({
    fractionDigits: 2,
    suffix: '%'
});
formatter.format(chartData, 1);

The 1 in the last call means the second column, in which I have float values.

Then I specify a format for the axis in the chart options, escaping the percentage sign as pointed out by documentation and others here:

var chartOptions = {
    vAxis: { format: '#\'%\'' }
};

I then draw the chart:

var chart = new google.visualization.ColumnChart(document.getElementById('chart'));
chart.draw(chartData, chartOptions);

This renders a left side axis with values like 10%, 20% and so on. And the tooltips looks like the default one but with a percentage like 10.10%, 20.20% and so on.

If you want two fraction digits in the left side axis as well, use this as format in the chart options instead:

vAxis: { format: '#.00\'%\'' }
静若繁花 2024-12-01 06:13:13
var formatter = new google.visualization.NumberFormat({ 
  pattern: '#%', 
  fractionDigits: 2
});

感谢 http://groups.google.com/group/google-visualization-api/

var formatter = new google.visualization.NumberFormat({ 
  pattern: '#%', 
  fractionDigits: 2
});

Thanks to http://groups.google.com/group/google-visualization-api/

夏九 2024-12-01 06:13:13

必须用单引号将百分号 (%) 本身引起来。

我用来执行此操作的行如下所示: options['vAxis'] = {'format': "#,###'%'"};

将其与上面的格式化程序结合起来,您可以可以使垂直轴有一个百分比符号,也可以使工具提示也包含它。

You must surround the percent (%) symbol itself in single quotes.

The line I used to do this looks like this: options['vAxis'] = {'format': "#,###'%'"};

Combining this with your formatter above, you can make the vertical axis have a percent symbol and also make the tooltip include it too.

是你 2024-12-01 06:13:13

好吧...所以这有点晚了。我承认七年前我不需要这个。尽管如此,这对我有用。

var rows = data.getNumberOfRows();

for (var i = 0; i < rows; i++) {
    data.setFormattedValue(i, 4, (data.getFormattedValue(i, 4)*100).toFixed(1) + "%"); //LY
    data.setFormattedValue(i, 3, (data.getFormattedValue(i, 3)*100).toFixed(1) + "%"); //TY
}

就我而言,我使用四列,其中两列以百分比分配给右轴。我希望这些列的工具提示能够反映正确的百分比而不是小数表示形式。

这是 Google 文档的链接:

https://developers.google.com/chart/interactive/docs /reference#DataTable_setFormattedValue

我希望这可以帮助一些随机的陌生人寻找它。 ;)

Ok... So this is a little late. I admit I didn't need this seven years ago. Nevertheless, this worked for me.

var rows = data.getNumberOfRows();

for (var i = 0; i < rows; i++) {
    data.setFormattedValue(i, 4, (data.getFormattedValue(i, 4)*100).toFixed(1) + "%"); //LY
    data.setFormattedValue(i, 3, (data.getFormattedValue(i, 3)*100).toFixed(1) + "%"); //TY
}

In my case, I am using four columns, two of which are assigned to the right axis with percentages. I wanted those columns' tooltips to reflect the proper percentage rather than the decimal representation.

Here is a link to the Google docs:

https://developers.google.com/chart/interactive/docs/reference#DataTable_setFormattedValue

I hope this helps some random stranger looking for it. ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文