calendarview1 = {
const svg = d3.select(DOM.svg(width, height2 * yearsdatapreds.length+40))
.style("font", "10px sans-serif")
.style("width", "100%")
.style("height", "auto")
.attr("text-anchor", "middle");
const year = svg.selectAll("g")
.data(yearsdatapreds)
.join("g")
.attr("transform", (d, i) => `translate(40,${height2 * i + cellSize * 1.5})`);
year.append("text")
.attr("x", -5)
.attr("y", -5)
.attr("font-weight", "bold")
.attr("text-anchor", "end")
.text(d => d.key);
year.append("g")
.attr("text-anchor", "end")
.selectAll("text")
.data(( d3.range(1, 13) ).map(i => new Date(1995, i, 0)))
.join("text")
.attr("x", d => (countMonth(d) + 1.5) * cellSize)
.attr("y", -5)
.attr("dx", "0.31em")
.text(formatMonth);
year.append("g")
.selectAll("rect")
.data(d => d.values)
.join("rect")
.attr("width", cellSize - 0.5)
.attr("height", cellSize - 0.5)
.attr("x", (d,i) => (i*2+ 1.5) * cellSize-5) // Use the index i for each month in each year grouping
.attr("y", 10)
.attr("fill", d => color(d.Price))
.attr("stroke",(function(d) {
if(d.Month > "2019-03") { // Add condition to
return "lightblue"}`;
} else {
return "darkgrey"}`;
}))
.attr("stroke-width",(function(d) {
if(d.Month > "2019-03") { // Add condition to
return 5}`;
} else {
return 3}`;
}))
.append("title")
.text(function(d) {
if(d.Month > "2019-03") {
return `${"Date: "+d.Month+"\nForecast Price: $"+Math.round(d.Price)}`; // Add condition to add Forecast to label and round Price to whole number
} else {
return `${"Date: "+d.Month+"\nPrice: $"+ Math.round(d.Price)}`
}});
//Add Title
svg.append("text")
.attr("transform", `translate(${height2 * yearsdata.length+10},${height2 * yearsdata.length+70})`)
.style("text-anchor", "middle")
.style("font-weight", 700)
.text("NZ Monthly Weighted Average Retail and Forecast Price of 1 kg Avocados (NZD$)");
const boxes = year.append("g")
.selectAll("rect")
.data(d => d.values)
.join("rect")
.attr("width", cellSize - 0.5)
.attr("height", cellSize - 0.5)
.attr("x", (d,i) => (i*2+ 1.5) * cellSize-5) // Use the index i for each month in each year grouping
.attr("y", 10)
.attr("fill", d => color(d.Price))
.attr("stroke",(function(d) {
if(d.Month > "2019-03") { // Add condition to
return "lightblue"}`;
} else {
return "white"}`;
}))
.attr("stroke-width",(function(d) {
if(d.Month > "2019-03") { // Add condition to
return 6}`;
} else {
return 3}`;
}))
;
yield svg.node();
await boxes.transition()
.duration(1000)
.attr("stroke",(function(d) {
if(d.Month > "2019-03") { // Add condition to
return "lightblue"}`;
} else {
return "white"}`;
}))
.end();
while (true) {
yield svg.node();
await boxes.transition()
.duration(200)
.attr("stroke",(function(d) {
if(d.Month > "2019-03") { // Add condition to
return "yellow"}`;
} else {
return "white"}`;
}))
.end();
}
return svg.node();
}