2023-09-22 15:47:12 +02:00
|
|
|
import {ChartCallback, ChartJSNodeCanvas} from 'chartjs-node-canvas';
|
|
|
|
import {Chart} from 'chart.js';
|
|
|
|
import {promises as fs} from 'fs';
|
|
|
|
import * as dataOrgmode from '/home/tykayn/Nextcloud/ressources/social sorting/output/export_all_tasks.org_parsed.json'
|
|
|
|
|
|
|
|
const ouput_folder = '/home/tykayn/Nextcloud/ressources/social sorting/output/'
|
2023-09-22 16:09:39 +02:00
|
|
|
const width = 1000 // define width and height of canvas
|
|
|
|
const height = 1000
|
|
|
|
const typeOfRender = 'png'
|
|
|
|
const chartType = 'line'
|
|
|
|
|
2023-09-22 15:47:12 +02:00
|
|
|
// time in : weeks, months, years, days
|
|
|
|
|
|
|
|
// const time_agreggate = 'days'
|
|
|
|
// const time_agreggate = 'weeks'
|
2023-09-22 16:09:39 +02:00
|
|
|
// const time_agreggate = 'months'
|
|
|
|
const time_agreggate = 'years'
|
2023-09-22 15:47:12 +02:00
|
|
|
function convertStats(tasks: any) {
|
|
|
|
let converted = []
|
|
|
|
let weeks = Object.keys(tasks['statistics']['statistics']['dates'][time_agreggate]);
|
|
|
|
|
|
|
|
let labels = []
|
|
|
|
let dataSets_done = {
|
|
|
|
label: 'Done tasks',
|
|
|
|
data: [],
|
2023-09-22 16:09:39 +02:00
|
|
|
backgroundColor: 'rgb(81,148,84)',
|
2023-09-22 15:47:12 +02:00
|
|
|
fill: {
|
|
|
|
target: 'origin',
|
2023-09-22 16:09:39 +02:00
|
|
|
above: 'rgb(81,148,84)',
|
2023-09-22 15:47:12 +02:00
|
|
|
below: 'rgb(115,190,75)',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let dataSets_created = {
|
|
|
|
label: 'Created tasks',
|
|
|
|
data: [],
|
2023-09-22 16:09:39 +02:00
|
|
|
backgroundColor: 'rgb(69,219,255)',
|
2023-09-22 15:47:12 +02:00
|
|
|
fill: {
|
|
|
|
target: 'origin',
|
2023-09-22 16:09:39 +02:00
|
|
|
above: 'rgb(69,219,255)',
|
2023-09-22 15:47:12 +02:00
|
|
|
below: 'rgb(115,190,75)',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
weeks.forEach(time_period => {
|
|
|
|
|
|
|
|
if (time_period !== 'Nan-Nan') {
|
|
|
|
labels.push(time_period)
|
|
|
|
let info = tasks['statistics']['statistics']['dates'][time_agreggate][time_period]
|
|
|
|
if (info.created) {
|
|
|
|
dataSets_created.data.push(info.created)
|
|
|
|
} else {
|
|
|
|
dataSets_created.data.push(0)
|
|
|
|
}
|
|
|
|
if (info.closed) {
|
2023-09-22 16:09:39 +02:00
|
|
|
dataSets_done.data.push(info.closed)
|
2023-09-22 15:47:12 +02:00
|
|
|
} else {
|
|
|
|
dataSets_created.data.push(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// console.log('weeks', weeks)
|
|
|
|
return {
|
|
|
|
labels,
|
|
|
|
dataSets: {
|
|
|
|
dataSets_created,
|
|
|
|
dataSets_done,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let convertedStats = convertStats(dataOrgmode)
|
|
|
|
console.log('convertedStats', convertedStats)
|
|
|
|
|
2023-09-22 16:09:39 +02:00
|
|
|
|
2023-09-22 15:47:12 +02:00
|
|
|
|
|
|
|
async function main(): Promise<void> {
|
|
|
|
|
|
|
|
const configurationWithData: any = {
|
|
|
|
type: chartType,
|
|
|
|
title: {
|
|
|
|
display: true,
|
|
|
|
text: 'Custom Chart Title'
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
labels: convertedStats.labels,
|
|
|
|
datasets:
|
|
|
|
[
|
|
|
|
convertedStats.dataSets.dataSets_created,
|
|
|
|
convertedStats.dataSets.dataSets_done,
|
|
|
|
]
|
|
|
|
},
|
|
|
|
options: {},
|
|
|
|
plugins: [{
|
|
|
|
id: 'background-colour',
|
|
|
|
beforeDraw: (chart: Chart): void => {
|
|
|
|
const ctx = chart.ctx;
|
|
|
|
ctx.save();
|
|
|
|
ctx.fillStyle = 'white';
|
|
|
|
ctx.fillRect(0, 0, width, height);
|
|
|
|
ctx.restore();
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
const chartCallback: ChartCallback = (ChartJS: any) => {
|
|
|
|
ChartJS.defaults.responsive = true;
|
|
|
|
ChartJS.defaults.borderColor = '#36a2eb';
|
|
|
|
ChartJS.defaults.maintainAspectRatio = false;
|
|
|
|
};
|
|
|
|
const chartJSNodeCanvas = new ChartJSNodeCanvas({width, height, chartCallback});
|
|
|
|
const buffer = await chartJSNodeCanvas.renderToBuffer(configurationWithData);
|
|
|
|
let outpath = ouput_folder + 'tasks_in_'+time_agreggate+'-created_and_done_' + chartType + ' -- chart.'+typeOfRender
|
|
|
|
|
|
|
|
await fs.writeFile(outpath, buffer, 'base64');
|
|
|
|
console.log('wrote graph ',outpath
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
main().then(r => console.log(r));
|