liquid_feedback_frontend
view env/ui/bargraph.lua @ 1231:e7fc3fed1593
Refactored password reset functionality
| author | bsw | 
|---|---|
| date | Tue Dec 01 18:12:00 2015 +0100 (2015-12-01) | 
| parents | 701a5cf6b067 | 
| children | 32cc544d5a5b | 
 line source
     1 function ui.bargraph(args)
     2   local text = ""
     3   for i, bar in ipairs(args.bars) do
     4     if #text > 0 then
     5       text = text .. " / "
     6     end
     7     text = text .. tostring(bar.value)
     8   end
     9   ui.container{
    10     attr = {
    11       class = args.class or "bargraph",
    12       title = tostring(text)
    13     },
    14     content = function()
    15       local at_least_one_bar = false
    16       local quorum = args.quorum and args.quorum * args.width / args.max_value or nil
    17       local length = 0
    18       local rest = 0
    19       local last_visiable_bar = 0
    20       for i, bar in ipairs(args.bars) do
    21         if bar.value > 0 then
    22           last_visiable_bar = i
    23         end
    24       end
    25       for i, bar in ipairs(args.bars) do
    26         if bar.value > 0 then
    27           at_least_one_bar = true
    28           local value = bar.value * args.width / args.max_value
    29           if quorum and quorum < length + value then
    30             local dlength = math.max(quorum - length - 1, 0)
    31             local dlength_abs = math.floor(dlength)
    32             local rest = rest + dlength - dlength_abs
    33             if dlength > 0 then
    34               ui.container{
    35                 attr = {
    36                   style = "width: " .. tostring(dlength_abs) .. "px; background-color: " .. bar.color .. ";",
    37                 },
    38                 content = ""
    39               }
    40             end
    41             ui.container{
    42               attr = {
    43                 class = "quorum",
    44                 style = "width: 1px; background-color: " .. (args.quorum_color or "blue") ..";",
    45               },
    46               content = ""
    47             }
    48             length = dlength + 1
    49             value = value - dlength
    50             quorum = nil
    51           end
    52           local value_abs = math.floor(value)
    53           rest = rest + value - value_abs
    54           if i == last_visiable_bar then
    55             value_abs = value_abs + rest
    56           end
    57           length = length + value_abs
    58           ui.container{
    59             attr = {
    60               style = "width: " .. tostring(value_abs) .. "px; background-color: " .. bar.color .. ";",
    61             },
    62             content = ""
    63           }
    64         end
    65       end
    66       if not at_least_one_bar then
    67         slot.put(" ")
    68       end
    69     end
    70   }
    71 end
