Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pie chart #1467

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions hledger-web/Hledger/Web/Foundation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ instance Yesod App where
addScript $ StaticR js_jquery_hotkeys_js
addScript $ StaticR js_jquery_flot_min_js
addScript $ StaticR js_jquery_flot_selection_min_js
addScript $ StaticR js_jquery_flot_pie_min_js
addScript $ StaticR js_jquery_flot_time_min_js
addScript $ StaticR js_jquery_flot_tooltip_min_js
toWidget [hamlet| \<!--[if lte IE 8]> <script type="text/javascript" src="@{StaticR js_excanvas_min_js}"></script> <![endif]--> |]
Expand Down
36 changes: 35 additions & 1 deletion hledger-web/Hledger/Web/Handler/RegisterR.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

module Hledger.Web.Handler.RegisterR where

import Data.List (intersperse, nub, partition)
import Data.List (intersperse, nub, partition, sortOn)
import qualified Data.Text as T
import Text.Hamlet (hamletFile)

Expand Down Expand Up @@ -51,6 +51,12 @@ getRegisterR = do
| isJust (inAccount qopts) = "Period Total"
| otherwise = "Total"
transactionFrag = transactionFragment j
(chartQuery, depth) =
case (,) <$> inAccount qopts <*> inAccountQuery qopts of
Nothing -> (Any, 0)
Just ((an, _), aq) -> (aq, accountNameLevel an)
rspecWithQuery = rspec { rsQuery = (And [chartQuery, Depth $ depth + 1, m]) }
balancereport = balanceReport rspecWithQuery j
defaultLayout $ do
setTitle "register - hledger-web"
$(widgetFile "register")
Expand Down Expand Up @@ -111,6 +117,34 @@ registerChartHtml q title percommoditytxnreports = $(hamletFile "templates/chart
shownull c = if null c then " " else c
nodatelink = (RegisterR, [("q", T.unwords $ removeDates q)])

data PieHalf = Positive | Negative

moreThanOne :: Eq a => [a] -> Bool
moreThanOne [] = False
moreThanOne (x : xs) = rec xs
where
rec [] = False
rec (y : ys) = x /= y || (rec ys)

-- | Generate javascript/html for a mockup pie chart
registerPieChartHtml :: PieHalf -> Text -> BalanceReport -> HtmlUrl AppRoute
registerPieChartHtml half q (items, _) = $(hamletFile "templates/piechart.hamlet")
where
labelData =
reverse $
sortOn (\(_, quant, _) -> quant) $
filter (\(_, quant, _) -> case half of Positive -> quant >= 0
Negative -> quant < 0) $
flip concatMap items $ \(accname, _, _, Mixed as) ->
flip map as $ \a -> (accname, aquantity a, acommodity a)
moreThanOneCommodity = moreThanOne $ map (\(_, _, com) -> com) labelData
showChart = if (moreThanOneCommodity) then "false" else "true" :: String
noacctlink = (RegisterR, [("q", T.unwords $ removeInacct q)])
chartId = case half of Positive -> "postive" :: String
Negative -> "negative" :: String
legendPosition = case half of Positive -> "nw" :: String
Negative -> "ne" :: String

dayToJsTimestamp :: Day -> Integer
dayToJsTimestamp d =
read (formatTime defaultTimeLocale "%s" t) * 1000 -- XXX read
Expand Down
53 changes: 53 additions & 0 deletions hledger-web/templates/piechart.hamlet
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<div style="width: 45%; margin-bottom:1em; float: left">
<div id="register-pie-chart-#{chartId}" style="height: 150px; display: none">
<div id="register-pie-hover-#{chartId}">
<script type=text/javascript>
\$(document).ready(function() {
var $chartdiv = $('#register-pie-chart-#{chartId}');
if (#{showChart}) {
\$chartdiv.show();
var data =
[
$forall (label, dat, _) <- labelData
{ label: "#{label}", data: #{dat} },
];
function legendFormatter(label, series) {
return Math.round(series.percent).toFixed(2) + '% ' + label;
};
var options = {
series: {
pie: {
show: true
}
},
grid: {
hoverable: true,
clickable: true
},
legend: {
show: true,
labelFormatter: legendFormatter,
position: "#{legendPosition}"
}
};
\$.plot($chartdiv, data, options);
\$chartdiv.bind("plothover", function(event, pos, obj) {
if (!obj) {
return;
}
var percent = parseFloat(obj.series.percent).toFixed(2);
\$("#register-pie-hover-#{chartId}").html("<span style='font-weight:bold; color:" + obj.series.color + "'>" + obj.series.label + " (" + percent + "%)</span>");
});
\$chartdiv.bind("plotclick", function(event, pos, obj) {
if (!obj) {
return;
}
var baselink = "@?{noacctlink}";
if (baselink.endsWith("?q")) {
document.location = baselink + "=inacct:%22" + obj.series.label + "%22";
} else {
document.location = baselink + "%20inacct:%22" + obj.series.label + "%22";
}
});
}
});
2 changes: 2 additions & 0 deletions hledger-web/templates/register.hamlet
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

<div .hidden-xs>
^{registerChartHtml qparam balancelabel $ accountTransactionsReportByCommodity items}
^{registerPieChartHtml Positive q balancereport }
^{registerPieChartHtml Negative q balancereport }

<div.table-responsive>
<table .table.table-striped.table-condensed>
Expand Down