javascript 以编程方式格式化谷歌图表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7298685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:38:56  来源:igfitidea点击:

Formatting google charts programmatically

javascripthtmlchartsformattinggoogle-visualization

提问by Maxim Gershkovich

Using the following code how can I set the formatting so that CurrencyValue1 and CurrencyValue2 is shown with a dollar (as a currency value) in the chart?

使用以下代码如何设置格式,以便 CurrencyValue1 和 CurrencyValue2 在图表中显示为美元(作为货币值)?

function drawChart() {
    var data = new google.visualization.DataTable();
    data.addColumn('date', 'Date');
    data.addColumn('number', 'CurrencyValue1');
    data.addColumn('number', 'CurrencyValue2');

    data.addRows(1);
    data.setValue(0, 0, new Date(2011, 8, 12));
    data.setValue(0, 1, 300.0000);
    data.setValue(0, 2, 759.1707);

    var chart = new google.visualization.LineChart(document.getElementById('chart_div'));

    chart.draw(data, { width: 660, height: 470, title: 'Heading', is3D: true, backgroundColor: '#f5f3e5' });
}

回答by Marek Sebera

see documentation: http://code.google.com/intl/cs-CZ/apis/chart/interactive/docs/reference.html#numberformatter

请参阅文档:http: //code.google.com/intl/cs-CZ/apis/chart/interactive/docs/reference.html#numberformatter

var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'CurrencyValue1');
data.addColumn('number', 'CurrencyValue2');

var formatter = new google.visualization.NumberFormat(
      {prefix: '$', negativeColor: 'red', negativeParens: true});
formatter.format(data, 1);
formatter.format(data, 2);

This will format columns two and three like money (prefixed with dollar sign like "$15.00")

这将格式化第二列和第三列,就像钱一样(以美元符号为前缀,如“$15.00”)

回答by overallduka

This is perfect format to Brazilian currency:

这是巴西货币的完美格式:

  var formatter = new google.visualization.NumberFormat({decimalSymbol: ',',groupingSymbol: '.', negativeColor: 'red', negativeParens: true, prefix: 'R$ '});
  formatter.format(data, 1);

Works fine whit dollar also, some change the R$to $

用美元也可以正常工作,有些更改R$$

10500.5stay 10.500,50, more prefix

10500.5停留10.500,50, 更多前缀

10500stay 10.500,00, more prefix

10500停留10.500,00, 更多前缀

回答by R.Alonso

Use Data.SetFormattedValue and change 3# param.

使用 Data.SetFormattedValue 并更改 3# 参数。

Like this:

像这样:

For i As Integer = 0 To dt.Rows.Count - 1
....

   str.Append("data.setValue( " & i & "," & 0 & "," & "'" & Cadena & "');")
   str.Append("data.setValue(" & i & "," & 1 & "," & Valor & ") ;")
    str.Append("data.setFormattedValue(" & i & "," & 1 & ",'" &  FormatCurrency(Valor.Replace(".", ",")) & "') ;")
next