credger/public/pure.html

154 lines
6.8 KiB
HTML
Raw Normal View History

2019-11-20 17:04:29 +01:00
<html>
<head>
2019-11-21 12:38:35 +01:00
<meta charset="UTF-8">
2019-11-20 17:04:29 +01:00
<script>
2019-11-21 11:23:20 +01:00
2019-11-25 16:40:14 +01:00
const API = {
fetch: async ( endpoint, params = {} ) => {
let url = new URL( endpoint, `${location.protocol}//${location.host}` );
url.search = new URLSearchParams( params );
2019-11-21 11:23:20 +01:00
2019-11-25 16:40:14 +01:00
const response = await fetch( url );
2019-11-21 11:23:20 +01:00
2019-11-25 16:40:14 +01:00
return await response.json();
},
balance: ( period, categories, depth ) => API.fetch( "/api/ledger/balance", { period: period, categories: categories, depth: depth } ),
register: ( period, categories ) => API.fetch( "/api/ledger/register", { period: period, categories: categories } ),
graph_values: ( period, categories, granularity ) => API.fetch( "/api/ledger/graph_values", { period: period, categories: categories, granularity: granularity } ),
accounts: () => API.fetch( "/api/ledger/accounts" )
2019-11-20 17:04:29 +01:00
}
2019-11-21 11:23:20 +01:00
const Utils = {
2019-11-25 16:40:14 +01:00
text_to_color: text => {
2019-11-21 11:23:20 +01:00
let hash = 0
for (let i = 0; i < text.length; i++)
hash = (((hash << 5) - hash) + text.charCodeAt(i)) | 0;
hash = Math.abs( hash );
let shash = hash.toString(16).substr(0, 6).padEnd( 6, '0' );
return `#${shash}`;
2019-11-25 16:40:14 +01:00
},
readable_date: date => {
const months = { 0: 'Janvier', 1: 'Février', 2: 'Mars', 3: 'Avril', 4: 'Mai', 5: 'Juin', 6: 'Juillet', 7: 'Août', 8: 'Septembre', 9: 'Octobre', 10: 'Novembre', 11: 'Décembre' };
return `${months[ date.getMonth() ]} ${date.getFullYear()}`;
2019-11-21 11:23:20 +01:00
}
};
const UI = {
2019-11-21 12:38:49 +01:00
/* https://medium.com/@heyoka/scratch-made-svg-donut-pie-charts-in-html5-2c587e935d72 */
2019-11-21 11:23:20 +01:00
donut: ( element, dataset ) => {
2019-11-21 12:38:49 +01:00
const thickness = 9;
2019-11-21 11:23:20 +01:00
let filed_percent = 0;
const data_to_donut_segment = ( data ) => {
2019-11-21 12:38:49 +01:00
if ( data.amount > 0 ) {
const stroke_dashoffset = ( percent ) => {
let offset = 100 - filed_percent;
2019-11-21 11:23:20 +01:00
2019-11-21 12:38:49 +01:00
return offset > 100 ? offset - 100 : offset;
};
2019-11-21 11:23:20 +01:00
2019-11-21 12:38:49 +01:00
const donut_segment = `<circle class="donut-segment ${data.account.split(':').join(' ')}" cx="21" cy="21" r="15.91549430918954" fill="transparent" stroke="${data.color}" stroke-width="${thickness}" stroke-dasharray="${data.percent} ${100 - data.percent}" stroke-dashoffset="${stroke_dashoffset( data.percent )}"><title>${data.tooltip}</title></circle>`;
filed_percent += data.percent;
2019-11-21 11:23:20 +01:00
2019-11-21 12:38:49 +01:00
return donut_segment;
2019-11-25 16:40:14 +01:00
} else {
2019-11-21 12:38:49 +01:00
return '';
2019-11-25 16:40:14 +01:00
}
2019-11-21 11:23:20 +01:00
};
element.innerHTML = `<svg width="100%" height="100%" viewBox="0 0 42 42" class="donut">
<circle class="donut-hole" cx="21" cy="21" r="15.91549430918954" fill="#fff"></circle>
2019-11-21 12:38:49 +01:00
<circle class="donut-ring" cx="21" cy="21" r="15.91549430918954" fill="transparent" stroke="#d2d3d4" stroke-width="${thickness}"></circle>
2019-11-21 11:23:20 +01:00
${dataset.map( line => data_to_donut_segment( line ) ).join("")}
2019-11-21 15:37:07 +01:00
</svg>`;
2019-11-25 15:48:35 +01:00
}
};
2019-11-21 12:38:49 +01:00
2019-11-25 16:02:09 +01:00
let selected_accounts = [];
2019-11-25 15:48:35 +01:00
let current_period;
2019-11-21 15:37:07 +01:00
2019-11-25 15:48:35 +01:00
const Controls = {
period: {
init: () => Controls.period.set( new Date() ),
2019-11-25 16:02:09 +01:00
set: period => {
2019-11-25 15:48:35 +01:00
current_period = period;
2019-11-25 16:40:14 +01:00
document.querySelector( "#period #display" ).innerHTML = Utils.readable_date( current_period );
2019-11-25 15:48:35 +01:00
2019-11-25 16:02:09 +01:00
monthly( current_period, selected_accounts );
2019-11-21 15:37:07 +01:00
},
get: () => current_period,
prev: () => {
2019-11-25 15:48:35 +01:00
current_period.setMonth( current_period.getMonth() - 1 );
Controls.period.set( current_period );
2019-11-21 15:37:07 +01:00
},
next: () => {
2019-11-25 15:48:35 +01:00
current_period.setMonth( current_period.getMonth() + 1 );
Controls.period.set( current_period );
2019-11-21 15:37:07 +01:00
},
2019-11-25 15:48:35 +01:00
},
accounts: {
2019-11-25 11:51:02 +01:00
init: async () => {
2019-11-25 16:02:09 +01:00
let account_to_option = ( account, selected ) => `<option value="${account.join(':')}" ${selected ? "selected" : ""}>${account.join(':')}</option>`;
2019-11-25 15:48:35 +01:00
let accounts = await API.accounts();
let select = document.querySelector("select#accounts");
select.innerHTML = '';
for ( let i = 1 ; i < accounts.reduce( (memo, a) => a.length > memo ? a.length : memo, 0 ) ; i++ ) {
2019-11-25 16:02:09 +01:00
select.innerHTML += `<optgroup label="Depth: ${i}">${accounts.filter( a => a.length == i ).map( account => account_to_option( account, i == 1 )).join('')}</optgroup>`;
2019-11-25 15:48:35 +01:00
}
},
2019-11-25 16:02:09 +01:00
onchange: accounts_selected => {
selected_accounts = accounts_selected;
monthly( current_period, selected_accounts );
2019-11-25 11:51:02 +01:00
}
}
2019-11-21 12:38:49 +01:00
};
2019-11-25 16:40:14 +01:00
const monthly = async ( period, categories ) => {
let balance = await API.balance( period.toISOString().split("T")[0].slice( 0, -3 ), categories.join(" "), 999 )
const total = balance.reduce( (memo, line) => memo + line.amount, 0 );
UI.donut( document.querySelector( `#month #donut` ),
balance.sort( (a, b) => b.amount - a.amount )
.map( line => {
line.color = Utils.text_to_color( line.account );
line.percent = ( line.amount / total ) * 100;
line.tooltip = `${line.account} : ${line.amount} €`;
return line;
} ) );
console.log( await API.graph_values( "", selected_accounts.join(" "), "monthly" ) )
2019-11-21 12:38:49 +01:00
};
2019-11-20 17:04:29 +01:00
</script>
</head>
<body>
2019-11-21 15:37:07 +01:00
<div id="controls">
<div id="period">
<button onclick="Controls.period.prev()">-</button>
<button onclick="Controls.period.next()">+</button>
<h3 id="display"></h3>
</div>
2019-11-21 12:38:49 +01:00
</div>
2019-11-25 15:48:35 +01:00
<select id="accounts" name="accounts" multiple size="20" onchange="Controls.accounts.onchange( Array.from( this.options ).filter( o => o.selected ).map( o => o.value ) )"></select>
2019-11-21 12:38:49 +01:00
<div id="month">
<div id="donut" style="height: 256; width: 256;"></div>
</div>
2019-11-21 11:23:20 +01:00
2019-11-20 17:04:29 +01:00
<script>
2019-11-25 11:51:02 +01:00
(async () => {
await Controls.accounts.init();
2019-11-25 15:48:35 +01:00
await Controls.period.init();
2019-11-25 11:51:02 +01:00
})();
2019-11-20 17:04:29 +01:00
</script>
</body>
</html>