class CTioga2::Graphics::Styles::AxisTicks
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.
Public Class Methods
tweak_format_string(str)
click to toggle source
# 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
Public Instance Methods
ticks_specs(t, info, transform)
click to toggle source
Returns the specifications that should be added to the information
# 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