~ubuntu-branches/ubuntu/oneiric/ctioga2/oneiric

« back to all changes in this revision

Viewing changes to lib/ctioga2/graphics/styles/map-axes.rb

  • Committer: Bazaar Package Importer
  • Author(s): Vincent Fourmond
  • Date: 2011-01-24 21:36:06 UTC
  • Revision ID: james.westby@ubuntu.com-20110124213606-9ettx0ugl83z0bzp
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# axes.rb: the style of one axis or edge 
 
2
# copyright (c) 2009 by Vincent Fourmond
 
3
  
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
  
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details (in the COPYING file).
 
13
 
 
14
require 'ctioga2/utils'
 
15
require 'ctioga2/log'
 
16
 
 
17
# This module contains all the classes used by ctioga
 
18
module CTioga2
 
19
 
 
20
  Version::register_svn_info('$Revision$', '$Date$')
 
21
 
 
22
  module Graphics
 
23
 
 
24
    module Styles
 
25
 
 
26
 
 
27
      # This class handles the display of a Z axis color map, in the
 
28
      # form of a colored bar with ticks and a label.
 
29
      class MapAxisStyle < AxisStyle
 
30
 
 
31
        # The actual color map
 
32
        attr_accessor :color_map
 
33
        
 
34
        # Zmin and Zmax boundaries
 
35
        attr_accessor :bounds
 
36
 
 
37
        # Size of the bar (not counting the label)
 
38
        attr_accessor :bar_size
 
39
 
 
40
        # Space to be left between the graph and the beginning of the
 
41
        # graph
 
42
        attr_accessor :bar_shift
 
43
 
 
44
        # Space to be left on the side
 
45
        attr_accessor :padding
 
46
 
 
47
        # Creates a new MapAxisStyle object at the given location with
 
48
        # the given style.
 
49
        def initialize()
 
50
          super()
 
51
 
 
52
          @bar_size = Types::Dimension.new(:dy, 2, :x)
 
53
 
 
54
          # Shifting away from the location.
 
55
          @bar_shift = Types::Dimension.new(:dy, 0.3, :x)
 
56
 
 
57
          ## @todo maybe use different padding for left and right ?
 
58
          @padding = Types::Dimension.new(:dy, 0.5, :x)
 
59
 
 
60
          @decoration = AXIS_WITH_TICKS_AND_NUMERIC_LABELS
 
61
 
 
62
          # To be implemented one day...
 
63
          @other_side_decoration = nil
 
64
        end
 
65
 
 
66
        def set_color_map(color_map, zmin, zmax)
 
67
          @bounds = [zmin, zmax]
 
68
          @color_map = color_map
 
69
 
 
70
        end
 
71
 
 
72
        def draw_axis(t)
 
73
          # Not beautiful at all
 
74
          size = Types::Dimension.new(:dy, extension(t), 
 
75
                                      @location.orientation)
 
76
          label_size = 
 
77
            Types::Dimension.new(:dy, labels_only_extension(t, style = nil),
 
78
                                 @location.orientation)
 
79
 
 
80
          @location.do_sub_frame(t, size) do
 
81
            # This is a necessary workaround for a small bug
 
82
            t.set_subframe([0,0,0,0])
 
83
            # Here, do the correct setup, using a MarginsBox:
 
84
            # * correctly setup the axes/edges
 
85
            # * handle the sides correctly.
 
86
            # * position the subplot within accordingly
 
87
            # * use draw_axis for the axis ?
 
88
 
 
89
            plot_box = Types::MarginsBox.
 
90
              new(*@location.reorient_margins(@bar_shift, label_size, 
 
91
                                              @padding, @padding))
 
92
 
 
93
            # We wrap the call within a subplot
 
94
            t.subplot(plot_box.to_frame_margins(t)) do
 
95
              bounds = if @location.vertical?
 
96
                         [0, 1, @bounds.last, @bounds.first]
 
97
                       else
 
98
                         [@bounds.first, @bounds.last, 0, 1]
 
99
                       end
 
100
              t.set_bounds(bounds)
 
101
              t.context do 
 
102
                t.clip_to_frame
 
103
                cmap, zmin, zmax = *@color_map.to_colormap(t, @bounds.first,
 
104
                                                          @bounds.last)
 
105
 
 
106
                sp = [0.5, zmin]
 
107
                ep = [0.5, zmax]
 
108
                if ! @location.vertical?
 
109
                  sp.reverse!
 
110
                  ep.reverse!
 
111
                end
 
112
                t.axial_shading(
 
113
                                'start_point' => sp,
 
114
                                'end_point' => ep,
 
115
                                'colormap' => cmap
 
116
                                )
 
117
              end
 
118
              ## @todo handle axis color ?
 
119
              t.stroke_frame
 
120
              ## @todo potentially handle decorations for the other
 
121
              ## side too.
 
122
 
 
123
              ## @todo This is a ugly hack, but Ruby doesn't allow a
 
124
              ## clean one. Though
 
125
              ## http://stackoverflow.com/questions/1251178/calling-another-method-in-super-class-in-ruby
 
126
              ## seems like the way to go ! To be implemented one day.
 
127
              self.class.superclass.instance_method(:draw_axis).
 
128
                bind(self).call(t)
 
129
              
 
130
            end
 
131
 
 
132
          end
 
133
        end
 
134
 
 
135
        def set_bounds_for_axis(t, range = nil)
 
136
          # Useless here
 
137
        end
 
138
 
 
139
        # Draw the axis background lines:
 
140
        def draw_background_lines(t)
 
141
          # Nothing to do
 
142
        end
 
143
 
 
144
        def extension(t, style = nil)
 
145
          base = super(t, style)
 
146
 
 
147
          base += @bar_size.to_text_height(t, @location.orientation)
 
148
          base += @bar_shift.to_text_height(t, @location.orientation)
 
149
          return base
 
150
        end
 
151
 
 
152
        # Whether the axis is vertical or not
 
153
        def vertical?
 
154
          return @location.vertical?
 
155
        end
 
156
 
 
157
      end
 
158
 
 
159
      ZAxisStyle = FullAxisStyle.dup
 
160
      ZAxisStyle['bar_size'] = CmdArg.new('dimension')
 
161
      ZAxisStyle['bar_shift'] = CmdArg.new('dimension')
 
162
    end
 
163
  end
 
164
end