为了让我的D3图表正常工作,我需要孩子的名字不是“关键”而是“标签”。父母仍然需要“关键”。
这是我的代码和输出。下面是我试过的一些事情,没有成功。
拉伸目标,如果可能的话:我不一定要“硬编码”它。我希望能够将孩子名字的任何级别(在这种情况下只是第一级)改为我想要的任何级别(在这种情况下为“标签”)。
var csv = `breakfastItem,gluten,breakfastItemType,month,year,dateCode,value
Bagel,Yes,Cinnamon Raisin,May,2017,42856,300
Bagel,Yes,Cinnamon Raisin,March,2017,42795,100
Bagel,Yes,Cinnamon Raisin,November,2016,42675,20
Bagel,Yes,Blueberry,February,2017,42767,50
Bagel,Yes,Blueberry,November,2016,42675,30
Bagel,Yes,Blueberry,March,2016,42430,100
Bagel,Yes,Plain,February,2018,43132,200
Bagel,Yes,Plain,December,2017,43070,202
Bagel,Yes,Plain,February,2016,42401,201`;
var data = d3.csvParse(csv, function(d) {
d.value = +d.value;
d.year = +d.year;
d.dateCode = +d.dateCode;
return d;
});
var breakfastCombinations = d3.nest()
.key(function(d) {
return d.breakfastItem;
})
.key(function(d) {
return d.breakfastItemType;
})
.rollup(function(oldestDate) {
return oldestDate.sort(function(a, b) {
return b.dateCode - a.dateCode
})[0].value
})
.entries(data);
console.log(breakfastCombinations)
<script src="https://d3js.org/d3.v4.min.js"></script>
here的例子是我第一眼看到的。
var expenseMetrics = d3.nest()
.key(function(d) { return d.name; })
.rollup(function(v) { return {
count: v.length,
total: d3.sum(v, function(d) { return d.amount; }),
avg: d3.mean(v, function(d) { return d.amount; })
}; })
第二个例子是我认为可行的,但我不确定如何访问孩子,而不仅仅是父母。
breakfastCombinations.forEach(function(d) {
d.label = d.key;
});
提前致谢!
答案 0 :(得分:1)
您可以使用d3.map来映射项目:
例如,如果您要查找从“key”到“label”的父映射,请使用以下命令:
.entries(data).map(function(group) {
// parent mapping
return {label: group.key, values: group.values};
})
var csv = `breakfastItem,gluten,breakfastItemType,month,year,dateCode,value
Bagel,Yes,Cinnamon Raisin,May,2017,42856,300
Bagel,Yes,Cinnamon Raisin,March,2017,42795,100
Bagel,Yes,Cinnamon Raisin,November,2016,42675,20
Bagel,Yes,Blueberry,February,2017,42767,50
Bagel,Yes,Blueberry,November,2016,42675,30
Bagel,Yes,Blueberry,March,2016,42430,100
Bagel,Yes,Plain,February,2018,43132,200
Bagel,Yes,Plain,December,2017,43070,202
Bdagel,Yes,Plain,December,2017,43070,202
Bagel,Yes,Plain,February,2016,42401,201`;
var data = d3.csvParse(csv, function(d) {
d.value = +d.value;
d.year = +d.year;
d.dateCode = +d.dateCode;
return d;
});
var breakfastCombinations = d3.nest()
.key(function(d) {
return d.breakfastItem;
})
.key(function(d) {
return d.breakfastItemType;
})
.rollup(function(oldestDate) {
return oldestDate.sort(function(a, b) {
return b.dateCode - a.dateCode
})[0].value
})
.entries(data).map(function(group) {
// parent mapping
return {label: group.key, values: group.values};
})
console.log(breakfastCombinations);
<script src="https://d3js.org/d3.v4.min.js"></script>
如果您正在寻找儿童地图,请使用以下内容:
.entries(data).map(function(group) {
// children mapping
return {key: group.key, values: group.values.map(function(d) { return {label: d.key, value: d.value}})};
})
var csv = `breakfastItem,gluten,breakfastItemType,month,year,dateCode,value
Bagel,Yes,Cinnamon Raisin,May,2017,42856,300
Bagel,Yes,Cinnamon Raisin,March,2017,42795,100
Bagel,Yes,Cinnamon Raisin,November,2016,42675,20
Bagel,Yes,Blueberry,February,2017,42767,50
Bagel,Yes,Blueberry,November,2016,42675,30
Bagel,Yes,Blueberry,March,2016,42430,100
Bagel,Yes,Plain,February,2018,43132,200
Bagel,Yes,Plain,December,2017,43070,202
Bdagel,Yes,Plain,December,2017,43070,202
Bagel,Yes,Plain,February,2016,42401,201`;
var data = d3.csvParse(csv, function(d) {
d.value = +d.value;
d.year = +d.year;
d.dateCode = +d.dateCode;
return d;
});
var breakfastCombinations = d3.nest()
.key(function(d) {
return d.breakfastItem;
})
.key(function(d) {
return d.breakfastItemType;
})
.rollup(function(oldestDate) {
return oldestDate.sort(function(a, b) {
return b.dateCode - a.dateCode
})[0].value
})
.entries(data).map(function(group) {
// children mapping
return {key: group.key, values: group.values.map(function(d) { return {label: d.key, value: d.value}})};
})
console.log(breakfastCombinations);
<script src="https://d3js.org/d3.v4.min.js"></script>
基本上,您可以根据要求使用d3.map操作数组。
希望这有帮助。