我有一堆數據是六個類別之一,每個數據片段都有一個與之相關的時間。我需要使用直方圖將這些數據分類到每月分檔中,這很容易,但我也需要將每個分類進行堆疊。我一直在尋找一個堆疊直方圖的例子,但我能找到的唯一的例子是d3 v3,這在堆疊API中顯然是非常不同的。現在我被卡在stack()
後,我得到無意義的數據,我不能用它來生成堆積條形圖。d3 v4:使用堆棧和直方圖數據?
var data = this.data;
var margin = {top: 20, right: 20, bottom: 30, left: 50},
width = this.width - margin.left - margin.right,
height = this.height - margin.top - margin.bottom;
data.forEach(function(d) {
d.date = d3.isoParse(d.createdDate);
});
// set the ranges
var x = d3.scaleTime()
.domain(d3.extent(data, function(d) { return d.date; }))
.rangeRound([0, width]);
var y = d3.scaleLinear()
.range([height, 0]);
var colours = d3.scaleOrdinal(d3.schemeCategory10);
var svg = d3.select(this.$.chart);
var svg2 = svg.select("#canvas");
var histogram = d3.histogram()
.value(function(d) { return d.date; })
.domain(x.domain())
.thresholds(x.ticks(d3.timeMonth));
var dataGroupedByType = d3.nest()
.key(function(d) {
return d.type;
})
.object(data, d3.map);
var histDataByType = [];
for (var key in dataGroupedByType) {
var histData = histogram(dataGroupedByType[key]);
histDataByType.push({type: key, values: histData});
}
var stack = d3.stack()
.keys(["A","B","C","D","E","F"])
.value(function(d, key) {
return d.values;
});
var stackedHistData = stack(histDataByType);
dataGroupedByType
是具有六個鍵控對象(A到F),其每一個都包含數據對象的陣列的對象。然後,我製作了histDataByType
,這導致了一個由6個對象組成的數組,其中每個對象都具有type
屬性(A到F)和values
數組,它們的長度始終相同(本例中爲91,因爲我的數據跨度91個月)。在該數組中是另一個帶有bin數據的數組(如果存在)以及x0
和x1
值。在這一點上,分箱已完成,我所需要的是將所有數據堆疊起來,並獲得y0
和y1
的值。
所以,我打電話stack
,但它給了我垃圾; stackedHistData
是一個6的數組,每個數組都有一個0屬性,它等於0,一個屬性等於'NaN',而data
屬性具有這個91長的數組,索引和鍵(A到F)。我甚至沒有看到由堆棧調用產生的y0
和y1
值。這種直方圖數據如何使用?