Project

General

Profile

Feature #32856 » bar-charts-in-the-spent-time-report.patch

Takenori TAKAKI, 2020-01-23 08:43

View differences:

app/helpers/timelog_helper.rb
cancel_button_tag(fallback_path)
end
def report_to_chart_datas(report, period_name='month')
return {} if report.periods.nil?
chart_datas = {}
chart_datas[period_name] = { :labels => [], :type => 'bar', :datasets => [{:data => []}] }
report.periods.each do |period|
chart_datas[period_name][:labels] << period
chart_datas[period_name][:datasets].first[:data] << sum_hours(select_hours(report.hours, report.columns, period.to_s))
end
report.criteria.each_with_index do |criteria, level|
chart_datas[criteria] = { :labels => [], :type => 'horizontalBar', :datasets => [{:data => []}]}
report.hours.collect {|h| h[criteria]}.uniq.each do |value|
hours_for_value = select_hours(report.hours, criteria, value)
next if hours_for_value.empty?
chart_datas[criteria][:labels] << format_criteria_value(report.available_criteria[report.criteria[level]], value, false).to_s
chart_datas[criteria][:datasets].first[:data] << report.periods.collect {|period| sum_hours(select_hours(hours_for_value, report.columns, period.to_s)) }.sum
end
end
chart_datas
end
end
app/views/timelog/report.html.erb
<%= hidden_field_tag 'criteria[]', criterion, :id => nil %>
<% end %>
<%= render :partial => 'timelog/date_range' %>
<p class="contextual"><%= link_to l(:label_statistics), '#', :onclick => "showAndScrollTo('timelog_chart')", :class => 'icon icon-stats' if @report.criteria.any? && @report.hours.any? %></p>
<p><label for='columns'><%= l(:label_details) %></label>: <%= select_tag 'columns', options_for_select([[l(:label_year), 'year'],
[l(:label_month), 'month'],
......
<% other_formats_links do |f| %>
<%= f.link_to_with_query_parameters 'CSV', {}, :onclick => "showModal('csv-export-options', '330px'); return false;" %>
<% end %>
<div id="timelog_chart"></div>
<% end %>
<div id="csv-export-options" style="display: none;">
<h3 class="title"><%= l(:label_export_options, :export_format => 'CSV') %></h3>
......
<%= javascript_tag do %>
function renderChart(canvas_id, chart_data, title){
var backgroundColors = ['rgba(255, 99, 132, 0.2)', 'rgba(54, 162, 235, 0.2)', 'rgba(255, 206, 86, 0.2)', 'rgba(75, 192, 192, 0.2)', 'rgba(153, 102, 255, 0.2)', 'rgba(255, 159, 64, 0.2)'];
var borderColors = ['rgba(255, 99, 132, 1)', 'rgba(54, 162, 235, 1)', 'rgba(255, 206, 86, 1)', 'rgba(75, 192, 192, 1)', 'rgba(153, 102, 255, 1)', 'rgba(255, 159, 64, 1)'];
chart_data.datasets[0].backgroundColor = [];
chart_data.datasets[0].borderColor = [];
chart_data.datasets[0].borderWidth = [];
for (var i = 0; i < chart_data.datasets[0].data.length; i++) {
chart_data.datasets[0].backgroundColor[i] = backgroundColors[i % backgroundColors.length];
chart_data.datasets[0].borderColor[i] = borderColors[i % backgroundColors.length];
chart_data.datasets[0].borderWidth[i] = 1;
}
if (chart_data.type=='horizontalBar' & chart_data.labels.length > 40) {
var ctx = $(canvas_id);
ctx.attr('height', chart_data.labels.length * 5);
}
new Chart($(canvas_id), {
type: chart_data.type,
data: chart_data,
options: {
responsive: true,
legend: { display: false },
title: { display: true, text: title },
scales: {
xAxes: [ { ticks: { beginAtZero: true } } ],
yAxes: [ { ticks: { beginAtZero: true } } ]
},
},
});
}
$(document).ready(function(){
$('input#csv-export-button').click(function(){
$('form input#encoding').val($('select#encoding option:selected').val());
......
$('form#query_form').attr('action', '<%= _report_time_entries_path(@project, nil) %>');
hideModal(this);
});
<% report_to_chart_datas(@report, params[:columns]).each do |column, chart_data| %>
$('#timelog_chart').append('<canvas id="<%= column %>_timelog_chart"></canvas>');
renderChart("#<%= column %>_timelog_chart",
<%= chart_data.to_json.html_safe %>,
"<%= l("label_total_spent_time_per", :column => l_or_humanize(column)) %>");
<% end %>
});
<% end %>
<% content_for :header_tags do %>
<%= javascript_include_tag "Chart.bundle.min" %>
<% end %>
config/locales/en.yml
label_f_hour_plural: "%{value} hours"
label_f_hour_short: "%{value} h"
label_time_tracking: Time tracking
label_total_spent_time_per: Total spent time per %{column}
label_year_spent_time_per: Annual spent time per %{column}
label_month_spent_time_per: Monthly spent time per %{column}
label_week_spent_time_per: Weekly spent time per %{column}
label_day_spent_time_per: Daily spent time per %{column}
label_change_plural: Changes
label_statistics: Statistics
label_commits_per_month: Commits per month
(2-2/2)