Class: Trackplot::PhlexComponent

Inherits:
Phlex::HTML
  • Object
show all
Defined in:
lib/trackplot/phlex_component.rb

Instance Method Summary collapse

Constructor Details

#initialize(data:, **options, &block) ⇒ PhlexComponent

Returns a new instance of PhlexComponent.



10
11
12
13
14
# File 'lib/trackplot/phlex_component.rb', line 10

def initialize(data:, **options, &block)
  @data = data
  @options = options
  @block = block
end

Instance Method Details

#view_templateObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/trackplot/phlex_component.rb', line 16

def view_template
  builder = ChartBuilder.new(@data, **@options)
  @block&.call(builder)
  config = builder.send(:build_config)

  chart_id = @options[:id] || "trackplot-#{SecureRandom.hex(8)}"
  width = @options[:width] || "100%"
  height = @options[:height] || "400px"

  tag(
    "trackplot-chart",
    id: chart_id,
    config: config.to_json,
    style: "display:block;width:#{width};height:#{height};",
    class: ["trackplot-chart", @options[:class]].compact.join(" "),
    role: @options[:title] ? "img" : nil,
    **aria_attributes
  )
end