chart4 = {
const height = 300;
const padding = {left:35, right:30, top:40, bottom:25};
let max = 100;
let space_between_bar_boxes = 70;
let single_bar_width = 14;
let bar_box_width = (width - padding.left -padding.right)/7 - space_between_bar_boxes/2;
const x = d3.scaleLinear([0,7],[0 + padding.left, width - padding.right]);
const y = d3.scaleLinear([0, max_chr_len.y],[0+padding.top, height - padding.bottom - padding.top]);
const y_bars = d3.scaleLinear([0,max],[0 +padding.top ,height - padding.bottom]);
const x_bars = d3.scaleLinear([0,6],[0,bar_box_width]);
const chr_offset = {'X' : 0,'Y':3}
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("width", width)
.attr("height", height)
.attr("style", "max-width: 100%; height: auto;");
for ( const sp of species){
for (const chr of ["X", "Y"]){
let data = te_s.filter( x => (x.species == sp & x.chr ==chr));
data = data.filter(x => ["ancestral", "ampliconic","palindromes"].includes(x.region));
svg.selectAll(`.bar_${sp}_${chr}`)
.data(data)
.join("rect")
.attr("class", `bar_${sp}_${chr}`)
.attr("x", (d,i) => x(species.indexOf(sp)) + x_bars(i + chr_offset[chr]))
.attr("y", d => y_bars(0))
.attr("width", single_bar_width)
.attr("height", d => y_bars(d.value) - padding.top)
.attr("fill", d => colorScheme[density_name_to_seqclass[d.region]])
.attr("stroke", "black")
svg.append("rect")
.attr("x", x(species.indexOf(sp)) + x_bars(chr_offset[chr]))
.attr("y", height - padding.bottom + 9)
.attr("height",1)
.attr("width",single_bar_width *3 + 13)
svg.append("text")
.attr('x', x(species.indexOf(sp)) + x_bars(chr_offset[chr]) + 21)
.attr('y',height - padding.bottom + 25)
.text(chr)
.attr("font-family", "sans-serif")
.attr("font-size", 12)
}
svg.append("g")
.call(d3.axisLeft(y_bars))
.attr("transform",
`translate(${padding.left-10 + (space_between_bar_boxes/2 + bar_box_width)*species.indexOf(sp)},0)`);
}
svg.selectAll(".label")
.data(species)
.join("text")
.attr("class", "label")
.text( d => d)
.attr("x", d => x(species.indexOf(d) ) )
.attr("y", y(0)-10)
.attr("font-family", "sans-serif")
.attr("text-anchor", "middle");
return svg.node();
}