Module: PredictabilityEngine::PdfVisualizer::Primitives

Defined in:
lib/predictability_engine/pdf_visualizer/primitives.rb

Class Method Summary collapse

Class Method Details

.chart_heightObject



7
# File 'lib/predictability_engine/pdf_visualizer/primitives.rb', line 7

def self.chart_height = 100

.chart_widthObject



6
# File 'lib/predictability_engine/pdf_visualizer/primitives.rb', line 6

def self.chart_width = 250

.draw_bar_chart(pdf, labels, values) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/predictability_engine/pdf_visualizer/primitives.rb', line 41

def self.draw_bar_chart(pdf, labels, values)
  max_y = values.max || 1
  bar_width = labels.empty? ? chart_width : chart_width / labels.size.to_f

  draw_canvas(pdf) do
    values.each_with_index do |v, i|
      h = (v.to_f / max_y) * chart_height
      pdf.fill_color '3366CC'
      pdf.fill_rectangle [i * bar_width, h], [bar_width - 2, 1].max, h
    end
  end
end

.draw_canvas(pdf) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/predictability_engine/pdf_visualizer/primitives.rb', line 72

def self.draw_canvas(pdf)
  pdf.bounding_box([0, pdf.cursor], width: chart_width, height: chart_height) do
    pdf.stroke_bounds
    yield
    pdf.stroke_color '000000'
    pdf.fill_color '000000'
  end
  pdf.move_down 5
end

.draw_legend(pdf, series) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/predictability_engine/pdf_visualizer/primitives.rb', line 30

def self.draw_legend(pdf, series)
  # Use smaller legend for dashboard
  series.each do |s|
    pdf.fill_color s[:color]
    pdf.fill_rectangle [0, pdf.cursor], 8, 8
    pdf.fill_color '000000'
    pdf.draw_text s[:label], at: [12, pdf.cursor - 6], size: 6
    pdf.move_down 10
  end
end

.draw_line_chart(pdf, _labels, series) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/predictability_engine/pdf_visualizer/primitives.rb', line 9

def self.draw_line_chart(pdf, _labels, series)
  max_y = series.map { |s| s[:values].max }.max || 1

  draw_canvas(pdf) do
    series.each { |s| draw_series(pdf, s, series.first[:values].size, max_y) }
  end
  draw_legend(pdf, series)
end

.draw_scatter_plot(pdf, labels, values) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/predictability_engine/pdf_visualizer/primitives.rb', line 54

def self.draw_scatter_plot(pdf, labels, values)
  max_y = values.max || 1

  draw_canvas(pdf) do
    values.each_with_index do |v, i|
      pdf.fill_circle [x_coord(i, labels.size), y_coord(v, max_y)], 1.5
    end
  end
end

.draw_series(pdf, series_data, labels_count, max_y) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/predictability_engine/pdf_visualizer/primitives.rb', line 18

def self.draw_series(pdf, series_data, labels_count, max_y)
  pdf.stroke_color series_data[:color]
  points = series_data[:values].each_with_index.map do |v, i|
    [x_coord(i, labels_count), y_coord(v, max_y)]
  end

  pdf.stroke do
    pdf.move_to(*points[0])
    points[1..].each { |p| pdf.line_to(*p) }
  end
end

.x_coord(index, total) ⇒ Object



64
65
66
# File 'lib/predictability_engine/pdf_visualizer/primitives.rb', line 64

def self.x_coord(index, total)
  total <= 1 ? 0 : (index.to_f / (total - 1)) * chart_width
end

.y_coord(value, max_y) ⇒ Object



68
69
70
# File 'lib/predictability_engine/pdf_visualizer/primitives.rb', line 68

def self.y_coord(value, max_y)
  (value.to_f / max_y) * chart_height
end