d3在对象数组上得到分位数

时间:2017-11-17 03:21:42

标签: javascript arrays d3.js nested quantile

我试图得到一个对象数组的第一个四分位数,中位数和最后四分位数。它们的值。

0: Object { key: "Aeronautical, Mechanical, Chemical and Manufacturing Engineering", value: 0.6100000000000001 }
1: Object { key: "Agriculture, Veterinary and Food Science", value: 1.3400000000000003 }
2: Object { key: "Allied Health Professions, Dentistry, Nursing and Pharmacy", value: 2.2699999999999996 }
3: Object { key: "Anthropology and Development Studies", value: 1.1400000000000003 }
4: Object { key: "Architecture, Built Environment and Planning", value: 4.21 }
5: Object { key: "Area Studies", value: 0.9800000000000001 }
6: Object { key: "Art and Design: History, Practice and Theory", value: 6.659999999999997 }
7: Object { key: "Biological Sciences", value: 1.3500000000000003 }
8: Object { key: "Business and Management Studies", value: 8.409999999999993 }
9: Object { key: "Chemistry", value: 0.15000000000000002 }

我想返回一个带有3个对象的对象数组。 我的最终目标是用它画一个饼图。

到目前为止我尝试过这种方法

        var rank = dmObj.getRank();
        console.log("test",rank);
        // using nest function from d3 to "classify" data into years
        // then rollup to change array of values into sum of money
        var nestedData = d3.nest()
            .key(function(d){ return d.naAsses;})
            .rollup(function (v) { return {
                first: d3.quantile(v.map(function(d){return rank.value ;}),.25),
                median: d3.median(v,function(d){return rank.value}),
                last: d3.quantile(v.map(function(d){return rank.value;}),.75)
            };

            })
            .sortKeys(d3.ascending)
            .entries(filteredData());

        console.log("quartil :data",nestedData);

        return nestedData;
    }

rank是上面链接的数据集。

1 个答案:

答案 0 :(得分:0)

您可以直接在数组中直接使用这些方法,而不是使用d3.nest()

var data = [{
  key: "Aeronautical, Mechanical, Chemical and Manufacturing Engineering",
  value: 0.6100000000000001
}, {
  key: "Agriculture, Veterinary and Food Science",
  value: 1.3400000000000003
}, {
  key: "Allied Health Professions, Dentistry, Nursing and Pharmacy",
  value: 2.2699999999999996
}, {
  key: "Anthropology and Development Studies",
  value: 1.1400000000000003
}, {
  key: "Architecture, Built Environment and Planning",
  value: 4.21
}, {
  key: "Area Studies",
  value: 0.9800000000000001
}, {
  key: "Art and Design: History, Practice and Theory",
  value: 6.659999999999997
}, {
  key: "Biological Sciences",
  value: 1.3500000000000003
}, {
  key: "Business and Management Studies",
  value: 8.409999999999993
}, {
  key: "Chemistry",
  value: 0.15000000000000002
}];

data.sort(function(a, b) {
  return d3.ascending(a.value, b.value)
});

var firstQuartile = d3.quantile(data.map(function(d) {
  return d.value
}), 0.25);
var median = d3.median(data.map(function(d) {
  return d.value
}));
var lastQuartile = d3.quantile(data.map(function(d) {
  return d.value
}), 0.75);

console.log("First quartile: " + firstQuartile)
console.log("Median: " + median)
console.log("Last quartile: " + lastQuartile)
<script src="https://d3js.org/d3.v4.min.js"></script>

编辑:根据your comment,您可以根据四分位数创建新属性:

var data = [{
  key: "Aeronautical, Mechanical, Chemical and Manufacturing Engineering",
  value: 0.6100000000000001
}, {
  key: "Agriculture, Veterinary and Food Science",
  value: 1.3400000000000003
}, {
  key: "Allied Health Professions, Dentistry, Nursing and Pharmacy",
  value: 2.2699999999999996
}, {
  key: "Anthropology and Development Studies",
  value: 1.1400000000000003
}, {
  key: "Architecture, Built Environment and Planning",
  value: 4.21
}, {
  key: "Area Studies",
  value: 0.9800000000000001
}, {
  key: "Art and Design: History, Practice and Theory",
  value: 6.659999999999997
}, {
  key: "Biological Sciences",
  value: 1.3500000000000003
}, {
  key: "Business and Management Studies",
  value: 8.409999999999993
}, {
  key: "Chemistry",
  value: 0.15000000000000002
}];

data.sort(function(a, b) {
  return d3.ascending(a.value, b.value)
})

data.forEach(function(d, i, arr) {
  d.quartile = d.value < d3.quantile(arr.map(function(d) {
    return d.value
  }), 0.25) ? "belowFirst" : d.value > d3.quantile(arr.map(function(d) {
    return d.value
  }), 0.75) ? "aboveThird" : "betweenFirstandThird"
});

console.log(data)
<script src="https://d3js.org/d3.v4.min.js"></script>