2019-11-20 17:04:29 +01:00
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<script>
|
2019-11-21 11:23:20 +01:00
|
|
|
const fetch_from_API = async ( endpoint, params = {} ) => {
|
|
|
|
let url = new URL( endpoint, `${location.protocol}//${location.host}` );
|
|
|
|
url.search = new URLSearchParams( params );
|
|
|
|
|
|
|
|
const response = await fetch( url );
|
|
|
|
|
|
|
|
return await response.json();
|
|
|
|
};
|
|
|
|
|
2019-11-20 17:04:29 +01:00
|
|
|
const API = {
|
2019-11-21 11:23:20 +01:00
|
|
|
balance: (period, categories, depth) => fetch_from_API( "/api/ledger/balance",
|
|
|
|
{ period: period,
|
|
|
|
categories: categories,
|
|
|
|
depth: depth } ),
|
|
|
|
|
|
|
|
register: (period, categories) => fetch_from_API( "/api/ledger/register",
|
|
|
|
{ period: period,
|
|
|
|
categories: categories } ),
|
2019-11-20 17:04:29 +01:00
|
|
|
|
2019-11-21 11:23:20 +01:00
|
|
|
graph_values: (period, categories, granularity) => fetch_from_API( "/api/ledger/graph_values",
|
|
|
|
{ period: period,
|
|
|
|
categories: categories,
|
|
|
|
granularity: granularity } ),
|
|
|
|
|
|
|
|
accounts: () => fetch_from_API( "/api/ledger/accounts" )
|
2019-11-20 17:04:29 +01:00
|
|
|
}
|
2019-11-21 11:23:20 +01:00
|
|
|
|
|
|
|
const Utils = {
|
|
|
|
text_to_color: ( text ) => {
|
|
|
|
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}`;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const UI = {
|
|
|
|
donut: ( element, dataset ) => {
|
|
|
|
let filed_percent = 0;
|
|
|
|
const data_to_donut_segment = ( data ) => {
|
|
|
|
const stroke_dashoffset = ( percent ) => {
|
|
|
|
let offset = ( 100 - filed_percent ) + 25;
|
|
|
|
|
|
|
|
return offset > 100 ? offset - 100 : offset;
|
|
|
|
};
|
|
|
|
|
|
|
|
const donut_segment = `<circle class="donut-segment" cx="21" cy="21" r="15.91549430918954" fill="transparent" stroke="${data.color}" stroke-width="3" stroke-dasharray="${data.percent} ${100 - data.percent}" stroke-dashoffset="${stroke_dashoffset( data.percent )}"></circle>`;
|
|
|
|
filed_percent += data.percent;
|
|
|
|
|
|
|
|
return donut_segment;
|
|
|
|
};
|
|
|
|
|
|
|
|
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>
|
|
|
|
<circle class="donut-ring" cx="21" cy="21" r="15.91549430918954" fill="transparent" stroke="#d2d3d4" stroke-width="3"></circle>
|
|
|
|
|
|
|
|
${dataset.map( line => data_to_donut_segment( line ) ).join("")}
|
|
|
|
</svg><pre>${JSON.stringify( dataset )}</pre>`;
|
|
|
|
}
|
|
|
|
};
|
2019-11-20 17:04:29 +01:00
|
|
|
</script>
|
|
|
|
</head>
|
|
|
|
<body>
|
2019-11-21 11:23:20 +01:00
|
|
|
<div id="donut"></div>
|
|
|
|
|
2019-11-20 17:04:29 +01:00
|
|
|
<script>
|
2019-11-21 11:23:20 +01:00
|
|
|
const period = (new Date()).toISOString().split("T")[0].slice( 0, -3 );
|
|
|
|
API.balance( period, ["Expenses"].join(" "), 2 )
|
|
|
|
.then( balance => {
|
|
|
|
const total = balance.reduce( (memo, line) => memo + line.amount, 0 );
|
|
|
|
|
|
|
|
UI.donut( document.getElementById( "donut" ),
|
|
|
|
balance.map( line => {
|
|
|
|
line.color = Utils.text_to_color( line.account );
|
|
|
|
line.percent = ( line.amount / total ) * 100;
|
2019-11-20 17:04:29 +01:00
|
|
|
|
2019-11-21 11:23:20 +01:00
|
|
|
return line;
|
|
|
|
} ) );
|
|
|
|
} );
|
2019-11-20 17:04:29 +01:00
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|