Class: AlGraph::GraphBase

Inherits:
GraphView show all
Defined in:
lib/al_graph_base.rb

Overview

各グラフのベースクラス

Direct Known Subclasses

Graph, AlGraphPie::GraphPie

Constant Summary collapse

COLOR_LIST =
[
'#004586', '#ff420e', '#ffd320', '#579d1c', '#7e0021', '#83caff',
'#314004', '#aecf00', '#4b1f6f', '#ff950e', '#c5000b', '#0084d1' ]

Constants inherited from GraphView

AlGraph::GraphView::ATTR_NAMES

Instance Attribute Summary collapse

Attributes inherited from GraphView

#data_series, #height, #width

Instance Method Summary collapse

Constructor Details

#initialize(width, height) ⇒ GraphBase

(GraphBase) constructor

Parameters:

  • width (Integer)

  • height (Integer)

    高さ



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/al_graph_base.rb', line 164

def initialize(width, height)
  super( width, height )

  @at_graph_area = { :width=>@width, :height=>@height,
                     :stroke_width=>1, :stroke=>"black", :fill=>"white" }
  @at_plot_area = { :x=>0, :y=>0, :width=>0, :height=>0 }
  @at_main_title = nil
  @at_legend = nil
  @color_list = COLOR_LIST

  # 追加任意タグ
  @aux_tags = []
  # 動作モード (see set_mode() function.)
  @work_mode = {:SVGTAG_START=>true, :SVGTAG_END=>true}

  @output = Kernel
end

Instance Attribute Details

#at_graph_areaHash

Returns グラフエリアアトリビュート.

Returns:

  • (Hash)

    グラフエリアアトリビュート



139
140
141
# File 'lib/al_graph_base.rb', line 139

def at_graph_area
  @at_graph_area
end

#at_legendHash

Returns 凡例アトリビュート.

Returns:

  • (Hash)

    凡例アトリビュート



148
149
150
# File 'lib/al_graph_base.rb', line 148

def at_legend
  @at_legend
end

#at_main_titleHash

Returns メインタイトルアトリビュート.

Returns:

  • (Hash)

    メインタイトルアトリビュート



145
146
147
# File 'lib/al_graph_base.rb', line 145

def at_main_title
  @at_main_title
end

#at_plot_areaHash

Returns プロットエリアアトリビュート.

Returns:

  • (Hash)

    プロットエリアアトリビュート



142
143
144
# File 'lib/al_graph_base.rb', line 142

def at_plot_area
  @at_plot_area
end

#color_listArray<String>

Returns 色リスト.

Returns:

  • (Array<String>)

    色リスト



151
152
153
# File 'lib/al_graph_base.rb', line 151

def color_list
  @color_list
end

#outputKernel, StringIO

Returns 出力制御オブジェクト.

Returns:

  • (Kernel, StringIO)

    出力制御オブジェクト



154
155
156
# File 'lib/al_graph_base.rb', line 154

def output
  @output
end

Instance Method Details

#add_aux_tag(text) ⇒ Object

(GraphBase) 任意タグを追加

Parameters:

  • text (String)

    タグテキスト



284
285
286
# File 'lib/al_graph_base.rb', line 284

def add_aux_tag(text)
  @aux_tags << text
end

#add_legendObject

(GraphBase) 凡例表示追加

自動追加されるので、たいていの場合、ユーザがこのメソッドを使うことはないかもしれない。


270
271
272
273
274
275
276
# File 'lib/al_graph_base.rb', line 270

def add_legend()
  return if @at_legend

  right = @width - @at_plot_area[:width] - @at_plot_area[:x] + 70
  set_margin(nil, right, nil, nil)
  @at_legend = {:x=>@width - 60, :font_size=>10, :line_spacing=>4}
end

#add_main_title(title_string) ⇒ Object

(GraphBase) メインタイトルの追加

Parameters:

  • title_string (String)

    タイトル文字列



259
260
261
262
# File 'lib/al_graph_base.rb', line 259

def add_main_title(title_string)
  set_margin(25, nil, nil, nil)
  @at_main_title = {:node_value=>title_string, :y=>20, :font_size=>16, :text_anchor=>'middle'}
end

#add_text(x, y, text) ⇒ Object

(GraphBase) テキスト追加

Parameters:

  • x (Integer)

    X座標

  • y (Integer)

    Y座標

  • text (String)

    テキスト

    addAuxTag()の簡易テキスト版。 フォントサイズの指定などは、<tspan>要素を使える。



299
300
301
# File 'lib/al_graph_base.rb', line 299

def add_text(x, y, text)
  @aux_tags << %!<text x="#{x}" y="#{y}">#{text}</text>\n!
end

#draw_bufferObject

(GraphBase) バッファーへ描画

Examples:

graph = AlGraph.new
str = graph.draw_buffer()   # insted of draw()


240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/al_graph_base.rb', line 240

def draw_buffer()
  @work_mode[:CONTENT_TYPE] = false if !@work_mode.key?(:CONTENT_TYPE)
  @work_mode[:XML_DECLARATION] = false if !@work_mode.key?(:XML_DECLARATION)

  output_bak = @output
  s = ""
  @output = StringIO.new(s)
  draw()
  @output = output_bak

  return s
end

#set_margin(top, right, bottom, left) ⇒ Object

(GraphBase) プロットエリアのマージン設定

Parameters:

  • top (Integer)

    上マージン

  • right (Integer)

    右マージン

  • bottom (Integer)

    下マージン

  • left (Integer)

    左マージン

    上下左右個別に設定できる。 設定値を変えない場合は、そのパラメータをnilにしてcallする。



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/al_graph_base.rb', line 216

def set_margin(top, right, bottom, left)
  # calculate insufficiency parameters.
  top    ||= @at_plot_area[:y]
  right  ||= @width - @at_plot_area[:width] - @at_plot_area[:x]
  bottom ||= @height - @at_plot_area[:height] - @at_plot_area[:y]
  left   ||= @at_plot_area[:x]

  # set plot area parameters
  @at_plot_area[:x] = left
  @at_plot_area[:y] = top
  @at_plot_area[:width] = @width - left - right
  @at_plot_area[:height] = @height - top - bottom
  @at_plot_area[:width] = 0 if @at_plot_area[:width] < 0
  @at_plot_area[:height] = 0 if @at_plot_area[:height] < 0
end

#set_mode(mode) ⇒ Object

(GraphBase) 動作モード指定

Parameters:

  • mode (Symbol)

    動作モード

    設定可能モード(先頭にNOをつけると意味を反転)

    :CONTENT_TYPE     ContentType ヘッダを出力する/しない
    :XML_DECLARATION  XML宣言およびDOCTYPE宣言を出力する/しない
    :SVGTAG_START     SVG開始タグを出力する/しない
    :SVGTAG_END       SVG終了タグを出力する/しない
    


194
195
196
197
198
199
200
201
202
# File 'lib/al_graph_base.rb', line 194

def set_mode(mode)
  mode = mode.to_s
  if mode.start_with?("NO_")
    mode.slice!(0, 3)
    @work_mode[mode.to_sym] = false
  else
    @work_mode[mode.to_sym] = true
  end
end