Skip to content

Commit

Permalink
extract statement from into a separate functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ramsayleung committed Dec 11, 2024
1 parent 1b5f4c9 commit b5ff9eb
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions layouts/shortcodes/heatmap.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
postsByDate.set(date, data);
{{- end -}}

years = Array.from(years).sort().reverse();
return [postsByDate, years];
}

Expand All @@ -162,6 +163,21 @@
myChart.setOption(option);
}

function renderYearSelector(years){
// Populate year selector
var yearSelector = document.getElementById('yearSelector');
years.forEach(year => {
var option = document.createElement('option');
option.value = year;
option.text = year;
yearSelector.appendChild(option);
});

yearSelector.addEventListener('change', function() {
updateHeatmap(this.value);
});
}


// Watch for theme changes
const observer = new MutationObserver((mutations) => {
Expand All @@ -179,23 +195,8 @@
});

var option;
//var postsByDate = new Map();
//var years = new Set();
var [postsByDate, years] = populatePostData();

// Populate year selector
years = Array.from(years).sort().reverse();
var yearSelector = document.getElementById('yearSelector');
years.forEach(year => {
var option = document.createElement('option');
option.value = year;
option.text = year;
yearSelector.appendChild(option);
});

yearSelector.addEventListener('change', function() {
updateHeatmap(this.value);
});
const [postsByDate, years] = populatePostData();
const latestYear = years && years.length > 0 ? years[0]: new Date().getFullYear();

option = {
title: { show: false },
Expand Down Expand Up @@ -259,10 +260,12 @@
type: 'heatmap',
coordinateSystem: 'calendar',
calendarIndex: 0,
data: getHeatmapData(years[0])
data: getHeatmapData(latestYear)
}
};

renderYearSelector(years);

// Initial chart initialization
initOrUpdateChart();
</script>
Expand Down

0 comments on commit b5ff9eb

Please sign in to comment.