Class: AlGraph::Graph

Inherits:
GraphBase show all
Defined in:
lib/al_graph.rb

Overview

折れ線と棒グラフ用クラス

Direct Known Subclasses

AlGraphXY::GraphXY

Constant Summary collapse

SHAPE_LIST =

マーカ形状リスト初期値

[:circle, :rectangle, :diamond, :triangle, :cock]

Constants inherited from GraphBase

AlGraph::GraphBase::COLOR_LIST

Constants inherited from GraphView

AlGraph::GraphView::ATTR_NAMES

Instance Attribute Summary collapse

Attributes inherited from GraphBase

#at_graph_area, #at_legend, #at_main_title, #at_plot_area, #color_list, #output

Attributes inherited from GraphView

#data_series, #height, #width

Instance Method Summary collapse

Methods inherited from GraphBase

#add_aux_tag, #add_legend, #add_main_title, #add_text, #draw_buffer, #set_mode

Constructor Details

#initialize(width = 320, height = 240) ⇒ Graph

(Graph) constructor

Parameters:

  • width (Integer) (defaults to: 320)

  • height (Integer) (defaults to: 240)

    高さ



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/al_graph.rb', line 74

def initialize(width = 320, height = 240)
  super

  @shape_list = SHAPE_LIST
  @at_plot_area = {:x=>40, :y=>10, :fill=>'#eee'}

  # make default
  @at_plot_area[:width] = @width - 50
  @at_plot_area[:width] = 0 if @at_plot_area[:width] < 0
  @at_plot_area[:height] = @height - 30
  @at_plot_area[:Height] = 0 if @at_plot_area[:height] < 0
  @x_axis = XAxis.new(@at_plot_area[:width], @at_plot_area[:height])
  @y_axis = YAxis.new(@at_plot_area[:width], @at_plot_area[:height])
end

Instance Attribute Details

#at_xaxis_titleHash

Returns X軸タイトルアトリビュート.

Returns:

  • (Hash)

    X軸タイトルアトリビュート



38
39
40
# File 'lib/al_graph.rb', line 38

def at_xaxis_title
  @at_xaxis_title
end

#at_xaxis_unitHash

Returns X軸単位表示アトリビュート.

Returns:

  • (Hash)

    X軸単位表示アトリビュート



41
42
43
# File 'lib/al_graph.rb', line 41

def at_xaxis_unit
  @at_xaxis_unit
end

#at_y2axis_unitHash

Returns Y2軸単位表示アトリビュート.

Returns:

  • (Hash)

    Y2軸単位表示アトリビュート



50
51
52
# File 'lib/al_graph.rb', line 50

def at_y2axis_unit
  @at_y2axis_unit
end

#at_yaxis_titleHash

Returns Y軸タイトルアトリビュート.

Returns:

  • (Hash)

    Y軸タイトルアトリビュート



44
45
46
# File 'lib/al_graph.rb', line 44

def at_yaxis_title
  @at_yaxis_title
end

#at_yaxis_unitHash

Returns Y軸単位表示アトリビュート.

Returns:

  • (Hash)

    Y軸単位表示アトリビュート



47
48
49
# File 'lib/al_graph.rb', line 47

def at_yaxis_unit
  @at_yaxis_unit
end

#bar_plotBarPlot

Returns 棒グラフオブジェクト.

Returns:

  • (BarPlot)

    棒グラフオブジェクト



65
66
67
# File 'lib/al_graph.rb', line 65

def bar_plot
  @bar_plot
end

#line_plotLinePlot

Returns 折れ線グラフオブジェクト.

Returns:

  • (LinePlot)

    折れ線グラフオブジェクト



62
63
64
# File 'lib/al_graph.rb', line 62

def line_plot
  @line_plot
end

#shape_listArray<Symbol>

Returns マーカ形状リスト.

Returns:

  • (Array<Symbol>)

    マーカ形状リスト



35
36
37
# File 'lib/al_graph.rb', line 35

def shape_list
  @shape_list
end

#x_axisXAxis

Returns X軸オブジェクト.

Returns:

  • (XAxis)

    X軸オブジェクト



53
54
55
# File 'lib/al_graph.rb', line 53

def x_axis
  @x_axis
end

#y2_axisY2Axis

Returns Y2軸オブジェクト(もしあれば).

Returns:

  • (Y2Axis)

    Y2軸オブジェクト(もしあれば)



59
60
61
# File 'lib/al_graph.rb', line 59

def y2_axis
  @y2_axis
end

#y_axisYAxis

Returns Y軸オブジェクト.

Returns:

  • (YAxis)

    Y軸オブジェクト



56
57
58
# File 'lib/al_graph.rb', line 56

def y_axis
  @y_axis
end

Instance Method Details

#add_data_bar(ydata, legend = nil, base_bar = nil) ⇒ ContainerBar

(Graph) 棒グラフの追加

Parameters:

  • ydata (Array<Numeric>)

    データの配列

  • legend (String) (defaults to: nil)

    データの名前(凡例)

  • base_bar (ContainerBar) (defaults to: nil)

    積み重ねする場合、ベースになるデータコンテナ

Returns:

  • (ContainerBar)

    データコンテナオブジェクト



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/al_graph.rb', line 161

def add_data_bar(ydata, legend = nil, base_bar = nil)
  #
  # 積み重ねの場合、Y値を調整。
  #
  if base_bar
    diffsize = base_bar.y_data.size - ydata.size
    ydata.concat( Array.new(diffsize, 0) )  if diffsize > 0

    ydata.each_with_index {|yd, i|
      ydata[i] = 0  if !ydata[i]
      ydata[i] += base_bar.y_data[i]  if base_bar.y_data[i]
    }
  end

  add_legend() if legend

  @bar_plot = BarPlot.new(@width, @height) if ! @bar_plot
  @x_axis.change_mode(:CENTER)

  #
  # コンテナオブジェクトの生成
  #
  data_obj = ContainerBar.new(ydata, legend)
  data_obj.x_axis = @x_axis
  data_obj.y_axis = @y_axis
  data_obj.plot = @bar_plot
  data_obj.at_bar[:fill] = @color_list[ @data_series.size % @color_list.size ]

  #
  # コンテナを配列に保存
  #
  if base_bar
    data_obj.set_stack( base_bar )
    @data_series.each_with_index {|ds, i|
      if ds == base_bar
        @data_series.insert(i, data_obj)
        break
      end
    }
  else
    @data_series << data_obj
  end

  @bar_plot.add_data_series(data_obj, base_bar)
  @x_axis.add_data_series(data_obj)
  @y_axis.add_data_series(data_obj)

  return data_obj
end

#add_data_line(ydata, legend = nil) ⇒ ContainerLine

(Graph) 折れ線の追加

Parameters:

  • ydata (Array<Numeric>)

    データの配列

  • legend (String) (defaults to: nil)

    データの名前(凡例)

Returns:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/al_graph.rb', line 97

def add_data_line(ydata, legend = nil)
  add_legend() if legend
  @line_plot = LinePlot.new(@width, @height) if ! @line_plot

  data_obj = ContainerLine.new(ydata, legend)
  data_obj.x_axis = @x_axis
  data_obj.y_axis = @y_axis
  data_obj.plot = @line_plot
  data_obj.at_marker[:shape] = @shape_list[ @data_series.size % @shape_list.size ]
  data_obj.at_marker[:fill] = @color_list[ @data_series.size % @color_list.size ]
  data_obj.at_plot_line[:stroke] = data_obj.at_marker[:fill]

  add_data_series(data_obj)
  @line_plot.add_data_series(data_obj)
  @x_axis.add_data_series(data_obj)
  @y_axis.add_data_series(data_obj)

  return data_obj
end

#add_data_line_y2(ydata, legend = nil) ⇒ ContainerLine

(Graph) 第2Y軸上へ折れ線の追加

Parameters:

  • ydata (Array<Numeric>)

    データの配列

  • legend (String) (defaults to: nil)

    データの名前(凡例)

Returns:



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
# File 'lib/al_graph.rb', line 125

def add_data_line_y2(ydata, legend = nil)
  add_legend() if legend
  if ! @y2_axis
    @y2_axis =
      Y2Axis.new(@at_plot_area[:width], @at_plot_area[:height])
    right = @width - @at_plot_area[:width] - @at_plot_area[:x] + 20
    set_margin(nil, right, nil, nil)
  end

  @line_plot = LinePlot.new(@width, @height) if ! @line_plot

  data_obj = ContainerLine.new(ydata, legend)
  data_obj.x_axis = @x_axis
  data_obj.y_axis = @y2_axis
  data_obj.plot = @line_plot
  data_obj.at_marker[:shape] = @shape_list[ @data_series.size % @shape_list.size ]
  data_obj.at_marker[:fill] = @color_list[ @data_series.size % @color_list.size ]
  data_obj.at_plot_line[:stroke] = data_obj.at_marker[:fill]

  add_data_series(data_obj)
  @line_plot.add_data_series(data_obj)
  @x_axis.add_data_series(data_obj)
  @y2_axis.add_data_series(data_obj)

  return data_obj
end

#add_xaxis_title(title_string) ⇒ Object

(Graph) X軸タイトルの追加

Parameters:

  • title_string (String)

    タイトル文字列



402
403
404
405
406
# File 'lib/al_graph.rb', line 402

def add_xaxis_title(title_string)
  set_margin(nil, nil, 35, nil)
  @at_xaxis_title =
    {:node_value=>title_string, :font_size=>12, :text_anchor=>'middle'}
end

#add_xaxis_unit(unit_string) ⇒ Object

(Graph) X軸単位表示の追加

Parameters:

  • unit_string (String)

    単位文字列



414
415
416
417
418
# File 'lib/al_graph.rb', line 414

def add_xaxis_unit(unit_string)
  set_margin(nil, nil, 35, nil)
  @at_xaxis_unit =
    {:node_value=>unit_string, :font_size=>12, :text_anchor=>'middle'}
end

#add_y2axis_unit(unit_string) ⇒ Object

(Graph) 第2Y軸単位表示の追加

Parameters:

  • unit_string (String)

    単位文字列



451
452
453
454
455
# File 'lib/al_graph.rb', line 451

def add_y2axis_unit(unit_string)
  set_margin(25, nil, nil, nil)
  @at_y2axis_unit =
    {:node_value=>unit_string, :font_size=>12, :text_anchor=>'start'}
end

#add_yaxis_title(title_string) ⇒ Object

(Graph) Y軸タイトルの追加

Parameters:

  • title_string (String)

    タイトル文字列



426
427
428
429
430
# File 'lib/al_graph.rb', line 426

def add_yaxis_title(title_string)
  set_margin(nil, nil, nil, 50)
  @at_yaxis_title =
    {:node_value=>title_string, :font_size=>12, :text_anchor=>'middle'}
end

#add_yaxis_unit(unit_string) ⇒ Object

(Graph) Y軸単位表示の追加

Parameters:

  • unit_string (String)

    単位文字列



438
439
440
441
442
# File 'lib/al_graph.rb', line 438

def add_yaxis_unit(unit_string)
  set_margin(25, nil, nil, nil)
  @at_yaxis_unit =
    {:node_value=>unit_string, :font_size=>12, :text_anchor=>'start'}
end

#drawObject

(Graph) 描画

管理下のオブジェクトを次々とcallして、全体を描画する。 (ユーザが個々の内部オブジェクトのdrawメソッドを使うことは無い)

Raises:



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/al_graph.rb', line 222

def draw()
  #
  # scaling.
  #
  if !@x_axis.do_scaling
    raise AutoScaleErrorX
  end
  if !@y_axis.do_scaling
    raise AutoScaleErrorY
  end
  if @y2_axis && !@y2_axis.do_scaling
    raise AutoScaleErrorY2
  end

  #
  # draw base items.
  #
  draw_common1()

  #
  # output plot area's clipping path.
  #
  @output.printf(%!<clipPath id="plotarea">\n!)
  @output.printf(%!  <rect x="%d" y="%d" width="%d" height="%d" />\n!, -5, -5, @at_plot_area[:width] + 10, @at_plot_area[:height] + 10)
  @output.printf("</clipPath>\n")

  #
  # grouping in plot items.
  #
  @output.printf(%!<g transform="translate(%d,%d)">\n!, @at_plot_area[:x], @at_plot_area[:y])

  #
  # draw X,Y axis
  #
  @x_axis.draw_z1(@output)
  @y_axis.draw_z1(@output)
  @y2_axis.draw_z1(@output) if @y2_axis

  @x_axis.draw_z2(@output)
  @y_axis.draw_z2(@output)
  @y2_axis.draw_z2(@output) if @y2_axis

  #
  # draw data series.
  #
  @output.printf("\n<!-- draw lines and bars in clipping path -->\n")
  @output.printf(%!<g clip-path="url(#plotarea)">\n!)

  @bar_plot.draw(@output) if @bar_plot
  @line_plot.draw(@output) if @line_plot

  @output.printf("</g><!-- end of clip -->\n")

  #
  # end of group
  #
  @output.printf("</g><!-- end of plot area -->\n\n")


  #
  # draw legend
  #
  if @at_legend
    @output.printf("\n<!-- draw legends -->\n")
    if ! @at_legend[:y]
      @at_legend[:y] = (@height - @data_series.size * (@at_legend[:font_size] + @at_legend[:line_spacing])) / 2
      @at_legend[:y] = 0 if @at_legend[:y] < 0
    end

    attr = @at_legend.dup
    attr[:x] += 10
    attr[:y] += attr[:font_size]

    @data_series.each {|ds|
      @output.printf(%!<g%s>\n!, ds.id ? %! id="legend-#{ds.id}"! : "")

      @output.printf("  <text %s>%s</text>\n  ", make_common_attribute_string(attr), Alone::escape_html(ds.legend))
      ds.plot.draw_legend_marker(@output, attr[:x] - 10, (attr[:y] - attr[:font_size] / 3.0).to_i, ds)
      attr[:y] += attr[:font_size] + attr[:line_spacing]
      @output.printf("</g>\n")
    }
    @output.printf("\n")
  end

  #
  # draw x-axis title
  #
  if @at_xaxis_title
    @at_xaxis_title[:x] ||= @at_plot_area[:x] + @at_plot_area[:width] / 2
    @at_xaxis_title[:y] ||= @height - 5

    @output.printf("<text %s>%s</text>\n",
      make_common_attribute_string(@at_xaxis_title),
      Alone::escape_html(@at_xaxis_title[:node_value]))
  end

  #
  # draw x-axis unit
  #
  if @at_xaxis_unit
    @at_xaxis_unit[:x] ||= @at_plot_area[:x] + @at_plot_area[:width]
    @at_xaxis_unit[:y] ||= @height - 5

    @output.printf("<text %s>%s</text>\n",
      make_common_attribute_string(@at_xaxis_unit),
      Alone::escape_html(@at_xaxis_unit[:node_value]))
  end

  #
  # draw y-axis title
  #
  if @at_yaxis_title
    @at_yaxis_title[:x] ||= @at_yaxis_title[:font_size] + 5
    @at_yaxis_title[:y] ||= @at_plot_area[:y] + @at_plot_area[:height] / 2

    @output.printf(%!<text %s transform="rotate(-90,%d,%d)">%s</text>\n!,
      make_common_attribute_string(@at_yaxis_title),
      @at_yaxis_title[:x], @at_yaxis_title[:y],
      Alone::escape_html(@at_yaxis_title[:node_value]))
  end

  #
  # draw y-axis unit
  #
  if @at_yaxis_unit
    @at_yaxis_unit[:x] ||= @at_yaxis_unit[:font_size]
    @at_yaxis_unit[:y] ||= @at_plot_area[:y] - 6

    @output.printf(%!<text %s>%s</text>\n!,
      make_common_attribute_string(@at_yaxis_unit),
      Alone::escape_html(@at_yaxis_unit[:node_value]))
  end

  #
  # draw y2-axis unit
  #
  if @at_y2axis_unit
    @at_y2axis_unit[:x] ||= @at_plot_area[:x] + @at_plot_area[:width]
    @at_y2axis_unit[:y] ||= @at_plot_area[:y] - 6

    @output.printf(%!<text %s>%s</text>\n!,
      make_common_attribute_string(@at_y2axis_unit),
      Alone::escape_html(@at_y2axis_unit[:node_value]))
  end

  draw_common2()
end

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

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

Parameters:

  • top (Integer)

    上マージン

  • right (Integer)

    右マージン

  • bottom (Integer)

    下マージン

  • left (Integer)

    左マージン

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



382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/al_graph.rb', line 382

def set_margin(top, right, bottom, left)
  super

  # set axis objects parameters
  @x_axis.width = @at_plot_area[:width]
  @x_axis.height = @at_plot_area[:height]
  @y_axis.width = @at_plot_area[:width]
  @y_axis.height = @at_plot_area[:height]
  if @y2_axis
    @y2_axis.width = @at_plot_area[:width]
    @y2_axis.height = @at_plot_area[:height]
  end
end