lib/ctioga2/graphics/styles/plot.rb in ctioga2-0.10.1 vs lib/ctioga2/graphics/styles/plot.rb in ctioga2-0.11

- old
+ new

@@ -13,10 +13,11 @@ require 'ctioga2/utils' require 'ctioga2/log' require 'ctioga2/graphics/coordinates' +require 'ctioga2/graphics/elements/element' # This module contains all the classes used by ctioga module CTioga2 module Graphics @@ -83,44 +84,56 @@ # dimensions of the plot frame, using the given number as a # conversion factor from the output dimensions. attr_accessor :frame_real_size + # The target plot (ie the parent of all the small elements) + attr_accessor :target_plot + @@current_index = 0 - def initialize + def initialize(plt) # Default style for the plots. + @target_plot = plt + @axes = {} - @axes[:left] = - StyleSheet.style_for(AxisStyle, 'left', :left, - AXIS_WITH_TICKS_AND_NUMERIC_LABELS, - '$y$') - @axes[:bottom] = - StyleSheet.style_for(AxisStyle, 'bottom', :bottom, - AXIS_WITH_TICKS_AND_NUMERIC_LABELS, - '$x$') + for ax in [:left, :right, :top, :bottom] + + cls = [ax.to_s] + cls << if (ax == :bottom or ax == :top) + 'x' + else + 'y' + end + dec = if (ax == :bottom or ax == :left) + AXIS_WITH_TICKS_AND_NUMERIC_LABELS + else + AXIS_WITH_TICKS_ONLY + end + label = nil + if ax == :bottom + label = '$x$' + elsif ax == :left + label = '$y$' + end + + axis = Elements::AxisElement.new + axis.setup_style(@target_plot, {'class' => cls}) + axis.initialize_style(ax, dec, label) + @axes[ax] = axis + end - @axes[:right] = - StyleSheet.style_for(AxisStyle, 'right', :right, - AXIS_WITH_TICKS_ONLY) - @axes[:top] = - StyleSheet.style_for(AxisStyle, 'top', :top, - AXIS_WITH_TICKS_ONLY) - @xaxis_location = :bottom @yaxis_location = :left - @title = - StyleSheet.style_for(TextLabel, 'title', - nil, - Types::PlotLocation.new(:top)) + @title = Elements::TitleElement.new(@target_plot, {}) + @plot_margin = nil @transforms = CoordinateTransforms.new - @background = - StyleSheet.style_for(BackgroundStyle, 'background') + @background = Elements::BackgroundElement.new(@target_plot, {}) # A padding of 6bp ? Why ?? Why not ? @padding = Types::Dimension.new(:bp, 6) @@ -150,16 +163,16 @@ # \todo This really should move to Axis when transformations # are handled correctly. def set_log_scale(which, val) case which when :x - @axes[:top].log = val - @axes[:bottom].log = val + @axes[:top].style.log = val + @axes[:bottom].style.log = val @transforms.x_log = val when :y - @axes[:left].log = val - @axes[:right].log = val + @axes[:left].style.log = val + @axes[:right].style.log = val @transforms.y_log = val else raise "Unknown axis: #{which.inspect}" end end @@ -184,16 +197,16 @@ # given location # # \todo Maybe x2 and y2 could be provided to signify "the side # which isn't the default" ? def get_axis_style(name) - style = @axes[get_axis_key(name)] - if ! style + axis = @axes[get_axis_key(name)] + if ! axis ## @todo Type-safe exception here raise "Unkown named axis: '#{name}'" else - return style + return axis.style end end # Returns the key corresponding to the named axis. See # #get_axis_style for more information; though ultimately the @@ -204,13 +217,13 @@ else return clean_axis_name(name) end end - def set_axis_style(name, style) + def set_axis(name, axis) key = get_axis_key(name) - @axes[key] = style + @axes[key] = axis end # Returns a BaseTextStyle or similar for the given @@ -221,11 +234,11 @@ # # If neither label nor ticks is specified in the first form, # ticks are implied. def get_label_style(location) if location =~ /^\s*title\s*$/ - return @title + return @title.style end location =~ /^\s*(.*?)(?:_(ticks?|label))?\s*$/i which = $2 axis = get_axis_style($1) if which =~ /label/ @@ -253,26 +266,26 @@ # coordinate system or if they just follow what's around. def draw_all_axes(t, bounds) for which, axis in @axes t.context do begin - axis.set_bounds_for_axis(t, bounds[which]) - axis.draw_axis(t, @text_sizes) + axis.style.set_bounds_for_axis(t, bounds[which]) + axis.style.draw_axis(t, @text_sizes) rescue Exception => e error { "Impossible to draw axis #{which}: #{e.message}" } - debug { "Full message: #{e.inspect}\n#{e.backtrace.join("\n")}" } + info { "Full message: #{e.inspect}\n#{e.backtrace.join("\n")}" } end end end # We draw the title last - title.draw(t, 'title', "title-#{@text_size_index}") + title.style.draw(t, 'title', "title-#{@text_size_index}") end # Draws all axes background lines for the plot. def draw_all_background_lines(t) for which, axis in @axes - axis.draw_background_lines(t) + axis.style.draw_background_lines(t) end end # Sets up the FigureMaker object for the plot. To be called # just after the outermost context call for the concerned @@ -303,14 +316,14 @@ # # Returns a Types::MarginsBox def estimate_margins(t) margins = [:left, :right, :top, :bottom].map do |side| exts = axes_for_side(side).map do |ax| - ax.extension(t,self) + ax.style.extension(t, self) end - if @title.loc.is_side?(side) - exts << @title.label_extension(t, 'title', @title.loc) * + if @title.style.loc.is_side?(side) + exts << @title.style.label_extension(t, 'title', @title.style.loc) * (@text_scale || 1) end Types::Dimension.new(:dy, exts.max || 0) end @@ -372,11 +385,11 @@ # Returns the list of AxisStyle corresponding to the given # side (:top, :eft, etc...) def axes_for_side(side) ret = [] for k,v in @axes - ret << v if v.location.is_side?(side) + ret << v if v.style.location.is_side?(side) end return ret end end @@ -546,11 +559,40 @@ describe("Sets the color of the background lines", <<"EOH", AxisGroup) Sets the color of the background lines for the given axis. EOH + BackgroundGridCommand = + Cmd.new('background-grid', nil, '--background-grid', + [ + CmdArg.new('color-or-false') + ], + StrokeStyle.options_hash().without('color') + ) do |plotmaker, color, options| + for which in [:left, :bottom] + axis = AxisStyle.current_axis_style(plotmaker, which) + if color + style = {'color' => color} + style.merge!(options) + if axis.background_lines + axis.background_lines.set_from_hash(style) + else + axis.background_lines = StrokeStyle.from_hash(style) + end + else + axis.background_lines = false + end + end + end + + BackgroundGridCommand. + describe("Sets the color of the background lines", + <<"EOH", AxisGroup) +Shortcut to set the color for the left and bottom axes +EOH + %w{x y}.each do |axis| labelcmd = Cmd.new("#{axis}label", "-#{axis}", "--#{axis}label", [ CmdArg.new('text') ], TextLabel.options_hash().without('text') ) do |plotmaker, label, options| @@ -649,14 +691,16 @@ NewZAxisCommand = Cmd.new('new-zaxis', nil, '--new-zaxis', [ CmdArg.new('text') - ],ZAxisStyle) do |plotmaker, name, options| - axis = Styles::MapAxisStyle.new - PlotStyle.current_plot_style(plotmaker). - set_axis_style(name, axis) - axis.set_from_hash(options) + ], ZAxisStyle.merge(Elements::TiogaElement::StyleBaseOptions) + ) do |plotmaker, name, options| + + cps = PlotStyle.current_plot_style(plotmaker) + axis = Elements::MapAxisElement.new(cps.target_plot, options) + cps.set_axis(name, axis) + axis.style.set_from_hash(options) end NewZAxisCommand. describe("Creates a Z axis", <<"EOH", AxisGroup)