Class: IronAdmin::Dashboards::ChartComponent

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/iron_admin/dashboards/chart_component.rb

Overview

Renders a chart on the dashboard.

Constant Summary collapse

TYPES =

Supported chart types.

Returns:

  • (Array<Symbol>)
%i[line bar pie doughnut].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title:, type: :line, data: [], labels: [], colors: nil, height: 300) ⇒ ChartComponent

Returns a new instance of ChartComponent.

Parameters:

  • title (String)

    Chart title

  • type (Symbol) (defaults to: :line)

    Chart type (default: :line)

  • data (Array) (defaults to: [])

    Chart data

  • labels (Array) (defaults to: [])

    Chart labels

  • colors (Array<String>, nil) (defaults to: nil)

    Per-chart color palette (overrides theme)

  • height (Integer) (defaults to: 300)

    Height (default: 300)



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

#dataArray (readonly)

Returns Chart data.

Returns:

  • (Array)

    Chart data



15
16
17
# File 'app/components/iron_admin/dashboards/chart_component.rb', line 15

def data
  @data
end

#heightInteger (readonly)

Returns Chart height in pixels.

Returns:

  • (Integer)

    Chart height in pixels



21
22
23
# File 'app/components/iron_admin/dashboards/chart_component.rb', line 21

def height
  @height
end

#labelsArray (readonly)

Returns Chart labels.

Returns:

  • (Array)

    Chart labels



18
19
20
# File 'app/components/iron_admin/dashboards/chart_component.rb', line 18

def labels
  @labels
end

#titleString (readonly)

Returns Chart title.

Returns:

  • (String)

    Chart title



9
10
11
# File 'app/components/iron_admin/dashboards/chart_component.rb', line 9

def title
  @title
end

#typeSymbol (readonly)

Returns Chart type (:line, :bar, :pie, :doughnut).

Returns:

  • (Symbol)

    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_colorsArray<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.

Returns:

  • (Array<String>)

    Resolved chart color palette



84
85
86
# File 'app/components/iron_admin/dashboards/chart_component.rb', line 84

def chart_colors
  resolved_colors
end

#chart_configString

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.

Returns:

  • (String)

    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_idString

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.

Returns:

  • (String)

    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

#themeIronAdmin::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.

Returns:



44
45
46
# File 'app/components/iron_admin/dashboards/chart_component.rb', line 44

def theme
  IronAdmin.configuration.theme
end