Rails: Gruff进阶应用

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

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


[img]/upload/attachment/131689/191fbafa-335c-3dfd-89ce-15f0d90126d9.png[/img]
#   :background_colors => %w(black grey),  
# :background_image => 'squirrel.png'

[img]/upload/attachment/131671/d9ca1abf-f9b3-3d26-a91d-8e9cc9c4effc.png[/img]

[img]/upload/attachment/131669/2326b19a-6fe9-3b38-b1ed-6aedb95750d6.png[/img]

[img]/upload/attachment/131679/811ffe10-395a-34ca-b6fe-951a1aa344d4.png[/img]

[img]/upload/attachment/131675/2875c27a-c2a1-3539-a1a0-ffad0c71b047.png[/img]

[img]/upload/attachment/131672/14755250-4a38-3f5f-919d-b2e47c6919d6.png[/img]

[img]/upload/attachment/131681/e1cb722e-c7fa-3466-b9ff-9bbdaf945a5f.png[/img]

[img]/upload/attachment/131677/2744dab4-8a81-3efc-9046-6f1dbed48bcd.png[/img]

[img]/upload/attachment/131680/1dbb28c8-04a9-3b32-aff4-b4f4e84f0060.png[/img]

[img]/upload/attachment/131685/3a3fb4e8-8469-323b-bb13-4e235b7a16c6.png[/img]

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

一些有用的参数:

    # 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值