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



112
113
114
115
116
117
118
119
120
121
# File 'app/helpers/rails_pulse/chart_helper.rb', line 112

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, xaxis_formatter: nil, tooltip_formatter: nil, zoom_start: nil, zoom_end: nil, chart_data: nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/helpers/rails_pulse/chart_helper.rb', line 59

def bar_chart_options(units: nil, zoom: false, chart_start: 0, chart_end: 100, xaxis_formatter: nil, tooltip_formatter: nil, 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_tooltip_formatter(options, tooltip_formatter)
  apply_xaxis_formatter(options, xaxis_formatter)
  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
# File 'app/helpers/rails_pulse/chart_helper.rb', line 29

def base_chart_options(units: nil, zoom: false)
  {
    tooltip: {
      trigger: "axis",
      axisPointer: { type: "shadow" }
    },
    toolbox: {
      feature: { saveAsImage: { show: false } }
    },
    xAxis: {
      axisLine: { show: false },
      axisTick: { show: false }
    },
    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, xaxis_formatter: nil, tooltip_formatter: nil, zoom_start: nil, zoom_end: nil, chart_data: nil) ⇒ Object



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

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

  apply_tooltip_formatter(options, tooltip_formatter)
  apply_xaxis_formatter(options, xaxis_formatter)
  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
# 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({
    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