Rails: Gruff进阶应用
生活随笔
收集整理的這篇文章主要介紹了
Rails: Gruff进阶应用
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Gruff的介紹和文檔都不怎么多,大部分都要靠自己看代碼來(lái)猜怎么用,下面是部分的進(jìn)階內(nèi)容,和收集的資料。以備自己和可能用到的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圖 市場(chǎng)份額一類的比較方便
[*]Gruff::Bar 直方圖 年份變化一類都可用
[*]Gruff:Area 面積圖 似乎用處不大
[*]Gruff:Scene 場(chǎng)景圖
[*]Gruff::Spider
[*]Gruff::Net
[/list]
一些有用的參數(shù):
# 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軸坐標(biāo)
attr_accessor :x_axis_label
# Y軸的坐標(biāo)
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
# 設(shè)定可以顯示的參考線數(shù)量
attr_accessor :marker_count
# You can manually set a minimum value instead of having the values guessed for you.
#這個(gè)很有用,我們當(dāng)時(shí)就發(fā)現(xiàn)這里有問(wèn)題
# 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
我們還用到了一個(gè)參數(shù)baseline_value,這個(gè)也郁悶了我們一段時(shí)間,設(shè)定這個(gè)值會(huì)確定一個(gè)基礎(chǔ)線
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
或者,我們可以先看看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圖 市場(chǎng)份額一類的比較方便
[*]Gruff::Bar 直方圖 年份變化一類都可用
[*]Gruff:Area 面積圖 似乎用處不大
[*]Gruff:Scene 場(chǎng)景圖
[*]Gruff::Spider
[*]Gruff::Net
[/list]
一些有用的參數(shù):
# 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軸坐標(biāo)
attr_accessor :x_axis_label
# Y軸的坐標(biāo)
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
# 設(shè)定可以顯示的參考線數(shù)量
attr_accessor :marker_count
# You can manually set a minimum value instead of having the values guessed for you.
#這個(gè)很有用,我們當(dāng)時(shí)就發(fā)現(xiàn)這里有問(wèn)題
# 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
我們還用到了一個(gè)參數(shù)baseline_value,這個(gè)也郁悶了我們一段時(shí)間,設(shè)定這個(gè)值會(huì)確定一個(gè)基礎(chǔ)線
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
總結(jié)
以上是生活随笔為你收集整理的Rails: Gruff进阶应用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python做项目管理_python项目
- 下一篇: Win11怎么设置用独立显卡来运行游戏