Commit 00aef830ba0dc71d3ca8ef7f01b362fd1e6ab113

Authored by Daniel
1 parent 48a6cd84

Fixed graphic for Metric Results not displaying correctly

The graphic for Metric results previously did not have a set size.
Changed it so that it fills the whole space available, and made it
responsive to window resizing. That broke some particular cases of
histories with few changes and number of points that wasn't very small.

In addition, made the graphic display a single point instead of an
unhelpful message.
app/assets/javascripts/module/graphic.js.coffee
1 class Module.Graphic 1 class Module.Graphic
2 constructor: (@container, @metric_name, @module_id) -> 2 constructor: (@container, @metric_name, @module_id) ->
3 - $('tr#'+@container).slideToggle('slow')  
4 - this.load() 3 + drawer = $('tr#'+@container)
  4 + if drawer.is(":hidden")
  5 + drawer.slideDown('slow')
  6 + this.load()
  7 + else
  8 + drawer.slideUp('slow')
5 9
6 load: -> 10 load: ->
7 $.post '/modules/' + @module_id + '/metric_history', 11 $.post '/modules/' + @module_id + '/metric_history',
@@ -11,7 +15,13 @@ class Module.Graphic @@ -11,7 +15,13 @@ class Module.Graphic
11 } 15 }
12 16
13 @display: (dates, values, container) -> 17 @display: (dates, values, container) ->
14 - opts = {bezierCurve: false} 18 + canvas = $('canvas#'+container).get(0)
  19 +
  20 + opts = {
  21 + bezierCurve: false,
  22 + responsive: true,
  23 + maintainAspectRatio: false
  24 + }
15 25
16 data = { 26 data = {
17 labels : dates, 27 labels : dates,
@@ -26,4 +36,8 @@ class Module.Graphic @@ -26,4 +36,8 @@ class Module.Graphic
26 ] 36 ]
27 } 37 }
28 38
29 - graphic = new Chart($('canvas#'+container).get(0).getContext("2d")).Line(data, opts) 39 + if canvas.hasOwnProperty("chart") && canvas.chart != null
  40 + canvas.chart.destroy()
  41 + canvas.chart = null
  42 +
  43 + canvas.chart = new Chart(canvas.getContext("2d")).Line(data, opts)
app/assets/stylesheets/boilerplate/graphic.css
@@ -1,3 +0,0 @@ @@ -1,3 +0,0 @@
1 -div.graphic_container {  
2 - text-align: center;  
3 -}  
4 \ No newline at end of file 0 \ No newline at end of file
app/assets/stylesheets/module_results.css 0 → 100644
@@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
  1 +table.metric_results {
  2 + table-layout: fixed;
  3 +}
  4 +
  5 +.metric_results div.graphic_container {
  6 + width: 100%;
  7 + max-height: 20em;
  8 +}
  9 +
  10 +.metric_results div.graphic_container > canvas {
  11 + width: 100%;
  12 + height: 100%;
  13 +}
0 \ No newline at end of file 14 \ No newline at end of file
app/views/modules/_metric_result.html.erb
@@ -17,7 +17,6 @@ @@ -17,7 +17,6 @@
17 <td colspan="4"> 17 <td colspan="4">
18 <span id="loader_container<%= metric_result.id %>"><%= image_tag 'loader.gif' %> <%= t('loading_data') %></span> 18 <span id="loader_container<%= metric_result.id %>"><%= image_tag 'loader.gif' %> <%= t('loading_data') %></span>
19 <canvas id="container<%= metric_result.id %>" class="graphic_container" style="display: none"></canvas> 19 <canvas id="container<%= metric_result.id %>" class="graphic_container" style="display: none"></canvas>
20 - <span id="container<%= metric_result.id %>" style="display: none"><%= t('only_point_printed_chart') %></span>  
21 </td> 20 </td>
22 </tr> 21 </tr>
23 <% end %> 22 <% end %>
app/views/modules/_metric_results.html.erb
1 -<table class="table table-hover"> 1 +<table class="table table-hover metric_results">
2 <thead> 2 <thead>
3 <th><%= t('activemodel.attributes.metric_result.metric') %></th> 3 <th><%= t('activemodel.attributes.metric_result.metric') %></th>
4 <th><%= t('activemodel.attributes.metric_result.value') %></th> 4 <th><%= t('activemodel.attributes.metric_result.value') %></th>
app/views/modules/metric_history.js.erb
@@ -10,11 +10,6 @@ @@ -10,11 +10,6 @@
10 <% end %> 10 <% end %>
11 11
12 $("#loader_<%= @container %>").hide(); 12 $("#loader_<%= @container %>").hide();
13 - if (dates.length > 1) {  
14 - $("canvas#<%= @container %>").show();  
15 - Module.Graphic.display(dates, values, '<%= @container %>');  
16 - }  
17 - else {  
18 - $('span#<%= @container %>').show();  
19 - } 13 + $("canvas#<%= @container %>").show();
  14 + Module.Graphic.display(dates, values, '<%= @container %>');
20 <% end %> 15 <% end %>
21 \ No newline at end of file 16 \ No newline at end of file