org-report-stats/render_chart.ts

118 lines
3.5 KiB
TypeScript

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/'
const width = 1000 // define width and height of canvas
const height = 1000
const typeOfRender = 'png'
const chartType = 'line'
// time in : weeks, months, years, days
const time_agreggate = 'days'
// const time_agreggate = 'weeks'
// const time_agreggate = 'months'
// const time_agreggate = 'years'
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: [],
backgroundColor: 'rgb(81,148,84)',
fill: {
target: 'origin',
above: 'rgb(81,148,84)',
below: 'rgb(115,190,75)',
}
}
let dataSets_created = {
label: 'Created tasks',
data: [],
backgroundColor: 'rgb(69,219,255)',
fill: {
target: 'origin',
above: 'rgb(69,219,255)',
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) {
dataSets_done.data.push(info.closed)
} 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)
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));