11

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

4

2 回答 2

10

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

于 2015-10-07T15:19:13.153 回答
2

因此,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
    )

现在您只需为趋势线填充 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

这是最终产品看起来像带有趋势线的 Grafana

它不漂亮,但它有效。

于 2021-04-23T06:32:22.950 回答