Class: LesliView::Charts::General

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
lib/lesli_view/charts/general.rb

Direct Known Subclasses

Bar, Line

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, title: nil, subtitle: nil, labels: nil, dataset: nil, datasets: nil, database_to_dataset: nil, database_to_datasets: nil, height: "400px", compact: false) ⇒ General

Returns a new instance of General.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/lesli_view/charts/general.rb', line 47

def initialize(
    id:nil, 
    title:nil, 
    subtitle:nil, 
    labels:nil, 
    dataset:nil,
    datasets:nil, 
    database_to_dataset:nil,
    database_to_datasets:nil,
    height:"400px",
    compact:false
)
    @id = id || "lesli-chart-#{rand(1000)}" 
    @title = title
    @subtitle = subtitle
    @labels = labels
    @datasets = datasets
    @height = height
    @compact = compact

    @datasets = [{ name: title, data: dataset }] if dataset
    @datasets = database_to_dataset(database_to_dataset) if database_to_dataset
    @datasets = database_to_datasets(database_to_datasets) if database_to_datasets
end

Instance Attribute Details

#compactObject (readonly)

Returns the value of attribute compact.



37
38
39
# File 'lib/lesli_view/charts/general.rb', line 37

def compact
  @compact
end

#datasetsObject (readonly)

Returns the value of attribute datasets.



37
38
39
# File 'lib/lesli_view/charts/general.rb', line 37

def datasets
  @datasets
end

#heightObject (readonly)

Returns the value of attribute height.



37
38
39
# File 'lib/lesli_view/charts/general.rb', line 37

def height
  @height
end

#idObject (readonly)

Returns the value of attribute id.



37
38
39
# File 'lib/lesli_view/charts/general.rb', line 37

def id
  @id
end

#labelsObject (readonly)

Returns the value of attribute labels.



37
38
39
# File 'lib/lesli_view/charts/general.rb', line 37

def labels
  @labels
end

#subtitleObject (readonly)

Returns the value of attribute subtitle.



37
38
39
# File 'lib/lesli_view/charts/general.rb', line 37

def subtitle
  @subtitle
end

#titleObject (readonly)

Returns the value of attribute title.



37
38
39
# File 'lib/lesli_view/charts/general.rb', line 37

def title
  @title
end

Instance Method Details

#database_to_dataset(data) ⇒ Object



76
77
78
# File 'lib/lesli_view/charts/general.rb', line 76

def database_to_dataset(data)
    [{ data: data.map { |d| { x: d['xaxiskey'], y: d['yaxiskey'] }}}]
end

#database_to_datasets(data) ⇒ Object

Transforms a database query result into a structure compatible with Chart.js

Expected input:

An enumerable of records responding to:
  - :name  → series name (ex: browser, category, status)
  - :label → x-axis value (ex: date, category label)
  - :data  → y-axis value (numeric)

Example: 
    browsers = current_user.account.audit.account_devices
    .group(:agent_browser, :created_at)
    .select(
        'created_at as label',
        'agent_browser as name',
        'sum(agent_count) as data'
    )


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/lesli_view/charts/general.rb', line 97

def database_to_datasets(data)

    # Group records by series name
    # Example: group all "Chrome" rows together
    data
    .group_by { |records| records['dataname'] }
    .map do |name, records|
        {
            # Series name (used in legend and tooltips)
            label: name,

            # Convert each database row into an (x, y) point
            data: records.map do |record|
                {
                    # X-axis value (date, category, etc.)
                    x: record['xaxiskey'],

                    # Y-axis value (must be numeric)
                    y: record['yaxiskey']
                }
            end
        }
    end
end

#typeObject



72
73
74
# File 'lib/lesli_view/charts/general.rb', line 72

def type
    nil
end