From a32504eab6b7211bede7450e0ef3735199b2a64e Mon Sep 17 00:00:00 2001 From: Arnout Engelen Date: Fri, 29 Jan 2021 11:58:26 +0100 Subject: [PATCH] Hide chart when more than one commodity present Since the result would be hard to understand anyway --- hledger-web/Hledger/Web/Handler/RegisterR.hs | 21 ++++++++++++++------ hledger-web/templates/piechart.hamlet | 2 +- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/hledger-web/Hledger/Web/Handler/RegisterR.hs b/hledger-web/Hledger/Web/Handler/RegisterR.hs index a282503d7e6..babaa28ef9f 100644 --- a/hledger-web/Hledger/Web/Handler/RegisterR.hs +++ b/hledger-web/Hledger/Web/Handler/RegisterR.hs @@ -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 diff --git a/hledger-web/templates/piechart.hamlet b/hledger-web/templates/piechart.hamlet index e688568f633..c268248a09f 100644 --- a/hledger-web/templates/piechart.hamlet +++ b/hledger-web/templates/piechart.hamlet @@ -9,7 +9,7 @@ \$chartdiv.show(); var data = [ - $forall (label, dat) <- labelDataTuples + $forall (label, dat, _) <- labelData { label: "#{label}", data: #{dat} }, ]; function legendFormatter(label, series) {