chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
svg.append("g")
.attr("class", "grid")
.attr("transform", `translate(${x.bandwidth()/2}, ${height - margin.bottom})`)
.call(make_x_gridlines()
.tickSize(-height)
.tickFormat("")
)
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(d.data.hour))
.attr("y", d => y(d[1]))
.attr("height", d => y(d[0]) - y(d[1]))
svg.append("path")
.datum(line_series)
.attr("fill", "none")
.attr("stroke", "orange")
.attr("stroke-width", 2.5)
// .attr("stroke-linejoin", "round")
// .attr("stroke-linecap", "round")
.attr("d", line);
// Append the X-axis
svg.append("g")
.call(xAxis);
// Append the Y-axis
let y_axis = svg.append("g")
.attr("transform", `translate(${margin.left},0)`)
.attr("id", "y-axis")
.attr("class", "y axis")
.call(yAxis)
// Replace 0 tick on y-axis with -
y_axis.selectAll("g.tick")
.filter( d => d == 0)
.select("text")
.text("-");
// Y label
svg.append("g")
.attr("transform", `translate(${margin.left/2}, ${(height - margin.top)/2})`)
.attr("class", "y-axis-label")
.append("text")
.attr("transform", "rotate(-90)")
.text("mW");
// 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")
// Line chart legend
svg.append('rect')
.attr('x', width - margin.right)
.attr('y', margin.top)
.attr('width', legendRectSize)
.attr('height', legendRectSize)
.style('fill', 'orange')
.style('stroke', 'orange');
svg.append('text')
.attr("class", "legend-text")
.attr('x', width - margin.right + legendRectSize + legendSpacing)
.attr('y', margin.top + legendRectSize)
.text('SOC');
// Bar chart legends
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+1) * 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("class", "legend-text")
.attr('x', legendRectSize + legendSpacing)
.attr('y', legendRectSize)
.text(d => d);
// Return generated svg
return svg.node();
}