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

Posted

技术标签:

【中文标题】如何在 grafana 图表中创建趋势线【英文标题】:How do you create trend line in grafana charts 【发布时间】:2014-12-13 03:12:18 【问题描述】:

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

【问题讨论】:

【参考方案1】:

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

【讨论】:

【参考方案2】:

因此,Grafana 无法添加趋势线。这无疑是一场悲剧。

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

这是我的做法。

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

一旦你有了 y 值,你就可以计算你的趋势线。 更多关于趋势线方程的信息在这里https://clas-s-room.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

它不漂亮,但它有效。

【讨论】:

以上是关于如何在 grafana 图表中创建趋势线的主要内容,如果未能解决你的问题,请参考以下文章

如何在我的网站管理面板中安全地显示 grafana 图表?

如何在 R 中创建显示预测模型、数据和残差的图表

如何在 Grafana 中创建健康检查?

如何在 grafana 中创建普罗米修斯图来划分两个查询?

如何在 Grafana/InfluxDB 中创建百分比/比率列?

如何在基于 sysdig 指标标签的 grafana 仪表板中创建下拉菜单