chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height * years.length])
.attr("font-family", "sans-serif")
.attr("font-size", 10);
const year = svg.selectAll("g")
.data(years)
.join("g")
.attr("transform", function(d,i){
if(type == "calendar"){
return `translate(40.5,${height * i + cellSize * 1.5})`
} else {
return `translate(40.5,${height * i + cellSize * 8})`
}
})
.attr("id",d => d[0])
year.append("text")
.attr("x", -5)
.attr("y", -5)
.attr("font-weight", "bold")
.attr("text-anchor", "end")
.text(([key]) => key);
year.append("g")
.attr("text-anchor", "end")
.selectAll("text")
.data((weekday === "weekday" ? d3.range(2, 7) : d3.range(7)).map(i => new Date(1995, 0, i)))
.join("text")
.attr("x", -5)
.attr("y", d => (countDay(d) + 0.5) * cellSize)
.attr("dy", "0.31em")
.text((type === "calendar" ? formatDay : ""))
year.append("g")
.selectAll("rect")
.data(weekday === "weekday"
? ([, values]) => values.filter(d => ![0, 6].includes(d.date.getUTCDay()))
: ([, values]) => values)
.enter().append("rect")
.attr("width", function(d,i){
if(type == "calendar"){
return cellSize - 1
} else {
return 1.7
}
})
.attr("height", function(d){
if(type == "calendar"){
return cellSize - 1
} else {
return d.value * (125/max)
}
})
.attr("x", function(d,i){
if(type == "calendar"){
return timeWeek.count(d3.utcYear(d.date), d.date) * cellSize + 0.5
} else {
return i * 2.2
}
})
.attr("y", function(d){
if(type == "calendar"){
return countDay(d.date) * cellSize + 0.5
} else {
return d.value * -(125/max)
}
})
// .attr("fill", "#eee")
.attr("fill", d => color(d.value))
.on("mouseover", d => getDays(d))
// .transition().duration(1000)
// .attr("fill", d => color(d.value))
const month = year.append("g")
.selectAll("g")
.data(([, values]) => d3.utcMonths(d3.utcMonth(values[0].date), values[values.length - 1].date))
.join("g");
month.filter((d, i) => i).append("path")
.attr("fill", "none")
.attr("stroke", "#fff")
.attr("stroke-width", 3)
.attr("d", pathMonth);
month.append("text")
.attr("x", d => timeWeek.count(d3.utcYear(d), timeWeek.ceil(d)) * cellSize + 2)
.attr("y", -5)
.text(formatMonth);
const day_2018 = svg.append("rect")
.attr("width", cellSize - 1)
.attr("height", cellSize - 1)
.attr("x",-100)
.attr("stroke", "#000")
.attr("stroke-width", 2)
.attr("fill", "none")
const day_2019 = svg.append("rect")
.attr("width", cellSize - 1)
.attr("height", cellSize - 1)
.attr("x",-100)
.attr("stroke", "#000")
.attr("stroke-width", 2)
.attr("fill", "none")
const day_2020 = svg.append("rect")
.attr("width", cellSize - 1)
.attr("height", cellSize - 1)
.attr("x",-100)
.attr("stroke", "#000")
.attr("stroke-width", 2)
.attr("fill", "none")
function getDays(d){
var date = d.date,
value = d.value
console.log(date,value)
if(type === "calendar"){
day_2018.attr("x", 40.5+(timeWeek.count(d3.utcYear(date), date) * cellSize + 0.5))
day_2018.attr("y", (height * 2 + cellSize * 1.5)+countDay(date) * cellSize + 0.5)
day_2019.attr("x", 40.5+(timeWeek.count(d3.utcYear(date), date) * cellSize + 0.5))
day_2019.attr("y", (height * 1 + cellSize * 1.5)+countDay(date) * cellSize + 0.5)
day_2020.attr("x", 40.5+(timeWeek.count(d3.utcYear(date), date) * cellSize + 0.5))
day_2020.attr("y", (height * 0 + cellSize * 1.5)+countDay(date) * cellSize + 0.5)
} else {
day_2018.attr("x", -100)
day_2019.attr("x", -100)
day_2020.attr("x", -100)
}
}
// function updateBar(){
// //Update all rects
// //console.log("update")
// svg.selectAll("rect")
// .data(data)
// .transition()
// .duration(2000)
// .attr("fill", d => color(d.value))
// }
// updateBar()
return svg.node();
}