Class: MotionPlot::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/motion-plot/chart/base.rb

Direct Known Subclasses

Bar, Line, Pie

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#axesObject

Returns the value of attribute axes.



6
7
8
# File 'lib/motion-plot/chart/base.rb', line 6

def axes
  @axes
end

#data_labelObject

Returns the value of attribute data_label.



6
7
8
# File 'lib/motion-plot/chart/base.rb', line 6

def data_label
  @data_label
end

#graphObject (readonly)

Returns the value of attribute graph.



4
5
6
# File 'lib/motion-plot/chart/base.rb', line 4

def graph
  @graph
end

#layer_hosting_viewObject (readonly)

Returns the value of attribute layer_hosting_view.



4
5
6
# File 'lib/motion-plot/chart/base.rb', line 4

def layer_hosting_view
  @layer_hosting_view
end

#legendObject

Returns the value of attribute legend.



6
7
8
# File 'lib/motion-plot/chart/base.rb', line 6

def legend
  @legend
end

#major_grid_line_styleObject (readonly)

Returns the value of attribute major_grid_line_style.



4
5
6
# File 'lib/motion-plot/chart/base.rb', line 4

def major_grid_line_style
  @major_grid_line_style
end

#plot_optionsObject

Returns the value of attribute plot_options.



6
7
8
# File 'lib/motion-plot/chart/base.rb', line 6

def plot_options
  @plot_options
end

#plot_spaceObject (readonly)

Returns the value of attribute plot_space.



4
5
6
# File 'lib/motion-plot/chart/base.rb', line 4

def plot_space
  @plot_space
end

#plotsObject (readonly)

Returns the value of attribute plots.



4
5
6
# File 'lib/motion-plot/chart/base.rb', line 4

def plots
  @plots
end

#seriesObject (readonly)

Returns the value of attribute series.



4
5
6
# File 'lib/motion-plot/chart/base.rb', line 4

def series
  @series
end

#themeObject

Returns the value of attribute theme.



6
7
8
# File 'lib/motion-plot/chart/base.rb', line 6

def theme
  @theme
end

#titleObject (readonly)

Returns the value of attribute title.



4
5
6
# File 'lib/motion-plot/chart/base.rb', line 4

def title
  @title
end

#xaxisObject (readonly)

Returns the value of attribute xaxis.



4
5
6
# File 'lib/motion-plot/chart/base.rb', line 4

def xaxis
  @xaxis
end

#yaxisObject (readonly)

Returns the value of attribute yaxis.



4
5
6
# File 'lib/motion-plot/chart/base.rb', line 4

def yaxis
  @yaxis
end

Instance Method Details

#add_axis_title(axis, title) ⇒ Object



163
164
165
166
167
# File 'lib/motion-plot/chart/base.rb', line 163

def add_axis_title(axis, title)
  axis.title            = title.text
  axis.titleTextStyle   = title.text_style
  axis.titleOffset      = title.style.offset
end

#add_chart_title(title) ⇒ Object



157
158
159
160
161
# File 'lib/motion-plot/chart/base.rb', line 157

def add_chart_title(title)
  @graph.title                      = title.text
  @graph.titleTextStyle             = title.text_style
  @graph.titlePlotAreaFrameAnchor   = title.position
end

#add_legendObject



169
170
171
172
173
# File 'lib/motion-plot/chart/base.rb', line 169

def add_legend
  @graph.legend               = @legend.cpt_legend(@graph)
  @graph.legendAnchor         = @legend.position
  @graph.legendDisplacement   = @legend.displacement
end

#add_plot_spaceObject



133
134
135
136
137
# File 'lib/motion-plot/chart/base.rb', line 133

def add_plot_space
  @plot_space                       = @graph.defaultPlotSpace
  @plot_space.delegate              = self
  @plot_space.allowsUserInteraction = true
end

#add_plot_symbol(plot, symbol) ⇒ Object



139
140
141
142
# File 'lib/motion-plot/chart/base.rb', line 139

def add_plot_symbol(plot, symbol)
  plot.plotSymbol                       = symbol
  plot.plotSymbolMarginForHitDetection  = 5.0
end

#add_xy_rangeObject



144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/motion-plot/chart/base.rb', line 144

def add_xy_range
  return if(not @axes[:x].enabled? and not @axes[:y].enabled?)
  @plot_space.scaleToFitPlots(@plots)
  x_range = @plot_space.xRange.mutableCopy
  y_range = @plot_space.yRange.mutableCopy

  x_range.expandRangeByFactor(CPTDecimalFromDouble(1.03))
  y_range.expandRangeByFactor(CPTDecimalFromDouble(1.03))

  @plot_space.xRange = x_range
  @plot_space.yRange = y_range
end

#bootstrap(options) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/motion-plot/chart/base.rb', line 8

def bootstrap(options)
  options.each_pair {|key, value|
    send("#{key}=", value) if(respond_to?("#{key}="))
  }

  if(options[:title])
    @title = Title.new(options[:title])
  end

  @axes = {}
  if(options[:xAxis])
    @axes[:x] = Axis.new(options[:xAxis].merge(type: 'xaxis'))
  else
    @axes[:x] = Axis.new(type: 'xaxis', enabled: false)
  end

  if(options[:yAxis])
    @axes[:y] = Axis.new(options[:yAxis].merge(type: 'yaxis'))
  else
    @axes[:y] = Axis.new(type: 'yaxis', enabled: false)
  end

  if(@plot_options)
    @plot_options = PlotOptions.new(@plot_options)
  end

  @series = {}
  series = options[:series]
  series && series.each_with_index {|hash, index|
    hash.reverse_merge!({
      index: index, 
      defaults: default_style, 
      plot_options: @plot_options, 
      type: self.plot_type,
      plot_symbol: (@plot_symbol ? options[:plot_symbol] : nil)
    })
    @series[hash[:name]] = Series.new(hash)
  }

  if(options[:legend])
    @legend = Legend.new(options[:legend])
  end

  if(options[:data_label])
    @data_label = DataLabel.new(options[:data_label])
  end

  @plots = []
end

#default_paddingObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/motion-plot/chart/base.rb', line 175

def default_padding
  @graph.plotAreaFrame.masksToBorder    = false
  @graph.plotAreaFrame.borderLineStyle  = nil
  @graph.plotAreaFrame.paddingLeft      = 50.0
  @graph.plotAreaFrame.paddingTop       = 10.0
  @graph.plotAreaFrame.paddingRight     = 20.0
  @graph.plotAreaFrame.paddingBottom    = 20.0


  @graph.paddingLeft                    = 5.0
  @graph.paddingRight                   = 0.0
  @graph.paddingTop                     = 10.0
  @graph.paddingBottom                  = 10.0
end

#initWithOptions(options, containerView: container) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
# File 'lib/motion-plot/chart/base.rb', line 58

def initWithOptions(options, containerView:container)
  bootstrap(options)

  @layer_hosting_view = CPTGraphHostingView.alloc.initWithFrame([[0, 0], [container.frame.size.width, container.frame.size.height]])

  bounds = @layer_hosting_view.bounds

  # create and assign chart to the hosting view.
  @graph = CPTXYGraph.alloc.initWithFrame(bounds)
  @layer_hosting_view.hostedGraph = @graph

  add_chart_title(@title) if(@title)

  @graph.applyTheme(@theme || Theme.default)

  default_padding
  
  @chart_layers                         = [NSNumber.numberWithInt(CPTGraphLayerTypePlots), NSNumber.numberWithInt(CPTGraphLayerTypeMajorGridLines), NSNumber.numberWithInt(CPTGraphLayerTypeMinorGridLines), NSNumber.numberWithInt(CPTGraphLayerTypeAxisLines), NSNumber.numberWithInt(CPTGraphLayerTypeAxisLabels), NSNumber.numberWithInt(CPTGraphLayerTypeAxisTitles)]
  @graph.topDownLayerOrder              = @chart_layers
  
  
  # add plot space
  add_plot_space

  @major_grid_line_style            = CPTMutableLineStyle.lineStyle
  @major_grid_line_style.lineWidth  = 0.75
  @major_grid_line_style.lineColor  = CPTColor.grayColor.colorWithAlphaComponent(0.25)

  axisSet                           = @graph.axisSet

  # Setting up x-axis
  if(@axes[:x].enabled?)
    axis                          = @axes[:x]
    @xaxis                        = axisSet.xAxis
    @xaxis.majorGridLineStyle     = @major_grid_line_style
    @xaxis.minorTicksPerInterval  = 1

    add_axis_title(@xaxis, axis.title)

    if(axis.labels)
      labels = axis.labels.each_with_index.map do |l, i|
        @xaxis.labelingPolicy = CPTAxisLabelingPolicyNone
        label                 = CPTAxisLabel.alloc.initWithText(l, textStyle: axis.text_style)
        label.tickLocation    = CPTDecimalFromInt(i)
        label.offset          = 3.0
        label  
      end

      @xaxis.axisLabels = NSSet.setWithArray(labels)  
    end
  end

  # Setting up y-axis
  if(@axes[:y].enabled?)
    @yaxis                            = axisSet.yAxis
    @yaxis.majorGridLineStyle         = @major_grid_line_style
    @yaxis.minorTicksPerInterval      = 1
    @yaxis.labelingPolicy             = CPTAxisLabelingPolicyAutomatic

    axis = @axes[:y]
    add_axis_title(@yaxis, axis.title)
    @yaxis.setLabelTextStyle(axis.text_style)
  end

  @graph.axisSet = nil if(not @axes[:x].enabled? and not @axes[:y].enabled?)

  add_series

  add_legend if(@legend.enabled?)

  add_xy_range

  @layer_hosting_view
end