Class: PhantomGraph::Convert::Highchart

Inherits:
Base
  • Object
show all
Defined in:
lib/phantom_graph/convert/highchart.rb

Direct Known Subclasses

Stockchart

Constant Summary collapse

CH_OPTIONS =
{width:              600, 
scale:              1, 
constr:             "Chart",
filename:           nil, 
json_file_path:     "/tmp", 
image_file_path:    "/tmp", 
callback_file_path: "/tmp"}

Instance Method Summary collapse

Methods inherited from Base

attributes

Constructor Details

#initialize(js_json, opts = {}, callback_json = nil) ⇒ Highchart

Returns a new instance of Highchart.



13
14
15
16
17
18
# File 'lib/phantom_graph/convert/highchart.rb', line 13

def initialize( js_json, opts = {}, callback_json = nil )
  options.merge!(PhantomGraph.setting.default_options.merge(opts))
  prepare_tmp_files
  flush_callback(callback_json) if callback_json
  flush_json(js_json)
end

Instance Method Details

#build_cmdObject



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/phantom_graph/convert/highchart.rb', line 68

def build_cmd
  [:phantom_js_path, :json_file, :image_file].each do |key|
    check_error(key)
  end
  cmd  = "#{options[:phantom_js_path]} #{options[:highcharts_convert_path]} "
  cmd += "-infile #{json_file.path} -outfile #{image_file.path} "
  cmd += "-scale #{options[:scale]} "   if options[:scale]
  cmd += "-width #{options[:width]} "   if options[:width]
  cmd += "-width #{options[:height]} "  if options[:height]
  cmd += "-constr #{options[:constr]}"  if options[:constr]
  cmd += " -theme #{options[:highcharts_theme_path]}" if options[:highcharts_theme_path]
  cmd
end

#check_error(filekey) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/phantom_graph/convert/highchart.rb', line 82

def check_error(filekey)
  if filekey == :phantom_js_path
    raise PhantomGraph::Error::NoExecutable.new unless File.exists?( options[filekey].to_s )
  else
    raise PhantomGraph::Error::InvalidSource.new(filekey) unless send(filekey)
  end
end

#file_extObject



100
101
102
# File 'lib/phantom_graph/convert/highchart.rb', line 100

def file_ext
  options[:filename] ? options[:filename].match(/\.\w+$/i).to_s.downcase : ".png"
end

#flush_callback(callback_json) ⇒ Object



56
57
58
59
# File 'lib/phantom_graph/convert/highchart.rb', line 56

def flush_callback(callback_json)
  callback_file.write("function(chart) {\n#{callback_json}\n}")
  callback_file.flush
end

#flush_json(js_json) ⇒ Object



49
50
51
52
53
54
# File 'lib/phantom_graph/convert/highchart.rb', line 49

def flush_json(js_json)
  self.json = js_json
  json_file.write(js_json)
  json_file.flush
  process if options[:auto_process]
end

#imageObject



24
25
26
27
28
29
# File 'lib/phantom_graph/convert/highchart.rb', line 24

def image
  f = File.new([options[:image_file_path], random_filename].join("/"), "w+")
  f.write(self.image_file.read)
  f.close
  f
end

#log(msg) ⇒ Object



96
97
98
# File 'lib/phantom_graph/convert/highchart.rb', line 96

def log(msg)
  logger.debug(msg) if options[:logger]
end

#loggerObject



45
46
47
# File 'lib/phantom_graph/convert/highchart.rb', line 45

def logger
  @logger ||= ::Logger.new(STDOUT)
end

#optionsObject



20
21
22
# File 'lib/phantom_graph/convert/highchart.rb', line 20

def options
  @options ||= CH_OPTIONS
end

#prepare_tmp_filesObject



90
91
92
93
94
# File 'lib/phantom_graph/convert/highchart.rb', line 90

def prepare_tmp_files
  self.json_file     = Tempfile.new( ['json',     '.json'], options[:json_file_path] )
  self.callback_file = Tempfile.new( ['callback', '.json'], options[:callback_file_path] )
  self.image_file    = Tempfile.new( ['chart',    file_ext],  options[:image_file_path]  )
end

#processObject



40
41
42
43
# File 'lib/phantom_graph/convert/highchart.rb', line 40

def process
  log(to_s)
  log(`#{build_cmd}`)
end

#random_filenameObject



35
36
37
38
# File 'lib/phantom_graph/convert/highchart.rb', line 35

def random_filename
  return @options[:filename] if @options[:filename]
  "#{rand(100000)}-#{Time.now.to_i}.png"
end

#resultObject



31
32
33
# File 'lib/phantom_graph/convert/highchart.rb', line 31

def result
  image
end

#to_sObject



61
62
63
64
65
66
# File 'lib/phantom_graph/convert/highchart.rb', line 61

def to_s
  "<ImageHighchart path='#{options[:phantom_js_path]}' " + 
  "highcharts_convert_path='#{options[:highcharts_convert_path]}' " +
  "config_temp='#{json_file.path}' image_temp='#{image_file.path}' " + 
  "phantom_js_command='#{build_cmd}'\njson: #{json.inspect}\n>"
end