Skip to content

Commit

Permalink
Also display inactive months in chart
Browse files Browse the repository at this point in the history
  • Loading branch information
cmahnke committed Jan 5, 2025
1 parent 88641d9 commit 972ef97
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 10 deletions.
49 changes: 39 additions & 10 deletions assets/js/chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import * as echarts from 'echarts';

const defaultHeightVH = 50;
const addEmptyMonths = true

const colors = [
'#c23531',
Expand All @@ -18,6 +19,12 @@ const colors = [
'#c4ccd3'
];

function formatIndex(dateStr) {
const date = new Date(dateStr);
const month = `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}`;
return month;
}

async function loadCSVFromURL(url) {
try {
const response = await fetch(url);
Expand All @@ -39,18 +46,17 @@ async function loadCSVFromURL(url) {
}
}

const groupedData = {};
let groupedData = {};
data.forEach(row => {
const date = new Date(row['Date']);
const month = `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}`;

const month = formatIndex(row['Date'])
if (!groupedData[month]) {
groupedData[month] = {};
}
const category = row['Blog'];
groupedData[month][category] = (groupedData[month][category] || 0) + 1;
});

//return data[0].map((_, i) => data.map(row => row[i]));
return groupedData;
} catch (error) {
console.error('Error loading CSV from URL:', error);
Expand All @@ -64,16 +70,34 @@ function chart(csvFile, element, options) {
}
loadCSVFromURL(csvFile).then(data => {
const container = document.getElementById(element)
var width = container.offsetWidth;
let width = container.offsetWidth;
if (width === 0) {
width = container.parentElement.parentElement.offsetWidth;
}
var height = container.offsetHeight;
let height = container.offsetHeight;
if (height === 0) {
height = Math.round(window.innerHeight / 100) * defaultHeightVH;
}

const xAxisData = Object.keys(data);
let xAxisData = [];
if (addEmptyMonths) {
const filledData = {};
const first = new Date(Object.keys(data)[0]);
const last = new Date(Object.keys(data).slice(-1));
[...Array(last.getFullYear() - first.getFullYear()).keys()].forEach((i) => {
const y = first.getFullYear() + i;
[...Array(12).keys()].forEach((m) => {
if ((y == first.getFullYear() && m < first.getMonth()) || (y == last.getFullYear() && m > last.getMonth())) {
return
}
let missingMonth = `${y}-${(m + 1).toString().padStart(2, '0')}`
xAxisData.push(formatIndex(missingMonth))
});
});
} else {
xAxisData = Object.keys(data);
}

const seriesData = [];
let colorIndex = 0;
const blogs = new Set(Object.keys(data).map((key) => { return Object.keys(data[key])[0] }));
Expand All @@ -83,7 +107,12 @@ function chart(csvFile, element, options) {
name: blog,
type: 'bar',
stack: 'total',
data: xAxisData.map(month => data[month][blog] || 0),
data: xAxisData.map(month => {
if (month in data) {
return data[month][blog] || 0
}
return 0;
}),
itemStyle: {
color: colors[colorIndex % colors.length]
},
Expand All @@ -94,7 +123,7 @@ function chart(csvFile, element, options) {
colorIndex++;
}

var chartOptions = {
let chartOptions = {
color: colors,
legend: {
right: '15%',
Expand Down Expand Up @@ -141,7 +170,7 @@ function chart(csvFile, element, options) {
chartOptions = {...chartOptions, ...options};
}

var chart = echarts.init(container, null, { renderer: "svg", width: width, height: height});
let chart = echarts.init(container, null, { renderer: "svg", width: width, height: height});
chart.setOption(chartOptions);
});
}
Expand Down
56 changes: 56 additions & 0 deletions static/images/cm-solid.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 972ef97

Please sign in to comment.