From cff645a9cf042f4e0cec929df3c5d54609ebb07b Mon Sep 17 00:00:00 2001 From: Gwenhael Le Moine Date: Thu, 21 Nov 2019 12:38:49 +0100 Subject: [PATCH] donut's tooltip; period navigation --- public/pure.html | 80 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 22 deletions(-) diff --git a/public/pure.html b/public/pure.html index 5abdd76..a57c96f 100644 --- a/public/pure.html +++ b/public/pure.html @@ -43,48 +43,84 @@ }; const UI = { + /* https://medium.com/@heyoka/scratch-made-svg-donut-pie-charts-in-html5-2c587e935d72 */ donut: ( element, dataset ) => { + const thickness = 9; let filed_percent = 0; const data_to_donut_segment = ( data ) => { - const stroke_dashoffset = ( percent ) => { - let offset = ( 100 - filed_percent ) + 25; + if ( data.amount > 0 ) { + const stroke_dashoffset = ( percent ) => { + let offset = 100 - filed_percent; - return offset > 100 ? offset - 100 : offset; - }; + return offset > 100 ? offset - 100 : offset; + }; - const donut_segment = ``; - filed_percent += data.percent; + const donut_segment = `${data.tooltip}`; + filed_percent += data.percent; - return donut_segment; + return donut_segment; + } else + return ''; }; element.innerHTML = ` - + ${dataset.map( line => data_to_donut_segment( line ) ).join("")}
${JSON.stringify( dataset )}
`; } }; + + let current_period; + const Period = { + set: ( period ) => { + current_period = period; + document.querySelector( "#period #display" ).innerHTML = current_period.toISOString(); + + monthly( current_period.toISOString().split("T")[0].slice( 0, -3 ), "#month" ); + }, + get: () => current_period, + prev: () => { + current_period.setMonth( current_period.getMonth() - 1 ); + Period.set( current_period ); + }, + next: () => { + current_period.setMonth( current_period.getMonth() + 1 ); + Period.set( current_period ); + }, + }; + + const monthly = ( month, element_selector ) => { + API.balance( month, ["Expenses"].join(" "), 3 ) + .then( balance => { + const total = balance.reduce( (memo, line) => memo + line.amount, 0 ); + + UI.donut( document.querySelector( `${element_selector} #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; + } ) ); + } ); + }; -
+
+ + +

+
+
+
+