chart = {
const svg = d3.create("svg")
.attr("viewBox", `0 0 ${width} ${height}`)
.attr("class", "svg-content")
svg.append("g")
.attr("class", "axis")
.attr('transform', `translate(0, ${height - margin.bottom})`)
.call(xAxis);
svg.append("g")
.attr("class", "axis")
.attr('transform', `translate(${margin.left}, 0)`)
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("x", - margin.top)
.attr("y", 16)
.style("text-anchor", "end")
.text("Closing Price");
const lines = svg.selectAll("lines")
.data(slices)
.enter()
.append("g")
lines.append("path")
.attr("class", d => `line-${d.num}`)
.attr("fill", "none")
.attr("stroke-width", 2)
.attr("d", d => line(d.values));
lines.append("text")
.attr("class","series-label")
.datum(d => {
return {
id: d.id,
value: d.values[d.values.length - 1]};
})
.attr("transform", d => {
return `translate( ${(xScale(d.value.date))}, ${(yScale(d.value.price))})`;})
.attr("x", 20)
.attr("y", 5)
.text(d => d.id);
lines.append("rect")
.datum(d => {
return {value: d.values[d.values.length - 1]};
})
.attr("transform", d => {
return `translate( ${xScale(d.value.date)}, ${yScale(d.value.price)})`;})
.attr("x", 5)
.attr("y", -5)
.attr("width", 10)
.attr("height", 10)
return svg.node();
}