From fccb28e9a4569e54f50afde6da409a4145766a5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B0=D0=B7=D1=8C=D0=BC=D0=B8=D0=BD=20=D0=92=D0=B0?= =?UTF-8?q?=D1=81=D0=B8=D0=BB=D0=B8=D0=B9=20=D0=92=D0=B0=D1=81=D0=B8=D0=BB?= =?UTF-8?q?=D1=8C=D0=B5=D0=B2=D0=B8=D1=87?= Date: Thu, 12 Sep 2019 18:32:03 +0300 Subject: [PATCH 1/3] bug fix for rails 5.2 --- app/controllers/milestones_controller.rb | 6 +++--- app/models/milestone.rb | 4 ++-- db/migrate/001_create_milestones.rb | 2 +- db/migrate/002_create_milestone_versions.rb | 2 +- db/migrate/003_preset_permissions.rb | 2 +- lib/advanced_roadmap/calendars_controller_patch.rb | 2 +- lib/advanced_roadmap/journal_patch.rb | 3 ++- lib/advanced_roadmap/projects_helper_patch.rb | 3 ++- lib/advanced_roadmap/redmine_helpers_gantt_patch.rb | 3 ++- lib/advanced_roadmap/version_patch.rb | 6 ++++-- lib/advanced_roadmap/versions_controller_patch.rb | 3 ++- 11 files changed, 21 insertions(+), 15 deletions(-) diff --git a/app/controllers/milestones_controller.rb b/app/controllers/milestones_controller.rb index ba74846..cd9d8c7 100644 --- a/app/controllers/milestones_controller.rb +++ b/app/controllers/milestones_controller.rb @@ -2,9 +2,9 @@ class MilestonesController < ApplicationController menu_item :roadmap - before_filter :find_project, :only => [:new, :create] - before_filter :find_milestone, :only => [:show, :edit, :update, :destroy] - before_filter :authorize, :except => [:show, :total_graph] + before_action :find_project, :only => [:new, :create] + before_action :find_milestone, :only => [:show, :edit, :update, :destroy] + before_action :authorize, :except => [:show, :total_graph] helper :custom_fields helper :projects diff --git a/app/models/milestone.rb b/app/models/milestone.rb index 2d75473..fb032c9 100644 --- a/app/models/milestone.rb +++ b/app/models/milestone.rb @@ -9,8 +9,8 @@ class Milestone < ActiveRecord::Base validates_uniqueness_of :name, :scope => [:project_id] validates_length_of :name, :maximum => 60 validates :effective_date, :date => true, :allow_nil => true - - attr_accessible :name, :description, :effective_date + # TODO: Fix this later. Not for Rails 5.2 + #attr_accessible :name, :description, :effective_date def to_s name diff --git a/db/migrate/001_create_milestones.rb b/db/migrate/001_create_milestones.rb index 3e4f90d..ff7eb99 100644 --- a/db/migrate/001_create_milestones.rb +++ b/db/migrate/001_create_milestones.rb @@ -1,4 +1,4 @@ -class CreateMilestones < ActiveRecord::Migration +class CreateMilestones < ActiveRecord::Migration[5.2] def self.up create_table :milestones, :force => true do |t| t.column :name, :string, :null => false diff --git a/db/migrate/002_create_milestone_versions.rb b/db/migrate/002_create_milestone_versions.rb index e58af02..8c5fb65 100644 --- a/db/migrate/002_create_milestone_versions.rb +++ b/db/migrate/002_create_milestone_versions.rb @@ -1,4 +1,4 @@ -class CreateMilestoneVersions < ActiveRecord::Migration +class CreateMilestoneVersions < ActiveRecord::Migration[5.2] def self.up create_table :milestone_versions, :force => true do |t| t.column :milestone_id, :integer, :null => false diff --git a/db/migrate/003_preset_permissions.rb b/db/migrate/003_preset_permissions.rb index efb9699..6e6f79f 100644 --- a/db/migrate/003_preset_permissions.rb +++ b/db/migrate/003_preset_permissions.rb @@ -1,4 +1,4 @@ -class PresetPermissions < ActiveRecord::Migration +class PresetPermissions < ActiveRecord::Migration[5.2] def self.up role = nil begin diff --git a/lib/advanced_roadmap/calendars_controller_patch.rb b/lib/advanced_roadmap/calendars_controller_patch.rb index 7b5bb8b..4feb41a 100644 --- a/lib/advanced_roadmap/calendars_controller_patch.rb +++ b/lib/advanced_roadmap/calendars_controller_patch.rb @@ -5,7 +5,7 @@ module CalendarsControllerPatch def self.included(base) base.class_eval do - around_filter :add_milestones, :only => [:show] + around_action :add_milestones, :only => [:show] def add_milestones yield diff --git a/lib/advanced_roadmap/journal_patch.rb b/lib/advanced_roadmap/journal_patch.rb index d453617..2b4898a 100644 --- a/lib/advanced_roadmap/journal_patch.rb +++ b/lib/advanced_roadmap/journal_patch.rb @@ -6,7 +6,8 @@ def self.included(base) base.send(:include, InstanceMethods) base.class_eval do - alias_method_chain :visible_details, :estimated_time_hidden + alias_method :estimated_time_hidden, :visible_details + alias_method :visible_details, :estimated_time_hidden end end end diff --git a/lib/advanced_roadmap/projects_helper_patch.rb b/lib/advanced_roadmap/projects_helper_patch.rb index 2316418..de7c43c 100644 --- a/lib/advanced_roadmap/projects_helper_patch.rb +++ b/lib/advanced_roadmap/projects_helper_patch.rb @@ -13,7 +13,8 @@ def project_settings_tabs_with_more_tabs end return(tabs) end - alias_method_chain :project_settings_tabs, :more_tabs + alias_method :more_tabs, :project_settings_tabs + alias_method :project_settings_tabs, :more_tabs end end end diff --git a/lib/advanced_roadmap/redmine_helpers_gantt_patch.rb b/lib/advanced_roadmap/redmine_helpers_gantt_patch.rb index e58e7bc..1502d6a 100644 --- a/lib/advanced_roadmap/redmine_helpers_gantt_patch.rb +++ b/lib/advanced_roadmap/redmine_helpers_gantt_patch.rb @@ -19,7 +19,8 @@ def render_version_with_milestones(project, version, options = {}) @last_rendered_project = project render_version_without_milestones(project, version, options) end - alias_method_chain :render_version, :milestones + alias_method :milestones, :render_version + alias_method :render_version, :milestones def render_milestone(project, milestone, options = {}) # Milestone header diff --git a/lib/advanced_roadmap/version_patch.rb b/lib/advanced_roadmap/version_patch.rb index 1becae0..2654952 100644 --- a/lib/advanced_roadmap/version_patch.rb +++ b/lib/advanced_roadmap/version_patch.rb @@ -12,13 +12,15 @@ def completed_percent_with_advanced_info calculate_advance_info unless @total_ratio @total_ratio end - alias_method_chain :completed_percent, :advanced_info + alias_method :advanced_info, :completed_percent + alias_method :completed_percent, :advanced_info def closed_percent_with_advanced_info calculate_advance_info unless @total_finished_ratio @total_finished_ratio end - alias_method_chain :closed_percent, :advanced_info + alias_method :advanced_info, :closed_percent + alias_method :closed_percent, :advanced_info def rest_hours calculate_advance_info unless @total_pending diff --git a/lib/advanced_roadmap/versions_controller_patch.rb b/lib/advanced_roadmap/versions_controller_patch.rb index d9428fa..044f619 100644 --- a/lib/advanced_roadmap/versions_controller_patch.rb +++ b/lib/advanced_roadmap/versions_controller_patch.rb @@ -15,7 +15,8 @@ def index_with_plugin end if params[:only_open] end - alias_method_chain :index, :plugin + alias_method :plugin, :index + alias_method :index, :plugin def show @issues = @version.sorted_fixed_issues From 084f37fe6941fe9d25c544afe20136cab12f70fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B0=D0=B7=D1=8C=D0=BC=D0=B8=D0=BD=20=D0=92=D0=B0?= =?UTF-8?q?=D1=81=D0=B8=D0=BB=D0=B8=D0=B9=20=D0=92=D0=B0=D1=81=D0=B8=D0=BB?= =?UTF-8?q?=D1=8C=D0=B5=D0=B2=D0=B8=D1=87?= Date: Tue, 17 Sep 2019 17:51:35 +0300 Subject: [PATCH 2/3] Worked --- Gemfile | 1 + app/controllers/milestones_controller.rb | 9 +- app/views/versions/_total.html.erb | 2 +- app/views/versions/index.html.erb | 2 +- app/views/versions/show.html.erb | 2 +- lib/advanced_roadmap/gruff.rb | 28 - lib/advanced_roadmap/gruff/accumulator_bar.rb | 27 - lib/advanced_roadmap/gruff/area.rb | 58 - lib/advanced_roadmap/gruff/bar.rb | 87 -- lib/advanced_roadmap/gruff/bar_conversion.rb | 46 - lib/advanced_roadmap/gruff/base.rb | 1129 ----------------- lib/advanced_roadmap/gruff/bullet.rb | 109 -- lib/advanced_roadmap/gruff/deprecated.rb | 41 - lib/advanced_roadmap/gruff/dot.rb | 113 -- lib/advanced_roadmap/gruff/line.rb | 135 -- lib/advanced_roadmap/gruff/mini/bar.rb | 37 - lib/advanced_roadmap/gruff/mini/legend.rb | 82 -- lib/advanced_roadmap/gruff/mini/pie.rb | 36 - lib/advanced_roadmap/gruff/mini/side_bar.rb | 35 - lib/advanced_roadmap/gruff/net.rb | 140 -- lib/advanced_roadmap/gruff/photo_bar.rb | 100 -- lib/advanced_roadmap/gruff/pie.rb | 124 -- lib/advanced_roadmap/gruff/scene.rb | 209 --- lib/advanced_roadmap/gruff/side_bar.rb | 115 -- .../gruff/side_stacked_bar.rb | 74 -- lib/advanced_roadmap/gruff/spider.rb | 130 -- lib/advanced_roadmap/gruff/stacked_area.rb | 67 - lib/advanced_roadmap/gruff/stacked_bar.rb | 54 - lib/advanced_roadmap/gruff/stacked_mixin.rb | 23 - .../versions_controller_patch.rb | 5 +- 30 files changed, 14 insertions(+), 3006 deletions(-) create mode 100644 Gemfile delete mode 100644 lib/advanced_roadmap/gruff.rb delete mode 100644 lib/advanced_roadmap/gruff/accumulator_bar.rb delete mode 100644 lib/advanced_roadmap/gruff/area.rb delete mode 100644 lib/advanced_roadmap/gruff/bar.rb delete mode 100644 lib/advanced_roadmap/gruff/bar_conversion.rb delete mode 100644 lib/advanced_roadmap/gruff/base.rb delete mode 100644 lib/advanced_roadmap/gruff/bullet.rb delete mode 100644 lib/advanced_roadmap/gruff/deprecated.rb delete mode 100644 lib/advanced_roadmap/gruff/dot.rb delete mode 100644 lib/advanced_roadmap/gruff/line.rb delete mode 100644 lib/advanced_roadmap/gruff/mini/bar.rb delete mode 100644 lib/advanced_roadmap/gruff/mini/legend.rb delete mode 100644 lib/advanced_roadmap/gruff/mini/pie.rb delete mode 100644 lib/advanced_roadmap/gruff/mini/side_bar.rb delete mode 100644 lib/advanced_roadmap/gruff/net.rb delete mode 100644 lib/advanced_roadmap/gruff/photo_bar.rb delete mode 100644 lib/advanced_roadmap/gruff/pie.rb delete mode 100644 lib/advanced_roadmap/gruff/scene.rb delete mode 100644 lib/advanced_roadmap/gruff/side_bar.rb delete mode 100644 lib/advanced_roadmap/gruff/side_stacked_bar.rb delete mode 100644 lib/advanced_roadmap/gruff/spider.rb delete mode 100644 lib/advanced_roadmap/gruff/stacked_area.rb delete mode 100644 lib/advanced_roadmap/gruff/stacked_bar.rb delete mode 100644 lib/advanced_roadmap/gruff/stacked_mixin.rb diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..ea1c42e --- /dev/null +++ b/Gemfile @@ -0,0 +1 @@ +gem 'gruff' \ No newline at end of file diff --git a/app/controllers/milestones_controller.rb b/app/controllers/milestones_controller.rb index cd9d8c7..ec65ebc 100644 --- a/app/controllers/milestones_controller.rb +++ b/app/controllers/milestones_controller.rb @@ -1,4 +1,4 @@ -require "advanced_roadmap/gruff/pie" if Object.const_defined?(:Magick) +require 'gruff/pie' if Object.const_defined?(:Magick) class MilestonesController < ApplicationController menu_item :roadmap @@ -100,7 +100,7 @@ def destroy end def total_graph - g = AdvancedRoadmap::Gruff::Pie.new(params[:size] || "500x400") + g = Gruff::Pie.new(params[:size] || "500x400") g.hide_title = true g.theme = graph_theme g.margins = 0 @@ -120,6 +120,11 @@ def total_graph private + def milestone_params + params.require(:milestone).permit(:name, :description, :effective_date) + end + + def find_project @project = Project.find(params[:project_id]) rescue ActiveRecord::RecordNotFound diff --git a/app/views/versions/_total.html.erb b/app/views/versions/_total.html.erb index 16d542e..c11709a 100644 --- a/app/views/versions/_total.html.erb +++ b/app/views/versions/_total.html.erb @@ -1,6 +1,6 @@ <%= tag "a", :name => l(:label_total) %>

<%= l(:label_total) %>

-<%= progress_bar([totals[:closed_percent], totals[:completed_percent]], :width => '40em', :legend => ('%0.0f%' % totals[:completed_percent])) %> +<%= progress_bar([totals[:closed_percent], totals[:completed_percent]], :width => '40em', :legend => ('%0.0f%%' % totals[:completed_percent])) %>
<%= l(:label_time_tracking) %> diff --git a/app/views/versions/index.html.erb b/app/views/versions/index.html.erb index d55511b..74f986d 100644 --- a/app/views/versions/index.html.erb +++ b/app/views/versions/index.html.erb @@ -42,4 +42,4 @@ <% html_title(l(:label_roadmap)) %> -<%= context_menu issues_context_menu_path %> +<%= context_menu %> diff --git a/app/views/versions/show.html.erb b/app/views/versions/show.html.erb index 1aabb01..812c29e 100644 --- a/app/views/versions/show.html.erb +++ b/app/views/versions/show.html.erb @@ -45,4 +45,4 @@ <% html_title @version.name %> -<%= context_menu issues_context_menu_path %> +<%= context_menu %> diff --git a/lib/advanced_roadmap/gruff.rb b/lib/advanced_roadmap/gruff.rb deleted file mode 100644 index a809963..0000000 --- a/lib/advanced_roadmap/gruff.rb +++ /dev/null @@ -1,28 +0,0 @@ -# Extra full path added to fix loading errors on some installations. - -%w( - base - area - bar - line - dot - pie - spider - net - stacked_area - stacked_bar - side_stacked_bar - side_bar - accumulator_bar - - scene - - mini/legend - mini/bar - mini/pie - mini/side_bar -).each do |filename| - require File.dirname(__FILE__) + "/gruff/#{filename}" -end - -# TODO bullet diff --git a/lib/advanced_roadmap/gruff/accumulator_bar.rb b/lib/advanced_roadmap/gruff/accumulator_bar.rb deleted file mode 100644 index 28b8bb6..0000000 --- a/lib/advanced_roadmap/gruff/accumulator_bar.rb +++ /dev/null @@ -1,27 +0,0 @@ -require File.dirname(__FILE__) + '/base' - -## -# A special bar graph that shows a single dataset as a set of -# stacked bars. The bottom bar shows the running total and -# the top bar shows the new value being added to the array. - -class AdvancedRoadmap::Gruff::AccumulatorBar < AdvancedRoadmap::Gruff::StackedBar - - def draw - raise(Gruff::IncorrectNumberOfDatasetsException) unless @data.length == 1 - - accumulator_array = [] - index = 0 - - increment_array = @data.first[DATA_VALUES_INDEX].inject([]) {|memo, value| - memo << ((index > 0) ? (value + memo.max) : value) - accumulator_array << memo[index] - value - index += 1 - memo - } - data "Accumulator", accumulator_array - - super - end - -end diff --git a/lib/advanced_roadmap/gruff/area.rb b/lib/advanced_roadmap/gruff/area.rb deleted file mode 100644 index 7a57533..0000000 --- a/lib/advanced_roadmap/gruff/area.rb +++ /dev/null @@ -1,58 +0,0 @@ - -require File.dirname(__FILE__) + '/base' - -class AdvancedRoadmap::Gruff::Area < AdvancedRoadmap::Gruff::Base - - def draw - super - - return unless @has_data - - @x_increment = @graph_width / (@column_count - 1).to_f - @d = @d.stroke 'transparent' - - @norm_data.each do |data_row| - poly_points = Array.new - prev_x = prev_y = 0.0 - @d = @d.fill data_row[DATA_COLOR_INDEX] - - data_row[DATA_VALUES_INDEX].each_with_index do |data_point, index| - # Use incremented x and scaled y - new_x = @graph_left + (@x_increment * index) - new_y = @graph_top + (@graph_height - data_point * @graph_height) - - if prev_x > 0 and prev_y > 0 then - poly_points << new_x - poly_points << new_y - - #@d = @d.polyline(prev_x, prev_y, new_x, new_y) - else - poly_points << @graph_left - poly_points << @graph_bottom - 1 - poly_points << new_x - poly_points << new_y - - #@d = @d.polyline(@graph_left, @graph_bottom, new_x, new_y) - end - - draw_label(new_x, index) - - prev_x = new_x - prev_y = new_y - end - - # Add closing points, draw polygon - poly_points << @graph_right - poly_points << @graph_bottom - 1 - poly_points << @graph_left - poly_points << @graph_bottom - 1 - - @d = @d.polyline(*poly_points) - - end - - @d.draw(@base_image) - end - - -end diff --git a/lib/advanced_roadmap/gruff/bar.rb b/lib/advanced_roadmap/gruff/bar.rb deleted file mode 100644 index c5f9fcb..0000000 --- a/lib/advanced_roadmap/gruff/bar.rb +++ /dev/null @@ -1,87 +0,0 @@ -require File.dirname(__FILE__) + '/base' -require File.dirname(__FILE__) + '/bar_conversion' - -class AdvancedRoadmap::Gruff::Bar < AdvancedRoadmap::Gruff::Base - - # Spacing factor applied between bars - attr_accessor :bar_spacing - - def draw - # Labels will be centered over the left of the bar if - # there are more labels than columns. This is basically the same - # as where it would be for a line graph. - @center_labels_over_point = (@labels.keys.length > @column_count ? true : false) - - super - return unless @has_data - - draw_bars - end - -protected - - def draw_bars - # Setup spacing. - # - # Columns sit side-by-side. - @bar_spacing ||= 0.9 # space between the bars - @bar_width = @graph_width / (@column_count * @data.length).to_f - padding = (@bar_width * (1 - @bar_spacing)) / 2 - - @d = @d.stroke_opacity 0.0 - - # Setup the BarConversion Object - conversion = Gruff::BarConversion.new() - conversion.graph_height = @graph_height - conversion.graph_top = @graph_top - - # Set up the right mode [1,2,3] see BarConversion for further explanation - if @minimum_value >= 0 then - # all bars go from zero to positiv - conversion.mode = 1 - else - # all bars go from 0 to negativ - if @maximum_value <= 0 then - conversion.mode = 2 - else - # bars either go from zero to negativ or to positiv - conversion.mode = 3 - conversion.spread = @spread - conversion.minimum_value = @minimum_value - conversion.zero = -@minimum_value/@spread - end - end - - # iterate over all normalised data - @norm_data.each_with_index do |data_row, row_index| - - data_row[DATA_VALUES_INDEX].each_with_index do |data_point, point_index| - # Use incremented x and scaled y - # x - left_x = @graph_left + (@bar_width * (row_index + point_index + ((@data.length - 1) * point_index))) + padding - right_x = left_x + @bar_width * @bar_spacing - # y - conv = [] - conversion.getLeftYRightYscaled( data_point, conv ) - - # create new bar - @d = @d.fill data_row[DATA_COLOR_INDEX] - @d = @d.rectangle(left_x, conv[0], right_x, conv[1]) - - # Calculate center based on bar_width and current row - label_center = @graph_left + - (@data.length * @bar_width * point_index) + - (@data.length * @bar_width / 2.0) - # Subtract half a bar width to center left if requested - draw_label(label_center - (@center_labels_over_point ? @bar_width / 2.0 : 0.0), point_index) - end - - end - - # Draw the last label if requested - draw_label(@graph_right, @column_count) if @center_labels_over_point - - @d.draw(@base_image) - end - -end diff --git a/lib/advanced_roadmap/gruff/bar_conversion.rb b/lib/advanced_roadmap/gruff/bar_conversion.rb deleted file mode 100644 index c97f7bd..0000000 --- a/lib/advanced_roadmap/gruff/bar_conversion.rb +++ /dev/null @@ -1,46 +0,0 @@ -## -# Original Author: David Stokar -# -# This class perfoms the y coordinats conversion for the bar class. -# -# There are three cases: -# -# 1. Bars all go from zero in positive direction -# 2. Bars all go from zero to negative direction -# 3. Bars either go from zero to positive or from zero to negative -# -class AdvancedRoadmap::Gruff::BarConversion - attr_writer :mode - attr_writer :zero - attr_writer :graph_top - attr_writer :graph_height - attr_writer :minimum_value - attr_writer :spread - - def getLeftYRightYscaled( data_point, result ) - case @mode - when 1 then # Case one - # minimum value >= 0 ( only positiv values ) - result[0] = @graph_top + @graph_height*(1 - data_point) + 1 - result[1] = @graph_top + @graph_height - 1 - when 2 then # Case two - # only negativ values - result[0] = @graph_top + 1 - result[1] = @graph_top + @graph_height*(1 - data_point) - 1 - when 3 then # Case three - # positiv and negativ values - val = data_point-@minimum_value/@spread - if ( data_point >= @zero ) then - result[0] = @graph_top + @graph_height*(1 - (val-@zero)) + 1 - result[1] = @graph_top + @graph_height*(1 - @zero) - 1 - else - result[0] = @graph_top + @graph_height*(1 - (val-@zero)) + 1 - result[1] = @graph_top + @graph_height*(1 - @zero) - 1 - end - else - result[0] = 0.0 - result[1] = 0.0 - end - end - -end diff --git a/lib/advanced_roadmap/gruff/base.rb b/lib/advanced_roadmap/gruff/base.rb deleted file mode 100644 index 4f3d502..0000000 --- a/lib/advanced_roadmap/gruff/base.rb +++ /dev/null @@ -1,1129 +0,0 @@ -require 'rubygems' - -require File.dirname(__FILE__) + '/deprecated' - -## -# = Gruff. Graphs. -# -# Author:: Geoffrey Grosenbach boss@topfunky.com -# -# Originally Created:: October 23, 2005 -# -# Extra thanks to Tim Hunter for writing RMagick, and also contributions by -# Jarkko Laine, Mike Perham, Andreas Schwarz, Alun Eyre, Guillaume Theoret, -# David Stokar, Paul Rogers, Dave Woodward, Frank Oxener, Kevin Clark, Cies -# Breijs, Richard Cowin, and a cast of thousands. -# -# See Gruff::Base#theme= for setting themes. -module AdvancedRoadmap - module Gruff - - # This is the version of Gruff you are using. - VERSION = '0.3.6' - - class Base - - include Magick - include Deprecated - - # Draw extra lines showing where the margins and text centers are - DEBUG = false - - # Used for navigating the array of data to plot - DATA_LABEL_INDEX = 0 - DATA_VALUES_INDEX = 1 - DATA_COLOR_INDEX = 2 - - # Space around text elements. Mostly used for vertical spacing - LEGEND_MARGIN = TITLE_MARGIN = 20.0 - LABEL_MARGIN = 10.0 - DEFAULT_MARGIN = 20.0 - - DEFAULT_TARGET_WIDTH = 800 - - THOUSAND_SEPARATOR = ',' - - # Blank space above the graph - attr_accessor :top_margin - - # Blank space below the graph - attr_accessor :bottom_margin - - # Blank space to the right of the graph - attr_accessor :right_margin - - # Blank space to the left of the graph - attr_accessor :left_margin - - # Blank space below the title - attr_accessor :title_margin - - # Blank space below the legend - attr_accessor :legend_margin - - # A hash of names for the individual columns, where the key is the array - # index for the column this label represents. - # - # Not all columns need to be named. - # - # Example: 0 => 2005, 3 => 2006, 5 => 2007, 7 => 2008 - attr_accessor :labels - - # Rotation angle for labels into axis X. - # - # By default, labels are not rotated (0.0). - attr_accessor :labels_x_rotation - - # Used internally for spacing. - # - # By default, labels are centered over the point they represent. - attr_accessor :center_labels_over_point - - # Used internally for horizontal graph types. - attr_accessor :has_left_labels - - # A label for the bottom of the graph - attr_accessor :x_axis_label - - # A label for the left side of the graph - attr_accessor :y_axis_label - - # attr_accessor :x_axis_increment - - # Manually set increment of the horizontal marking lines - attr_accessor :y_axis_increment - - # Get or set the list of colors that will be used to draw the bars or lines. - attr_accessor :colors - - # The large title of the graph displayed at the top - attr_accessor :title - - # Font used for titles, labels, etc. Works best if you provide the full - # path to the TTF font file. RMagick must be built with the Freetype - # libraries for this to work properly. - # - # Uses default RMagick Redmine configured font (rmagick_font_path) - # - # The font= method below fulfills the role of the writer, so we only need - # a reader here. - attr_reader :font - - attr_accessor :font_color - - # Prevent drawing of line markers - attr_accessor :hide_line_markers - - # Prevent drawing of the legend - attr_accessor :hide_legend - - # Prevent drawing of the title - attr_accessor :hide_title - - # Prevent drawing of line numbers - attr_accessor :hide_line_numbers - - # Message shown when there is no data. Fits up to 20 characters. Defaults - # to "No Data." - attr_accessor :no_data_message - - # The font size of the large title at the top of the graph - attr_accessor :title_font_size - - # Optionally set the size of the font. Based on an 800x600px graph. - # Default is 20. - # - # Will be scaled down if graph is smaller than 800px wide. - attr_accessor :legend_font_size - - # The font size of the labels around the graph - attr_accessor :marker_font_size - - # The color of the auxiliary lines - attr_accessor :marker_color - - # The number of horizontal lines shown for reference - attr_accessor :marker_count - - # You can manually set a minimum value instead of having the values - # guessed for you. - # - # Set it after you have given all your data to the graph object. - attr_accessor :minimum_value - - # You can manually set a maximum value, such as a percentage-based graph - # that always goes to 100. - # - # If you use this, you must set it after you have given all your data to - # the graph object. - attr_accessor :maximum_value - - # Set to false if you don't want the data to be sorted with largest avg - # values at the back. - attr_accessor :sort - - # Experimental - attr_accessor :additional_line_values - - # Experimental - attr_accessor :stacked - - # Optionally set the size of the colored box by each item in the legend. - # Default is 20.0 - # - # Will be scaled down if graph is smaller than 800px wide. - attr_accessor :legend_box_size - - # If one numerical argument is given, the graph is drawn at 4/3 ratio - # according to the given width (800 results in 800x600, 400 gives 400x300, - # etc.). - # - # Or, send a geometry string for other ratios ('800x400', '400x225'). - # - # Looks for Redmine RMagick configured font as the default (rmagick_font_path) - def initialize(target_width=DEFAULT_TARGET_WIDTH) - if not Numeric === target_width - geometric_width, geometric_height = target_width.split('x') - @columns = geometric_width.to_f - @rows = geometric_height.to_f - else - @columns = target_width.to_f - @rows = target_width.to_f * 0.75 - end - - initialize_ivars - - reset_themes - theme_keynote - end - - # Set instance variables for this object. - # - # Subclasses can override this, call super, then set values separately. - # - # This makes it possible to set defaults in a subclass but still allow - # developers to change this values in their program. - def initialize_ivars - # Internal for calculations - @raw_columns = 800.0 - @raw_rows = 800.0 * (@rows/@columns) - @column_count = 0 - @marker_count = nil - @maximum_value = @minimum_value = nil - @has_data = false - @data = Array.new - @labels = Hash.new - @labels_x_rotation = 0.0 - @labels_seen = Hash.new - @sort = true - @title = nil - - @scale = @columns / @raw_columns - - vera_font_path = File.expand_path('Vera.ttf', ENV['MAGICK_FONT_PATH']) - @font = File.exists?(vera_font_path) ? vera_font_path : Redmine::Configuration['rmagick_font_path'] - - @marker_font_size = 21.0 - @legend_font_size = 20.0 - @title_font_size = 36.0 - - @top_margin = @bottom_margin = @left_margin = @right_margin = DEFAULT_MARGIN - @legend_margin = LEGEND_MARGIN - @title_margin = TITLE_MARGIN - - @legend_box_size = 20.0 - - @no_data_message = "No Data" - - @hide_line_markers = @hide_legend = @hide_title = @hide_line_numbers = false - @center_labels_over_point = true - @has_left_labels = false - - @additional_line_values = [] - @additional_line_colors = [] - @theme_options = {} - - @x_axis_label = @y_axis_label = nil - @y_axis_increment = nil - @stacked = nil - @norm_data = nil - end - - # Sets the top, bottom, left and right margins to +margin+. - def margins=(margin) - @top_margin = @left_margin = @right_margin = @bottom_margin = margin - end - - # Sets the font for graph text to the font at +font_path+. - def font=(font_path) - @font = font_path - @d.font = @font - end - - # Add a color to the list of available colors for lines. - # - # Example: - # add_color('#c0e9d3') - def add_color(colorname) - @colors << colorname - end - - # Replace the entire color list with a new array of colors. Also - # aliased as the colors= setter method. - # - # If you specify fewer colors than the number of datasets you intend - # to draw, 'increment_color' will cycle through the array, reusing - # colors as needed. - # - # Note that (as with the 'theme' method), you should set up your color - # list before you send your data (via the 'data' method). Calls to the - # 'data' method made prior to this call will use whatever color scheme - # was in place at the time data was called. - # - # Example: - # replace_colors ['#cc99cc', '#d9e043', '#34d8a2'] - def replace_colors(color_list=[]) - @colors = color_list - @color_index = 0 - end - - # You can set a theme manually. Assign a hash to this method before you - # send your data. - # - # graph.theme = { - # :colors => %w(orange purple green white red), - # :marker_color => 'blue', - # :background_colors => %w(black grey) - # } - # - # :background_image => 'squirrel.png' is also possible. - # - # (Or hopefully something better looking than that.) - # - def theme=(options) - reset_themes() - - defaults = { - :colors => ['black', 'white'], - :additional_line_colors => [], - :marker_color => 'white', - :font_color => 'black', - :background_colors => nil, - :background_image => nil - } - @theme_options = defaults.merge options - - @colors = @theme_options[:colors] - @marker_color = @theme_options[:marker_color] - @font_color = @theme_options[:font_color] || @marker_color - @additional_line_colors = @theme_options[:additional_line_colors] - - render_background - end - - # A color scheme similar to the popular presentation software. - def theme_keynote - # Colors - @blue = '#6886B4' - @yellow = '#FDD84E' - @green = '#72AE6E' - @red = '#D1695E' - @purple = '#8A6EAF' - @orange = '#EFAA43' - @white = 'white' - @colors = [@yellow, @blue, @green, @red, @purple, @orange, @white] - - self.theme = { - :colors => @colors, - :marker_color => 'white', - :font_color => 'white', - :background_colors => ['black', '#4a465a'] - } - end - - # A color scheme plucked from the colors on the popular usability blog. - def theme_37signals - # Colors - @green = '#339933' - @purple = '#cc99cc' - @blue = '#336699' - @yellow = '#FFF804' - @red = '#ff0000' - @orange = '#cf5910' - @black = 'black' - @colors = [@yellow, @blue, @green, @red, @purple, @orange, @black] - - self.theme = { - :colors => @colors, - :marker_color => 'black', - :font_color => 'black', - :background_colors => ['#d1edf5', 'white'] - } - end - - # A color scheme from the colors used on the 2005 Rails keynote - # presentation at RubyConf. - def theme_rails_keynote - # Colors - @green = '#00ff00' - @grey = '#333333' - @orange = '#ff5d00' - @red = '#f61100' - @white = 'white' - @light_grey = '#999999' - @black = 'black' - @colors = [@green, @grey, @orange, @red, @white, @light_grey, @black] - - self.theme = { - :colors => @colors, - :marker_color => 'white', - :font_color => 'white', - :background_colors => ['#0083a3', '#0083a3'] - } - end - - # A color scheme similar to that used on the popular podcast site. - def theme_odeo - # Colors - @grey = '#202020' - @white = 'white' - @dark_pink = '#a21764' - @green = '#8ab438' - @light_grey = '#999999' - @dark_blue = '#3a5b87' - @black = 'black' - @colors = [@grey, @white, @dark_blue, @dark_pink, @green, @light_grey, @black] - - self.theme = { - :colors => @colors, - :marker_color => 'white', - :font_color => 'white', - :background_colors => ['#ff47a4', '#ff1f81'] - } - end - - # A pastel theme - def theme_pastel - # Colors - @colors = [ - '#a9dada', # blue - '#aedaa9', # green - '#daaea9', # peach - '#dadaa9', # yellow - '#a9a9da', # dk purple - '#daaeda', # purple - '#dadada' # grey - ] - - self.theme = { - :colors => @colors, - :marker_color => '#aea9a9', # Grey - :font_color => 'black', - :background_colors => 'white' - } - end - - # A greyscale theme - def theme_greyscale - # Colors - @colors = [ - '#282828', # - '#383838', # - '#686868', # - '#989898', # - '#c8c8c8', # - '#e8e8e8', # - ] - - self.theme = { - :colors => @colors, - :marker_color => '#aea9a9', # Grey - :font_color => 'black', - :background_colors => 'white' - } - end - - # Parameters are an array where the first element is the name of the dataset - # and the value is an array of values to plot. - # - # Can be called multiple times with different datasets for a multi-valued - # graph. - # - # If the color argument is nil, the next color from the default theme will - # be used. - # - # NOTE: If you want to use a preset theme, you must set it before calling - # data(). - # - # Example: - # data("Bart S.", [95, 45, 78, 89, 88, 76], '#ffcc00') - def data(name, data_points=[], color=nil) - data_points = Array(data_points) # make sure it's an array - @data << [name, data_points, (color || increment_color)] - # Set column count if this is larger than previous counts - @column_count = (data_points.length > @column_count) ? data_points.length : @column_count - - # Pre-normalize - data_points.each_with_index do |data_point, index| - next if data_point.nil? - - # Setup max/min so spread starts at the low end of the data points - if @maximum_value.nil? && @minimum_value.nil? - @maximum_value = @minimum_value = data_point - end - - # TODO Doesn't work with stacked bar graphs - # Original: @maximum_value = larger_than_max?(data_point, index) ? max(data_point, index) : @maximum_value - @maximum_value = larger_than_max?(data_point) ? data_point : @maximum_value - @has_data = true if @maximum_value >= 0 - - @minimum_value = less_than_min?(data_point) ? data_point : @minimum_value - @has_data = true if @minimum_value < 0 - end - end - - # Writes the graph to a file. Defaults to 'graph.png' - # - # Example: - # write('graphs/my_pretty_graph.png') - def write(filename="graph.png") - draw() - @base_image.write(filename) - end - - # Return the graph as a rendered binary blob. - def to_blob(fileformat='PNG') - draw() - return @base_image.to_blob do - self.format = fileformat - end - end - - - - protected - - # Overridden by subclasses to do the actual plotting of the graph. - # - # Subclasses should start by calling super() for this method. - def draw - make_stacked if @stacked - setup_drawing - - debug { - # Outer margin - @d.rectangle( @left_margin, @top_margin, - @raw_columns - @right_margin, @raw_rows - @bottom_margin) - # Graph area box - @d.rectangle( @graph_left, @graph_top, @graph_right, @graph_bottom) - } - end - - # Calculates size of drawable area and draws the decorations. - # - # * line markers - # * legend - # * title - def setup_drawing - # Maybe should be done in one of the following functions for more granularity. - unless @has_data - draw_no_data() - return - end - - normalize() - setup_graph_measurements() - sort_norm_data() if @sort # Sort norm_data with avg largest values set first (for display) - - draw_legend() - draw_line_markers() - draw_axis_labels() - draw_title - end - - # Make copy of data with values scaled between 0-100 - def normalize(force=false) - if @norm_data.nil? || force - @norm_data = [] - return unless @has_data - - calculate_spread - - @data.each do |data_row| - norm_data_points = [] - data_row[DATA_VALUES_INDEX].each do |data_point| - if data_point.nil? - norm_data_points << nil - else - norm_data_points << ((data_point.to_f - @minimum_value.to_f ) / @spread) - end - end - @norm_data << [data_row[DATA_LABEL_INDEX], norm_data_points, data_row[DATA_COLOR_INDEX]] - end - end - end - - def calculate_spread # :nodoc: - @spread = @maximum_value.to_f - @minimum_value.to_f - @spread = @spread > 0 ? @spread : 1 - end - - ## - # Calculates size of drawable area, general font dimensions, etc. - - def setup_graph_measurements - @marker_caps_height = @hide_line_markers ? 0 : - calculate_caps_height(@marker_font_size) - @title_caps_height = @hide_title ? 0 : - calculate_caps_height(@title_font_size) - @legend_caps_height = @hide_legend ? 0 : - calculate_caps_height(@legend_font_size) - - if @hide_line_markers - (@graph_left, - @graph_right_margin, - @graph_bottom_margin) = [@left_margin, @right_margin, @bottom_margin] - else - longest_left_label_width = 0 - if @has_left_labels - longest_left_label_width = calculate_width(@marker_font_size, - labels.values.inject('') { |value, memo| (value.to_s.length > memo.to_s.length) ? value : memo }) * 1.25 - else - longest_left_label_width = calculate_width(@marker_font_size, - label(@maximum_value.to_f)) - end - - # Shift graph if left line numbers are hidden - line_number_width = @hide_line_numbers && !@has_left_labels ? - 0.0 : - (longest_left_label_width + LABEL_MARGIN * 2) - - @graph_left = @left_margin + - line_number_width + - (@y_axis_label.nil? ? 0.0 : @marker_caps_height + LABEL_MARGIN * 2) - - # Make space for half the width of the rightmost column label. - # Might be greater than the number of columns if between-style bar markers are used. - last_label = @labels.keys.sort.last.to_i - extra_room_for_long_label = (last_label >= (@column_count-1) && @center_labels_over_point) ? - calculate_width(@marker_font_size, @labels[last_label]) / 2.0 : - 0 - @graph_right_margin = @right_margin + extra_room_for_long_label - - @graph_bottom_margin = @bottom_margin + - @marker_caps_height + LABEL_MARGIN - end - - @graph_right = @raw_columns - @graph_right_margin - @graph_width = @raw_columns - @graph_left - @graph_right_margin - - # When @hide title, leave a title_margin space for aesthetics. - # Same with @hide_legend - @graph_top = @top_margin + - (@hide_title ? title_margin : @title_caps_height + title_margin ) + - (@hide_legend ? legend_margin : @legend_caps_height + legend_margin) - - x_axis_label_height = @x_axis_label.nil? ? 0.0 : - @marker_caps_height + LABEL_MARGIN - @graph_bottom = @raw_rows - @graph_bottom_margin - x_axis_label_height - @graph_height = @graph_bottom - @graph_top - end - - # Draw the optional labels for the x axis and y axis. - def draw_axis_labels - unless @x_axis_label.nil? - # X Axis - # Centered vertically and horizontally by setting the - # height to 1.0 and the width to the width of the graph. - x_axis_label_y_coordinate = @graph_bottom + LABEL_MARGIN * 2 + @marker_caps_height - - # TODO Center between graph area - @d.fill = @font_color - @d.font = @font if @font - @d.stroke('transparent') - @d.pointsize = scale_fontsize(@marker_font_size) - @d.gravity = NorthGravity - @d = @d.annotate_scaled( @base_image, - @raw_columns, 1.0, - 0.0, x_axis_label_y_coordinate, - @x_axis_label, @scale) - debug { @d.line 0.0, x_axis_label_y_coordinate, @raw_columns, x_axis_label_y_coordinate } - end - - unless @y_axis_label.nil? - # Y Axis, rotated vertically - @d.rotation = 90.0 - @d.gravity = CenterGravity - @d = @d.annotate_scaled( @base_image, - 1.0, @raw_rows, - @left_margin + @marker_caps_height / 2.0, 0.0, - @y_axis_label, @scale) - @d.rotation = -90.0 - end - end - - # Draws horizontal background lines and labels - def draw_line_markers - return if @hide_line_markers - - @d = @d.stroke_antialias false - - if @y_axis_increment.nil? - # Try to use a number of horizontal lines that will come out even. - # - # TODO Do the same for larger numbers...100, 75, 50, 25 - if @marker_count.nil? - (3..7).each do |lines| - if @spread % lines == 0.0 - @marker_count = lines - break - end - end - @marker_count ||= 4 - end - @increment = (@spread > 0) ? significant(@spread / @marker_count) : 1 - else - # TODO Make this work for negative values - @maximum_value = [@maximum_value.ceil, @y_axis_increment].max - @minimum_value = @minimum_value.floor - calculate_spread - normalize(true) - - @marker_count = (@spread / @y_axis_increment).to_i - @increment = @y_axis_increment - end - @increment_scaled = @graph_height.to_f / (@spread / @increment) - - # Draw horizontal line markers and annotate with numbers - (0..@marker_count).each do |index| - y = @graph_top + @graph_height - index.to_f * @increment_scaled - - @d = @d.fill(@marker_color) - @d = @d.line(@graph_left, y, @graph_right, y) - - marker_label = index * @increment + @minimum_value.to_f - - unless @hide_line_numbers - @d.fill = @font_color - @d.font = @font if @font - @d.stroke('transparent') - @d.pointsize = scale_fontsize(@marker_font_size) - @d.gravity = EastGravity - - # Vertically center with 1.0 for the height - @d = @d.annotate_scaled( @base_image, - @graph_left - LABEL_MARGIN, 1.0, - 0.0, y, - label(marker_label), @scale) - end - end - - # # Submitted by a contibutor...the utility escapes me - # i = 0 - # @additional_line_values.each do |value| - # @increment_scaled = @graph_height.to_f / (@maximum_value.to_f / value) - # - # y = @graph_top + @graph_height - @increment_scaled - # - # @d = @d.stroke(@additional_line_colors[i]) - # @d = @d.line(@graph_left, y, @graph_right, y) - # - # - # @d.fill = @additional_line_colors[i] - # @d.font = @font if @font - # @d.stroke('transparent') - # @d.pointsize = scale_fontsize(@marker_font_size) - # @d.gravity = EastGravity - # @d = @d.annotate_scaled( @base_image, - # 100, 20, - # -10, y - (@marker_font_size/2.0), - # "", @scale) - # i += 1 - # end - - @d = @d.stroke_antialias true - end - - ## - # Return the sum of values in an array. - # - # Duplicated to not conflict with active_support in Rails. - - def sum(arr) - arr.inject(0) { |i, m| m + i } - end - - ## - # Return a calculation of center - - def center(size) - (@raw_columns - size) / 2 - end - - ## - # Draws a legend with the names of the datasets matched - # to the colors used to draw them. - - def draw_legend - return if @hide_legend - - @legend_labels = @data.collect {|item| item[DATA_LABEL_INDEX] } - - legend_square_width = @legend_box_size # small square with color of this item - - # May fix legend drawing problem at small sizes - @d.font = @font if @font - @d.pointsize = @legend_font_size - - label_widths = [[]] # Used to calculate line wrap - @legend_labels.each do |label| - metrics = @d.get_type_metrics(@base_image, label.to_s) - label_width = metrics.width + legend_square_width * 2.7 - label_widths.last.push label_width - - if sum(label_widths.last) > (@raw_columns * 0.9) - label_widths.push [label_widths.last.pop] - end - end - - current_x_offset = center(sum(label_widths.first)) - current_y_offset = @hide_title ? - @top_margin + title_margin : - @top_margin + title_margin + @title_caps_height - - @legend_labels.each_with_index do |legend_label, index| - - # Draw label - @d.fill = @font_color - @d.font = @font if @font - @d.pointsize = scale_fontsize(@legend_font_size) - @d.stroke('transparent') - @d.font_weight = NormalWeight - @d.gravity = WestGravity - @d = @d.annotate_scaled( @base_image, - @raw_columns, 1.0, - current_x_offset + (legend_square_width * 1.7), current_y_offset, - legend_label.to_s, @scale) - - # Now draw box with color of this dataset - @d = @d.stroke('transparent') - @d = @d.fill @data[index][DATA_COLOR_INDEX] - @d = @d.rectangle(current_x_offset, - current_y_offset - legend_square_width / 2.0, - current_x_offset + legend_square_width, - current_y_offset + legend_square_width / 2.0) - - @d.pointsize = @legend_font_size - metrics = @d.get_type_metrics(@base_image, legend_label.to_s) - current_string_offset = metrics.width + (legend_square_width * 2.7) - - # Handle wrapping - label_widths.first.shift - if label_widths.first.empty? - debug { @d.line 0.0, current_y_offset, @raw_columns, current_y_offset } - - label_widths.shift - current_x_offset = center(sum(label_widths.first)) unless label_widths.empty? - line_height = [@legend_caps_height, legend_square_width].max + legend_margin - if label_widths.length > 0 - # Wrap to next line and shrink available graph dimensions - current_y_offset += line_height - @graph_top += line_height - @graph_height = @graph_bottom - @graph_top - end - else - current_x_offset += current_string_offset - end - end - @color_index = 0 - end - - # Draws a title on the graph. - def draw_title - return if (@hide_title || @title.nil?) - - @d.fill = @font_color - @d.font = @font if @font - @d.stroke('transparent') - @d.pointsize = scale_fontsize(@title_font_size) - @d.font_weight = BoldWeight - @d.gravity = NorthGravity - @d = @d.annotate_scaled( @base_image, - @raw_columns, 1.0, - 0, @top_margin, - @title, @scale) - end - - # Draws column labels below graph, centered over x_offset - #-- - # TODO Allow WestGravity as an option - def draw_label(x_offset, index) - return if @hide_line_markers - - if !@labels[index].nil? && @labels_seen[index].nil? - y_offset = @graph_bottom + LABEL_MARGIN - - @d.fill = @font_color - @d.font = @font if @font - @d.stroke('transparent') - @d.font_weight = NormalWeight - @d.pointsize = scale_fontsize(@marker_font_size) - @d.gravity = NorthGravity - @d.rotation = @labels_x_rotation - @d = @d.annotate_scaled(@base_image, - 1.0, 1.0, - x_offset, y_offset, - @labels[index], @scale) - @d.rotation = -@labels_x_rotation - @labels_seen[index] = 1 - debug { @d.line 0.0, y_offset, @raw_columns, y_offset } - end - end - - # Shows an error message because you have no data. - def draw_no_data - @d.fill = @font_color - @d.font = @font if @font - @d.stroke('transparent') - @d.font_weight = NormalWeight - @d.pointsize = scale_fontsize(80) - @d.gravity = CenterGravity - @d = @d.annotate_scaled( @base_image, - @raw_columns, @raw_rows/2.0, - 0, 10, - @no_data_message, @scale) - end - - # Finds the best background to render based on the provided theme options. - # - # Creates a @base_image to draw on. - def render_background - case @theme_options[:background_colors] - when Array - @base_image = render_gradiated_background(*@theme_options[:background_colors]) - when String - @base_image = render_solid_background(@theme_options[:background_colors]) - else - @base_image = render_image_background(*@theme_options[:background_image]) - end - end - - # Make a new image at the current size with a solid +color+. - def render_solid_background(color) - Image.new(@columns, @rows) { - self.background_color = color - } - end - - # Use with a theme definition method to draw a gradiated background. - def render_gradiated_background(top_color, bottom_color) - Image.new(@columns, @rows, - GradientFill.new(0, 0, 100, 0, top_color, bottom_color)) - end - - # Use with a theme to use an image (800x600 original) background. - def render_image_background(image_path) - image = Image.read(image_path) - if @scale != 1.0 - image[0].resize!(@scale) # TODO Resize with new scale (crop if necessary for wide graph) - end - image[0] - end - - # Use with a theme to make a transparent background - def render_transparent_background - Image.new(@columns, @rows) do - self.background_color = 'transparent' - end - end - - # Resets everything to defaults (except data). - def reset_themes - @color_index = 0 - @labels_seen = {} - @theme_options = {} - - @d = Draw.new - # Scale down from 800x600 used to calculate drawing. - @d = @d.scale(@scale, @scale) - end - - def scale(value) # :nodoc: - value * @scale - end - - # Return a comparable fontsize for the current graph. - def scale_fontsize(value) - new_fontsize = value * @scale - # return new_fontsize < 10.0 ? 10.0 : new_fontsize - return new_fontsize - end - - def clip_value_if_greater_than(value, max_value) # :nodoc: - (value > max_value) ? max_value : value - end - - # Overridden by subclasses such as stacked bar. - def larger_than_max?(data_point, index=0) # :nodoc: - data_point > @maximum_value - end - - def less_than_min?(data_point, index=0) # :nodoc: - data_point < @minimum_value - end - - # Overridden by subclasses that need it. - def max(data_point, index) # :nodoc: - data_point - end - - # Overridden by subclasses that need it. - def min(data_point, index) # :nodoc: - data_point - end - - def significant(inc) # :nodoc: - return 1.0 if inc == 0 # Keep from going into infinite loop - factor = 1.0 - while (inc < 10) - inc *= 10 - factor /= 10 - end - - while (inc > 100) - inc /= 10 - factor *= 10 - end - - res = inc.floor * factor - if (res.to_i.to_f == res) - res.to_i - else - res - end - end - - # Sort with largest overall summed value at front of array so it shows up - # correctly in the drawn graph. - def sort_norm_data - @norm_data.sort! { |a,b| sums(b[DATA_VALUES_INDEX]) <=> sums(a[DATA_VALUES_INDEX]) } - end - - def sums(data_set) # :nodoc: - total_sum = 0 - data_set.collect {|num| total_sum += num.to_f } - total_sum - end - - # Used by StackedBar and child classes. - # - # May need to be moved to the StackedBar class. - def get_maximum_by_stack - # Get sum of each stack - max_hash = {} - @data.each do |data_set| - data_set[DATA_VALUES_INDEX].each_with_index do |data_point, i| - max_hash[i] = 0.0 unless max_hash[i] - max_hash[i] += data_point.to_f - end - end - - # @maximum_value = 0 - max_hash.keys.each do |key| - @maximum_value = max_hash[key] if max_hash[key] > @maximum_value - end - @minimum_value = 0 - end - - def make_stacked # :nodoc: - stacked_values = Array.new(@column_count, 0) - @data.each do |value_set| - value_set[DATA_VALUES_INDEX].each_with_index do |value, index| - stacked_values[index] += value - end - value_set[DATA_VALUES_INDEX] = stacked_values.dup - end - end - - private - - # Takes a block and draws it if DEBUG is true. - # - # Example: - # debug { @d.rectangle x1, y1, x2, y2 } - def debug - if DEBUG - @d = @d.fill 'transparent' - @d = @d.stroke 'turquoise' - @d = yield - end - end - - # Returns the next color in your color list. - def increment_color - @color_index = (@color_index + 1) % @colors.length - return @colors[@color_index - 1] - end - - # Return a formatted string representing a number value that should be - # printed as a label. - def label(value) - label = if (@spread.to_f % @marker_count.to_f == 0) || !@y_axis_increment.nil? - value.to_i.to_s - elsif @spread > 10.0 - sprintf("%0i", value) - elsif @spread >= 3.0 - sprintf("%0.2f", value) - else - value.to_s - end - - parts = label.split('.') - parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{THOUSAND_SEPARATOR}") - parts.join('.') - end - - # Returns the height of the capital letter 'X' for the current font and - # size. - # - # Not scaled since it deals with dimensions that the regular scaling will - # handle. - def calculate_caps_height(font_size) - @d.pointsize = font_size - @d.get_type_metrics(@base_image, 'X').height - end - - # Returns the width of a string at this pointsize. - # - # Not scaled since it deals with dimensions that the regular - # scaling will handle. - def calculate_width(font_size, text) - @d.pointsize = font_size - @d.get_type_metrics(@base_image, text.to_s).width - end - - end # Gruff::Base - - class IncorrectNumberOfDatasetsException < StandardError; end - - end # Gruff -end - -module Magick - - class Draw - - # Additional method to scale annotation text since Draw.scale doesn't. - def annotate_scaled(img, width, height, x, y, text, scale) - scaled_width = (width * scale) >= 1 ? (width * scale) : 1 - scaled_height = (height * scale) >= 1 ? (height * scale) : 1 - - self.annotate( img, - scaled_width, scaled_height, - x * scale, y * scale, - text) - end - - end - -end # Magick diff --git a/lib/advanced_roadmap/gruff/bullet.rb b/lib/advanced_roadmap/gruff/bullet.rb deleted file mode 100644 index 3414c87..0000000 --- a/lib/advanced_roadmap/gruff/bullet.rb +++ /dev/null @@ -1,109 +0,0 @@ -require File.dirname(__FILE__) + '/base' - -class AdvancedRoadmap::Gruff::Bullet < AdvancedRoadmap::Gruff::Base - - def initialize(target_width="400x40") - if not Numeric === target_width - geometric_width, geometric_height = target_width.split('x') - @columns = geometric_width.to_f - @rows = geometric_height.to_f - else - @columns = target_width.to_f - @rows = target_width.to_f / 5.0 - end - - initialize_ivars - - reset_themes - theme_greyscale - @title_font_size = 20 - end - - def data(value, maximum_value, options={}) - @value = value.to_f - @maximum_value = maximum_value.to_f - @options = options - @options.map { |k, v| @options[k] = v.to_f if v === Numeric } - end - - # def setup_drawing - # # Maybe should be done in one of the following functions for more granularity. - # unless @has_data - # draw_no_data() - # return - # end - # - # normalize() - # setup_graph_measurements() - # sort_norm_data() if @sort # Sort norm_data with avg largest values set first (for display) - # - # draw_legend() - # draw_line_markers() - # draw_axis_labels() - # draw_title - # end - - def draw - # TODO Left label - # TODO Bottom labels and markers - # @graph_bottom - # Calculations are off 800x??? - - @colors.reverse! - - draw_title - - @margin = 30.0 - @thickness = @raw_rows / 6.0 - @right_margin = @margin - @graph_left = @title_width * 1.3 rescue @margin # HACK Need to calculate real width - @graph_width = @raw_columns - @graph_left - @right_margin - @graph_height = @thickness * 3.0 - - # Background - @d = @d.fill @colors[0] - @d = @d.rectangle(@graph_left, 0, @graph_left + @graph_width, @graph_height) - - [:high, :low].each_with_index do |indicator, index| - next unless @options.has_key?(indicator) - @d = @d.fill @colors[index + 1] - indicator_width_x = @graph_left + @graph_width * (@options[indicator] / @maximum_value) - @d = @d.rectangle(@graph_left, 0, indicator_width_x, @graph_height) - end - - if @options.has_key?(:target) - @d = @d.fill @font_color - target_x = @graph_left + @graph_width * (@options[:target] / @maximum_value) - half_thickness = @thickness / 2.0 - @d = @d.rectangle(target_x, half_thickness, target_x + half_thickness, @thickness * 2 + half_thickness) - end - - # Value - @d = @d.fill @font_color - @d = @d.rectangle(@graph_left, @thickness, @graph_left + @graph_width * (@value / @maximum_value), @thickness * 2) - - @d.draw(@base_image) - end - - def draw_title - return unless @title - - @font_height = calculate_caps_height(scale_fontsize(@title_font_size)) - @title_width = calculate_width(@title_font_size, @title) - - @d.fill = @font_color - @d.font = @font if @font - @d.stroke('transparent') - @d.font_weight = NormalWeight - @d.pointsize = scale_fontsize(@title_font_size) - @d.gravity = NorthWestGravity - @d = @d.annotate_scaled(*[ - @base_image, - 1.0, 1.0, - @font_height/2, @font_height/2, - @title, - @scale - ]) - end - -end diff --git a/lib/advanced_roadmap/gruff/deprecated.rb b/lib/advanced_roadmap/gruff/deprecated.rb deleted file mode 100644 index 0d9d9d4..0000000 --- a/lib/advanced_roadmap/gruff/deprecated.rb +++ /dev/null @@ -1,41 +0,0 @@ - -## -# A mixin for methods that need to be deleted or have been -# replaced by cleaner code. - -module AdvancedRoadmap - module Gruff - module Deprecated - - def scale_measurements - setup_graph_measurements - end - - def total_height - @rows + 10 - end - - def graph_top - @graph_top * @scale - end - - def graph_height - @graph_height * @scale - end - - def graph_left - @graph_left * @scale - end - - def graph_width - @graph_width * @scale - end - - # TODO Should be calculate_graph_height - # def setup_graph_height - # @graph_height = @graph_bottom - @graph_top - # end - - end - end -end diff --git a/lib/advanced_roadmap/gruff/dot.rb b/lib/advanced_roadmap/gruff/dot.rb deleted file mode 100644 index 90639f3..0000000 --- a/lib/advanced_roadmap/gruff/dot.rb +++ /dev/null @@ -1,113 +0,0 @@ -require File.dirname(__FILE__) + '/base' - -## -# Graph with dots and labels along a vertical access -# see: 'Creating More Effective Graphs' by Robbins - -class AdvancedRoadmap::Gruff::Dot < AdvancedRoadmap::Gruff::Base - - def draw - @has_left_labels = true - super - - return unless @has_data - - # Setup spacing. - # - spacing_factor = 1.0 - - @items_width = @graph_height / @column_count.to_f - @item_width = @items_width * spacing_factor / @norm_data.size - @d = @d.stroke_opacity 0.0 - height = Array.new(@column_count, 0) - length = Array.new(@column_count, @graph_left) - padding = (@items_width * (1 - spacing_factor)) / 2 - - @norm_data.each_with_index do |data_row, row_index| - data_row[DATA_VALUES_INDEX].each_with_index do |data_point, point_index| - - x_pos = @graph_left + (data_point * @graph_width) - (@item_width.to_f/6.0).round - y_pos = @graph_top + (@items_width * point_index) + padding + (@item_width.to_f/2.0).round - - if row_index == 0 - @d = @d.stroke(@marker_color) - @d = @d.stroke_width 1.0 - @d = @d.opacity 0.1 - @d = @d.line(@graph_left, y_pos, @graph_left + @graph_width, y_pos) - end - - @d = @d.fill data_row[DATA_COLOR_INDEX] - @d = @d.stroke('transparent') - @d = @d.circle(x_pos, y_pos, x_pos + (@item_width.to_f/3.0).round, y_pos) - - # Calculate center based on item_width and current row - label_center = @graph_top + (@items_width * point_index + @items_width / 2) + padding - draw_label(label_center, point_index) - end - - end - - @d.draw(@base_image) - end - -protected - - # Instead of base class version, draws vertical background lines and label - def draw_line_markers - - return if @hide_line_markers - - @d = @d.stroke_antialias false - - # Draw horizontal line markers and annotate with numbers - @d = @d.stroke(@marker_color) - @d = @d.stroke_width 1 - number_of_lines = 5 - - # TODO Round maximum marker value to a round number like 100, 0.1, 0.5, etc. - increment = significant(@maximum_value.to_f / number_of_lines) - (0..number_of_lines).each do |index| - - line_diff = (@graph_right - @graph_left) / number_of_lines - x = @graph_right - (line_diff * index) - 1 - @d = @d.line(x, @graph_bottom, x, @graph_bottom + 0.5 * LABEL_MARGIN) - diff = index - number_of_lines - marker_label = diff.abs * increment - - unless @hide_line_numbers - @d.fill = @font_color - @d.font = @font if @font - @d.stroke = 'transparent' - @d.pointsize = scale_fontsize(@marker_font_size) - @d.gravity = CenterGravity - # TODO Center text over line - @d = @d.annotate_scaled( @base_image, - 0, 0, # Width of box to draw text in - x, @graph_bottom + (LABEL_MARGIN * 2.0), # Coordinates of text - marker_label.to_s, @scale) - end # unless - @d = @d.stroke_antialias true - end - end - - ## - # Draw on the Y axis instead of the X - - def draw_label(y_offset, index) - if !@labels[index].nil? && @labels_seen[index].nil? - @d.fill = @font_color - @d.font = @font if @font - @d.stroke = 'transparent' - @d.font_weight = NormalWeight - @d.pointsize = scale_fontsize(@marker_font_size) - @d.gravity = EastGravity - @d = @d.annotate_scaled(@base_image, - 1, 1, - -@graph_left + LABEL_MARGIN * 2.0, y_offset, - @labels[index], @scale) - @labels_seen[index] = 1 - end - end - -end - diff --git a/lib/advanced_roadmap/gruff/line.rb b/lib/advanced_roadmap/gruff/line.rb deleted file mode 100644 index dc7085f..0000000 --- a/lib/advanced_roadmap/gruff/line.rb +++ /dev/null @@ -1,135 +0,0 @@ - -require File.dirname(__FILE__) + '/base' - -## -# Here's how to make a Line graph: -# -# g = Gruff::Line.new -# g.title = "A Line Graph" -# g.data 'Fries', [20, 23, 19, 8] -# g.data 'Hamburgers', [50, 19, 99, 29] -# g.write("test/output/line.png") -# -# There are also other options described below, such as #baseline_value, #baseline_color, #hide_dots, and #hide_lines. - -class AdvancedRoadmap::Gruff::Line < AdvancedRoadmap::Gruff::Base - - # Draw a dashed line at the given value - attr_accessor :baseline_value - - # Color of the baseline - attr_accessor :baseline_color - - # Dimensions of lines and dots; calculated based on dataset size if left unspecified - attr_accessor :line_width - attr_accessor :dot_radius - - # Hide parts of the graph to fit more datapoints, or for a different appearance. - attr_accessor :hide_dots, :hide_lines - - # Call with target pixel width of graph (800, 400, 300), and/or 'false' to omit lines (points only). - # - # g = Gruff::Line.new(400) # 400px wide with lines - # - # g = Gruff::Line.new(400, false) # 400px wide, no lines (for backwards compatibility) - # - # g = Gruff::Line.new(false) # Defaults to 800px wide, no lines (for backwards compatibility) - # - # The preferred way is to call hide_dots or hide_lines instead. - def initialize(*args) - raise ArgumentError, "Wrong number of arguments" if args.length > 2 - if args.empty? or ((not Numeric === args.first) && (not String === args.first)) then - super() - else - super args.shift - end - - @hide_dots = @hide_lines = false - @baseline_color = 'red' - @baseline_value = nil - end - - def draw - super - - return unless @has_data - - # Check to see if more than one datapoint was given. NaN can result otherwise. - @x_increment = (@column_count > 1) ? (@graph_width / (@column_count - 1).to_f) : @graph_width - - if (defined?(@norm_baseline)) then - level = @graph_top + (@graph_height - @norm_baseline * @graph_height) - @d = @d.push - @d.stroke_color @baseline_color - @d.fill_opacity 0.0 - @d.stroke_dasharray(10, 20) - @d.stroke_width 5 - @d.line(@graph_left, level, @graph_left + @graph_width, level) - @d = @d.pop - end - - @norm_data.each do |data_row| - prev_x = prev_y = nil - - @one_point = contains_one_point_only?(data_row) - - data_row[DATA_VALUES_INDEX].each_with_index do |data_point, index| - new_x = @graph_left + (@x_increment * index) - next if data_point.nil? - - draw_label(new_x, index) - - new_y = @graph_top + (@graph_height - data_point * @graph_height) - - # Reset each time to avoid thin-line errors - @d = @d.stroke data_row[DATA_COLOR_INDEX] - @d = @d.fill data_row[DATA_COLOR_INDEX] - @d = @d.stroke_opacity 1.0 - @d = @d.stroke_width line_width || - clip_value_if_greater_than(@columns / (@norm_data.first[DATA_VALUES_INDEX].size * 4), 5.0) - - - circle_radius = dot_radius || - clip_value_if_greater_than(@columns / (@norm_data.first[DATA_VALUES_INDEX].size * 2.5), 5.0) - - if !@hide_lines and !prev_x.nil? and !prev_y.nil? then - @d = @d.line(prev_x, prev_y, new_x, new_y) - elsif @one_point - # Show a circle if there's just one_point - @d = @d.circle(new_x, new_y, new_x - circle_radius, new_y) - end - @d = @d.circle(new_x, new_y, new_x - circle_radius, new_y) unless @hide_dots - - prev_x = new_x - prev_y = new_y - end - - end - - @d.draw(@base_image) - end - - def normalize - @maximum_value = [@maximum_value.to_f, @baseline_value.to_f].max - super - @norm_baseline = (@baseline_value.to_f / @maximum_value.to_f) if @baseline_value - end - - def contains_one_point_only?(data_row) - # Spin through data to determine if there is just one_value present. - one_point = false - data_row[DATA_VALUES_INDEX].each do |data_point| - if !data_point.nil? - if one_point - # more than one point, bail - return false - else - # there is at least one data point - return true - end - end - end - return one_point - end - -end diff --git a/lib/advanced_roadmap/gruff/mini/bar.rb b/lib/advanced_roadmap/gruff/mini/bar.rb deleted file mode 100644 index 5ee4124..0000000 --- a/lib/advanced_roadmap/gruff/mini/bar.rb +++ /dev/null @@ -1,37 +0,0 @@ -## -# -# Makes a small bar graph suitable for display at 200px or even smaller. -# -module Gruff - module Mini - - class Bar < Gruff::Bar - - include Gruff::Mini::Legend - - def initialize_ivars - super - - @hide_legend = true - @hide_title = true - @hide_line_numbers = true - - @marker_font_size = 50.0 - @minimum_value = 0.0 - @maximum_value = 0.0 - @legend_font_size = 60.0 - end - - def draw - expand_canvas_for_vertical_legend - - super - - draw_vertical_legend - @d.draw(@base_image) - end - - end - - end -end diff --git a/lib/advanced_roadmap/gruff/mini/legend.rb b/lib/advanced_roadmap/gruff/mini/legend.rb deleted file mode 100644 index b869cb1..0000000 --- a/lib/advanced_roadmap/gruff/mini/legend.rb +++ /dev/null @@ -1,82 +0,0 @@ -module Gruff - module Mini - module Legend - - attr_accessor :hide_mini_legend - - ## - # The canvas needs to be bigger so we can put the legend beneath it. - - def expand_canvas_for_vertical_legend - return if @hide_mini_legend - - @original_rows = @raw_rows - @rows += @data.length * calculate_caps_height(scale_fontsize(@legend_font_size)) * 1.7 - render_background - end - - ## - # Draw the legend beneath the existing graph. - - def draw_vertical_legend - return if @hide_mini_legend - - @legend_labels = @data.collect {|item| item[Gruff::Base::DATA_LABEL_INDEX] } - - legend_square_width = 40.0 # small square with color of this item - legend_square_margin = 10.0 - @legend_left_margin = 100.0 - legend_top_margin = 40.0 - - # May fix legend drawing problem at small sizes - @d.font = @font if @font - @d.pointsize = @legend_font_size - - current_x_offset = @legend_left_margin - current_y_offset = @original_rows + legend_top_margin - - debug { @d.line 0.0, current_y_offset, @raw_columns, current_y_offset } - - @legend_labels.each_with_index do |legend_label, index| - - # Draw label - @d.fill = @font_color - @d.font = @font if @font - @d.pointsize = scale_fontsize(@legend_font_size) - @d.stroke = 'transparent' - @d.font_weight = Magick::NormalWeight - @d.gravity = Magick::WestGravity - @d = @d.annotate_scaled( @base_image, - @raw_columns, 1.0, - current_x_offset + (legend_square_width * 1.7), current_y_offset, - truncate_legend_label(legend_label), @scale) - - # Now draw box with color of this dataset - @d = @d.stroke 'transparent' - @d = @d.fill @data[index][Gruff::Base::DATA_COLOR_INDEX] - @d = @d.rectangle(current_x_offset, - current_y_offset - legend_square_width / 2.0, - current_x_offset + legend_square_width, - current_y_offset + legend_square_width / 2.0) - - current_y_offset += calculate_caps_height(@legend_font_size) * 1.7 - end - @color_index = 0 - end - - ## - # Shorten long labels so they will fit on the canvas. - # - # Department of Hu... - - def truncate_legend_label(label) - truncated_label = label.to_s - while calculate_width(scale_fontsize(@legend_font_size), truncated_label) > (@columns - @legend_left_margin - @right_margin) && (truncated_label.length > 1) - truncated_label = truncated_label[0..truncated_label.length-2] - end - truncated_label + (truncated_label.length < label.to_s.length ? "..." : '') - end - - end - end -end diff --git a/lib/advanced_roadmap/gruff/mini/pie.rb b/lib/advanced_roadmap/gruff/mini/pie.rb deleted file mode 100644 index 7822ba5..0000000 --- a/lib/advanced_roadmap/gruff/mini/pie.rb +++ /dev/null @@ -1,36 +0,0 @@ -## -# -# Makes a small pie graph suitable for display at 200px or even smaller. -# -module Gruff - module Mini - - class Pie < Gruff::Pie - - include Gruff::Mini::Legend - - def initialize_ivars - super - - @hide_legend = true - @hide_title = true - @hide_line_numbers = true - - @marker_font_size = 60.0 - @legend_font_size = 60.0 - end - - def draw - expand_canvas_for_vertical_legend - - super - - draw_vertical_legend - - @d.draw(@base_image) - end # def draw - - end # class Pie - - end -end diff --git a/lib/advanced_roadmap/gruff/mini/side_bar.rb b/lib/advanced_roadmap/gruff/mini/side_bar.rb deleted file mode 100644 index 46be4f4..0000000 --- a/lib/advanced_roadmap/gruff/mini/side_bar.rb +++ /dev/null @@ -1,35 +0,0 @@ -## -# -# Makes a small pie graph suitable for display at 200px or even smaller. -# -module Gruff - module Mini - - class SideBar < Gruff::SideBar - - include Gruff::Mini::Legend - - def initialize_ivars - super - @hide_legend = true - @hide_title = true - @hide_line_numbers = true - - @marker_font_size = 50.0 - @legend_font_size = 50.0 - end - - def draw - expand_canvas_for_vertical_legend - - super - - draw_vertical_legend - - @d.draw(@base_image) - end - - end - - end -end diff --git a/lib/advanced_roadmap/gruff/net.rb b/lib/advanced_roadmap/gruff/net.rb deleted file mode 100644 index 47a8bb7..0000000 --- a/lib/advanced_roadmap/gruff/net.rb +++ /dev/null @@ -1,140 +0,0 @@ - -require File.dirname(__FILE__) + '/base' - -# Experimental!!! See also the Spider graph. -class AdvancedRoadmap::Gruff::Net < AdvancedRoadmap::Gruff::Base - - # Hide parts of the graph to fit more datapoints, or for a different appearance. - attr_accessor :hide_dots - - # Dimensions of lines and dots; calculated based on dataset size if left unspecified - attr_accessor :line_width - attr_accessor :dot_radius - - def initialize(*args) - super - - @hide_dots = false - @hide_line_numbers = true - end - - def draw - - super - - return unless @has_data - - @radius = @graph_height / 2.0 - @center_x = @graph_left + (@graph_width / 2.0) - @center_y = @graph_top + (@graph_height / 2.0) - 10 # Move graph up a bit - - @x_increment = @graph_width / (@column_count - 1).to_f - circle_radius = dot_radius || - clip_value_if_greater_than(@columns / (@norm_data.first[DATA_VALUES_INDEX].size * 2.5), 5.0) - - @d = @d.stroke_opacity 1.0 - @d = @d.stroke_width line_width || - clip_value_if_greater_than(@columns / (@norm_data.first[DATA_VALUES_INDEX].size * 4), 5.0) - - if (defined?(@norm_baseline)) then - level = @graph_top + (@graph_height - @norm_baseline * @graph_height) - @d = @d.push - @d.stroke_color @baseline_color - @d.fill_opacity 0.0 - @d.stroke_dasharray(10, 20) - @d.stroke_width 5 - @d.line(@graph_left, level, @graph_left + @graph_width, level) - @d = @d.pop - end - - @norm_data.each do |data_row| - prev_x = prev_y = nil - @d = @d.stroke data_row[DATA_COLOR_INDEX] - @d = @d.fill data_row[DATA_COLOR_INDEX] - - data_row[DATA_VALUES_INDEX].each_with_index do |data_point, index| - next if data_point.nil? - - rad_pos = index * Math::PI * 2 / @column_count - point_distance = data_point * @radius - start_x = @center_x + Math::sin(rad_pos) * point_distance - start_y = @center_y - Math::cos(rad_pos) * point_distance - - next_index = index + 1 < data_row[DATA_VALUES_INDEX].length ? index + 1 : 0 - - next_rad_pos = next_index * Math::PI * 2 / @column_count - next_point_distance = data_row[DATA_VALUES_INDEX][next_index] * @radius - end_x = @center_x + Math::sin(next_rad_pos) * next_point_distance - end_y = @center_y - Math::cos(next_rad_pos) * next_point_distance - - @d = @d.line(start_x, start_y, end_x, end_y) - - @d = @d.circle(start_x, start_y, start_x - circle_radius, start_y) unless @hide_dots - end - - end - - @d.draw(@base_image) - end - - - # the lines connecting in the center, with the first line vertical - def draw_line_markers - return if @hide_line_markers - - - # have to do this here (AGAIN)... see draw() in this class - # because this funtion is called before the @radius, @center_x and @center_y are set - @radius = @graph_height / 2.0 - @center_x = @graph_left + (@graph_width / 2.0) - @center_y = @graph_top + (@graph_height / 2.0) - 10 # Move graph up a bit - - - # Draw horizontal line markers and annotate with numbers - @d = @d.stroke(@marker_color) - @d = @d.stroke_width 1 - - - (0..@column_count-1).each do |index| - rad_pos = index * Math::PI * 2 / @column_count - - @d = @d.line(@center_x, @center_y, @center_x + Math::sin(rad_pos) * @radius, @center_y - Math::cos(rad_pos) * @radius) - - - marker_label = labels[index] ? labels[index].to_s : '000' - - draw_label(@center_x, @center_y, rad_pos * 360 / (2 * Math::PI), @radius, marker_label) - end - end - -private - - def draw_label(center_x, center_y, angle, radius, amount) - r_offset = 1.1 - x_offset = center_x # + 15 # The label points need to be tweaked slightly - y_offset = center_y # + 0 # This one doesn't though - x = x_offset + (radius * r_offset * Math.sin(angle.deg2rad)) - y = y_offset - (radius * r_offset * Math.cos(angle.deg2rad)) - - # Draw label - @d.fill = @marker_color - @d.font = @font if @font - @d.pointsize = scale_fontsize(20) - @d.stroke = 'transparent' - @d.font_weight = BoldWeight - @d.gravity = CenterGravity - @d.annotate_scaled(@base_image, 0, 0, x, y, amount, @scale) - end - -end - -# # This method is already in Float -# class Float -# # Used for degree => radian conversions -# def deg2rad -# self * (Math::PI/180.0) -# end -# end - - - diff --git a/lib/advanced_roadmap/gruff/photo_bar.rb b/lib/advanced_roadmap/gruff/photo_bar.rb deleted file mode 100644 index 1581e78..0000000 --- a/lib/advanced_roadmap/gruff/photo_bar.rb +++ /dev/null @@ -1,100 +0,0 @@ -require File.dirname(__FILE__) + '/base' - -# EXPERIMENTAL! -# -# Doesn't work yet. -# -class AdvancedRoadmap::Gruff::PhotoBar < AdvancedRoadmap::Gruff::Base - -# TODO -# -# define base and cap in yml -# allow for image directory to be located elsewhere -# more exact measurements for bar heights (go all the way to the bottom of the graph) -# option to tile images instead of use a single image -# drop base label a few px lower so photo bar graphs can have a base dropping over the lower marker line -# - - # The name of a pre-packaged photo-based theme. - attr_reader :theme - -# def initialize(target_width=800) -# super -# init_photo_bar_graphics() -# end - - def draw - super - return unless @has_data - - return # TODO Remove for further development - - init_photo_bar_graphics() - - #Draw#define_clip_path() - #Draw#clip_path(pathname) - #Draw#composite....with bar graph image OverCompositeOp - # - # See also - # - # Draw.pattern # define an image to tile as the filling of a draw object - # - - # Setup spacing. - # - # Columns sit side-by-side. - spacing_factor = 0.9 - @bar_width = @norm_data[0][DATA_COLOR_INDEX].columns - - @norm_data.each_with_index do |data_row, row_index| - - data_row[DATA_VALUES_INDEX].each_with_index do |data_point, point_index| - data_point = 0 if data_point.nil? - # Use incremented x and scaled y - left_x = @graph_left + (@bar_width * (row_index + point_index + ((@data.length - 1) * point_index))) - left_y = @graph_top + (@graph_height - data_point * @graph_height) + 1 - right_x = left_x + @bar_width * spacing_factor - right_y = @graph_top + @graph_height - 1 - - bar_image_width = data_row[DATA_COLOR_INDEX].columns - bar_image_height = right_y.to_f - left_y.to_f - - # Crop to scale for data - bar_image = data_row[DATA_COLOR_INDEX].crop(0, 0, bar_image_width, bar_image_height) - - @d.gravity = NorthWestGravity - @d = @d.composite(left_x, left_y, bar_image_width, bar_image_height, bar_image) - - # Calculate center based on bar_width and current row - label_center = @graph_left + (@data.length * @bar_width * point_index) + (@data.length * @bar_width / 2.0) - draw_label(label_center, point_index) - end - - end - - @d.draw(@base_image) - end - - - # Return the chosen theme or the default - def theme - @theme || 'plastik' - end - -protected - - # Sets up colors with a list of images that will be used. - # Images should be 340px tall - def init_photo_bar_graphics - color_list = Array.new - theme_dir = File.dirname(__FILE__) + '/../../assets/' + theme - - Dir.open(theme_dir).each do |file| - next unless /\.png$/.match(file) - color_list << Image.read("#{theme_dir}/#{file}").first - end - @colors = color_list - end - -end - diff --git a/lib/advanced_roadmap/gruff/pie.rb b/lib/advanced_roadmap/gruff/pie.rb deleted file mode 100644 index d1181b1..0000000 --- a/lib/advanced_roadmap/gruff/pie.rb +++ /dev/null @@ -1,124 +0,0 @@ -require File.dirname(__FILE__) + '/base' - -## -# Here's how to make a Pie graph: -# -# g = Gruff::Pie.new -# g.title = "Visual Pie Graph Test" -# g.data 'Fries', 20 -# g.data 'Hamburgers', 50 -# g.write("test/output/pie_keynote.png") -# -# To control where the pie chart starts creating slices, use #zero_degree. - -class AdvancedRoadmap::Gruff::Pie < AdvancedRoadmap::Gruff::Base - - TEXT_OFFSET_PERCENTAGE = 0.15 - - # Can be used to make the pie start cutting slices at the top (-90.0) - # or at another angle. Default is 0.0, which starts at 3 o'clock. - attr_accessor :zero_degree - - def initialize_ivars - super - @zero_degree = 0.0 - end - - def draw - @hide_line_markers = true - - super - - return unless @has_data - - diameter = @graph_height - radius = ([@graph_width, @graph_height].min / 2.0) * 0.8 - top_x = @graph_left + (@graph_width - diameter) / 2.0 - center_x = @graph_left + (@graph_width / 2.0) - center_y = @graph_top + (@graph_height / 2.0) - 10 # Move graph up a bit - total_sum = sums_for_pie() - prev_degrees = @zero_degree - - # Use full data since we can easily calculate percentages - data = (@sort ? @data.sort{ |a, b| a[DATA_VALUES_INDEX].first <=> b[DATA_VALUES_INDEX].first } : @data) - data.each do |data_row| - if data_row[DATA_VALUES_INDEX].first > 0 - @d = @d.stroke data_row[DATA_COLOR_INDEX] - @d = @d.fill 'transparent' - @d.stroke_width(radius) # stroke width should be equal to radius. we'll draw centered on (radius / 2) - - current_degrees = (data_row[DATA_VALUES_INDEX].first / total_sum) * 360.0 - - # ellipse will draw the the stroke centered on the first two parameters offset by the second two. - # therefore, in order to draw a circle of the proper diameter we must center the stroke at - # half the radius for both x and y - @d = @d.ellipse(center_x, center_y, - radius / 2.0, radius / 2.0, - prev_degrees, prev_degrees + current_degrees + 0.5) # <= +0.5 'fudge factor' gets rid of the ugly gaps - - half_angle = prev_degrees + ((prev_degrees + current_degrees) - prev_degrees) / 2 - - # Following line is commented to allow display of the percentiles - # bug appeared between r90 and r92 - # unless @hide_line_markers then - # End the string with %% to escape the single %. - # RMagick must use sprintf with the string and % has special significance. - label_string = ((data_row[DATA_VALUES_INDEX].first / total_sum) * - 100.0).round.to_s + '%%' - @d = draw_label(center_x,center_y, half_angle, - radius + (radius * TEXT_OFFSET_PERCENTAGE), - label_string) - # end - - prev_degrees += current_degrees - end - end - - # TODO debug a circle where the text is drawn... - - @d.draw(@base_image) - end - -private - - ## - # Labels are drawn around a slightly wider ellipse to give room for - # labels on the left and right. - def draw_label(center_x, center_y, angle, radius, amount) - # TODO Don't use so many hard-coded numbers - r_offset = 20.0 # The distance out from the center of the pie to get point - x_offset = center_x # + 15.0 # The label points need to be tweaked slightly - y_offset = center_y # This one doesn't though - radius_offset = (radius + r_offset) - ellipse_factor = radius_offset * 0.15 - x = x_offset + ((radius_offset + ellipse_factor) * Math.cos(angle.deg2rad)) - y = y_offset + (radius_offset * Math.sin(angle.deg2rad)) - - # Draw label - @d.fill = @font_color - @d.font = @font if @font - @d.pointsize = scale_fontsize(@marker_font_size) - @d.stroke = 'transparent' - @d.font_weight = BoldWeight - @d.gravity = CenterGravity - @d.annotate_scaled( @base_image, - 0, 0, - x, y, - amount, @scale) - end - - def sums_for_pie - total_sum = 0.0 - @data.collect {|data_row| total_sum += data_row[DATA_VALUES_INDEX].first } - total_sum - end - -end - -class Float - # Used for degree => radian conversions - def deg2rad - self * (Math::PI/180.0) - end -end - diff --git a/lib/advanced_roadmap/gruff/scene.rb b/lib/advanced_roadmap/gruff/scene.rb deleted file mode 100644 index f722f07..0000000 --- a/lib/advanced_roadmap/gruff/scene.rb +++ /dev/null @@ -1,209 +0,0 @@ - -require "observer" -require File.dirname(__FILE__) + '/base' - -## -# A scene is a non-linear graph that assembles layers together to tell a story. -# Layers are folders with appropriately named files (see below). You can group -# layers and control them together or just set their values individually. -# -# Examples: -# -# * A city scene that changes with the time of day and the weather conditions. -# * A traffic map that shows red lines on streets that are crowded and green on free-flowing ones. -# -# Usage: -# -# g = Gruff::Scene.new("500x100", "path/to/city_scene_directory") -# -# # Define order of layers, back to front -# g.layers = %w(background haze sky clouds) -# -# # Define groups that will be controlled by the same input -# g.weather_group = %w(clouds) -# g.time_group = %w(background sky) -# -# # Set values for the layers or groups -# g.weather = "cloudy" -# g.time = Time.now -# g.haze = true -# -# # Write the final graph to disk -# g.write "hazy_daytime_city_scene.png" -# -# -# There are several rules that will magically select a layer when possible. -# -# * Numbered files will be selected according to the closest value that is less than the input value. -# * 'true.png' and 'false.png' will be used as booleans. -# * Other named files will be used if the input matches the filename (without the filetype extension). -# * If there is a file named 'default.png', it will be used unless other input values are set for the corresponding layer. - -class AdvancedRoadmap::Gruff::Scene < AdvancedRoadmap::Gruff::Base - - # An array listing the foldernames that will be rendered, from back to front. - # - # g.layers = %w(sky clouds buildings street people) - # - attr_reader :layers - - def initialize(target_width, base_dir) - @base_dir = base_dir - @groups = {} - @layers = [] - super target_width - end - - def draw - # Join all the custom paths and filter out the empty ones - image_paths = @layers.map { |layer| layer.path }.select { |path| !path.empty? } - images = Magick::ImageList.new(*image_paths) - @base_image = images.flatten_images - end - - def layers=(ordered_list) - ordered_list.each do |layer_name| - @layers << Gruff::Layer.new(@base_dir, layer_name) - end - end - - # Group layers to input values - # - # g.weather_group = ["sky", "sea", "clouds"] - # - # Set input values - # - # g.weather = "cloudy" - # - def method_missing(method_name, *args) - case method_name.to_s - when /^(\w+)_group=$/ - add_group $1, *args - return - when /^(\w+)=$/ - set_input $1, args.first - return - end - super - end - -private - - def add_group(input_name, layer_names) - @groups[input_name] = Gruff::Group.new(input_name, @layers.select { |layer| layer_names.include?(layer.name) }) - end - - def set_input(input_name, input_value) - if not @groups[input_name].nil? - @groups[input_name].send_updates(input_value) - else - if chosen_layer = @layers.detect { |layer| layer.name == input_name } - chosen_layer.update input_value - end - end - end - -end - - -class Gruff::Group - - include Observable - attr_reader :name - - def initialize(folder_name, layers) - @name = folder_name - layers.each do |layer| - layer.observe self - end - end - - def send_updates(value) - changed - notify_observers value - end - -end - - -class Gruff::Layer - - attr_reader :name - - def initialize(base_dir, folder_name) - @base_dir = base_dir.to_s - @name = folder_name.to_s - @filenames = Dir.open(File.join(base_dir, folder_name)).entries.select { |file| file =~ /^[^.]+\.png$/ } - @selected_filename = select_default - end - - # Register this layer so it receives updates from the group - def observe(obj) - obj.add_observer self - end - - # Choose the appropriate filename for this layer, based on the input - def update(value) - @selected_filename = case value.to_s - when /^(true|false)$/ - select_boolean value - when /^(\w|\s)+$/ - select_string value - when /^-?(\d+\.)?\d+$/ - select_numeric value - when /(\d\d):(\d\d):\d\d/ - select_time "#{$1}#{$2}" - else - select_default - end - # Finally, try to use 'default' if we're still blank - @selected_filename ||= select_default - end - - # Returns the full path to the selected image, or a blank string - def path - unless @selected_filename.nil? || @selected_filename.empty? - return File.join(@base_dir, @name, @selected_filename) - end - '' - end - -private - - # Match "true.png" or "false.png" - def select_boolean(value) - file_exists_or_blank value.to_s - end - - # Match -5 to _5.png - def select_numeric(value) - file_exists_or_blank value.to_s.gsub('-', '_') - end - - def select_time(value) - times = @filenames.map { |filename| filename.gsub('.png', '') } - times.each_with_index do |time, index| - if (time > value) && (index > 0) - return "#{times[index - 1]}.png" - end - end - return "#{times.last}.png" - end - - # Match "partly cloudy" to "partly_cloudy.png" - def select_string(value) - file_exists_or_blank value.to_s.gsub(' ', '_') - end - - def select_default - @filenames.include?("default.png") ? "default.png" : '' - end - - # Returns the string "#{filename}.png", if it exists. - # - # Failing that, it returns default.png, or '' if that doesn't exist. - def file_exists_or_blank(filename) - @filenames.include?("#{filename}.png") ? "#{filename}.png" : select_default - end - -end diff --git a/lib/advanced_roadmap/gruff/side_bar.rb b/lib/advanced_roadmap/gruff/side_bar.rb deleted file mode 100644 index 49a101c..0000000 --- a/lib/advanced_roadmap/gruff/side_bar.rb +++ /dev/null @@ -1,115 +0,0 @@ -require File.dirname(__FILE__) + '/base' - -## -# Graph with individual horizontal bars instead of vertical bars. - -class AdvancedRoadmap::Gruff::SideBar < AdvancedRoadmap::Gruff::Base - - def draw - @has_left_labels = true - super - - return unless @has_data - - # Setup spacing. - # - @bar_spacing ||= 0.9 - - @bars_width = @graph_height / @column_count.to_f - @bar_width = @bars_width * @bar_spacing / @norm_data.size - @d = @d.stroke_opacity 0.0 - height = Array.new(@column_count, 0) - length = Array.new(@column_count, @graph_left) - padding = (@bars_width * (1 - @bar_spacing)) / 2 - - @norm_data.each_with_index do |data_row, row_index| - @d = @d.fill data_row[DATA_COLOR_INDEX] - - data_row[DATA_VALUES_INDEX].each_with_index do |data_point, point_index| - - # Using the original calcs from the stacked bar chart - # to get the difference between - # part of the bart chart we wish to stack. - temp1 = @graph_left + (@graph_width - data_point * @graph_width - height[point_index]) - temp2 = @graph_left + @graph_width - height[point_index] - difference = temp2 - temp1 - - left_x = length[point_index] - 1 - left_y = @graph_top + (@bars_width * point_index) + (@bar_width * row_index) + padding - right_x = left_x + difference - right_y = left_y + @bar_width - - height[point_index] += (data_point * @graph_width) - - @d = @d.rectangle(left_x, left_y, right_x, right_y) - - # Calculate center based on bar_width and current row - label_center = @graph_top + (@bars_width * point_index + @bars_width / 2) - draw_label(label_center, point_index) - end - - end - - @d.draw(@base_image) - end - -protected - - # Instead of base class version, draws vertical background lines and label - def draw_line_markers - - return if @hide_line_markers - - @d = @d.stroke_antialias false - - # Draw horizontal line markers and annotate with numbers - @d = @d.stroke(@marker_color) - @d = @d.stroke_width 1 - number_of_lines = 5 - - # TODO Round maximum marker value to a round number like 100, 0.1, 0.5, etc. - increment = significant(@maximum_value.to_f / number_of_lines) - (0..number_of_lines).each do |index| - - line_diff = (@graph_right - @graph_left) / number_of_lines - x = @graph_right - (line_diff * index) - 1 - @d = @d.line(x, @graph_bottom, x, @graph_top) - diff = index - number_of_lines - marker_label = diff.abs * increment - - unless @hide_line_numbers - @d.fill = @font_color - @d.font = @font if @font - @d.stroke = 'transparent' - @d.pointsize = scale_fontsize(@marker_font_size) - @d.gravity = CenterGravity - # TODO Center text over line - @d = @d.annotate_scaled( @base_image, - 0, 0, # Width of box to draw text in - x, @graph_bottom + (LABEL_MARGIN * 2.0), # Coordinates of text - marker_label.to_s, @scale) - end # unless - @d = @d.stroke_antialias true - end - end - - ## - # Draw on the Y axis instead of the X - - def draw_label(y_offset, index) - if !@labels[index].nil? && @labels_seen[index].nil? - @d.fill = @font_color - @d.font = @font if @font - @d.stroke = 'transparent' - @d.font_weight = NormalWeight - @d.pointsize = scale_fontsize(@marker_font_size) - @d.gravity = EastGravity - @d = @d.annotate_scaled(@base_image, - 1, 1, - -@graph_left + LABEL_MARGIN * 2.0, y_offset, - @labels[index], @scale) - @labels_seen[index] = 1 - end - end - -end diff --git a/lib/advanced_roadmap/gruff/side_stacked_bar.rb b/lib/advanced_roadmap/gruff/side_stacked_bar.rb deleted file mode 100644 index 65ea77e..0000000 --- a/lib/advanced_roadmap/gruff/side_stacked_bar.rb +++ /dev/null @@ -1,74 +0,0 @@ -require File.dirname(__FILE__) + '/base' -require File.dirname(__FILE__) + '/side_bar' -require File.dirname(__FILE__) + '/stacked_mixin' - -## -# New gruff graph type added to enable sideways stacking bar charts -# (basically looks like a x/y flip of a standard stacking bar chart) -# -# alun.eyre@googlemail.com - -class AdvancedRoadmap::Gruff::SideStackedBar < AdvancedRoadmap::Gruff::SideBar - include StackedMixin - - def draw - @has_left_labels = true - get_maximum_by_stack - super - - return unless @has_data - - # Setup spacing. - # - # Columns sit stacked. - @bar_spacing ||= 0.9 - - @bar_width = @graph_height / @column_count.to_f - @d = @d.stroke_opacity 0.0 - height = Array.new(@column_count, 0) - length = Array.new(@column_count, @graph_left) - padding = (@bar_width * (1 - @bar_spacing)) / 2 - - @norm_data.each_with_index do |data_row, row_index| - @d = @d.fill data_row[DATA_COLOR_INDEX] - - data_row[DATA_VALUES_INDEX].each_with_index do |data_point, point_index| - - ## using the original calcs from the stacked bar chart to get the difference between - ## part of the bart chart we wish to stack. - temp1 = @graph_left + (@graph_width - - data_point * @graph_width - - height[point_index]) + 1 - temp2 = @graph_left + @graph_width - height[point_index] - 1 - difference = temp2 - temp1 - - left_x = length[point_index] #+ 1 - left_y = @graph_top + (@bar_width * point_index) + padding - right_x = left_x + difference - right_y = left_y + @bar_width * @bar_spacing - length[point_index] += difference - height[point_index] += (data_point * @graph_width - 2) - - @d = @d.rectangle(left_x, left_y, right_x, right_y) - - # Calculate center based on bar_width and current row - label_center = @graph_top + (@bar_width * point_index) + (@bar_width * @bar_spacing / 2.0) - draw_label(label_center, point_index) - end - - end - - @d.draw(@base_image) - end - - protected - - def larger_than_max?(data_point, index=0) - max(data_point, index) > @maximum_value - end - - def max(data_point, index) - @data.inject(0) {|sum, item| sum + item[DATA_VALUES_INDEX][index]} - end - -end diff --git a/lib/advanced_roadmap/gruff/spider.rb b/lib/advanced_roadmap/gruff/spider.rb deleted file mode 100644 index eedf9b4..0000000 --- a/lib/advanced_roadmap/gruff/spider.rb +++ /dev/null @@ -1,130 +0,0 @@ - -require File.dirname(__FILE__) + '/base' - -# Experimental!!! See also the Net graph. -# -# Submitted by Kevin Clark http://glu.ttono.us/ -class AdvancedRoadmap::Gruff::Spider < AdvancedRoadmap::Gruff::Base - - # Hide all text - attr_reader :hide_text - attr_accessor :hide_axes - attr_reader :transparent_background - - def transparent_background=(value) - @transparent_background = value - @base_image = render_transparent_background if value - end - - def hide_text=(value) - @hide_title = @hide_text = value - end - - def initialize(max_value, target_width = 800) - super(target_width) - @max_value = max_value - @hide_legend = true; - end - - def draw - @hide_line_markers = true - - super - - return unless @has_data - - # Setup basic positioning - diameter = @graph_height - radius = @graph_height / 2.0 - top_x = @graph_left + (@graph_width - diameter) / 2.0 - center_x = @graph_left + (@graph_width / 2.0) - center_y = @graph_top + (@graph_height / 2.0) - 25 # Move graph up a bit - - @unit_length = radius / @max_value - - - total_sum = sums_for_spider - prev_degrees = 0.0 - additive_angle = (2 * Math::PI)/ @data.size - - current_angle = 0.0 - - # Draw axes - draw_axes(center_x, center_y, radius, additive_angle) unless hide_axes - - # Draw polygon - draw_polygon(center_x, center_y, additive_angle) - - - @d.draw(@base_image) - end - -private - - def normalize_points(value) - value * @unit_length - end - - def draw_label(center_x, center_y, angle, radius, amount) - r_offset = 50 # The distance out from the center of the pie to get point - x_offset = center_x # The label points need to be tweaked slightly - y_offset = center_y + 0 # This one doesn't though - x = x_offset + ((radius + r_offset) * Math.cos(angle)) - y = y_offset + ((radius + r_offset) * Math.sin(angle)) - - # Draw label - @d.fill = @marker_color - @d.font = @font if @font - @d.pointsize = scale_fontsize(legend_font_size) - @d.stroke = 'transparent' - @d.font_weight = BoldWeight - @d.gravity = CenterGravity - @d.annotate_scaled( @base_image, - 0, 0, - x, y, - amount, @scale) - end - - def draw_axes(center_x, center_y, radius, additive_angle, line_color = nil) - return if hide_axes - - current_angle = 0.0 - - @data.each do |data_row| - @d.stroke(line_color || data_row[DATA_COLOR_INDEX]) - @d.stroke_width 5.0 - - x_offset = radius * Math.cos(current_angle) - y_offset = radius * Math.sin(current_angle) - - @d.line(center_x, center_y, - center_x + x_offset, - center_y + y_offset) - - draw_label(center_x, center_y, current_angle, radius, data_row[DATA_LABEL_INDEX].to_s) unless hide_text - - current_angle += additive_angle - end - end - - def draw_polygon(center_x, center_y, additive_angle, color = nil) - points = [] - current_angle = 0.0 - @data.each do |data_row| - points << center_x + normalize_points(data_row[DATA_VALUES_INDEX].first) * Math.cos(current_angle) - points << center_y + normalize_points(data_row[DATA_VALUES_INDEX].first) * Math.sin(current_angle) - current_angle += additive_angle - end - - @d.stroke_width 1.0 - @d.stroke(color || @marker_color) - @d.fill(color || @marker_color) - @d.fill_opacity 0.4 - @d.polygon(*points) - end - - def sums_for_spider - @data.inject(0.0) {|sum, data_row| sum += data_row[DATA_VALUES_INDEX].first} - end - -end diff --git a/lib/advanced_roadmap/gruff/stacked_area.rb b/lib/advanced_roadmap/gruff/stacked_area.rb deleted file mode 100644 index 541c224..0000000 --- a/lib/advanced_roadmap/gruff/stacked_area.rb +++ /dev/null @@ -1,67 +0,0 @@ - -require File.dirname(__FILE__) + '/base' -require File.dirname(__FILE__) + '/stacked_mixin' - -class AdvancedRoadmap::Gruff::StackedArea < AdvancedRoadmap::Gruff::Base - include StackedMixin - attr_accessor :last_series_goes_on_bottom - - def draw - get_maximum_by_stack - super - - return unless @has_data - - @x_increment = @graph_width / (@column_count - 1).to_f - @d = @d.stroke 'transparent' - - height = Array.new(@column_count, 0) - - data_points = nil - iterator = last_series_goes_on_bottom ? :reverse_each : :each - @norm_data.send(iterator) do |data_row| - prev_data_points = data_points - data_points = Array.new - - @d = @d.fill data_row[DATA_COLOR_INDEX] - - data_row[DATA_VALUES_INDEX].each_with_index do |data_point, index| - # Use incremented x and scaled y - new_x = @graph_left + (@x_increment * index) - new_y = @graph_top + (@graph_height - data_point * @graph_height - height[index]) - - height[index] += (data_point * @graph_height) - - data_points << new_x - data_points << new_y - - draw_label(new_x, index) - - end - - if prev_data_points - poly_points = data_points.dup - (prev_data_points.length/2 - 1).downto(0) do |i| - poly_points << prev_data_points[2*i] - poly_points << prev_data_points[2*i+1] - end - poly_points << data_points[0] - poly_points << data_points[1] - else - poly_points = data_points.dup - poly_points << @graph_right - poly_points << @graph_bottom - 1 - poly_points << @graph_left - poly_points << @graph_bottom - 1 - poly_points << data_points[0] - poly_points << data_points[1] - end - @d = @d.polyline(*poly_points) - - end - - @d.draw(@base_image) - end - - -end diff --git a/lib/advanced_roadmap/gruff/stacked_bar.rb b/lib/advanced_roadmap/gruff/stacked_bar.rb deleted file mode 100644 index fb366ce..0000000 --- a/lib/advanced_roadmap/gruff/stacked_bar.rb +++ /dev/null @@ -1,54 +0,0 @@ - -require File.dirname(__FILE__) + '/base' -require File.dirname(__FILE__) + '/stacked_mixin' - -class AdvancedRoadmap::Gruff::StackedBar < AdvancedRoadmap::Gruff::Base - include StackedMixin - - # Draws a bar graph, but multiple sets are stacked on top of each other. - def draw - get_maximum_by_stack - super - return unless @has_data - - # Setup spacing. - # - # Columns sit stacked. - @bar_spacing ||= 0.9 - @bar_width = @graph_width / @column_count.to_f - padding = (@bar_width * (1 - @bar_spacing)) / 2 - - @d = @d.stroke_opacity 0.0 - - height = Array.new(@column_count, 0) - - @norm_data.each_with_index do |data_row, row_index| - data_row[DATA_VALUES_INDEX].each_with_index do |data_point, point_index| - @d = @d.fill data_row[DATA_COLOR_INDEX] - - # Calculate center based on bar_width and current row - label_center = @graph_left + (@bar_width * point_index) + (@bar_width * @bar_spacing / 2.0) - draw_label(label_center, point_index) - - next if (data_point == 0) - # Use incremented x and scaled y - left_x = @graph_left + (@bar_width * point_index) + padding - left_y = @graph_top + (@graph_height - - data_point * @graph_height - - height[point_index]) + 1 - right_x = left_x + @bar_width * @bar_spacing - right_y = @graph_top + @graph_height - height[point_index] - 1 - - # update the total height of the current stacked bar - height[point_index] += (data_point * @graph_height ) - - @d = @d.rectangle(left_x, left_y, right_x, right_y) - - end - - end - - @d.draw(@base_image) - end - -end diff --git a/lib/advanced_roadmap/gruff/stacked_mixin.rb b/lib/advanced_roadmap/gruff/stacked_mixin.rb deleted file mode 100644 index d955d70..0000000 --- a/lib/advanced_roadmap/gruff/stacked_mixin.rb +++ /dev/null @@ -1,23 +0,0 @@ - -module AdvancedRoadmap::Gruff::Base::StackedMixin - # Used by StackedBar and child classes. - # - # tsal: moved from Base 03 FEB 2007 - DATA_VALUES_INDEX = AdvancedRoadmap::Gruff::Base::DATA_VALUES_INDEX - def get_maximum_by_stack - # Get sum of each stack - max_hash = {} - @data.each do |data_set| - data_set[DATA_VALUES_INDEX].each_with_index do |data_point, i| - max_hash[i] = 0.0 unless max_hash[i] - max_hash[i] += data_point.to_f - end - end - - # @maximum_value = 0 - max_hash.keys.each do |key| - @maximum_value = max_hash[key] if max_hash[key] > @maximum_value - end - @minimum_value = 0 - end -end diff --git a/lib/advanced_roadmap/versions_controller_patch.rb b/lib/advanced_roadmap/versions_controller_patch.rb index 044f619..6cf0a36 100644 --- a/lib/advanced_roadmap/versions_controller_patch.rb +++ b/lib/advanced_roadmap/versions_controller_patch.rb @@ -15,8 +15,9 @@ def index_with_plugin end if params[:only_open] end - alias_method :plugin, :index - alias_method :index, :plugin + + alias_method :index_without_plugin, :index + alias_method :index, :index_with_plugin def show @issues = @version.sorted_fixed_issues From 024917a295f3e37e164700532094552fc5ddd959 Mon Sep 17 00:00:00 2001 From: awk-kazmin Date: Mon, 30 Sep 2019 17:21:29 +0300 Subject: [PATCH 3/3] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1c88d7e..bf280f7 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -Redmine Plugin: Advanced Roadmap v2 +Redmine Plugin: Advanced Roadmap v2 (for redmine 4.+) =========================== This plugin was a fork of Redmine Plugin: [advanced roadmap](https://redmine.ociotec.com/projects/advanced-roadmap), when it was not developed.