Skip to content

Commit

Permalink
Hide chart when more than one commodity present
Browse files Browse the repository at this point in the history
Since the result would be hard to understand anyway
  • Loading branch information
raboof committed Dec 20, 2023
1 parent 607090c commit a32504e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
21 changes: 15 additions & 6 deletions hledger-web/Hledger/Web/Handler/RegisterR.hs
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,28 @@ registerChartHtml q title percommoditytxnreports = $(hamletFile "templates/chart

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
charttitle = "Pie Chart" :: String
labelDataTuples =
labelData =
reverse $
sortOn snd $
filter (\(_, quant) -> case half of Positive -> quant >= 0
Negative -> quant < 0) $
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)
showChart = if ((length labelDataTuples) > 1) then "true" else "false" :: String
flip map as $ \a -> (accname, aquantity a, acommodity a)
moreThanOneAcct = moreThanOne $ map (\(acct, _, _) -> acct) labelData
moreThanOneCommodity = moreThanOne $ map (\(_, _, com) -> com) labelData
showChart = if (moreThanOneAcct && not moreThanOneCommodity) then "true" else "false" :: String
noacctlink = (RegisterR, [("q", T.unwords $ removeInacct q)])
chartId = case half of Positive -> "postive" :: String
Negative -> "negative" :: String
Expand Down
2 changes: 1 addition & 1 deletion hledger-web/templates/piechart.hamlet
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
\$chartdiv.show();
var data =
[
$forall (label, dat) <- labelDataTuples
$forall (label, dat, _) <- labelData
{ label: "#{label}", data: #{dat} },
];
function legendFormatter(label, series) {
Expand Down

0 comments on commit a32504e

Please sign in to comment.