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

时间:2014-10-17 13:55:22

标签: opentsdb grafana

我正在使用带有opentsdb的grafana。我可以用avg,max,min等创建图表,但我不知道如何添加趋势。是否可以将趋势线放在grafana的图表上?

1 个答案:

答案 0 :(得分:0)

因此,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 with trendline

它不漂亮,但很管用。