lib/SVG/Graph/Graph.rb in svg-graph-2.0.2.beta vs lib/SVG/Graph/Graph.rb in svg-graph-2.0.2

- old
+ new

@@ -196,15 +196,11 @@ # # print graph.burn # def burn raise "No data available" unless @data.size > 0 - - # undocumented and not used in any sublass - # to be removed - #calculations if methods.include? 'calculations' - + start_svg calculate_graph_dimensions @foreground = Element.new( "g" ) draw_graph draw_titles @@ -379,11 +375,12 @@ # Add popups for the data points on some graphs, default is false. attr_accessor :add_popups # Customize popup radius attr_accessor :popup_radius # Number format values and Y axis representation like 1.2345667 represent as 1.23, - # defaults to '%.2f' + # Any valid format accepted by sprintf can be specified. + # If you don't want to change the format in any way you can use "%s". Defaults to "%.2f" attr_accessor :number_format protected @@ -400,13 +397,10 @@ self.send( key.to_s+"=", value ) if self.respond_to? key } @popup_radius ||= 10 end - # unknown why needed - # attr_accessor :top_align, :top_font, :right_align, :right_font - # size of the square box in the legend which indicates the colors KEY_BOX_SIZE = 12 # Override this (and call super) to change the margin to the left # of the plot area. Results in @border_left being set. @@ -429,11 +423,11 @@ if !rotate_y_labels max_width = get_longest_label(get_y_labels).to_s.length * y_label_font_size * 0.6 else max_width = y_label_font_size + 3 end - max_width += 10 if stagger_y_labels + max_width += 5 + y_label_font_size if stagger_y_labels return max_width end # Override this (and call super) to change the margin to the right @@ -499,12 +493,12 @@ "onmouseover" => "document.getElementById(#{t.object_id}).setAttribute('visibility', 'visible' )", "onmouseout" => "document.getElementById(#{t.object_id}).setAttribute('visibility', 'hidden' )", }) - end # add_popups - end + end # if add_popups + end # add_popup # returns the longest label from an array of labels as string # each object in the array must support .to_s def get_longest_label(arry) longest_label = arry.max{|a,b| @@ -541,11 +535,11 @@ if rotate_x_labels max_height = get_longest_label(get_x_labels).to_s.length * x_label_font_size * 0.6 else max_height = x_label_font_size + 3 end - max_height += 10 if stagger_x_labels + max_height += 5 + x_label_font_size if stagger_x_labels return max_height end # Draws the background, axis, and labels. @@ -596,13 +590,15 @@ if show_data_values textStr = value if( numeric?(value) ) textStr = @number_format % value end - # change anchor is label overlaps axis - if x < textStr.length/2 * font_size * 0.6 + # change anchor is label overlaps axis, normally anchor is middle (that's why we compute length/2) + if x < textStr.length/2 * font_size style << "text-anchor: start;" + elsif x > @graph_width - textStr.length/2 * font_size + style << "text-anchor: end;" end # white background for better readability @foreground.add_element( "text", { "x" => x.to_s, "y" => y.to_s, @@ -692,13 +688,13 @@ def get_y_labels end # space in px between x-labels def field_width - #(@graph_width.to_f - font_size*2*right_font) / - # (get_x_labels.length - right_align) - @graph_width.to_f / get_x_labels.length + # -1 is to use entire x-axis + # otherwise there is always 1 division unused + @graph_width.to_f / ( get_x_labels.length - 1 ) end # space in px between the y-labels def field_height #(@graph_height.to_f - font_size*2*top_font) / @@ -878,17 +874,10 @@ y_offset = @border_top + 20 when :bottom x_offset = @border_left + 20 y_offset = @border_top + @graph_height + 5 if show_x_labels - # max_x_label_height_px = (not rotate_x_labels) ? - # x_label_font_size : - # get_x_labels.max{|a,b| - # a.to_s.length<=>b.to_s.length - # }.to_s.length * x_label_font_size * 0.6 - # x_label_font_size y_offset += max_x_label_height_px - y_offset += max_x_label_height_px + 5 if stagger_x_labels end y_offset += x_title_font_size + 5 if show_x_title end group.attributes["transform"] = "translate(#{x_offset} #{y_offset})" end