`

Rails: Gruff进阶应用

阅读更多
    Gruff的介绍和文档都不怎么多,大部分都要靠自己看代码来猜怎么用,下面是部分的进阶内容,和收集的资料。以备自己和可能用到的XD参考

    或者,我们可以先看看Gruff都可以干什么,然后有兴趣或者有需求的就可以接着看了。



#   :background_colors => %w(black grey),  
#   :background_image => 'squirrel.png' 



















  • Gruff::Line 就一般的线图 股市行情什么的可以用用
  • Gruff::Pie Pie图 市场份额一类的比较方便
  • Gruff::Bar 直方图 年份变化一类都可用
  • Gruff:Area 面积图 似乎用处不大
  • Gruff:Scene 场景图
  • Gruff::Spider
  • Gruff::Net


一些有用的参数:

    # By default, labels are centered over the point they represent.
    attr_accessor :center_labels_over_point

    # Used internally for horizontal graph types.
    attr_accessor :has_left_labels

    # X轴坐标
    attr_accessor :x_axis_label
    
    # Y轴的坐标
    attr_accessor :y_axis_label

    # attr_accessor :x_axis_increment
    
    # Manually set increment of the horizontal marking lines
    attr_accessor :y_axis_increment

    # Get or set the list of colors that will be used to draw the bars or lines.
    attr_accessor :colors

    # The large title of the graph displayed at the top
    attr_accessor :title

    # Font used for titles, labels, etc. Works best if you provide the full path to the TTF font file.
    # RMagick must be built with the Freetype libraries for this to work properly.
    #
    # Tries to find Bitstream Vera (Vera.ttf) in the location specified by
    # ENV['MAGICK_FONT_PATH']. Uses default RMagick font otherwise.
    #
    # The font= method below fulfills the role of the writer, so we only need 
    # a reader here.
    attr_reader :font

    attr_accessor :font_color

    # Hide various elements
    attr_accessor :hide_line_markers, :hide_legend, :hide_title, :hide_line_numbers

    # Message shown when there is no data. Fits up to 20 characters. Defaults to "No Data."
    attr_accessor :no_data_message

    # The font size of the large title at the top of the graph
    attr_accessor :title_font_size

    # Optionally set the size of the font. Based on an 800x600px graph. Default is 20.
    #
    # Will be scaled down if graph is smaller than 800px wide.
    attr_accessor :legend_font_size

    # The font size of the labels around the graph
    attr_accessor :marker_font_size
    
    # The color of the auxiliary lines
    attr_accessor :marker_color

    # 设定可以显示的参考线数量
    attr_accessor :marker_count
    

    # You can manually set a minimum value instead of having the values guessed for you.
    #这个很有用,我们当时就发现这里有问题
    # Set it after you have given all your data to the graph object.
    attr_accessor :minimum_value

    # You can manually set a maximum value, such as a percentage-based graph that always goes to 100.
    #
    # If you use this, you must set it after you have given all your data to the graph object.
    attr_accessor :maximum_value
    
    # Set to false if you don't want the data to be sorted with largest avg values at the back.
    attr_accessor :sort
    
    # Experimental
    attr_accessor :additional_line_values
    
    # Experimental
    attr_accessor :stacked
    
    
    # Optionally set the size of the colored box by each item in the legend. Default is 20.0
    #
    # Will be scaled down if graph is smaller than 800px wide.
    attr_accessor :legend_box_size


我们还用到了一个参数baseline_value,这个也郁闷了我们一段时间,设定这个值会确定一个基础线

class Gruff::Base
     def setup_graph_measurements
       # TODO Separate horizontal lines from line number labels so they 
can be shown or hidden independently
       # TODO Get width of longest left-hand vertical text label and 
space left margin accordingly
       unless @hide_line_markers
         @graph_left = 130.0 # TODO Calculate based on string width of 
labels
         @graph_right_margin = 80.0 # TODO see previous line
         @graph_bottom_margin = 400.0
       else
         @graph_left = @graph_right_margin = @graph_bottom_margin =  40
       end

       @graph_right = @raw_columns - @graph_right_margin

       @graph_width = @raw_columns - @graph_left - @graph_right_margin

       @graph_top = 150.0
       @graph_bottom = @raw_rows - @graph_bottom_margin
       @graph_height = @graph_bottom - @graph_top
     end


     def draw_label(x_offset, index)
       return if @hide_line_markers

       if !@labels[index].nil? && @labels_seen[index].nil?
           #@d.fill = @marker_color
         @d.font = @font if @font
         @d.stroke = 'transparent'
        @d.rotation = 90
        @d.text_align( LeftAlign)
         @d.font_weight = NormalWeight
         @d.pointsize = scale_fontsize(@marker_font_size)
         @d.gravity = NorthWestGravity #CenterGravity
         @d = @d.annotate_scaled(@base_image,
                                 1, 1000,
                                 x_offset, @raw_rows - 
(@graph_bottom_margin - 30),
                                 @labels[index], @scale)
        @d.rotation = -90
         @labels_seen[index] = 1
       end
     end
end
  • 大小: 57.4 KB
  • 大小: 60 KB
  • 大小: 48.2 KB
  • 大小: 49.7 KB
  • 大小: 48.4 KB
  • 大小: 109.1 KB
  • 大小: 45.4 KB
  • 大小: 43.8 KB
  • 大小: 75.5 KB
  • 大小: 511.8 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics