<!DOCTYPE html>
<html lang="en">
<head>
<title>Title here</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="../css/dc.css"/>
</head>
<body>
<div id = "select_box"> </div>
<div id = "chart"> </div>
<script type="text/javascript" src="../js/d3.js"></script>
<script type="text/javascript" src="../js/crossfilter.js"></script>
<script type="text/javascript" src="../js/reductio.js"></script>
<script type="text/javascript" src="../js/dc.js"></script>
<script type="text/javascript">
var facts = crossfilter([
{Dept: "A", HC: 500, attrition: 100, belongto: "abc", subtract: 23 },
{Dept: "A", HC: 500, attrition: 100, belongto: "pqr", subtract: 5 },
{Dept: "A", HC: 500, attrition: 100, belongto: "xyz", subtract: 25 },
{Dept: "B", HC: 200, attrition: 30, belongto: "abc", subtract: 6 },
{Dept: "B", HC: 200, attrition: 30, belongto: "pqr", subtract: 3 },
{Dept: "B", HC: 200, attrition: 30, belongto: "xyz", subtract: 10 }
]);
// Simplified view of facts //Not used //Just for understanding purpose
var simplifiedfacts = crossfilter([
{Dept: "A", HC: 500, attrition: 100, abc: 23, pqr: 5, xyz: 25},
{Dept: "B", HC: 200, attrition: 30, abc: 6, pqr: 3, xyz: 10}
]);
var catdim = facts.dimension(function(d) { return d.belongto; }); //category
var deptdim = facts.dimension(function(d) { return d.Dept; });
//Here is how I want the group to be
// (min(attrition) - abc - xyz - pqr) / min(HC) <- abc, xyz and pqr are part of checkbox
// So for dept A this will become (100 - 23 - 5 - 25) / 500 <- if all selection are on
// Three function used in dimension reduce.
function addR(p, v) {
p.att += Math.min(v.attrition);
p.abc += v.subtract; // Needs improvement
p.hc += Math.min(v.HC);
p.final = (p.att - p.abc)/p.hc;
return p
}
function removeR(p, v) {
p.att -= Math.min(v.attrition);
p.abc -= v.subtract; // Needs improvement
p.hc -= Math.min(v.HC);
p.final = (p.att + p.abc)/p.hc;
return p
}
function initialR(p, v) {
return {att:0, hc:0, abc:0}
}
var deptgroup = deptdim.group().reduce(addR, removeR, initialR)
dc.cboxMenu("#select_box")
.dimension(catdim)
.title(function(d) { return "Exlude: " + d.key })
.multiple(true)
.group(catdim.group());
dc.barChart("#chart")
.x(d3.scaleBand())
.xUnits(dc.units.ordinal)
.elasticY(true)
.dimension(deptdim)
.group(deptgroup)
.valueAccessor(function(p) { return p.value.final; });
dc.renderAll();
</script>
</body>
</html>
Q:-我需要数据
((损耗)的最小值-abc-xyz-pqr)/(HC)的最小值,其中abc,xyz和pqr是多选复选框选项。
如果未选中任何复选框,结果将是:
部门A:(100-0-0-0)/ 500 = 0.2
如果选中了abc(排除):(100-23-0-0)/ 500 = 0.154
如果检查了abc和pqr:(100-23-5-0)/ 500 = 0.144
如果所有复选框都选中:(100-23-5-25)/ 500 = 0.094,对于部门B同样。
我的尝试:
我尝试使用javascript Math.min函数,在第一次迭代中结果很好,但是在第二次迭代中没有给出准确的结果。
我也尝试使用归约函数,但无法产生所需的结果。