Grafana配置mysql展示自定义分组柱状图(Mac)
发布日期:2021-06-24 18:23:26 浏览次数:2 分类:技术文章

本文共 2230 字,大约阅读时间需要 7 分钟。

安装Grafana

安装使用环境为MAC,使用工具安装:

brew updatebrew install grafana

配置Grafana连接本地安装的mysql,mysql安装不做说明,配置文件列表如下:

配置文件应该位于/usr/local/etc/grafana/grafana.ini日志文件应该位于/usr/local/var/log/grafana/grafana.log如果你想手动安装一个插件放在这里:/usr/local/var/lib/grafana/plugins默认的sqlite数据库位于 /usr/local/var/lib/grafana

其中,编辑grafana.ini文件连接数据库,重点配置数据库的连接类型,账号,密码,数据库名(切记数据库建立对应的数据库)

clipboard.png

重启Grafana服务

brew tap homebrew/servicesbrew services start grafana

此时网页访问localhost:3000即为Grafana配置数据源页面(默认账号密码:admin / admin)

clipboard.png

配置mysql数据源

连接测试后保存,这样在图表展示数据源勾选默认即为mysql指定数据库,或者勾选指定连接名

clipboard.png

数据库构建表结构,录入测试数据

clipboard.png

新建一个用于展示的Graph

编辑,鉴于展示目的为不按照时序排列的柱状图,但是Grafana的展示要求有时间字段在列,命名与time相关,故查询时添加time字段为当前时间,返回结果可以为两种可用形式,故展示两种SQL查询:

select                now() as time,                case when (score >=80) then '[80, ~)'                when (score >=60 and score <80) then '[60, 80)'                when (score >=40 and score <60) then '[40, 60)'                when (score >=20 and score <40) then '[20, 40)'                else '(~, 20)'                end grade, count(*) numfrom grade group by                case when (score >=80) then '[80, ~)'                when (score >=60 and score <80) then '[60, 80)'                when (score >=40 and score <60) then '[40, 60)'                when (score >=20 and score <40) then '[20, 40)'                else '(~, 20)' end                order by 1;

展示结果为:

clipboard.png

select *, now() as timefrom                (select count(*) as '[80, ~)' from grade g where g.score >=80) a,                (select count(*) as '[60, 80)' from grade g where g.score >=60 and g.score <80) b,                (select count(*) as '[40, 60)' from grade g where g.score >=40 and g.score <60) c,                (select count(*) as '[20, 40)' from grade g where g.score >=20 and g.score <40) d,                (select count(*) as '(~, 20)' from grade g where g.score <20) e;

展示结果为:

clipboard.png

重要设置,选择series,选个代表数据是按series分组不是按时间,当前所选时间段进行计算。Y轴仍然表示 值。计算series的avg、min、max、total等。:

clipboard.png

效果图为:

clipboard.png

两种结果都能正常显示,根据显示规律总结为:

  1. 返回记录中包含字符串格式的情况下,取字符串值的一列为分组名,对应的柱状图值为avg、min、max、total选择的方法取聚合对应一行记录上的其他数字值:
    如果多出来一列字符串值,则图表报错
    # 如果没有字符串值列,则取数字列的列名为分组名,对应的柱状图值为每一列分组名下所有值的聚合
  2. 返回记录中不包含字符串格式的情况下,取数字列的列名为分组名,对应的柱状图值为每一列分组名下所有值的聚合avg、min、max、total
  3. 适用上述所有的,时间字段必须存在,即使目前在分组柱状图中无用,否则会报错,选择format as中的table表现形式为表格,不考虑。

clipboard.png

附上官网说明文档,

转载地址:https://blog.csdn.net/weixin_34248258/article/details/88827159 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:浅谈滚服游戏如果实现一键合服
下一篇:前端代码质量进阶:自定义 eslint 规则校验业务逻辑

发表评论

最新留言

不错!
[***.144.177.141]2024年04月07日 17时52分38秒