简体   繁体   English

d3.js:5942错误:的值无效 <g> 属性transform =“ translate(NaN,0)”

[英]d3.js:5942 Error: Invalid value for <g> attribute transform=“translate(NaN,0)”

Using d3.js v3.55, crossfilter.js v1.311 and dc.js v1.73 and trying to build an reduce function that returns an average as opposed to a sum or count. 使用d3.js v3.55,crossfilter.js v1.311和dc.js v1.73,并尝试构建一个reduce函数,该函数返回平均值而不是总和或计数。 The reduceSum which is builtin working fine, but when I try to add my own reduce function, based on many examples on the internet. 内置的reduceSum可以正常工作,但是基于互联网上的许多示例,当我尝试添加自己的reduce函数时。 I receive the messages: d3.js:5942 Error: Invalid value for attribute transform="translate(NaN,0)" d3.js:8718 Error: Invalid value for attribute width="NaN" 我收到消息:d3.js:5942错误:属性transform =“ translate(NaN,0)”的无效值d3.js:8718错误:属性width =“ NaN”的无效值

Here is the code reduced as much as I can without eliminating any important information: 这是我在不消除任何重要信息的情况下尽可能减少的代码:

<script type="text/javascript">
//
// Create the Chart Objects
//
var GeoZoneChart               = dc.rowChart("#chart-geo-zone");
var pdcData = [
{GeoZone: 'New York Metro', PDCLast365: 0.43}, 
{GeoZone: 'New York Metro', PDCLast365: 0.427}, 
{GeoZone: 'New York Metro', PDCLast365: 0.418}, 
{GeoZone: 'Los Angeles Metro', PDCLast365: 0.4085}, 
{GeoZone: 'Los Angeles Metro', PDCLast365: 0.40565}, 
{GeoZone: 'Chicago Metro', PDCLast365: 0.46789457}, 
{GeoZone: 'Chicago Metro', PDCLast365: 0.46366023}, 
{GeoZone: 'Chicago Metro', PDCLast365: 0.447781455}
];
//
// normalize/parse data
//   in this case, turn the decimel into a percentage
pdcData.forEach(function(d) {
    d.PDCLast365 = d.PDCLast365 * 100;
    d.PDCLast365 = d.PDCLast365.toFixed(2);
});

function isNumeric(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

function reduceAddAvg(attr) {
  return function(p,v) {
    if (isNumeric(v[attr]) ){
        ++p.count;
        p.sum += v[attr];
        p.avg = p.sum/p.count;
    }
    return p;
  };
}
function reduceRemoveAvg(attr) {
  return function(p,v) {
    if (isNumeric(v[attr]) ){
        --p.count;
        p.sum -= v[attr];
        p.avg = p.sum/p.count;
    }
    return p;
  };
}
function reduceInitAvg() {
  return {count:0, sum:0, avg:0};
}

//
// set crossfilter
//
var ndx = crossfilter(pdcData),
    GeoZoneDim           = ndx.dimension(function(d) {return d.GeoZone;}),
    PDCLast365PerGeoZone = 
          GeoZoneDim.group().reduce(
            reduceAddAvg('PDCLast365'), 
            reduceRemoveAvg('PDCLast365'), 
            reduceInitAvg);


GeoZoneChart
    .width(400).height(200)
    .dimension(GeoZoneDim)
    .group(PDCLast365PerGeoZone)
    .elasticX(true);

dc.renderAll();
</script>

You need a guard for when p.count falls to zero. p.count降至零时,您需要一个保护p.count So: 所以:

function reduceRemoveAvg(attr) {
  return function(p,v) {
    if (isNumeric(v[attr]) ){
        --p.count;
        p.sum -= v[attr];
        p.avg = p.count ? p.sum/p.count : 0;
    }
    return p;
  };
}

Obliged if you can point to any such bad examples here on SO or on the dc.js site or user group. 如果您可以在SO或dc.js网站或用户组上指出任何此类不良示例,则必须这样做。

EDIT: I'm not sure what effect it will have here, but toFixed() actually returns a string, not a number. 编辑:我不确定它将在这里产生什么效果,但是toFixed()实际上返回一个字符串,而不是一个数字。 And your isNumeric tests whether the value can be converted to a number, not whether it is a number. 然后您的isNumeric测试值是否可以转换为数字,而不是是否为数字。 So you might end up with string concatenation not addition. 因此,您可能最终得到的是字符串连接而不是加法。

Like I say below, the right way to figure this or is by putting breakpoints in the reduce functions and seeing what's actually being calculated. 就像我在下面说的那样,解决这个问题的正确方法是在reduce函数中放置断点,然后查看实际计算的内容。

I usually get that Error when the range is not an Array. 当范围不是数组时,我通常会收到该错误。

Check if the variable that .range receives is an actual Array 检查.range接收的变量是否为实际数组

暂无
暂无

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

相关问题 d3.js arc.centroid(d):错误:的值无效 <text> 属性transform =“ translate(NaN,NaN)” - d3.js arc.centroid(d) : Error: Invalid value for <text> attribute transform=“translate(NaN,NaN)” C3js和D3js错误:的值无效 <g> 属性transform =“ translate(0,NaN)” - C3js and D3js Error: Invalid value for <g> attribute transform=“translate(0,NaN)” D3.js 错误:<g> 属性变换:预期数字,“translate(NaN,NaN)”</g> - D3.js Error: <g> attribute transform: Expected number, “translate(NaN,NaN)” 错误:无效的值 <g> attribute transform =“scale(NaN)translate(NaN,NaN)” - Error: Invalid value for <g> attribute transform=“scale(NaN) translate(NaN, NaN)” 无效的值 <circle> 使用D3.js属性cx =“NaN” - Invalid value for <circle> attribute cx=“NaN” using D3.js 错误:<g>属性transform =“translate(undefined,undefined)”的值无效 - Error: Invalid value for <g> attribute transform=“translate(undefined,undefined)” D3.js“错误:无效的值 <path> 属性“为移动平均线 - D3.js “Error: Invalid value for <path> attribute” for moving average D3.js错误:的值无效 <path> 折线图的属性 - D3.js Error: Invalid value for <path> attribute for a line chart <g>使用交互式指南时的属性 transform=&quot;translate(NaN,NaN)&quot; - <g> attribute transform="translate(NaN,NaN)" when using Interactive Guideline d3.js在变换行中给出NAN - d3.js gives NAN in transform line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM