Class: GovukPublishingComponents::Presenters::ChartHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/govuk_publishing_components/presenters/chart_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ChartHelper

Returns a new instance of ChartHelper.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/govuk_publishing_components/presenters/chart_helper.rb', line 4

def initialize(options)
  @rows = options[:rows]
  @keys = options[:keys]
  @minimal = options[:minimal]
  @enable_interactivity = false
  @enable_interactivity = true unless @minimal
  @hide_legend = options[:hide_legend]
  @hide_legend = true if @minimal
  @point_size = options[:point_size] ||= 10
  @point_size = 0 if @minimal
  @height = options[:height] || 400
  @h_axis_title = options[:h_axis_title]
  @v_axis_title = options[:v_axis_title]
  @text_position = "none" if @minimal
  @y_axis_view_window_min = 0
  @y_axis_view_window_min = "auto" if options[:y_axis_auto_adjust]
  @line_colours = options[:line_colours]
  @line_styles = options[:line_styles]

  @h_axis_format = "YYYY-MM-dd" if options[:h_axis_format] == "date"
  @h_axis_format = "£#" if options[:h_axis_format] == "currency"
  @h_axis_format = "#'\%'" if options[:h_axis_format] == "percent"

  @v_axis_format = "YYYY-MM-dd" if options[:v_axis_format] == "date"
  @v_axis_format = "£#" if options[:v_axis_format] == "currency"
  @v_axis_format = "#'\%'" if options[:v_axis_format] == "percent"
end

Instance Method Details

#chart_format_dataObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/govuk_publishing_components/presenters/chart_helper.rb', line 64

def chart_format_data
  if !@rows.empty? && !@keys.empty?
    @rows.map do |row|
      {
        name: row[:label],
        data: @keys.zip(row[:values]),
      }
    end
  end
end

#chart_optionsObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/govuk_publishing_components/presenters/chart_helper.rb', line 33

def chart_options
  {
    chartArea: { width: "80%", height: "60%" },
    crosshair: { orientation: "vertical", trigger: "both", color: "#ccc" },
    curveType: "none",
    enableInteractivity: @enable_interactivity,
    legend: legend_options,
    pointSize: @point_size,
    height: @height,
    tooltip: { isHtml: true },
    series: series_options,
    hAxis: {
      textStyle: set_font_16,
      title: @h_axis_title,
      titleTextStyle: set_font_19,
      textPosition: @text_position,
      format: @h_axis_format,
    },
    vAxis: {
      textStyle: set_font_16,
      title: @v_axis_title,
      titleTextStyle: set_font_19,
      textPosition: @text_position,
      format: @v_axis_format,
      viewWindow: {
        min: @y_axis_view_window_min,
      },
    },
  }
end