Class: PhlexKit::Chart

Inherits:
BaseComponent show all
Defined in:
app/components/phlex_kit/chart/chart.rb

Overview

Chart, ported from ruby_ui's RubyUI::Chart — as a thin wrapper only. ruby_ui bundles chart.js; this kit deliberately does NOT (no charting dependency). It renders the plus the phlex-kit--chart Stimulus hook, which reads options: (a chart.js-shaped config hash) and then:

* if the host exposes chart.js as `window.Chart`, builds the chart with
ruby_ui's token-derived default colors (mapped to --pk-*), re-rendering
on theme change;
* otherwise dispatches "phlex-kit--chart:connect" with { canvas, options }
so the host can drive any charting library it ships.

render PhlexKit::Chart.new(options: { type: "line", data: { labels: [...],
datasets: [{ data: [...] }] } })

Instance Method Summary collapse

Methods inherited from BaseComponent

#on

Constructor Details

#initialize(options: {}, **attrs) ⇒ Chart

Returns a new instance of Chart.



16
17
18
19
# File 'app/components/phlex_kit/chart/chart.rb', line 16

def initialize(options: {}, **attrs)
  @options = options
  @attrs = attrs
end

Instance Method Details

#view_templateObject



21
22
23
24
25
26
27
28
29
# File 'app/components/phlex_kit/chart/chart.rb', line 21

def view_template(&)
  canvas(**mix({
    class: "pk-chart",
    data: {
      controller: "phlex-kit--chart",
      phlex_kit__chart_options_value: JSON.generate(@options)
    }
  }, @attrs), &)
end