bar-charts-in-the-spent-time-report.patch

Takenori TAKAKI, 2020-01-23 08:43

Download (5.38 KB)

View differences:

app/helpers/timelog_helper.rb
125 125
    cancel_button_tag(fallback_path)
126 126
  end
127 127

  
128
  def report_to_chart_datas(report, period_name='month')
129
    return {} if report.periods.nil?
130
    chart_datas = {}
131

  
132
    chart_datas[period_name] = { :labels => [], :type => 'bar', :datasets => [{:data => []}] }
133
    report.periods.each do |period|
134
      chart_datas[period_name][:labels] << period
135
      chart_datas[period_name][:datasets].first[:data] << sum_hours(select_hours(report.hours, report.columns, period.to_s))
136
    end
137

  
138
    report.criteria.each_with_index do |criteria, level|
139
      chart_datas[criteria] = { :labels => [], :type => 'horizontalBar', :datasets => [{:data => []}]}
140
      report.hours.collect {|h| h[criteria]}.uniq.each do |value|
141
        hours_for_value = select_hours(report.hours, criteria, value)
142
        next if hours_for_value.empty?
143
        chart_datas[criteria][:labels] << format_criteria_value(report.available_criteria[report.criteria[level]], value, false).to_s
144
        chart_datas[criteria][:datasets].first[:data] << report.periods.collect {|period| sum_hours(select_hours(hours_for_value, report.columns, period.to_s)) }.sum
145
      end
146
    end
147

  
148
    chart_datas
149
  end
128 150
end
app/views/timelog/report.html.erb
14 14
    <%= hidden_field_tag 'criteria[]', criterion, :id => nil %>
15 15
  <% end %>
16 16
  <%= render :partial => 'timelog/date_range' %>
17
  <p class="contextual"><%= link_to l(:label_statistics), '#', :onclick => "showAndScrollTo('timelog_chart')", :class => 'icon icon-stats' if @report.criteria.any? && @report.hours.any? %></p>
17 18

  
18 19
  <p><label for='columns'><%= l(:label_details) %></label>: <%= select_tag 'columns', options_for_select([[l(:label_year), 'year'],
19 20
                                                                            [l(:label_month), 'month'],
......
68 69
<% other_formats_links do |f| %>
69 70
  <%= f.link_to_with_query_parameters 'CSV', {}, :onclick => "showModal('csv-export-options', '330px'); return false;" %>
70 71
<% end %>
72
<div id="timelog_chart"></div>
71 73
<% end %>
72 74
<div id="csv-export-options" style="display: none;">
73 75
  <h3 class="title"><%= l(:label_export_options, :export_format => 'CSV') %></h3>
......
88 90

  
89 91

  
90 92
<%= javascript_tag do %>
93
function renderChart(canvas_id, chart_data, title){
94
  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)'];
95
  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)'];
96
  chart_data.datasets[0].backgroundColor = [];
97
  chart_data.datasets[0].borderColor = [];
98
  chart_data.datasets[0].borderWidth = [];
99
  for (var i = 0; i < chart_data.datasets[0].data.length; i++) {
100
    chart_data.datasets[0].backgroundColor[i] = backgroundColors[i % backgroundColors.length];
101
    chart_data.datasets[0].borderColor[i] = borderColors[i % backgroundColors.length];
102
    chart_data.datasets[0].borderWidth[i] = 1;
103
  }
104
  if (chart_data.type=='horizontalBar' & chart_data.labels.length > 40) {
105
     var ctx = $(canvas_id);
106
     ctx.attr('height', chart_data.labels.length * 5);
107
  }
108
  new Chart($(canvas_id), {
109
    type: chart_data.type,
110
    data: chart_data,
111
    options: {
112
      responsive: true,
113
      legend: { display: false },
114
      title: { display: true, text: title },
115
      scales: {
116
        xAxes: [ { ticks: { beginAtZero: true } } ],
117
        yAxes: [ { ticks: { beginAtZero: true } } ]
118
      },
119
    },
120
  });
121
}
91 122
$(document).ready(function(){
92 123
  $('input#csv-export-button').click(function(){
93 124
    $('form input#encoding').val($('select#encoding option:selected').val());
......
95 126
    $('form#query_form').attr('action', '<%= _report_time_entries_path(@project, nil) %>');
96 127
    hideModal(this);
97 128
  });
129
  <% report_to_chart_datas(@report, params[:columns]).each do |column, chart_data| %>
130
    $('#timelog_chart').append('<canvas id="<%= column %>_timelog_chart"></canvas>');
131
    renderChart("#<%= column %>_timelog_chart",
132
                <%= chart_data.to_json.html_safe %>,
133
                "<%= l("label_total_spent_time_per", :column => l_or_humanize(column)) %>");
134
  <% end %>
98 135
});
99 136
<% end %>
137
<% content_for :header_tags do %>
138
  <%= javascript_include_tag "Chart.bundle.min" %>
139
<% end %>
config/locales/en.yml
834 834
  label_f_hour_plural: "%{value} hours"
835 835
  label_f_hour_short: "%{value} h"
836 836
  label_time_tracking: Time tracking
837
  label_total_spent_time_per: Total spent time per %{column}
838
  label_year_spent_time_per: Annual spent time per %{column}
839
  label_month_spent_time_per: Monthly spent time per %{column}
840
  label_week_spent_time_per: Weekly spent time per %{column}
841
  label_day_spent_time_per: Daily spent time per %{column}
837 842
  label_change_plural: Changes
838 843
  label_statistics: Statistics
839 844
  label_commits_per_month: Commits per month