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