chart = {
const width = 800;
const height = 400;
const margin = {top: 50, right:50, left: 50, bottom: 50}
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("width", width)
.attr("height", height)
.attr("style", "max-width: 100%; height: auto;");
const x = d3.scaleLinear()
.domain([1,5])
.range([margin['left'], width - margin['right']]);
const y = d3.scaleLinear()
.domain([4,1])
.range([height - margin['bottom'], margin['top']]);
svg.selectAll("axes")
.data(questions).enter()
.append("g")
.attr("transform", (d, i) => { return "translate(" + x(i+1) + ")"; })
.each(function(d) {
d3.select(this).call(
d3.axisLeft()
.scale(y)
.tickValues(y.ticks().filter(tick => Number.isInteger(tick)))
);
})
.append("text")
.style("text-anchor", "middle")
.attr("y", 0)
.text(function(d) { return d; })
.style("fill", "black")
const filter = ["ame", "gura"];
const colours = {
"ame": "#fcba03",
"gura": "#57b0ff",
"Fauna": "#43c465",
"Fuwawa": "#88e3f7",
"Mococo": "#f788d2",
}
const path = (d) => {
return d3.line()(d.map((p, i) => { return [x(i+1), y(p)]}))
}
const drawPaths = (name, data, colour) => {
svg.selectAll(`${name}-path`)
.data(data)
.join("path")
.attr("d", path)
.style("fill", "none")
.style("stroke", colour)
.style("opacity", 0.5);
}
const drawCircles = (data, colour) => {
data.forEach((questionAnswers, i) => {
svg
.selectAll(`${name}-q${i}-answer-circles`)
.data(questionAnswers)
.join("circle")
.attr("cx", x(i+1))
.attr("cy", (d, j) => {
console.log(i, j, d)
return y(j+1);
})
.attr("r", d => d)
.attr("fill", colour);
});
}
data.filter(e => filter.includes(e.name))
.forEach((e) => drawCircles(e.answerTimes, colours[e.name]));
return svg.node();
}