如何在 grafana 图表中创建趋势线

use*_*980 8 opentsdb grafana

我正在将 grafana 与 opentsdb 一起使用。我可以使用平均值、最大值、最小值等创建图表,但我不知道如何添加趋势。是否可以在grafana的图表上放置趋势线?

小智 7

我找到了一种方法来做到这一点。使用movingAverage 函数,并将窗口大小设置为非常大的值,例如数千。设置得越高,趋势线就越平滑。


小智 5

所以,Grafana 没有办法添加趋势线。毫无疑问,这是一场悲剧。

这并不意味着不可能添加一个,但这非常耗时。

我是这样做的。

出于我的目的,我已经将 y 值作为单独的 grafana 变量,您可以复制我所做的,或者您可以创建另一个查询来填充您的数据,您需要能够单独调用每个 y 值。

一旦获得 y 值,您就可以计算趋势线。有关趋势线方程的更多信息,请参见此处https://classroom.synonym.com/calculate-trendline-2709.html

  with 
    a as (
      select 
        (12*($1*1 + $2*2 + $3*3 + $4*4 + $5*5 + $6*6 + $7*7 + $8*8 + $9*9 + $10*10 + $11*11 + $12*12)) as value
    ),

    b as (
    select
      ($1+$2+$3+$4+$5+$6+$7+$8+$9+$10+$11+$12)*(1+2+3+4+5+6+7+8+9+10+11+12) as value
    ),

    c as (
    select
      12*(1^2+2^2+3^2+4^2+5^2+6^2+7^2+8^2+9^2+10^2+11^2+12^2) as value
    ),

    d as (
    select
      (1+2+3+4+5+6+7+8+9+10+11+12)^2 as value
    ),

    slope as (
    select
    (a.value-b.value)/(c.value-d.value) as value
    from a, b, c, d),

    e as (
    select
      ($1+$2+$3+$4+$5+$6+$7+$8+$9+$10+$11+$12) as value
    ),

    f as (
    select
    slope.value*(1+2+3+4+5+6+7+8+9+10+11+12) as value
    from slope),

    y_intercept as (
    select
      (e.value-f.value)/12 as value
    from e, f
    )
Run Code Online (Sandbox Code Playgroud)

现在您只需填充趋势线的 x 值和 y 值。x 值必须是日期。我使用相对日期范围来匹配我的 y 值数据时间范围。

 select 
      x_value as time,
      trendline_value
    from
      (select
        now() - interval '1 month' as x_value,
        slope.value*1+y_intercept.value as trendline_value
      from
        slope, y_intercept
    union
      select
        now() - interval '2 month' as x_value,
        slope.value*2+y_intercept.value as trendline_value
      from
        slope, y_intercept
    union
      select
        now() - interval '3 month' as x_value,
        slope.value*3+y_intercept.value as trendline_value
      from
        slope, y_intercept
    union
      select
        now() - interval '4 month' as x_value,
        slope.value*4+y_intercept.value as trendline_value
      from
        slope, y_intercept
    union
      select
        now() - interval '5 month' as x_value,
        slope.value*5+y_intercept.value as trendline_value
      from
        slope, y_intercept
    union
      select
        now() - interval '6 month' as x_value,
        slope.value*6+y_intercept.value as trendline_value
      from
        slope, y_intercept
    union
      select
        now() - interval '7 month' as x_value,
        slope.value*7+y_intercept.value as trendline_value
      from
        slope, y_intercept
    union
      select
        now() - interval '8 month' as x_value,
        slope.value*8+y_intercept.value as trendline_value
      from
        slope, y_intercept
    union
      select
        now() - interval '9 month' as x_value,
        slope.value*9+y_intercept.value as trendline_value
      from
        slope, y_intercept
    union
      select
        now() - interval '10 month' as x_value,
        slope.value*10+y_intercept.value as trendline_value
      from
        slope, y_intercept
    union
      select
        now() - interval '11 month' as x_value,
        slope.value*11+y_intercept.value as trendline_value
      from
        slope, y_intercept
    union
      select
        now() - interval '12 month' as x_value,
        slope.value*12+y_intercept.value as trendline_value
      from
        slope, y_intercept
      ) as line_data
      
    order by time
Run Code Online (Sandbox Code Playgroud)

这是最终产品的样子,带有趋势线的 Grafana

它不漂亮,但很有效。