分段堆叠柱状图实现

分段堆叠柱状图实现

查询规定时间内的汇总堆叠柱状图效果

  • 实现效果如下


    1194505.png
  • 效果详细描述
    1.起止时间可在页面设置
    2.柱状的个数有限制
    3.数据区分级别

时间段切分

详见“将一段时间切分为N段”https://www.jianshu.com/writer#/notebooks/41037905/notes/57957591

错误的方式

  • 方式一
    24段时间分别进行查询数据库,即多少个柱状执行数据库查询操作多少次,
    致使,数据库资源消耗很大,下为简单的代码与sql描述


    查询java
SELECT 
RIGHT(DATE_FORMAT(FROM_UNIXTIME(`start`), '%Y-%m-%d %H:%i:%s'),8) TIME,
alv,
COUNT(*) COUNT FROM table_log 
WHERE 
START >= CAST(#{start} AS SIGNED) and 
START < CAST(#{end} AS SIGNED) 
GROUP BY alv  
  • 方式二
(SELECT RIGHT(DATE_FORMAT(FROM_UNIXTIME(`start`), '%Y-%m-%d %H:%i:%s'),8) TIME,'1463716740' AS fullstartText,alv, COUNT(*) COUNT FROM table_log WHERE START >= CAST('1463716740' AS SIGNED) AND START < CAST('1463717040' AS SIGNED) GROUP BY alv )
UNION
(SELECT RIGHT(DATE_FORMAT(FROM_UNIXTIME(`start`), '%Y-%m-%d %H:%i:%s'),8) TIME,'1463716440' AS fullstartText,alv, COUNT(*) COUNT FROM table_log WHERE START >= CAST('1463716440' AS SIGNED) AND START < CAST('1463723640' AS SIGNED)  GROUP BY alv )

union一两个还很快,5六个以后就缓慢了,并且,sql语句有长度限制,超过长度需要修改数据库设置。
并且,这种方式应该只是节省了请求数据库连接的时间,sql上并未做多少改善。

解决方案

  • sql语句


    sql截图

    以上sql为通过程序代码拼接后的结果,执行如下


    执行结果

优点是,一条sql查询出结果,缺点是,会产生一些无用的列。

  • 程序调用
    根据sql返回的结果进行迭代,将多条符合条件的结果捏在一起就可以了

查询"年月日时"

select               
update_time as updateTime,            
avg(io_total) as ioTotal          
from        
update_time as updateTime,
avg(io_total) as ioTotal
from
TB_DY_LUNINFO_COLL
--  实时
where (update_time between #starttime# and #endtime#)
group by hour(update_time),minute (update_time)
order by update_time asc
--  日
where (update_time between #starttime# and #endtime#)
group by day(update_time),hour(update_time)
order by update_time asc
--  月
where (update_time between #starttime# and #endtime#)
group by month(update_time),day (update_time)
order by update_time asc
--  年
where (update_time between #starttime# and #endtime#)
group by year(update_time),month (update_time)
order by update_time asc

你可能感兴趣的:(分段堆叠柱状图实现)