Class: AlGraph::Graph

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

Overview

Note:

ユーザがnewして利用するクラス。

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

Constant Summary

SHAPELIST =

マーカ形状リスト(折れ線グラフのみ)

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

Constants inherited from GraphBase

AlGraph::GraphBase::ATTR_NAMES, AlGraph::GraphBase::ATTR_UNITS

Instance Attribute Summary (collapse)

Attributes inherited from GraphUtil

#at_graph_area, #at_legend, #at_main_title, #color_list

Attributes inherited from GraphBase

#height, #output, #width

Instance Method Summary (collapse)

Methods inherited from GraphUtil

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

Constructor Details

- (Graph) initialize(width = 320, height = 240)

constructor

Parameters:

  • width (Integer) (defaults to: 320)

  • height (Integer) (defaults to: 240)

    高さ



730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
# File 'lib/al_graph.rb', line 730

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

  @at_plot_area = {:x => 40, :y =>10, :fill => '#eee'}
  @at_xaxis_title = nil
  @at_yaxis_title = nil
  @y2_axis = nil
  @line_plot = nil
  @bar_plot = nil
  
  # 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], @output) 
  @y_axis =
    YAxis.new(@at_plot_area[:width], @at_plot_area[:height], @output) 
end

Instance Attribute Details

- (Hash) at_plot_area

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

Returns:

  • (Hash)

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



701
702
703
# File 'lib/al_graph.rb', line 701

def at_plot_area
  @at_plot_area
end

- (Hash) at_xaxis_title

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

Returns:

  • (Hash)

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



704
705
706
# File 'lib/al_graph.rb', line 704

def at_xaxis_title
  @at_xaxis_title
end

- (Hash) at_yaxis_title

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

Returns:

  • (Hash)

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



707
708
709
# File 'lib/al_graph.rb', line 707

def at_yaxis_title
  @at_yaxis_title
end

- (BarPlot) bar_plot

Returns 棒グラフオブジェクト

Returns:

  • (BarPlot)

    棒グラフオブジェクト



722
723
724
# File 'lib/al_graph.rb', line 722

def bar_plot
  @bar_plot
end

- (LinePlot) line_plot

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

Returns:

  • (LinePlot)

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



719
720
721
# File 'lib/al_graph.rb', line 719

def line_plot
  @line_plot
end

- (XAxis) x_axis

Returns X軸オブジェクト

Returns:

  • (XAxis)

    X軸オブジェクト



710
711
712
# File 'lib/al_graph.rb', line 710

def x_axis
  @x_axis
end

- (Y2Axis) y2_axis

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

Returns:

  • (Y2Axis)

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



716
717
718
# File 'lib/al_graph.rb', line 716

def y2_axis
  @y2_axis
end

- (YAxis) y_axis

Returns Y軸オブジェクト

Returns:

  • (YAxis)

    Y軸オブジェクト



713
714
715
# File 'lib/al_graph.rb', line 713

def y_axis
  @y_axis
end

Instance Method Details

- (ContainerBar) add_data_bar(ydata, legend = nil, base_bar = nil)

棒グラフの追加

Parameters:

  • ydata (Array<Numeric>)

    データの配列

  • legend (String) (defaults to: nil)

    データの名前(凡例)

  • base_bar (ContainerBar) (defaults to: nil)

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

Returns:

  • (ContainerBar)

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



811
812
813
814
815
816
817
818
819
820
821
822
# File 'lib/al_graph.rb', line 811

def add_data_bar(ydata, legend = nil, base_bar = nil)
  data_obj = add_data_bar_common(ydata, legend, base_bar)
  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 ]

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

  return data_obj
end

- (ContainerLine) add_data_line(ydata, legend = '')

折れ線の追加

Parameters:

  • ydata (Array<Numeric>)

    データの配列

  • legend (String) (defaults to: '')

    データの名前(凡例)

Returns:



758
759
760
761
762
763
764
765
766
767
768
769
770
771
# File 'lib/al_graph.rb', line 758

def add_data_line(ydata, legend = '')
  data_obj = add_data_line_common(ydata, legend)
  data_obj.x_axis = @x_axis
  data_obj.y_axis = @y_axis
  data_obj.plot = @line_plot

  data_obj.at_plot_line[:stroke] = data_obj.at_marker[:fill] = @color_list[ @data_series.size % @color_list.size ]
  data_obj.at_marker[:shape] = SHAPELIST[ @data_series.size % SHAPELIST.size ]

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

  return data_obj
end

- (ContainerLine) add_data_line_y2(ydata, legend = nil)

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

Parameters:

  • ydata (Array<Numeric>)

    データの配列

  • legend (String) (defaults to: nil)

    データの名前(凡例)

Returns:



780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
# File 'lib/al_graph.rb', line 780

def add_data_line_y2(ydata, legend = nil)
  data_obj = add_data_line_common(ydata, legend)

  unless @y2_axis
    @y2_axis =
      Y2Axis.new(@at_plot_area[:width], @at_plot_area[:height],@output)
    right = @width - @at_plot_area[:width] - @at_plot_area[:x] + 20
    set_margin(nil, right, nil, nil)
  end

  data_obj.x_axis = @x_axis
  data_obj.y_axis = @y2_axis
  data_obj.plot = @line_plot

  data_obj.at_plot_line[:stroke] = data_obj.at_marker[:fill] = @color_list[ @data_series.size % @color_list.size ]
  data_obj.at_marker[:shape] = SHAPELIST[ @data_series.size % SHAPELIST.size ]

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

  return data_obj
end

- (Object) add_xaxis_title(title_string)

X軸タイトルの追加

Parameters:

  • title_string (String)

    タイトル文字列



974
975
976
977
978
# File 'lib/al_graph.rb', line 974

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

- (Object) add_yaxis_title(title_string)

Y軸タイトルの追加

Parameters:

  • title_string (String)

    タイトル文字列



985
986
987
988
989
# File 'lib/al_graph.rb', line 985

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

- (Object) draw

Note:

描画

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



831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
# File 'lib/al_graph.rb', line 831

def draw()
  #
  # scaling.
  #
  if ! @x_axis.do_scaling || ! @y_axis.do_scaling
    raise "Wrong data. Can't auto scaling."
    return
  end

  if @y2_axis && ! @y2_axis.do_scaling
    raise "Wrong data. Can't auto scaling."
    return
  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()
  @y_axis.draw_z1()
  @y2_axis.draw_z1() if @y2_axis

  @x_axis.draw_z2()
  @y_axis.draw_z2()
  @y2_axis.draw_z2() 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() if @bar_plot
  @line_plot.draw() 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
    unless @at_legend[:y] 
      @at_legend[:y] = (@height - @data_series.size * (@at_legend[:font_size] + 4)) / 2
      @at_legend[:y] = 0 if (@at_legend[:y] < 0) 
    end

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

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

  #
  # draw x-axis title
  #
  if @at_xaxis_title
    unless @at_xaxis_title[:x]
      @at_xaxis_title[:x] = @at_plot_area[:x] + @at_plot_area[:width] / 2
    end
    unless @at_xaxis_title[:y]
      @at_xaxis_title[:y] = @height - 5
    end
    @output.printf("<text %s>%s</text>\n",
      make_common_attribute_string(@at_xaxis_title),
      Alone::escape_html(@at_xaxis_title[:title]))
  end

  #
  # draw y-axis title
  #
  if @at_yaxis_title
    unless @at_yaxis_title[:x]
      @at_yaxis_title[:x] = @at_yaxis_title[:font_size] + 5
    end

    unless @at_yaxis_title[:y]
      @at_yaxis_title[:y] = @at_plot_area[:y] + @at_plot_area[:height] / 2
    end
    @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[:title]))
  end
  draw_common2()
end

- (Object) set_margin(top, right, bottom, left)

Note:

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

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

Parameters:

  • top (Integer)

    上マージン

  • right (Integer)

    右マージン

  • bottom (Integer)

    下マージン

  • left (Integer)

    左マージン



955
956
957
958
959
960
961
962
963
964
965
966
967
# File 'lib/al_graph.rb', line 955

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