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



117
118
119
120
121
122
123
124
125
126
# File 'app/helpers/rails_pulse/chart_helper.rb', line 117

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



63
64
65
66
67
68
69
70
71
72
73
# File 'app/helpers/rails_pulse/chart_helper.rb', line 63

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



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

def base_chart_options(units: nil, zoom: false)
  {
    tooltip: {
      trigger: "axis",
      axisPointer: { type: "shadow" },
      formatter: "tooltip_with_timestamp"
    },
    toolbox: {
      feature: { saveAsImage: { show: false } }
    },
    xAxis: {
      axisLine: { show: false },
      axisTick: { show: false },
      axisLabel: {
        formatter: "timestamp_to_date"
      }
    },
    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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/helpers/rails_pulse/chart_helper.rb', line 75

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

def render_stimulus_chart(data, type:, **options)
  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



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/helpers/rails_pulse/chart_helper.rb', line 90

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