在谷歌图表中以小数点后两位显示数字

showing the figure on two decimal places in google chart

本文关键字:两位 数字 显示 谷歌 小数点      更新时间:2023-09-26

我有一个带有 ControlWrapper 的组合图表,我希望在我的图表中只看到小数点后两位的数字,所以我使用格式化程序,这是我的代码

 view: {
           columns: [
                     {
                //transform the numbers into strings, so the steppedArea series will work
              type: "string",
              calc: function (dt, row) {
                  var date = dt.getValue(row, 2);
                  var formatter = new google.visualization.DateFormat({pattern:"dd/MM/yyyy HH:mm"});
                  return formatter.formatValue(date);
                  console.log(calc, typeof (calc));
              }
           },
           {
             type:"string",
    calc: function (dt, row) {
          var number = dt.getValue(row, 2);
          var number = dt.getValue(row, 3);
          var number = dt.getValue(row, 4);
             var formatter = new google.visualization.NumberFormat({fractionDigits:2});
          console.log(calc);
             return formatter.formatValue(number);
             }
           },   
           6,7]
       }

但我有这个错误:

一个或多个参与者未能抽奖()×

给定轴上的所有序列必须具有相同的数据类型×

你能帮我吗?

在 Google 可视化 API 讨论组中回答,但在这里为有类似问题的人重印:

当应该返回数字值时,您正在返回字符串值。 如果要格式化数据,可以将其作为具有"v"(值)和"f"(格式化值)属性的对象返回:

{
    type: 'number',
    calc: function (dt, row) {
        var number = dt.getValue(row, 2);
        var formatter = new google.visualization.NumberFormat({fractionDigits:2});
        return {
            v: number,
            f: formatter.formatValue(number)
        };
    }
}