需要group.all()调用才能正确填充数据 [英] group.all() call required for data to populate correctly

查看:102
本文介绍了需要group.all()调用才能正确填充数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,当交叉过滤器使用数组而不是文字数字时,在处理基于变量的分组时遇到了一个奇怪的问题。

So I've encountered a weird issue when dealing with making Groups based on a variable when the crossfilter is using an array, instead of a literal number.

I当前有一个日期输出数组,然后是4个值,然后我将其映射到复合图中。问题在于,这4个值可能会波动,具体取决于页面上的输入。我的意思是,根据收到的信息,我可以有3个值或10个值,并且无法提前知道。将它们放置在一个数组中,然后将其提供给交叉过滤器。在测试中,我使用

I currently have an output array of a date, then 4 values, that I then map into a composite graph. The problem is that the 4 values can fluctuate depending on the input given to the page. What I mean is that based on what it receives, I can have 3 values, or 10, and there's no way to know in advance. They're placed into an array which is then given to a crossfilter. When in testing, I was accessing using

dimension.group.reduceSum(function(d) { return d[0]; });

将0更改为我需要的值。但是我已经完成了大部分测试,并开始将其调整为一个可以更改的动态系统,但是至少总是有前两个。为此,我创建了一个整数,用于跟踪我所在的索引,然后在创建组后将其增加。正在使用以下代码:

Where 0 was changed to whatever I needed. But I've finished testing, for the most part, and began to adapt it into a dynamic system where it can change, but there's always at least the first two. To do this I created an integer that keeps track of what index I'm at, and then increases it after the group has been created. The following code is being used:

var range = crossfilter(results);
var dLen = 0;
var curIndex = 0;
var dateDimension = range.dimension(function(d) { dLen = d.length; return d[curIndex]; });
curIndex++;
var aGroup = dateDimension.group().reduceSum(function(d) { return d[curIndex]; });
curIndex++;
var bGroup = dateDimension.group().reduceSum(function(d) { return d[curIndex]; });
curIndex++;
var otherGroups = [];
for(var h = 0; h < dLen-3; h++) {
    otherGroups[h] = dateDimension.group().reduceSum(function(d) { return d[curIndex]; });
    curIndex++;
}
var charts = [];
for(var x = 0; x < dLen - 3; x++) {
    charts[x] = dc.barChart(dataGraph)
        .group(otherGroups[x], "Extra Group " + (x+1))
        .hidableStacks(true)
}
charts[charts.length] = dc.lineChart(dataGraph)
    .group(aGroup, "Group A")
    .hidableStacks(true)
charts[charts.length] = dc.lineChart(dataGraph)
    .group(aGroup, "Group B")
    .hidableStacks(true)

问题是这样的:
图形被构建为空。我多次检查了curIndex变量,它始终是正确的。我最终决定改用.all()方法检查实际组的结果数据。

The issue is this: The graph gets built empty. I checked the curIndex variable multiple times and it was always correct. I finally decided to instead check the actual group's resulting data using the .all() method.

奇怪的是,在我使用.all()之后,现在是数据了作品。如果没有.all()调用,则该图将无法确定数据并绝对不会输出任何内容,但是,如果在创建该组后立即调用.all(),它将正确填充。

The weird thing is that AFTER I used .all(), now the data works. Without a .all() call, the graph cannot determine the data and outputs absolutely nothing, however if I call .all() immediately after the group has been created, it populates correctly.

每个组都需要调用.all(),否则只有起作用的组才能调用。例如,当我第一次调试时,我仅在aGroup上使用.all(),并且仅aGroup填充到图中。当我将其添加到bGroup时,将同时填充aGroup和bGroup。因此,在当前版本中,每个组在创建后都会直接调用.all()。

Each Group needs to call .all(), or only the ones that do will work. For example, when I first was debugging, I used .all() only on aGroup, and only aGroup populated into the graph. When I added it to bGroup, then both aGroup and bGroup populated. So in the current build, every group has .all() called directly after it is created.

从技术上讲,这没有问题,但是我对为什么这样做感到困惑需要。我完全不知道这是什么原因,我想知道是否对此有任何见识。当我使用文字时,没有问题,只有在使用变量创建组时才会发生。我尝试稍后获取输出,当我这样做时,我收到所有值的NaN。我不太确定为什么.all()会将值更改为应更改的值,尤其是当仅在创建组后立即执行时才更改它。

Technically there's no issue, but I'm really confused on why this is required. I have absolutely no idea what the cause of this is, and I was wondering if there was any insight into it. When I was using literals, there was no issue, it only happens when I'm using a variable to create the groups. I tried to get output later, and when I do I received NaN for all the values. I'm not really sure why .all() is changing values into what they should be especially when it only occurs if I do it immediately after the group has been created.

下面是该图的屏幕截图。顶部是所有对象在创建后都调用.all()时,底部是额外组(在for循环中定义的组)不再具有.all()调用时。数据根本不存在,我不确定为什么。任何想法都会很棒。

Below is a screenshot of the graph. The top is when everything has a .all() call after being created, while the bottom is when the Extra Groups (the ones defined in the for loop) do not have the .all() call anymore. The data is just not there at all, I'm not really sure why. Any thoughts would be great.

http: //i.stack.imgur.com/0j1ey.jpg

推荐答案

似乎您可能遇到了经典的从循环生成lambda JavaScript问题。

It looks like you may have run into the classic "generating lambdas from loops" JavaScript problem.

您正在创建一堆引用 curIndex 的函数但是除非您立即调用这些函数,否则它们将在全局环境中引用 curIndex 的同一实例。因此,如果您在初始化后调用它们,它们可能都会尝试使用末尾的值。

You are creating a whole bunch of functions that reference curIndex but unless you call those functions immediately, they will refer to the same instance of curIndex in the global environment. So if you call them after initialization, they will probably all try to use a value which is past the end.

相反,您可以创建一个函数来生成lambda,像这样:

Instead, you might create a function which generates your lambdas, like so:

function accessor(curIndex) {
    return function(d) { return d[curIndex]; };
}

然后每次调用 .reduceSum(accessor( curIndex))

这将导致每次调用访问器函数(或者您可以将每个生成的函数视为具有自己的环境和自己的 curIndex )。

This will cause the value of curIndex to get copied each time you call the accessor function (or you can think of each generated function as having its own environment with its own curIndex).

这篇关于需要group.all()调用才能正确填充数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆