防止图表重新计算自己的百分比 [英] Prevent a graph from recalculating its own percentages

查看:221
本文介绍了防止图表重新计算自己的百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有三个行图表,我的代码计算和更新每个图表的百分比,每当用户首次登陆页面或点击图表的矩形条。这是计算百分比的方法


posChart

%Position = unique StoreNumber每个位置的计数/唯一StoreNumber所有POSITION的计数


deptChart

%Departments =所有DEPARTMENT的DEPARTMENT / POSITION计数的POSITION次数


stateChart

%States = unique每个STATE的StoreNumber计数/所有STATE的唯一StoreNumber计数



我想要的是用户点击如COUNTS BY STATE这样的rowChart的矩形条,则应该更新/重新计算图表的百分比(它不应影响其自身的百分比),但是,应重新计算百分比其他两个图表,即按部门计算和按位置计算。同样的情况也适用于其他图表。这是我想要的



如果用户点击了一个


  1. COUNTS BY DEPARTMENT图表 - >重新计算COUNTS BY POSITION和COUNTS BY STATE图表的百分比


  2. COUNTS BY POSITION图表 - - >重新计算COUNTS BY DISTRIBUTOR和COUNTS BY STATE图表的百分比

请帮助!

链接: http://jsfiddle.net / mfi_login / z860sz69 /





感谢您的回覆。
您提供的解决方案有问题。我正在查找所有过滤器的全局总计,但我不希望在用户点击当前图表的矩形栏时更改总计。

例如,
如果两个不同的POSITIONS(主管,客户经理)具有相同的StoreNumber(3),那么我想要StoreNumber被计为1不是2



如果我们举一个客户经理%计算示例(COUNTS BY POSITION图表)

 总唯一StoreNumbers = 3 
客户经理POSITIONs = 2
%= 2/3 = 66%

重新绘制其他两个图表而不触及当前的图表?

解决方案

在我看来,你真正想要的是使用总的图表的组,而不是总的。如果使用整体总计,则会观察所有过滤器,但如果您使用当前组的总计,则不会在当前图表上看到任何过滤器。



这将有你想要的效果 - 它不是为了防止任何计算,而是确保每个图表只受到其他图表上的过滤器的影响。



sum_group_xep bin_counter p>

  function sum_group(group,acc){
acc = acc || function(kv){return kv.value; };
return d3.sum(group.all()。filter(function(kv){
return acc(kv)> 0;
}),acc);
}

function sum_group_xep(group){
return sum_group(group,function(kv){
return kv.value.exceptionCount;
}) ;
}

我们将对每个图表使用这些,例如:

  posChart 
.valueAccessor(function(d){
//获取所选过滤器的唯一商店编号b $ b var value = sum_group_xep(posGrp)
var percent = value> 0?(d.value.exceptionCount / value)* 100:0
//返回x轴百分比
return percent
})

deptChart
.valueAccessor(function(d){
total = sum_group(deptGrp)
var percent = d.value ?(d.value / total)* 100:0
return percent
})

stateChart
.valueAccessor(function(d){
var value = sum_group_xep(stateGrp);
返回值> 0?(d.value.exceptionCount / value)* 100:0
})

...与其他6个地方一起使用。可能有一个更好的方式来组织这个没有这么多的代码复制,但我没有真正想到它!



你的小提琴的叉子: http://jsfiddle.net/gordonwoodhull/yggohcpv/8/



编辑:Reductio可能有更好的快捷方式,但我认为除以当前图表组中的值的总和,而不是使用groupAll观察所有过滤器的原则是正确的开始。 p>

I have three Row Charts and my code calculates and updates the percentages for each chart whenever a user first lands on the page or clicks a rectangle bar of a chart.  This is how it calculates the percentages

posChart:
% Position= unique StoreNumber counts per Position / unique StoreNumber counts for all POSITIONs

deptChart:
% Departments= POSITION counts per DEPARTMENT/POSITION counts for all DEPARTMENTs

stateChart:
% States= unique StoreNumber counts per STATE / unique StoreNumber counts for all STATEs

What I want is when a user clicks a rectangle bar of a rowChart such as "COUNTS BY STATE", it should NOT update/recalculate the percentages for that chart (it should not affect its own percentages), however, percentages should be recalculated for the other two charts i.e. "COUNTS BY DEPARTMENT" and "COUNTS BY POSITION".  The Same scenario holds for the other charts as well. This is what I want

If a user clicks a

  1. "COUNTS BY DEPARTMENT" chart --> recalculate percentages for "COUNTS BY POSITION" and "COUNTS BY STATE" charts

  2. "COUNTS BY POSITION" chart --> recalculate percentages for "COUNTS BY DISTRIBUTOR" and "COUNTS BY STATE" charts

Please Help!!
link:http://jsfiddle.net/mfi_login/z860sz69/

Thanks for the reply. There is a problem with the solution you provided. I am looking for the global total for all filters but I don’t want those totals to be changed when user clicks on a current chart's rectangular bar.
e.g. if there are two different POSITIONS (Supervisor, Account Manager) with the same StoreNumber (3), then I want StoreNumber to be counted as 1 not 2

If we take an example of Account Manager % calculation (COUNTS BY POSITION chart)

total unique StoreNumbers=3
Total Account Manager POSITIONs=2
% = 2/3=66%

Is there a way to redraw the other two charts without touching the current one?

解决方案

It seems to me that what you really want is to use the total of the chart's groups, not the overall total. If you use the overall total then all filters will be observed, but if you use the total for the current group, it will not observe any filters on the current chart.

This will have the effect you want - it's not about preventing any calculations, but about making sure each chart is affected only by the filters on the other charts.

So, instead of bin_counter, let's define sum_group and sum_group_xep:

function sum_group(group, acc) {
    acc = acc || function(kv) { return kv.value; };
  return d3.sum(group.all().filter(function(kv) {
    return acc(kv) > 0;
  }), acc); 
}

function sum_group_xep(group) {
    return sum_group(group, function(kv) {
    return kv.value.exceptionCount;
  });
}

And we'll use these for each chart, so e.g.:

    posChart
        .valueAccessor(function (d) { 
            //gets the total unique store numbers for selected filters                
            var value=sum_group_xep(posGrp)
            var percent=value>0?(d.value.exceptionCount/value)*100:0                
            //this returns the x-axis percentages
            return percent               
        })

    deptChart
        .valueAccessor(function (d) { 
                total=sum_group(deptGrp)
                var percent=d.value?(d.value/total)*100:0
                return percent
        })  

    stateChart 
        .valueAccessor(function (d) {
            var value=sum_group_xep(stateGrp);
            return value>0?(d.value.exceptionCount/value)*100:0
        })  

... along with the other 6 places these are used. There's probably a better way to organize this without so much duplication of code, but I didn't really think about it!

Fork of your fiddle: http://jsfiddle.net/gordonwoodhull/yggohcpv/8/

EDIT: Reductio might have better shortcuts for this, but I think the principle of dividing by the total of the values in the current chart's group, rather than using a groupAll which observes all filters, is the right start.

这篇关于防止图表重新计算自己的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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