chart2 = function(types) {
const n = types.length;
const width = 860;
const Allspecies = [
...new Set(
heatData.filter(d => types.includes(d.class)).map(d => d.species)
)
];
const height = Allspecies.length * 20;
const to_chart_species = [
...new Set(heatData.filter(d => d.class === types[0]).map(d => d.species))
];
const margin1 = { top: 20, right: 40, bottom: 40, left: 140 };
const chart_width = width - margin1.left - margin1.right;
let margin_others;
const svg = d3.select(DOM.svg(width, height));
const today = new Date();
const startExt = new Date();
const endExt = new Date();
startExt.setDate(today.getDate() - 1);
endExt.setDate(today.getDate() + 1);
let myVariables = {};
let myLines = {};
let myYs = {};
let myXs = {};
let y;
const x = d3
.scaleTime()
.range([0, chart_width])
.domain([
d3.min(heatData, d => d.monthly),
d3.timeMonth.ceil(+d3.max(heatData, d => d.monthly) + 1)
]);
const xAxis = d3.axisBottom(x);
const top_margins = [];
types.forEach((vari, i) => {
let yside = "y" + i;
const species = [
...new Set(heatData.filter(d => d.class === vari).map(d => d.species))
];
let h_each_plot = (height * species.length) / Allspecies.length;
top_margins.push(h_each_plot);
let chart_bounds =
(height * species.length) / Allspecies.length -
margin1.top -
margin1.bottom;
y = d3
.scaleBand()
.domain(species)
.range([chart_bounds, 0])
.padding(.2);
const yAxis = g =>
g.call(d3.axisLeft(y)).call(g => g.select(".domain").remove());
const mar_top =
i === 0
? 0
: i === 1
? top_margins[0]
: d3.sum(top_margins.slice(0, [i]));
margin_others = {
top: mar_top,
right: 20,
left: 140
};
let variableName = "focus" + i;
myVariables[variableName] = svg
.append("g")
.attr("class", "focus" + i)
.attr(
"transform",
`translate(${margin_others.left},${margin_others.top})`
);
myVariables[variableName]
.selectAll('rect')
.data(heatData.filter(d => d.class === vari))
.enter()
.append('rect')
.attr("class", "line" + i)
.attr('x', d => x(d.monthly))
.attr('y', d => {
return y(d.species);
})
.attr(
'width',
d => x(d3.timeMonth.ceil(+d.monthly + 1)) - x(d.monthly) - 4
)
.attr('height', y.bandwidth())
.attr('fill', d => {
return d.value > 0 ? "rgb(201, 37, 41)" : "#ddd";
})
.attr("clip-path", "url(#clip)");
myVariables[variableName]
.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + chart_bounds + ")")
.call(xAxis);
myVariables[variableName]
.append("g")
.attr("class", "axis axis--y")
.call(yAxis);
});
return svg.node();
}