使用d3.nest()来切片csv数据

时间:2016-12-01 03:10:23

标签: csv d3.js nested

我有一个这样的csv文件,我想要绘制图(总是在x轴上的日期),而cust是客户(team1,team2,team3),prod是产品(A,B,C,D)和单位是在给定日期为每种产品销售的单位:

date,cust,prod,units
2012-04-01,team1,A,34
2012-04-02,team1,B,45
2012-04-03,team2,C,67
2012-04-04,team1,A,78
2012-04-05,team3,D,89
2012-04-06,team2,C,99
2012-04-07,team2,A,101
2012-04-08,team3,A,122
2012-04-09,team1,D,134
2012-04-10,team1,D,160
2012-04-11,team2,D,180

我想绘制不同组合的折线图。所以基本上我希望能够仅仅根据产品(给定客户销售的A,B,C或D的单位)或(特定客户销售的单位,无论产品类型或特定类型的销售单位)或(仅与特定产品A,B,C或D的单位出售,不论客户如何)。这是我想要以一般方式得到的片段。在它下面它允许我过滤掉cust' team1'的数据。和刺激'。我怎么能概括这个呢?

 d3.csv("test.csv", function (error, tdata) {
        if (error) throw error;
        var _data = d3.nest()
                .key(function(d) {return d.cust; })
                .key(function(d) {return d.prod;})
                .entries(tdata);
        data = _data[0].values[0].values; // This gives me the filtered values I need for 

1 个答案:

答案 0 :(得分:2)

您可能在这里不需要nest(),但每种情况只需要一个简单的filter

例如,仅显示有关产品D的数据:

var dataProductD = data.filter(e=>e.prod==="D");



var data = [{
    "date": "2012-04-01",
    "cust": "team1",
    "prod": "A",
    "units": "34"
}, {
    "date": "2012-04-02",
    "cust": "team1",
    "prod": "B",
    "units": "45"
}, {
    "date": "2012-04-03",
    "cust": "team2",
    "prod": "C",
    "units": "67"
}, {
    "date": "2012-04-04",
    "cust": "team1",
    "prod": "A",
    "units": "78"
}, {
    "date": "2012-04-05",
    "cust": "team3",
    "prod": "D",
    "units": "89"
}, {
    "date": "2012-04-06",
    "cust": "team2",
    "prod": "C",
    "units": "99"
}, {
    "date": "2012-04-07",
    "cust": "team2",
    "prod": "A",
    "units": "101"
}, {
    "date": "2012-04-08",
    "cust": "team3",
    "prod": "A",
    "units": "122"
}, {
    "date": "2012-04-09",
    "cust": "team1",
    "prod": "D",
    "units": "134"
}, {
    "date": "2012-04-10",
    "cust": "team1",
    "prod": "D",
    "units": "160"
}, {
    "date": "2012-04-11",
    "cust": "team2",
    "prod": "D",
    "units": "180"
}];

var dataProductD = data.filter(e=>e.prod==="D");
console.log(dataProductD);

<script src="https://d3js.org/d3.v4.min.js"></script>
&#13;
&#13;
&#13;

或者显示有关为客户Team2销售的产品D的数据:

var dataProductDTeam2 = data.filter(e=>e.prod==="D" && e.cust==="team2");

&#13;
&#13;
var data = [{
    "date": "2012-04-01",
    "cust": "team1",
    "prod": "A",
    "units": "34"
}, {
    "date": "2012-04-02",
    "cust": "team1",
    "prod": "B",
    "units": "45"
}, {
    "date": "2012-04-03",
    "cust": "team2",
    "prod": "C",
    "units": "67"
}, {
    "date": "2012-04-04",
    "cust": "team1",
    "prod": "A",
    "units": "78"
}, {
    "date": "2012-04-05",
    "cust": "team3",
    "prod": "D",
    "units": "89"
}, {
    "date": "2012-04-06",
    "cust": "team2",
    "prod": "C",
    "units": "99"
}, {
    "date": "2012-04-07",
    "cust": "team2",
    "prod": "A",
    "units": "101"
}, {
    "date": "2012-04-08",
    "cust": "team3",
    "prod": "A",
    "units": "122"
}, {
    "date": "2012-04-09",
    "cust": "team1",
    "prod": "D",
    "units": "134"
}, {
    "date": "2012-04-10",
    "cust": "team1",
    "prod": "D",
    "units": "160"
}, {
    "date": "2012-04-11",
    "cust": "team2",
    "prod": "D",
    "units": "180"
}];

var dataProductDTeam2 = data.filter(e=>e.prod==="D" && e.cust==="team2");
console.log(dataProductDTeam2);
&#13;
<script src="https://d3js.org/d3.v4.min.js"></script>
&#13;
&#13;
&#13;

或者显示有关Team2的数据:

var dataTeam2 = data.filter(e=>e.cust==="team2");

&#13;
&#13;
var data = [{
    "date": "2012-04-01",
    "cust": "team1",
    "prod": "A",
    "units": "34"
}, {
    "date": "2012-04-02",
    "cust": "team1",
    "prod": "B",
    "units": "45"
}, {
    "date": "2012-04-03",
    "cust": "team2",
    "prod": "C",
    "units": "67"
}, {
    "date": "2012-04-04",
    "cust": "team1",
    "prod": "A",
    "units": "78"
}, {
    "date": "2012-04-05",
    "cust": "team3",
    "prod": "D",
    "units": "89"
}, {
    "date": "2012-04-06",
    "cust": "team2",
    "prod": "C",
    "units": "99"
}, {
    "date": "2012-04-07",
    "cust": "team2",
    "prod": "A",
    "units": "101"
}, {
    "date": "2012-04-08",
    "cust": "team3",
    "prod": "A",
    "units": "122"
}, {
    "date": "2012-04-09",
    "cust": "team1",
    "prod": "D",
    "units": "134"
}, {
    "date": "2012-04-10",
    "cust": "team1",
    "prod": "D",
    "units": "160"
}, {
    "date": "2012-04-11",
    "cust": "team2",
    "prod": "D",
    "units": "180"
}];

var dataTeam2 = data.filter(e=>e.cust==="team2");
console.log(dataTeam2);
&#13;
<script src="https://d3js.org/d3.v4.min.js"></script>
&#13;
&#13;
&#13;

我使用一组对象作为数据(这正是d3.csv的结果),因为我无法使用S.O.加载CSV。片段。