chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
svg.append("g")
.selectAll("g")
.data(series)
.enter().append("g")
.attr("fill", d => color(d.key))
.attr("data-legend", d => d.key)
.selectAll("rect")
.data(d => d)
.enter().append("rect")
.attr("width", x.bandwidth)
.attr("x", d => x(new Date(d.data.fordate)))
.attr("y", d => y(d[1]))
.attr("height", d => y(d[0]) - y(d[1]))
svg.append("g")
.call(xAxis);
// Append the Y-axis
svg.append("g")
.call(yAxis)
// Append X-axis label
// svg.append("text")
// .attr("class", "x-axis-label")
// .attr("transform", `translate(${width/2}, ${height - margin.top})`)
// .style("text-anchor", "middle")
// .text("Hour of the day")
// Append the legend
// svg.append("g")
// .attr("class", "legend_auto")
// .style('font-size', 12)
// .style('font-family', 'sans-serif')
// .attr("transform", "translate(650, 10)")
// .call(legend_auto)
const legend = svg.selectAll('.legend')
.data(color.domain())
.enter()
.append('g')
.attr('class', 'legend')
.attr('transform', (d, i) => {
let legendHeight = legendRectSize + legendSpacing;
let x = width - margin.right;
let y = i * legendHeight + margin.top;
return `translate(${x}, ${y})`;
});
legend.append('rect')
.attr('width', legendRectSize)
.attr('height', legendRectSize)
.style('fill', color)
.style('stroke', color);
legend.append('text')
.attr('x', legendRectSize + legendSpacing)
.attr('y', legendRectSize)
.text(d => d);
// Return generated svg
return svg.node();
}