Class | ChartsController |
In: |
app/controllers/charts_controller.rb
|
Parent: | ApplicationController |
File: | charts_controller.rb |
(C): | Hipposoft 2008, 2009 |
Purpose: | Use the Gruff library to generate charts in PNG format for inclusion in HTML reports. |
11-Mar-2008 (ADH): Created.
Show a graph. See charts_helper.rb for the way to generate a URL that will correctly convey the required parameters. Essentially, the ID is used to carry the chart type and extra query string parameters carry additional required parameters. This is more of a ‘create’ action than ‘show’, but since it‘s used for things like showing reports and the charts are usually generated on-demand as part of IMG tags, it is far more natural from the caller‘s perspective to use ‘show’.
# File app/controllers/charts_controller.rb, line 21 21: def show 22: 23: # Extract the request parameters 24: 25: type = params[ :id ] 26: 27: raise "Chart type #{type} is unknown" if ( type != ChartsHelper::CHART_TYPE_PIE.to_s ) 28: 29: leaf = ( params[ :leaf ] || 'chart' ) 30: width = ( params[ :width ] || '128' ).to_i 31: duration = ( params[ :duration ] || '1_0' ).sub( '_', '.' ).to_f 32: committed = ( params[ :committed ] || '0_2' ).sub( '_', '.' ).to_f 33: not_committed = ( params[ :not_committed ] || '0_5' ).sub( '_', '.' ).to_f 34: remaining = duration - committed - not_committed 35: 36: # Stop silly widths from loading the server unnecessarily. 37: 38: width = 40 if ( width < 40 ); 39: width = 640 if ( width > 640 ); 40: 41: g = Gruff::Mini::Pie.new( "#{ width }x#{ width * 0.75 }" ) 42: g.title = 'Chart' 43: g.font = "#{ RAILS_ROOT }/#{ GRAPH_FONT }" 44: g.hide_legend = true 45: g.hide_title = true 46: g.marker_font_size = 70 47: 48: # Is this a normal or overrun graph? 49: 50: if ( remaining >= 0 or duration == 0 ) 51: 52: g.theme = { 53: :colors => %w( #cc7777 #ffaa77 #99ff99 ), 54: :background_colors => %w( white white ), 55: :marker_color => 'black' 56: } 57: 58: # Can't have a pie chart where every entry has zero size; 59: # set the remaining time to 100 (as in, 100%) in that case. 60: 61: remaining = 0 if ( remaining < 0 ) # Zero duration task 62: remaining = 100 if ( remaining == 0 and committed == 0 and not_committed == 0 ) 63: 64: g.data( 'Committed', committed ) 65: g.data( 'Not committed', not_committed ) 66: g.data( 'Remaining', remaining ) 67: 68: else 69: 70: g.theme = { 71: :colors => %w( #ffaa77 #880000 #000000 ), 72: :background_colors => %w( white white ), 73: :marker_color => 'black' 74: } 75: 76: g.data( 'Duration', duration ) 77: g.data( 'Overrun', -remaining ) 78: g.data( '-', 0 ) 79: 80: end 81: 82: # Start the chart at 12:00 83: 84: g.zero_degree = -90 85: 86: # Send out the result 87: 88: send_data( 89: g.to_blob, 90: :disposition => 'inline', 91: :type => 'image/png', 92: :filename => "#{ leaf }.png" 93: ) 94: end