Module: RailsPulse::ChartHelper

Included in:
ApplicationHelper
Defined in:
app/helpers/rails_pulse/chart_helper.rb

Instance Method Summary collapse

Instance Method Details

#area_chart_optionsObject



126
127
128
129
130
131
132
133
134
135
# File 'app/helpers/rails_pulse/chart_helper.rb', line 126

def area_chart_options
  base_chart_options.deep_merge({
    series: {
      smooth: true,
      lineStyle: { width: 3 },
      symbol: "roundRect",
      symbolSize: 8
    }
  })
end

#bar_chart_options(units: nil, zoom: false, chart_start: 0, chart_end: 100, zoom_start: nil, zoom_end: nil, chart_data: nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'app/helpers/rails_pulse/chart_helper.rb', line 72

def bar_chart_options(units: nil, zoom: false, chart_start: 0, chart_end: 100, zoom_start: nil, zoom_end: nil, chart_data: nil)
  options = base_chart_options(units: units, zoom: zoom).deep_merge({
    series: {
      itemStyle: { borderRadius: [ 5, 5, 5, 5 ] }
    }
  })

  apply_zoom_configuration(options, zoom, zoom_start, zoom_end, chart_data)

  options
end

#base_chart_options(units: nil, zoom: false) ⇒ Object

Base chart options shared across all chart types



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
70
# File 'app/helpers/rails_pulse/chart_helper.rb', line 36

def base_chart_options(units: nil, zoom: false)
  hourly = @period_type == "hour" || (@time_diff_hours && @time_diff_hours <= 25)
  x_formatter = hourly ? "time" : "timestamp_to_date"
  {
    tooltip: {
      trigger: "axis",
      axisPointer: { type: "shadow" },
      formatter: "tooltip_with_timestamp"
    },
    toolbox: {
      feature: { saveAsImage: { show: false } }
    },
    xAxis: {
      axisLine: { show: false },
      axisTick: { show: false },
      axisLabel: {
        formatter: x_formatter
      }
    },
    yAxis: {
      splitArea: { show: false },
      axisLabel: {
        formatter: "{value} #{units}"
      }
    },
    grid: {
      left: "0",
      right: "2%",
      bottom: (zoom ? "60" : "0"),
      top: "10%",
      containLabel: true
    },
    animation: true
  }
end

#line_chart_options(units: nil, zoom: false, chart_start: 0, chart_end: 100, zoom_start: nil, zoom_end: nil, chart_data: nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/helpers/rails_pulse/chart_helper.rb', line 84

def line_chart_options(units: nil, zoom: false, chart_start: 0, chart_end: 100, zoom_start: nil, zoom_end: nil, chart_data: nil)
  options = base_chart_options(units: units, zoom: zoom).deep_merge({
    series: {
      smooth: false,
      lineStyle: { width: 3 },
      symbol: "circle",
      symbolSize: 8
    }
  })

  apply_zoom_configuration(options, zoom, zoom_start, zoom_end, chart_data)

  options
end

#render_stimulus_chart(data, type:, **options) ⇒ Object

Main chart rendering method - unified API for all chart types Uses Stimulus controller to handle chart initialization



5
6
7
8
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
# File 'app/helpers/rails_pulse/chart_helper.rb', line 5

def render_stimulus_chart(data, type:, **options)
  # Auto-inject deployment markers into time-axis charts only.
  # Time-axis charts store timestamps inside series data as [timestamp_ms, value] pairs
  # and have no separate :labels key. Category/string-label charts (e.g. dashboard) are excluded.
  if data.is_a?(Hash) && data[:labels].nil? && data[:series].present? && @deployment_markers&.any?
    data = data.merge(deployment_markers: @deployment_markers)
  end

  chart_id = options[:id] || "rails-pulse-chart-#{SecureRandom.hex(8)}"
  height = options[:height] || "400px"
  width = options[:width] || "100%"
  theme = options[:theme] || "railspulse"
  chart_options = options[:options] || {}

  # Build data attributes for Stimulus
  stimulus_data = {
    controller: "rails-pulse--chart",
    rails_pulse__chart_type_value: type,
    rails_pulse__chart_data_value: data.to_json,
    rails_pulse__chart_options_value: chart_options.to_json,
    rails_pulse__chart_theme_value: theme
  }

  (:div, "",
    id: chart_id,
    style: "height: #{height}; width: #{width};",
    data: stimulus_data
  )
end

#sparkline_chart_optionsObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/helpers/rails_pulse/chart_helper.rb', line 99

def sparkline_chart_options
  # Compact sparkline columns that fill the canvas with no axes/labels/gaps
  base_chart_options.deep_merge({
    tooltip: {
      trigger: "axis",
      axisPointer: { type: "shadow" },
      formatter: "sparkline_tooltip"
    },
    series: {
      type: "bar",
      itemStyle: { borderRadius: [ 2, 2, 0, 0 ] },
      barCategoryGap: "10%",
      barGap: "0%"
    },
    yAxis: { show: false, splitLine: { show: false } },
    xAxis: {
      type: "category",
      boundaryGap: true,
      axisLine: { show: false },
      axisTick: { show: false },
      splitLine: { show: false },
      axisLabel: { show: false }
    },
    grid: { left: 0, right: 0, top: 0, bottom: 0, containLabel: false, show: false }
  })
end