lib/squid/plotter.rb in squid-1.0.0.beta2 vs lib/squid/plotter.rb in squid-1.0.0.beta3

- old
+ new

@@ -20,22 +20,29 @@ end end # Draws the graph legend with the given labels. # @param [Array<LegendItem>] The labels to write as part of the legend. - def legend(labels, height:, offset: 0, colors: []) + def legend(labels, height:, right: 0, colors: []) left = @pdf.bounds.width/2 box(x: left, y: @pdf.bounds.top, w: left, h: height) do - x = @pdf.bounds.right - offset + x = @pdf.bounds.right - right options = {size: 7, height: @pdf.bounds.height, valign: :center} labels.each.with_index do |label, i| - color = Array.wrap(colors[labels.size - 1 - i]).first + index = labels.size - 1 - i + series_color = colors.fetch index, series_colors(index) + color = Array.wrap(series_color).first x = legend_item label, x, color, options end end end + def series_colors(index) + default_colors = %w(2e578c 5d9648 e7a13d bc2d30 6f3d79 7d807f) + default_colors.fetch(index % default_colors.size) + end + # Draws a horizontal line. def horizontal_line(y, options = {}) with options do at = y + @bottom @pdf.stroke_horizontal_line left, @pdf.bounds.right - right, at: at @@ -64,38 +71,39 @@ @pdf.text_box label, options if (index % every).zero? @pdf.stroke_vertical_line @bottom, @bottom - 2, at: x + w/2 if ticks end end - def points(series, colors: []) - items(series, colors: colors) do |point, w, i, padding| + def points(series, options = {}) + items(series, options) do |point, w, i, padding| x, y = (point.index + 0.5)*w + left, point.y + @bottom @pdf.fill_circle [x, y], 5 end end - def lines(series, colors: [], line_widths: []) + def lines(series, options = {}) x, y = nil, nil - items(series, colors: colors) do |point, w, i, padding| + line_widths = options.delete(:line_widths) { [] } + items(series, options) do |point, w, i, padding| prev_x, prev_y = x, y x, y = (point.index + 0.5)*w + left, point.y + @bottom - line_width = Array.wrap(line_widths).fetch(i, 1) + line_width = line_widths.fetch i, 3 with line_width: line_width, cap_style: :round do @pdf.line [prev_x, prev_y], [x,y] unless point.index.zero? || prev_y.nil? || prev_x > x end end end - def stacks(series, colors: []) - items(series, colors: colors, fill: true) do |point, w, i, padding| + def stacks(series, options = {}) + items(series, options.merge(fill: true)) do |point, w, i, padding| x, y = point.index*w + padding + left, point.y + @bottom @pdf.fill_rectangle [x, y], w - 2*padding, point.height end end - def columns(series, colors: []) - items(series, colors: colors, fill: true, count: series.size) do |point, w, i, padding| + def columns(series, options = {}) + items(series, options.merge(fill: true, count: series.size)) do |point, w, i, padding| item_w = (w - 2 * padding)/ series.size x, y = point.index*w + padding + left + i*item_w, point.y + @bottom @pdf.fill_rectangle [x, y], item_w, point.height end end @@ -125,16 +133,18 @@ options[:valign] = :center options[:overflow] = :shrink_to_fit options end - def items(series, colors: [], fill: false, count: 1, &block) + def items(series, colors: [], fill: false, count: 1, starting_at: 0, &block) series.reverse_each.with_index do |points, reverse_index| index = series.size - reverse_index - 1 + color_index = index + starting_at w = width / points.size.to_f - series_colors = Array.wrap(colors[index]).cycle + series_color = colors.fetch color_index, series_colors(color_index) + item_color = Array.wrap(series_color).cycle points.select(&:y).each do |point| - item point, series_colors.next, w, fill, index, count, &block + item point, item_color.next, w, fill, index, count, &block end end end def item(point, color, w, fill, index, count)