Class: IronAdmin::Dashboards::ChartComponent
- Inherits:
-
ViewComponent::Base
- Object
- ViewComponent::Base
- IronAdmin::Dashboards::ChartComponent
- Defined in:
- app/components/iron_admin/dashboards/chart_component.rb
Overview
Renders a chart on the dashboard.
Constant Summary collapse
- TYPES =
Supported chart types.
%i[line bar pie doughnut].freeze
Instance Attribute Summary collapse
-
#data ⇒ Array
readonly
Chart data.
-
#height ⇒ Integer
readonly
Chart height in pixels.
-
#labels ⇒ Array
readonly
Chart labels.
-
#title ⇒ String
readonly
Chart title.
-
#type ⇒ Symbol
readonly
Chart type (:line, :bar, :pie, :doughnut).
Instance Method Summary collapse
-
#chart_colors ⇒ Array<String>
private
Resolves chart colors with priority: per-chart > theme > default.
-
#chart_config ⇒ String
private
Chart.js configuration JSON.
-
#chart_id ⇒ String
private
Unique chart element ID.
-
#initialize(title:, type: :line, data: [], labels: [], colors: nil, height: 300) ⇒ ChartComponent
constructor
A new instance of ChartComponent.
-
#theme ⇒ IronAdmin::Configuration::Theme
private
Theme configuration.
Constructor Details
#initialize(title:, type: :line, data: [], labels: [], colors: nil, height: 300) ⇒ ChartComponent
Returns a new instance of ChartComponent.
33 34 35 36 37 38 39 40 |
# File 'app/components/iron_admin/dashboards/chart_component.rb', line 33 def initialize(title:, type: :line, data: [], labels: [], colors: nil, height: 300) @title = title @type = type.to_sym @data = data @labels = labels @colors = colors @height = height end |
Instance Attribute Details
#data ⇒ Array (readonly)
Returns Chart data.
15 16 17 |
# File 'app/components/iron_admin/dashboards/chart_component.rb', line 15 def data @data end |
#height ⇒ Integer (readonly)
Returns Chart height in pixels.
21 22 23 |
# File 'app/components/iron_admin/dashboards/chart_component.rb', line 21 def height @height end |
#labels ⇒ Array (readonly)
Returns Chart labels.
18 19 20 |
# File 'app/components/iron_admin/dashboards/chart_component.rb', line 18 def labels @labels end |
#title ⇒ String (readonly)
Returns Chart title.
9 10 11 |
# File 'app/components/iron_admin/dashboards/chart_component.rb', line 9 def title @title end |
#type ⇒ Symbol (readonly)
Returns Chart type (:line, :bar, :pie, :doughnut).
12 13 14 |
# File 'app/components/iron_admin/dashboards/chart_component.rb', line 12 def type @type end |
Instance Method Details
#chart_colors ⇒ Array<String>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Resolves chart colors with priority: per-chart > theme > default.
84 85 86 |
# File 'app/components/iron_admin/dashboards/chart_component.rb', line 84 def chart_colors resolved_colors end |
#chart_config ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns Chart.js configuration JSON.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'app/components/iron_admin/dashboards/chart_component.rb', line 56 def chart_config colors = resolved_colors border = resolved_border_color { type: type, data: { labels: labels, datasets: [{ data: data, borderColor: border, backgroundColor: type == :line ? line_fill_color(border) : colors, fill: type == :line, tension: 0.3, }], }, options: { responsive: true, maintainAspectRatio: false, plugins: { legend: { display: %i[pie doughnut].include?(type) }, }, }, }.to_json end |
#chart_id ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns Unique chart element ID.
50 51 52 |
# File 'app/components/iron_admin/dashboards/chart_component.rb', line 50 def chart_id "chart-#{object_id}" end |
#theme ⇒ IronAdmin::Configuration::Theme
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns Theme configuration.
44 45 46 |
# File 'app/components/iron_admin/dashboards/chart_component.rb', line 44 def theme IronAdmin.configuration.theme end |