Allow selecting the date range from the chart

By dragging a region with the mouse
This commit is contained in:
Arnout Engelen 2021-01-26 20:50:30 +01:00 committed by Simon Michael
parent 9cff45b2c5
commit fe6e9a79fe
6 changed files with 33 additions and 5 deletions

View File

@ -160,6 +160,7 @@ instance Yesod App where
addScript $ StaticR js_jquery_cookie_js addScript $ StaticR js_jquery_cookie_js
addScript $ StaticR js_jquery_hotkeys_js addScript $ StaticR js_jquery_hotkeys_js
addScript $ StaticR js_jquery_flot_min_js addScript $ StaticR js_jquery_flot_min_js
addScript $ StaticR js_jquery_flot_selection_min_js
addScript $ StaticR js_jquery_flot_time_min_js addScript $ StaticR js_jquery_flot_time_min_js
addScript $ StaticR js_jquery_flot_tooltip_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]--> |] toWidget [hamlet| \<!--[if lte IE 8]> <script type="text/javascript" src="@{StaticR js_excanvas_min_js}"></script> <![endif]--> |]

View File

@ -19,7 +19,7 @@ import Hledger.Web.WebOptions
import Hledger.Web.Widget.AddForm (addModal) import Hledger.Web.Widget.AddForm (addModal)
import Hledger.Web.Widget.Common import Hledger.Web.Widget.Common
(accountQuery, mixedAmountAsHtml, (accountQuery, mixedAmountAsHtml,
transactionFragment, removeInacct, replaceInacct) transactionFragment, removeDates, removeInacct, replaceInacct)
-- | The main journal/account register view, with accounts sidebar. -- | The main journal/account register view, with accounts sidebar.
getRegisterR :: Handler Html getRegisterR :: Handler Html
@ -99,8 +99,8 @@ decorateLinks =
-- | Generate javascript/html for a register balance line chart based on -- | Generate javascript/html for a register balance line chart based on
-- the provided "TransactionsReportItem"s. -- the provided "TransactionsReportItem"s.
registerChartHtml :: String -> [(CommoditySymbol, [TransactionsReportItem])] -> HtmlUrl AppRoute registerChartHtml :: Text -> String -> [(CommoditySymbol, [TransactionsReportItem])] -> HtmlUrl AppRoute
registerChartHtml title percommoditytxnreports = $(hamletFile "templates/chart.hamlet") registerChartHtml q title percommoditytxnreports = $(hamletFile "templates/chart.hamlet")
-- have to make sure plot is not called when our container (maincontent) -- have to make sure plot is not called when our container (maincontent)
-- is hidden, eg with add form toggled -- is hidden, eg with add form toggled
where where
@ -109,6 +109,7 @@ registerChartHtml title percommoditytxnreports = $(hamletFile "templates/chart.h
commoditiesIndex = zip (map fst percommoditytxnreports) [0..] :: [(CommoditySymbol,Int)] commoditiesIndex = zip (map fst percommoditytxnreports) [0..] :: [(CommoditySymbol,Int)]
simpleMixedAmountQuantity = maybe 0 aquantity . listToMaybe . amounts simpleMixedAmountQuantity = maybe 0 aquantity . listToMaybe . amounts
shownull c = if null c then " " else c shownull c = if null c then " " else c
nodatelink = (RegisterR, [("q", T.unwords $ removeDates q)])
dayToJsTimestamp :: Day -> Integer dayToJsTimestamp :: Day -> Integer
dayToJsTimestamp d = dayToJsTimestamp d =

View File

@ -15,6 +15,7 @@ module Hledger.Web.Widget.Common
, writeJournalTextIfValidAndChanged , writeJournalTextIfValidAndChanged
, journalFile404 , journalFile404
, transactionFragment , transactionFragment
, removeDates
, removeInacct , removeInacct
, replaceInacct , replaceInacct
) where ) where
@ -120,6 +121,13 @@ transactionFragment j Transaction{tindex, tsourcepos} =
-- or 0 if this txn has no known file (eg a forecasted txn) -- or 0 if this txn has no known file (eg a forecasted txn)
tfileindex = maybe 0 (+1) $ elemIndex (sourceFilePath tsourcepos) (journalFilePaths j) tfileindex = maybe 0 (+1) $ elemIndex (sourceFilePath tsourcepos) (journalFilePaths j)
removeDates :: Text -> [Text]
removeDates =
map quoteIfSpaced .
filter (\term ->
not $ T.isPrefixOf "date:" term || T.isPrefixOf "date2:" term) .
Query.words'' Query.prefixes
removeInacct :: Text -> [Text] removeInacct :: Text -> [Text]
removeInacct = removeInacct =
map quoteIfSpaced . map quoteIfSpaced .

View File

@ -58,7 +58,10 @@ function registerChart($container, series) {
{ /* general chart options */ { /* general chart options */
xaxis: { xaxis: {
mode: "time", mode: "time",
timeformat: "%Y/%m/%d" timeformat: "%Y/%m/%d",
},
selection: {
mode: "x"
}, },
legend: { legend: {
position: 'sw' position: 'sw'

View File

@ -55,5 +55,20 @@
] ]
var plot = registerChart($chartdiv, seriesData); var plot = registerChart($chartdiv, seriesData);
\$chartdiv.bind("plotclick", registerChartClick); \$chartdiv.bind("plotclick", registerChartClick);
plot.setSelection({ xaxis: { from: 500, to: 700 } });
\$chartdiv.bind("plotselected", function(event, ranges) {
console.log("selected", ranges.xaxis.from, ranges.xaxis.to);
var from = new Date(ranges.xaxis.from);
var to = new Date(ranges.xaxis.to);
var range =
from.getFullYear() + "/" + (from.getMonth() + 1) + "/" + from.getDate() + "-" +
to.getFullYear() + "/" + (to.getMonth() + 1) + "/" + to.getDate();
var baselink = "@?{nodatelink}";
if (baselink.endsWith("?q")) {
document.location = baselink + "=date:" + range;
} else {
document.location = baselink + "%20date:" + range;
}
});
}; };
}); });

View File

@ -2,7 +2,7 @@
#{header} #{header}
<div .hidden-xs> <div .hidden-xs>
^{registerChartHtml balancelabel $ transactionsReportByCommodity items} ^{registerChartHtml q balancelabel $ transactionsReportByCommodity items}
<div.table-responsive> <div.table-responsive>
<table .table.table-striped.table-condensed> <table .table.table-striped.table-condensed>