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

« back to all changes in this revision

Viewing changes to lib/ctioga2/graphics/generator.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
# root.rb: the root object for creating a plot.
 
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
require 'ctioga2/graphics/coordinates'
 
18
 
 
19
module CTioga2
 
20
 
 
21
  Version::register_svn_info('$Revision: 244 $', '$Date: 2011-01-23 23:36:02 +0100 (Sun, 23 Jan 2011) $')
 
22
 
 
23
  module Graphics
 
24
 
 
25
    # This class is in charge of generating Elements::TiogaElement,
 
26
    # such as Elements::Curve2D, from a dataset. It takes care of
 
27
    # generating the appropriate style and of transforming the
 
28
    # coordinates.
 
29
    class CurveGenerator
 
30
 
 
31
      # A Styles::CurveStyleFactory object that handles the
 
32
      # styles for every single curve that will be drawn.
 
33
      attr_accessor :style_factory
 
34
 
 
35
      # The provider of legends, a Legends::LegendProvider
 
36
      # object.
 
37
      attr_accessor :legend_provider
 
38
 
 
39
      # The current kind of generated. It is a symbol
 
40
      attr_accessor :current_curves
 
41
 
 
42
      # Creates a CurveGenerator object.
 
43
      def initialize
 
44
        @legend_provider = Legends::LegendProvider.new
 
45
        @style_factory = Styles::CurveStyleFactory.new
 
46
        @current_curves = :xy_plot
 
47
      end
 
48
 
 
49
      # Creates a Elements::TiogaElement representing the _dataset_
 
50
      # and returns it.
 
51
      #
 
52
      # \todo
 
53
      # * (other kinds of) coordinate transformations
 
54
      # * other kinds of curves (pseudo-3D, surfaces, histograms...)
 
55
      def curve_from_dataset(plot, dataset, options = {})
 
56
        # Does coordinate transforms first ?
 
57
        # \todo copy datasets here rather than overwriting them !
 
58
        plot.style.transforms.transform_2d!(dataset)
 
59
 
 
60
        return send(@current_curves, plot, dataset, options)
 
61
      end
 
62
 
 
63
      private
 
64
      
 
65
      ## \name Available kinds of curves
 
66
      # 
 
67
      # @{
 
68
      # 
 
69
      # The "classical" 2D plots.
 
70
      def xy_plot(plot, dataset, options = {})
 
71
        legend = @legend_provider.dataset_legend(dataset)
 
72
        style = @style_factory.next(options)
 
73
        style.legend ||= legend # The legend specified as option to
 
74
                                # the --plot command has precedence
 
75
                                # over the one specified by --legend.
 
76
        curve = Graphics::Elements::Curve2D.new(dataset, style)
 
77
        return curve
 
78
      end
 
79
 
 
80
      # XYZ plots formerly known as "parametric plots"
 
81
      def xy_parametric(plot, dataset, options = {})
 
82
        legend = @legend_provider.dataset_legend(dataset)
 
83
        style = @style_factory.next(options)
 
84
        style.legend = false
 
85
        style.legend ||= legend # The legend specified as option to
 
86
                                # the --plot command has precedence
 
87
                                # over the one specified by --legend.
 
88
        curve = Graphics::Elements::Parametric2D.new(dataset, style)
 
89
        return curve
 
90
      end
 
91
 
 
92
      # XYZ maps
 
93
      def xyz_map(plot, dataset, options = {})
 
94
        legend = @legend_provider.dataset_legend(dataset)
 
95
        style = @style_factory.next(options)
 
96
        style.legend = false
 
97
        style.legend ||= legend # The legend specified as option to
 
98
                                # the --plot command has precedence
 
99
                                # over the one specified by --legend.
 
100
        curve = Graphics::Elements::XYZMap.new(dataset, style)
 
101
        return curve
 
102
      end
 
103
 
 
104
 
 
105
      ## @}
 
106
      
 
107
    end
 
108
 
 
109
 
 
110
    # The group for chosing plot types.
 
111
    PlotTypesGroup =  
 
112
      CmdGroup.new('plot-types',
 
113
                   "Switch between different kinds of plots",
 
114
                   "How to switch between different kinds of plot types", 01)
 
115
    
 
116
 
 
117
    XYParametricPlotCommand = 
 
118
      Cmd.new("xy-parametric",nil,"--xy-parametric") do |plotmaker|
 
119
      plotmaker.curve_generator.current_curves = :xy_parametric
 
120
    end
 
121
    
 
122
    XYParametricPlotCommand.describe('select XY parametric plots', 
 
123
                                     <<EOH, PlotTypesGroup)
 
124
Switch to XY parametric plots, that is standard XY plots whose appearance
 
125
(such as color, marker color, and, potentially, marker kinds and more)
 
126
are governed by one (or more ?) Z values.
 
127
EOH
 
128
 
 
129
    XYPlotCommand = 
 
130
      Cmd.new("xy-plot",nil,"--xy-plot") do |plotmaker|
 
131
      plotmaker.curve_generator.current_curves = :xy_plot
 
132
    end
 
133
    
 
134
    XYPlotCommand.describe('select XY plots', 
 
135
                           <<EOH, PlotTypesGroup)
 
136
Switch (back) to standard XY plots (ctioga\'s default)
 
137
EOH
 
138
 
 
139
    XYZMapCommand = 
 
140
      Cmd.new("xyz-map",nil,"--xyz-map") do |plotmaker|
 
141
      plotmaker.curve_generator.current_curves = :xyz_map
 
142
    end
 
143
    
 
144
    XYZMapCommand.describe('select XYZ maps', 
 
145
                            <<EOH, PlotTypesGroup)
 
146
Switch to XYZ maps, ie plots where the color at a XY location is given by 
 
147
its Z value.
 
148
EOH
 
149
 
 
150
 
 
151
  end
 
152
end
 
153