使用减少添加删除功能删除空纸箱不起作用 [英] remove empty bins with reduce Add Remove function not working

查看:66
本文介绍了使用减少添加删除功能删除空纸箱不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种解决方案,以解决在使用减少添加/删除功能时如何删除空垃圾箱的问题。

I'm looking for a solution in how I can remove empty bins when using a reduce Add/Remove function.

我有一个jsfiddle 此处

I have a jsfiddle here

当我想提供一个点的简单总和,但是当我想使用平均值计算并在图表中使用valueAccessor时则不是。

Empty bins are removed when I want to provide a simple sum of 'Points', but not when I want to use an average calculation and using the valueAccessor in the charts.

我的数据设置如下:

{Season:"2016/17",
Manager:"Alan Curtis",
Points:1,
Formation:"4231",
date:"01 February 2017"},

{Season:"2016/17",
Manager:"Paul Clement",
Points:1,
Formation:"442",
date:"01 February 2018"},

{Season:"2015/16",
Manager:"Paul Clement",
Points:3,
Formation:"433",
date:"01 May 2017"},

我的目标是通过经理以及形成来提供平均每场得分。

And my aim is to provide a 'Points Per Game' average, by 'Manager', and also by 'Formation'.

我正在使用duce添加/删除函数:

I'm using the reduce Add/Remove functions:

function reduceAdd(p, v) {
    p.total += v.Points;
    ++p.count;
    p.ppg = d3.round((p.total / p.count), 2);
    return p;
    }

function reduceRemove(p, v) {
    p.total -= v.Points;
    --p.count;
    p.ppg = d3.round((p.total / p.count), 2);
    return p;
    }

function reduceInitial() {
    return {
        total: 0,
        count: 0,
        ppg: 0,
        };
    }

并删除空垃圾箱代码:

function remove_empty_bins(source_group) {
return {
    all:function () {
        return source_group.all().filter(function(d) {
            return d.value !=0;
        });
    }
};
}

我的图表代码:

managerChart
    .dimension(dimManager)
    .group(ManagerPPGGroup)
    .ordering(function(p) { return -p.value.ppg })
    .renderLabel(false)
    .othersGrouper(null)
    .renderTitle(false)
    .renderTitleLabel(true)
    .margins({top: 10, left: 10, right: 20, bottom: 80})
    .valueAccessor(function(p) 
        { if (p.value.ppg >0) {
        return p.value.ppg } else { return "n/a"}; });

formationChart
    .dimension(dimFormation)
    .group(filteredFormationPPGGroup)
    .ordering(function(p) { return -p.value.ppg })
    .renderLabel(false)
    .cap(10)
    .elasticX(true)
    .renderTitle(false)
    .renderTitleLabel(true)
    .margins({top: 10, left: 10, right: 20, bottom: 80})
    .valueAccessor(function(p) { return p.value.count > 0 ? p.value.ppg : "not used"; });

一切正常,但应用过滤器时不会删除空垃圾箱。

Everything works fine, apart from empty bins are not removed when a filter is applied.

我已经尝试了各种方法来解决此问题,更改了图表的valueAccessor和remove_empty_bins函数,但似乎无济于事。

I have tried all sorts of things to try and fix the issue, changing the valueAccessor of the charts and the remove_empty_bins function but nothing seems to work.

我当前的解决方法是在图形上提供未使用文本,以便用户知道Manager并未使用该格式,但我希望按预期的方式删除空容器。

My current workaround is to provide "not used" text on the graph so users know the Manager didn't use the formation, but I'd prefer to remove empty bins as intended.

预先感谢您的帮助。

推荐答案

是的, remove_empty_bins 如果减少量产生一个对象而不是一个数字,则需要进行调整。

Yes, remove_empty_bins needs to be adjusted if the reduction produces an object instead of just a number.

我想不出任何通用的方法来做到这一点不会使其效率低下, * ,所以让我们针对此用例调整功能:

I can't think of any general way to do this that won't make it inefficient,* so let's adjust the function for this use-case:

function remove_empty_bins(source_group) {
    return {
        all:function () {
            return source_group.all().filter(function(d) {
                return d.value.total != 0;
            });
        }
    };
}

我们只需要提取 .total 移出对象,因为对象(几乎)永远不等于零

We just need to pull .total out of the object, because an object (almost) never equals zero.

作为奖励,我还在您的小提琴中设置了固定的高度:

As a bonus, I've also set the bars to a fixed height in your fiddle:

formationChart
    .fixedBarHeight(30)

否则,当只有一个条形图时,它将增长到适合整个区域,这被很多人认为很丑。

Otherwise when there is a single bar, it will grow to fit the entire area, which many people consider ugly.

我还对经理人的rowChart应用了过滤。小提琴的叉子: https://jsfiddle.net/gordonwoodhull/qw0oe8ea/6/

I also applied filtering to the managers rowChart. Fork of your fiddle: https://jsfiddle.net/gordonwoodhull/qw0oe8ea/6/

* 也许是时候用谓词将其重构为 remove_bins()了吗?但是,直到没有箭头功能的浏览器消失之后,这才是简短的。

* Maybe it's time to refactor this into remove_bins() with a predicate? But that won't be terse until the no-arrow-function browsers go away.

这篇关于使用减少添加删除功能删除空纸箱不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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