Class: CTioga2::Graphics::Styles::AxisTicks

Inherits:
BasicStyle
  • Object
show all
Includes:
Log
Defined in:
lib/ctioga2/graphics/styles/ticks.rb

Overview

This class describes where to place ticks on the target axis and how to label them.

I really should drop the call to Tioga altogether. It makes everything too complicated.

Constant Summary

Constants inherited from BasicStyle

BasicStyle::AllStyles, BasicStyle::OldAttrAccessor

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log

context, debug, error, fatal, #format_exception, #identify, info, init_logger, log_to, logger, set_level, #spawn, warn

Methods inherited from BasicStyle

alias_for, aliases, attr_accessor, attribute_type, attribute_types, attributes, convert_string_hash, defined_aliases, deprecated_attribute, from_hash, inherited, #instance_variable_defined?, normalize_hash, normalize_in, normalize_out, options_hash, #set_from_hash, sub_style, sub_styles, #to_hash, typed_attribute, #update_from_other

Class Method Details

.tweak_format_string(str) ⇒ Object



84
85
86
# File 'lib/ctioga2/graphics/styles/ticks.rb', line 84

def self.tweak_format_string(str)
  return str.gsub("%b", "%2$").gsub("%p", "%3$d")
end

Instance Method Details

#ticks_specs(t, info, transform) ⇒ Object

Returns the specifications that should be added to the information



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/ctioga2/graphics/styles/ticks.rb', line 90

def ticks_specs(t, info, transform)
  ret = {}
  for k in %w{major_ticks minor_ticks labels}
    ret[k] = info[k]
  end
  if info['major']
    ret['minor_ticks'] = info['minor']
    ret['major_ticks'] = info['major']
  end
  fmt = @format

  # beginning or end of the axis. Not specifically x
  xl, xr = * (if info['vertical']
                [info['y0'], info['y1']]
              else
                [info['x0'], info['x1']]
              end)

  if xl > xr
    xl, xr = xr, xl
  end

  mn = if @major_number
         @major_number
       elsif @major_sep
         dx = @major_sep.to_figure(t, info['vertical'] ? :y : :x)
         mn = (xr - xl)/dx
       else
         nil
       end

  if @major
    ret['minor_ticks'] = Dobjects::Dvector.new
    ret['major_ticks'] = Dobjects::Dvector.new(@major)

    fmt ||= "$%g$"
  elsif @major_delta || mn
    delta = @major_delta || Utils::closest_subdivision(( (xr - xl)/mn))
    ret['major_ticks'] = Utils::integer_subdivisions(xl, xr, 
                                                     delta)
    fmt ||= "$%g$"
  end

  if @minor
    ret['minor_ticks'] = Dobjects::Dvector.new(@minor)
  elsif @minor_delta || @minor_sep || delta
    
    dt = if @minor_delta
           @minor_delta
         else
           nb = if @minor_number
                  @minor_number
                elsif @minor_sep_min
                  dx = @minor_sep_min.to_figure(t, info['vertical'] ? :y : :x)
                  mx = ((delta/dx).round - 1)
                  if mx > 3
                    3
                  else
                    mx
                  end
                else
                  3
                end
           delta/(nb+1)
         end
    ret['minor_ticks'] = Utils::integer_subdivisions(xl, xr, 
                                                     dt)
  end

  fmt_last = @format_last || fmt

  if @labels
    ret['labels'] = @labels
  elsif fmt
    ret['labels'] = []
    fmt = AxisTicks.tweak_format_string(fmt)
    fmt_last = AxisTicks.tweak_format_string(fmt_last)
    i = ret['major_ticks'].size
    common = Utils::common_pow10(ret['major_ticks'])
    fact = 10**(-common)
    for v in ret['major_ticks']
      i -= 1
      ret['labels'] << (i > 0 ? fmt : fmt_last) % [v, v*fact, common]
    end
  end
  return ret
end