luci-app-statistics: add options for graph series sorting and color
authorHannu Nyman <hannu.nyman@iki.fi>
Wed, 31 Jul 2019 21:41:14 +0000 (00:41 +0300)
committerHannu Nyman <hannu.nyman@iki.fi>
Wed, 31 Jul 2019 22:04:40 +0000 (01:04 +0300)
Add two data series-level options "negweight" and "posweight" that
will use the numerical value of the data instance name
as the sorting factor. This enables e.g. sorting by CPU frequency
in the cpufreq module.

Add new graph-level option "ordercolor" to use colorsfrom a
pre-defined color table for the data series. This enables keeping
similar colors for similar data series in different plugin instances.
E.g. CPU frequencies in several cores that are handled and displayed
separately.
 (note: the table has 8 items and if there are more series, it uses
  the same colors again. The table can be easily extended/modified)

Signed-off-by: Hannu Nyman <hannu.nyman@iki.fi>
applications/luci-app-statistics/luasrc/statistics/rrdtool.lua
applications/luci-app-statistics/luasrc/statistics/rrdtool/colors.lua

index 4586b23caa3b50c00bedcb28d13a5a865cf25924..c4ea88de924eaaf97078eaa5742ba38c40f0e75d 100644 (file)
@@ -407,7 +407,9 @@ function Graph._generic( self, opts, plugin, plugin_instance, dtype, index )
                                        transform_rpn = dopts.transform_rpn or "0,+",
                                        noarea   = dopts.noarea  or false,
                                        title    = dopts.title   or nil,
-                                       weight   = dopts.weight  or nil,
+                                       weight   = dopts.weight or
+                                                  (dopts.negweight and -tonumber(dinst)) or
+                                                  (dopts.posweight and tonumber(dinst)) or nil,
                                        ds       = dsource,
                                        type     = dtype,
                                        instance = dinst,
@@ -487,6 +489,13 @@ function Graph._generic( self, opts, plugin, plugin_instance, dtype, index )
                        return x < y
                end)
 
+               -- define colors in order
+               if opts.ordercolor then
+                   for i, source in ipairs(_sources) do
+                       source.color = self.colors:defined(i)
+                   end
+               end
+
                -- create DEF statements for each instance
                for i, source in ipairs(_sources) do
                        -- fixup properties for per instance mode...
index 0d3af712fd8a197c7a74d6cead7d32b2992e6fd5..88b3bbcfeb715d17bf51c4253ff13330d3012bb0 100644 (file)
@@ -25,6 +25,22 @@ function Instance.to_string( self, c )
        )
 end
 
+function Instance.defined( self, i )
+       local t = {
+               {230, 25, 75},
+               {245, 130, 48},
+               {255, 225, 25},
+               {60, 180, 75},
+               {70, 240, 240},
+               {0, 130, 200},
+               {0, 0, 128},
+               {170, 110, 40}
+       }
+       return string.format(
+               "%02x%02x%02x",
+               t[(i-1) % #t + 1][1], t[(i-1) % #t +1][2], t[(i-1) % #t + 1][3] )
+end
+
 function Instance.random( self )
        local r   = math.random(255)
        local g   = math.random(255)