简体   繁体   English

在谷歌图表图表中设置浮点数

[英]Setting floating points in google graph charts

I want to assign values such as 137.55 to the "number" type column in google chart and then plot it. 我想将值为137.55的值分配给谷歌图表中的“数字”类型列,然后绘制它。 Anytime I assign the floating number, It throws this exception : 无论何时我分配浮点数,它都会抛出此异常:

Uncaught Error: Type mismatch. Value 32.03 does not match type number in column index 1 

My code is something like this : 我的代码是这样的:

      var data = new google.visualization.DataTable();

   data.addColumn('number','Loop');
   data.addColumn('number','ReqSec');
   var myTicks = new Array();

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

   for (var i=0;i<arr.length;i++){
        myVal = $.trim(arr[i][10]);
        myVal = parseFloat(Math.round(myVal * 100) / 100).toFixed(2);
        data.addRow([parseInt(i), myVal]);
        myTicks[i] = i;
   }

   formatter.format(data, 1);

  // Create and draw the visualization.
  new google.visualization.AreaChart(document.getElementById('visualization')).
      draw(data, {curveType: "function",
                  width: 700, height: 400,
                  vAxis: {title: 'Throughput'},
                  hAxis: {title: 'Loop', ticks: myTicks}}
          );

I tried to use formatter, but I guess it's not for floating point data type insertion. 我试图使用格式化程序,但我想这不适用于浮点数据类型插入。 any advice or solution ? 任何建议或解决方案?

This is your problem: 这是你的问题:

myVal = parseFloat(Math.round(myVal * 100) / 100).toFixed(2);

The return value of the toFixed method is a string, not a number. toFixed方法的返回值是一个字符串,而不是一个数字。 Using Math.round(myVal * 100) / 100 will give you a floating point number with 2 decimal precision as is; 使用Math.round(myVal * 100) / 100将为您提供一个具有2位小数精度的浮点数; you don't need to call toFixed on it. 你不需要调用toFixed就可以了。 Also, calling parseFloat on a number is rather pointless - you should call it on the string value you are getting from your array. 另外,在数字上调用parseFloat是没有意义的 - 你应该在你从数组中得到的字符串值上调用它。 And third, calling parseInt(i) is also pointless, since i is already as much of an integer as it is going to get (technically, all numbers in javascript are floating point). 第三,调用parseInt(i)也没有意义,因为i已经得到了它将要获得的整数(技术上,javascript中的所有数字都是浮点数)。 Try this instead: 试试这个:

for (var i=0;i<arr.length;i++){
    myVal = parseFloat($.trim(arr[i][10]));
    myVal = Math.round(myVal * 100) / 100;
    data.addRow([i, myVal]);
    myTicks[i] = i;
}

That will parse the string as a float, then trim it to a precision of 2 decimal places. 这会将字符串解析为float,然后将其修剪为2位小数的精度。 If you are interested in charting the data with the precision given by the data in your array, but want to trim the value shown in the tooltips to 2 decimal places, then you can do this instead: 如果您有兴趣使用数组中数据给出的精度绘制数据图表,但想要将工具提示中显示的值修剪为2位小数,那么您可以这样做:

for (var i=0;i<arr.length;i++){
    myVal = parseFloat($.trim(arr[i][10]));
    data.addRow([i, {v: myVal, f: myval.toFixed(2)}]);
    myTicks[i] = i;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM