subtle-graph 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,29 @@
1
+ Copyright (c) 2010, Dominik Honnef <dominikho@gmx.net>
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are
6
+ met:
7
+
8
+ - Redistributions of source code must retain the above copyright
9
+ notice, this list of conditions and the following disclaimer.
10
+
11
+ - Redistributions in binary form must reproduce the above copyright
12
+ notice, this list of conditions and the following disclaimer in the
13
+ documentation and/or other materials provided with the distribution.
14
+
15
+ - Neither the name of the author nor the names of the
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
+ HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README ADDED
File without changes
@@ -0,0 +1,51 @@
1
+ require "subtle/dominikh/dynamic_icon"
2
+
3
+ module Dominikh
4
+ class Graph < DynamicIcon
5
+ attr_accessor :color_ranges
6
+ attr_reader :value
7
+
8
+ def initialize(*args)
9
+ super
10
+ @color_ranges = {}
11
+ @last_rendered_value = nil
12
+ @value = 0
13
+ end
14
+
15
+ # @return [String]
16
+ def to_str
17
+ unless @last_rendered_value == value
18
+ render
19
+ end
20
+ super
21
+ end
22
+ alias_method :to_s, :to_str
23
+
24
+ # Returns the color of the graph. If no suiting color can be found
25
+ # in @color_ranges, it will default to the color set by {DynamicIcon#color=}.
26
+ #
27
+ # @return [Subtlext::Color]
28
+ def color
29
+ color = @color_ranges.find {|key, value| key.include?(self.value) }
30
+ if color
31
+ Subtlext::Color.new(color[1])
32
+ else
33
+ @color
34
+ end
35
+ end
36
+
37
+ # Draws the graph. This method gets called automatically whenever
38
+ # the value of the graph changes. Extend this method in subclasses
39
+ # to define custom drawing routines.
40
+ #
41
+ # @return [void]
42
+ def render
43
+ super
44
+ @last_rendered_value = value
45
+ end
46
+ end
47
+ end
48
+
49
+ require "subtle/dominikh/graph/bar"
50
+ require "subtle/dominikh/graph/chart"
51
+ require "subtle/dominikh/graph/colored_graph"
@@ -0,0 +1,43 @@
1
+ require "subtle/dominikh/graph"
2
+ module Dominikh
3
+ class Bar < Graph
4
+ attr_reader :value
5
+
6
+ def initialize(width, height, orientation = :vertical)
7
+ super(width, height)
8
+ @orientation = orientation
9
+ @value = 0
10
+ end
11
+
12
+ # @return The value
13
+ def value=(val)
14
+ val = 100 if val > 100
15
+ @value = val
16
+ render
17
+ @value
18
+ end
19
+
20
+ # @return [void]
21
+ def render
22
+ super
23
+
24
+ if @orientation == :vertical
25
+ top = ((height / 100.0) * value).round
26
+ 0.upto(width) do |x|
27
+ height.downto(height - top) do |y|
28
+ draw(x, y)
29
+ end
30
+ end
31
+ elsif @orientation == :horizontal
32
+ right = ((width / 100.0) * value).round
33
+ 0.upto(height) do |y|
34
+ 0.upto(right) do |x|
35
+ draw(x, y)
36
+ end
37
+ end
38
+ else
39
+ # TODO raise
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,79 @@
1
+ require "subtle/dominikh/graph"
2
+
3
+ module Dominikh
4
+ class Chart < Graph
5
+ attr_reader :values
6
+
7
+ # @param [Fixnum] width The width of the chart. This corresponds
8
+ # with the number of values.
9
+ # @param [Fixnum] height The height of the chart.
10
+ # @param [Boolean] autoscale If false, values are expected to be
11
+ # percentage values. If true, values will be dynamically scaled
12
+ # based on the biggest one.
13
+ def initialize(width, height, autoscale = false)
14
+ super(width, height)
15
+ @values = [0]
16
+ @autoscale = autoscale
17
+ if autoscale
18
+ @biggest_value = 0
19
+ end
20
+ end
21
+
22
+ # @return [Number] The most current value
23
+ def value
24
+ @values.first
25
+ end
26
+
27
+ # Allows setting an array of values. If too many values are
28
+ # provied, the array will be truncated
29
+ #
30
+ # @param [Array<Numeric>] arr An array of numbers
31
+ # @return [Array] The array of values, possibly truncated
32
+ def values=(arr)
33
+ @values = arr[0..@icon.width]
34
+ max = @values.max
35
+ @biggest_value = max if max > @biggest_value
36
+ render
37
+ @values
38
+ end
39
+
40
+ # Adds a new value, potentially removing the oldest one if the max
41
+ # number of values is exceeded
42
+ #
43
+ # @param [Numeric] value A value to store
44
+ # @return The value
45
+ def push(value)
46
+ @biggest_value = value if @autoscale && value > @biggest_value
47
+ @values.unshift(value)
48
+
49
+ # only store as many values as we can display
50
+ if @values.size > width
51
+ @values.pop
52
+ end
53
+
54
+ render
55
+ value
56
+ end
57
+
58
+ def render
59
+ super
60
+ if @autoscale
61
+ factor = height.to_f / @biggest_value
62
+ end
63
+
64
+ @values.each_with_index do |value, index|
65
+ x = width - index
66
+ if !@autoscale
67
+ y = ((height / 100.0) * value).round
68
+ else
69
+ y = (factor*value).round
70
+ end
71
+
72
+ # draw the bar
73
+ height.downto(height - y) do |one_y|
74
+ draw(x, one_y)
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,11 @@
1
+ module Dominikh
2
+ module ColoredGraph
3
+ def color
4
+ red = ((255/100.0) * value)
5
+ green = 255 - ((255/100.0) * value)
6
+
7
+ color = "#%.2x%.2x00" % [red, green]
8
+ Subtlext::Color.new(color)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,2 @@
1
+ require File.absolute_path "#{__FILE__}/../../lib/CHANGEMELIBNAME"
2
+ require 'baretest/mocha'
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: subtle-graph
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dominik Honnef
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-04-15 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: dominikho@gmx.net
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/subtle
26
+ - lib/subtle/dominikh
27
+ - lib/subtle/dominikh/graph.rb
28
+ - lib/subtle/dominikh/graph
29
+ - lib/subtle/dominikh/graph/bar.rb
30
+ - lib/subtle/dominikh/graph/chart.rb
31
+ - lib/subtle/dominikh/graph/colored_graph.rb
32
+ - test/suite
33
+ - test/suite/lib
34
+ - test/fixtures
35
+ - test/setup.rb
36
+ - README
37
+ - LICENSE
38
+ has_rdoc: yard
39
+ homepage: ""
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 1.9.1
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.3.1
61
+ signing_key:
62
+ specification_version: 2
63
+ summary: Dynamic icons which represent different kinds of graphs.
64
+ test_files: []
65
+