Class: RailsuiCharts::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/railsui_charts/configuration.rb

Constant Summary collapse

SERIES_COUNT =

Categorical hues carry identity, so the order is the colourblind-safety mechanism rather than a style choice — it was picked by validating every ordering of these hues and keeping one that clears the lightness band, chroma floor, CVD separation, normal-vision floor, and 3:1 contrast in both light and dark. Reorder it and those guarantees go with it.

Forms where any two marks can sit side by side (pie, donut, polar area, scatter, bubble) hold to a stricter all-pairs test, which these hues clear for the first four slots. Past four, fold the tail into "Other".

8
SERIES_FALLBACKS =
%w[
  #6366f1 #ea580c #db2777 #c026d3
  #65a30d #0284c7 #dc2626 #0d9488
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/railsui_charts/configuration.rb', line 24

def initialize
  @default_height = 250
  @default_currency = "$"
  # Chart types registered by an extension, such as Rails UI Charts Pro.
  @extra_types = []
  @labelled_point_types = []
  @theme_css_prefix = "--rui-chart"
  @series_colors = (1..SERIES_COUNT).map { |i| "var(--rui-chart-series-#{i}, #{SERIES_FALLBACKS[i - 1]})" }
  @colors = {
    primary: "var(--rui-chart-primary, #4f46e5)",
    secondary: "var(--rui-chart-secondary, #0ea5e9)",
    accent: "var(--rui-chart-accent, #10b981)",
    muted: "var(--rui-chart-muted, #94a3b8)",
    grid: "var(--rui-chart-grid, rgba(148, 163, 184, 0.2))",
    text: "var(--rui-chart-text, #64748b)",
    # The card colour behind a chart. Touching marks separate with a gap in
    # this colour rather than a stroke drawn around them.
    surface: "var(--rui-chart-surface, #ffffff)"
  }

  # Type rides the same channel the colours do, because Apex takes these as
  # CSS strings and the controller resolves `var()` against the chart's own
  # computed style before handing the options over. That is worth more than
  # a Ruby setting here: a dense card can shrink the type on its own charts
  # without a second configuration, and a media query can do it per theme.
  #
  # The defaults are the sizes that were hardcoded before, so nothing moves
  # until someone sets a variable.
  @typography = {
    family: "var(--rui-chart-font-family, inherit)",
    size: "var(--rui-chart-font-size, 12px)",
    # Legend, annotation labels, and the compact breakpoint under 640px.
    size_sm: "var(--rui-chart-font-size-sm, 11px)"
  }

  # Numbers, and that is the whole reason they are here rather than in the
  # CSS. A resolved CSS variable arrives as a string, and Apex does
  # arithmetic on these — "4" + 1 is "41", so a radius set that way would
  # silently produce a chart with corners nobody asked for.
  @geometry = {
    bar_radius: 4,
    stroke_width: 2,
    marker_size: 0,
    marker_hover_size: 6
  }
end

Instance Attribute Details

#colorsObject

Returns the value of attribute colors.



5
6
7
# File 'lib/railsui_charts/configuration.rb', line 5

def colors
  @colors
end

#default_currencyObject

Returns the value of attribute default_currency.



5
6
7
# File 'lib/railsui_charts/configuration.rb', line 5

def default_currency
  @default_currency
end

#default_heightObject

Returns the value of attribute default_height.



5
6
7
# File 'lib/railsui_charts/configuration.rb', line 5

def default_height
  @default_height
end

#extra_typesObject

Returns the value of attribute extra_types.



5
6
7
# File 'lib/railsui_charts/configuration.rb', line 5

def extra_types
  @extra_types
end

#geometryObject

Returns the value of attribute geometry.



6
7
8
# File 'lib/railsui_charts/configuration.rb', line 6

def geometry
  @geometry
end

#labelled_point_typesObject

Returns the value of attribute labelled_point_types.



6
7
8
# File 'lib/railsui_charts/configuration.rb', line 6

def labelled_point_types
  @labelled_point_types
end

#series_colorsObject

Returns the value of attribute series_colors.



5
6
7
# File 'lib/railsui_charts/configuration.rb', line 5

def series_colors
  @series_colors
end

#theme_css_prefixObject

Returns the value of attribute theme_css_prefix.



5
6
7
# File 'lib/railsui_charts/configuration.rb', line 5

def theme_css_prefix
  @theme_css_prefix
end

#typographyObject

Returns the value of attribute typography.



6
7
8
# File 'lib/railsui_charts/configuration.rb', line 6

def typography
  @typography
end

Instance Method Details

#register_type(type, points: :values) ⇒ Object

Register a chart type this gem does not ship, so it passes validation and is drawn through the same options builder as everything else rather than around it.

RailsuiCharts.config.register_type :treemap, points: :labelled

points: :labelled keeps each point as y:. Most types put the label on the axis and send bare values, but a treemap draws its labels inside the rectangles, so they have to stay in the data.



79
80
81
82
83
84
85
86
# File 'lib/railsui_charts/configuration.rb', line 79

def register_type(type, points: :values)
  type = type.to_sym

  @extra_types |= [type]
  @labelled_point_types |= [type] if points == :labelled

  type
end