Class: Tiler::Widgets::BarChartQuery

Inherits:
Query::Base show all
Defined in:
lib/tiler/widgets/bar_chart.rb

Constant Summary collapse

DEFAULT_LIMIT =
10

Constants inherited from Query::Base

Query::Base::SAFE_COLUMN_RE

Instance Attribute Summary

Attributes inherited from Query::Base

#config, #panel, #source

Instance Method Summary collapse

Methods inherited from Query::Base

#initialize

Constructor Details

This class inherits a constructor from Tiler::Query::Base

Instance Method Details

#callObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/tiler/widgets/bar_chart.rb', line 9

def call
  group_col = config["group_column"]
  val_col   = config["value_column"]
  agg       = config["aggregation"] || "sum"
  limit     = (config["limit"] || DEFAULT_LIMIT).to_i.clamp(1, 50)

  return { labels: [], datasets: [], options: {} } if group_col.blank?

  groups = distinct_values(group_col).first(limit)
  data   = groups.map { |g| aggregate_for_group(group_col, g, val_col, agg) }
  # Per catalog: bars use the full viz palette in registration order.
  colors = groups.each_index.map { |i| chart_colors[i % chart_colors.size] }

  {
    labels: groups,
    datasets: [
      {
        label: (val_col || agg).to_s.humanize,
        data: data,
        backgroundColor: colors.map { |c| "#{c}cc" },
        borderColor: colors,
        borderWidth: 1
      }
    ],
    options: {}
  }
end