Skip to content

Commit d5b683f

Browse files
committedJan 25, 2021
Make pie slices clickable
1 parent c9d25a8 commit d5b683f

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed
 

‎hledger-web/Hledger/Web/Handler/RegisterR.hs

+3-2
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ registerChartHtml title percommoditytxnreports = $(hamletFile "templates/chart.h
117117
shownull c = if null c then " " else c
118118

119119
-- | Generate javascript/html for a mockup pie chart
120-
registerPieChartHtml :: BalanceReport -> HtmlUrl AppRoute
121-
registerPieChartHtml (items, _) = $(hamletFile "templates/piechart.hamlet")
120+
registerPieChartHtml :: Text -> BalanceReport -> HtmlUrl AppRoute
121+
registerPieChartHtml q (items, _) = $(hamletFile "templates/piechart.hamlet")
122122
where
123123
charttitle = "Pie Chart" :: String
124124
labelDataTuples =
@@ -127,6 +127,7 @@ registerPieChartHtml (items, _) = $(hamletFile "templates/piechart.hamlet")
127127
flip concatMap items $ \(accname, _, _, Mixed as) ->
128128
flip map as $ \a -> (accname, aquantity a)
129129
showChart = if ((length labelDataTuples) > 1) then "true" else "false" :: String
130+
noacctlink = (RegisterR, [("q", T.unwords $ removeInacct q)])
130131

131132
dayToJsTimestamp :: Day -> Integer
132133
dayToJsTimestamp d =

‎hledger-web/templates/piechart.hamlet

+13-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
}
2323
},
2424
grid: {
25-
hoverable: true
25+
hoverable: true,
26+
clickable: true
2627
},
2728
legend: {
2829
show: true,
@@ -37,5 +38,16 @@
3738
var percent = parseFloat(obj.series.percent).toFixed(2);
3839
\$("#piehover").html("<span style='font-weight:bold; color:" + obj.series.color + "'>" + obj.series.label + " (" + percent + "%)</span>");
3940
});
41+
\$chartdiv.bind("plotclick", function(event, pos, obj) {
42+
if (!obj) {
43+
return;
44+
}
45+
var baselink = "@?{noacctlink}";
46+
if (baselink.endsWith("?q")) {
47+
document.location = baselink + "=inacct:" + obj.series.label;
48+
} else {
49+
document.location = baselink + "%20inacct:" + obj.series.label;
50+
}
51+
});
4052
}
4153
});

‎hledger-web/templates/register.hamlet

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<div .hidden-xs>
55
^{registerChartHtml balancelabel $ transactionsReportByCommodity items}
6-
^{registerPieChartHtml balancereport }
6+
^{registerPieChartHtml q balancereport }
77

88
<div.table-responsive>
99
<table .table.table-striped.table-condensed>

0 commit comments

Comments
 (0)