直流和交叉滤波器-计算记录中的百分比 [英] dc and crossfilter - calculating percentage within records

查看:64
本文介绍了直流和交叉滤波器-计算记录中的百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在一张直流图表中计算并绘制百分比(%).

I'm trying to calculate and plot percentage (%) in one of my dc charts.

我已阅读API和有关reduce函数的多个示例(添加,删除和初始化等) 我指的是 1st & 第二输入我的代码.

I have read the API and multiple examples regarding reduce functions (add, remove & init etc) I referred to 1st & 2nd for my codes.

我想计算衰减百分比(sum(decay_data)/sum(all_data);衰减数据在数据中由"decay_ind == 1"定义)

I want to calculate decay percentage ( sum(decay_data)/sum(all_data) ; decay_data is defined in data by "decay_ind==1" )

由于某些问题,我在图表中仍然没有得到任何结果,还使用了值访问器,但是可能存在一些问题.

I'm still not getting anything in the graph due to some issue, the value accessor is also used but, there could be some issue in that.

在代码中,请仅看到百分比组"和日期维度",其他仅用于单独的图表.

In the code, please just see "percentgroup" and "datedimension" only, others are for separate charts.

到目前为止我的代码:

 var chart = dc.pieChart("#test");
var chart2 = dc.barChart("#test2");
var chart3 = dc.barChart("#test3");

  console.log(data[0]);
  //Filtering only target population
  data = data.filter(function (d) { return d.target_ind == "1"; });

  var ndx  = crossfilter(data);


  bucketDimension  = ndx.dimension(function(d) {return "Bucket : " +d.decay_bucket;});
  speedSumGroup = bucketDimension.group().reduceSum( function(d) { return d.Count ;});

  dateDimension  = ndx.dimension(function(d) {return d.duration ;});
  rechGroup = dateDimension.group().reduceSum ( function(d) { return d.recharge_revenue ;});

    var percentGroup = dateDimension.group().reduce(
        //add
        function(p, v) {
            ++p.count;
            p.decay += (v.decay_ind === '1' ? p.recharge_revenue : 0);
            p.total += p.recharge_revenue;
            p.percent = p.count ? p.decay/p.total : 0;
            return p;
        },
        function(p, v) {
            --p.count;
            p.decay -= (v.decay_ind === '1' ? p.recharge_revenue : 0);
            p.total -= p.recharge_revenue;
            p.percent = p.count ? p.decay/p.total : 0;
            return p;
        },
        function() {
            return {
                count: 0,
                decay : 0,
                total : 0,
                percent : 0

            };
        }
        );


        chart3
        .width(768)
        .height(480)
        .x(d3.scale.ordinal())
        .xUnits(dc.units.ordinal)
        //.brushOn(false)
        .yAxisLabel("Decay %")
        .dimension(dateDimension)
        //.group(decayGroup)

        .on('renderlet', function(chart) {
            chart.selectAll('rect').on("click", function(d) {
                console.log("click!", d);
            });
        });


      chart3.valueAccessor(function (p) {
        return p.value.percent;
        })
        .group(percentGroup);

我在这里想念什么? 任何方法/建议都将非常有帮助.

what am I missing here? Any approach/suggestions would be very helpful.

更新

好吧,我已经纠正了一些错误,现在,只是想获得总和,以便更好地理解这些功能并在控制台中查看它们.还添加了持续时间维度中的所有值.

Okay, I've corrected a few mistakes and now, just trying to get the sum to understand these functions better and see them in the console. Added all values in duration dimension also.

我正在控制台中汇总一个名为充值收入"的变量-添加功能可为我提供随机/垃圾值.删除功能让我无穷无尽.

I'm summing up a variable called "Recharge revenue", in the console - add function gives me, random/junk values. Remove function gives me infinity.

我的新代码:

var percentGroup = dateDimension.group().reduce(
    //add
    function(p, v) {
        ++p.count;
        p.total += v.recharge_revenue;
        //p.percent = p.count ? p.decay/p.total : 0;
        console.log(p.total);
        return p;
    },
    //remove
    function(p, v) {
        --p.count;
        p.total -= v.recharge_revenue;
        return p;
    },
    //init
    function() {
        return {
            count: 0,
            total : 0,

        };
    }
    );

chart3
    .width(768)
    .height(480)
    .x(d3.scale.ordinal())
    .xUnits(dc.units.ordinal)
    //.brushOn(false)
    .yAxisLabel("Decay %")
    .dimension(dateDimension)
    .valueAccessor(function (p) {
    return p.value.total;
     })
    .group(percentGroup)
    .on('renderlet', function(chart) {
        chart.selectAll('rect').on("click", function(d) {
            console.log("click!", d);
        });
    });

由于明显的原因,图表仍然为空(控制台显示)

The graph is still empty for obvious reasons (console displays)

请帮助我解决此问题,我在这里无法理解问题所在.完成总和后,我要计算百分比.

Please help me fix this, I just can't get what's wrong here. Once sum is done, I want to go about calculating percentage.

另一个警告:我的数据集(csv)中有一列"Count",这是否有可能在这里引起问题? (已更改为其他名称,仍然无法使用)

Another caveat : I have a column "Count" in my dataset (csv), is that causing the problem here by any chance? (changed to another name, still not working)

基本上,唯一起作用的是图形中的 count ,如果我使用任何总和,平均值,我将获得随机值且没有图形.

Basically, the only thing working is count in graphs, if I use any sum, average, I'm getting random values and no graph plot.

任何建议/反馈都将受到欢迎.

Any suggestions/feedback will be most welcome.

更新2:

这是小提琴

数据集有两个存储桶和两个持续时间

Dataset has two buckets and two durations

它显示了总计和平均值的垃圾/随机值.请立即帮助我解决问题.

It's showing junk/random values for both total and average. Please help me fix the issue now.

推荐答案

数字为字符串格式,必须将其解析为整数才能使函数读取它们,而值在连接它们时是垃圾.

The numbers are in string format, had to be parsed into integer for the functions to read them, the values were junk as it was concatenating them.

解决方案代码:

function(p, v) {
        ++p.count;
        p.total += parseInt(v.recharge_revenue);
        //p.percent = p.count ? p.decay/p.total : 0;
        console.log(p.total);
        return p;
    },
    //remove
    function(p, v) {
        --p.count;
        p.total -= parseInt(v.recharge_revenue);
        return p;
    }

这篇关于直流和交叉滤波器-计算记录中的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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