[i=s] 本帖最后由 行者赖 于 2016-4-29 14:37 编辑
[align=center] Redmine 入门七——Redmine 中报表功能的优化下 [/align] 4.统计条件的保存 查询条件 IssueQuery 类将数据保存在表 queries 中,我们可以采用同样的方法将统计的数据也保存在 queries 中使用类型进行区分。当然也可以创建新的数据库表存储对应的数据。 定义类 [code] class ReportQuery < IssueQuery 定义统计的类继承与原有的 IssueQuery End[/code] 定义操作方法 [code] class ReportQueriesController < ApplicationController # 新建方法 def new @query = ReportQuery.new @query.user = User.current @query.project = @project @query.build_from_params(params) End # 创建记录 def create @query = ReportQuery.new @query.user = User.current @query.project = @project update_query_from_params
if @query.save
flash[:notice] = l(:notice_successful_create)
redirect_to _queries_url(:_query_id => @query)
else
render :action => 'new', :layout => ! request.xhr?
end
end
# 编辑内容方法
def edit
@query.chart = @query.column_names
end
# 更新内容
def update
update_query_from_params
if @query.save
flash[:notice] = l(:notice_successful_update)
redirect_to _queries_url(:_query_id => @query)
else
render :action => 'edit'
end
end
# 删除内容
def destroy
@query.destroy
redirect_to _queries_url(:set_filter => 1)
end
End[/code]
定义保存界面,可以参考问题查询界面进行修改
5.增加分组条件
默认的 IssueQuery 类集成 Query 类,只能使用一个分组条件进行查询,然而在统计的过程中可能需要使用到两个或者更多的分组提交体现趋势或者不同的分布。通过在 ReportQuery 中改下原始的分组和分组查询个数的函数目前能够实现两个分组条件的统计。
[code] class ReportQuery < IssueQuery
# 定义统计的类继承与原有的 IssueQuery
# 修改分组条件类型,从字符串改为数组
def group_by=(arg)
c = []
if arg.is_a?(Hash)
arg = arg.keys.sort.collect {|k| arg[k]}
end
if arg
c = arg
end
write_attribute(:group_by, c)
end
def group_by read_attribute(:group_by) || [] end # 修改分组的列 def group_by_column column = [] group_by.each do |g| column << groupable_columns.detect {|c| c.groupable && c.name.to_s == g} end column end End[/code] 6.更新图标 js 库 在上一节中 redmine_monitoring_controlling 使用了 highchart 的 js 类库形成图标。highchart 是老牌的 js 类库使用的比较广泛,但是配色和功能已经不如新一代图标库表现丰富。这里我们使用百度提供的 echarts 作为替换。 Js 源文件可以再http://echarts.baidu.com/download.htmlasserts 中。下载,放在插件的 需要调用的界面加入如下代码:<%= javascript_include_tag 'echarts.js', :plugin => "redmine_report" %> Echarts 有很多丰富的图形表示方法,有兴趣的可以参照起官方网站的介绍。
[attach] 2535[/attach]