Class: HeatLoss::ChartExporter
- Inherits:
-
BaseService
- Object
- BaseService
- HeatLoss::ChartExporter
- Defined in:
- app/services/heat_loss/chart_exporter.rb
Overview
Service to export Highcharts configurations to PNG images
Uses the Highcharts Export Server API to convert chart configs to images
Defined Under Namespace
Classes: Result
Constant Summary collapse
- EXPORT_URL =
URL for export.
'https://export.highcharts.com/'- REFERER =
Referer.
'https://www.warmlyyours.com/'- USER_AGENT =
User agent.
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36'
Class Method Summary collapse
-
.month_chart_config(heat_loss) ⇒ Hash
Build a column chart configuration for heat loss vs heat supply by month.
-
.pie_chart_config(heat_loss) ⇒ Hash
Build a pie chart configuration for heat loss by type.
-
.temp_chart_config(room, heat_loss) ⇒ Hash
Build a line chart configuration for room vs outside temperature.
Instance Method Summary collapse
-
#process(chart_config:, width: 400, height: 300, type: 'image/png') ⇒ Result
Export a chart configuration to PNG image data.
Class Method Details
.month_chart_config(heat_loss) ⇒ Hash
Build a column chart configuration for heat loss vs heat supply by month
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'app/services/heat_loss/chart_exporter.rb', line 92 def self.month_chart_config(heat_loss) months = %w[Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec] hl_averages = (heat_loss['hlc_averages'] || []).map { |o| o['heat_loss'].round } heat_supplied = (heat_loss['heat_supplied'] || []).map { |o| o['heat_supplied'].round } { chart: { type: 'column', width: 500, height: 300, backgroundColor: '#ffffff' }, title: { text: 'Heat Loss vs. Heat Supply' }, xAxis: { categories: months, crosshair: true }, yAxis: { title: { text: 'BTU/hour' } }, series: [ { name: 'Heat Loss', data: hl_averages, color: '#FF6B6B' }, { name: 'Heat Supplied', data: heat_supplied, color: '#4ECDC4' } ], credits: { enabled: false } } end |
.pie_chart_config(heat_loss) ⇒ Hash
Build a pie chart configuration for heat loss by type
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'app/services/heat_loss/chart_exporter.rb', line 66 def self.pie_chart_config(heat_loss) pie_data = (heat_loss['hlc_by_object'] || []).map do |o| { name: o['name'], y: o['heat_loss'].round, color: o['color'] } end { chart: { type: 'pie', width: 400, height: 300, backgroundColor: '#ffffff' }, title: { text: 'Heat Loss by Type' }, plotOptions: { pie: { allowPointSelect: true, cursor: 'pointer', dataLabels: { enabled: true, format: '<b>{point.name}</b>: {point.y} BTU/hr' } } }, series: [{ name: 'Heat Loss', colorByPoint: true, data: pie_data }], credits: { enabled: false } } end |
.temp_chart_config(room, heat_loss) ⇒ Hash
Build a line chart configuration for room vs outside temperature
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'app/services/heat_loss/chart_exporter.rb', line 114 def self.temp_chart_config(room, heat_loss) months = %w[Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec] desired_temps = (heat_loss['avg_temperatures'] || []).map { |_o| room.desired_temperature.round } ambient_temps = (heat_loss['ambient_temperatures'] || []).map { |o| o['temperature'].round } avg_temps = (heat_loss['avg_temperatures'] || []).map { |o| o['temperature'].round } { chart: { type: 'line', width: 500, height: 300, backgroundColor: '#ffffff' }, title: { text: 'Room vs. Outside Temperature' }, xAxis: { categories: months }, yAxis: { title: { text: 'Temperature (°F)' } }, series: [ { name: 'Desired Temperature', data: desired_temps, color: '#FF6B6B', dashStyle: 'Dash' }, { name: 'Average Room Temperature', data: ambient_temps, color: '#4ECDC4' }, { name: 'Outside Temperature', data: avg_temps, color: '#95A5A6' } ], credits: { enabled: false } } end |
Instance Method Details
#process(chart_config:, width: 400, height: 300, type: 'image/png') ⇒ Result
Export a chart configuration to PNG image data
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/services/heat_loss/chart_exporter.rb', line 29 def process(chart_config:, width: 400, height: 300, type: 'image/png') response = connection.post do |req| req.body = { 'options' => chart_config.to_json, 'type' => type, 'width' => width.to_s, 'height' => height.to_s } end if response.success? Result.new( success: true, image_data: response.body, content_type: response.headers['content-type'], error: nil ) else Result.new( success: false, image_data: nil, content_type: nil, error: "HTTP #{response.status}: #{response.body&.truncate(200)}" ) end rescue Faraday::Error => e Result.new( success: false, image_data: nil, content_type: nil, error: "#{e.class}: #{e.}" ) end |