Class: PhantomGraph::Convert::Highchart
- Defined in:
- lib/phantom_graph/convert/highchart.rb
Direct Known Subclasses
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
- #build_cmd ⇒ Object
- #check_error(filekey) ⇒ Object
- #file_ext ⇒ Object
- #flush_callback(callback_json) ⇒ Object
- #flush_json(js_json) ⇒ Object
- #image ⇒ Object
-
#initialize(js_json, opts = {}, callback_json = nil) ⇒ Highchart
constructor
A new instance of Highchart.
- #log(msg) ⇒ Object
- #logger ⇒ Object
- #options ⇒ Object
- #prepare_tmp_files ⇒ Object
- #process ⇒ Object
- #random_filename ⇒ Object
- #result ⇒ Object
- #to_s ⇒ Object
Methods inherited from Base
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 ) .merge!(PhantomGraph.setting..merge(opts)) prepare_tmp_files flush_callback(callback_json) if callback_json flush_json(js_json) end |
Instance Method Details
#build_cmd ⇒ Object
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 = "#{[:phantom_js_path]} #{[:highcharts_convert_path]} " cmd += "-infile #{json_file.path} -outfile #{image_file.path} " cmd += "-scale #{[:scale]} " if [:scale] cmd += "-width #{[:width]} " if [:width] cmd += "-width #{[:height]} " if [:height] cmd += "-constr #{[:constr]}" if [:constr] cmd += " -theme #{[:highcharts_theme_path]}" if [: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?( [filekey].to_s ) else raise PhantomGraph::Error::InvalidSource.new(filekey) unless send(filekey) end end |
#file_ext ⇒ Object
100 101 102 |
# File 'lib/phantom_graph/convert/highchart.rb', line 100 def file_ext [:filename] ? [: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 [:auto_process] end |
#image ⇒ Object
24 25 26 27 28 29 |
# File 'lib/phantom_graph/convert/highchart.rb', line 24 def image f = File.new([[: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 [:logger] end |
#logger ⇒ Object
45 46 47 |
# File 'lib/phantom_graph/convert/highchart.rb', line 45 def logger @logger ||= ::Logger.new(STDOUT) end |
#options ⇒ Object
20 21 22 |
# File 'lib/phantom_graph/convert/highchart.rb', line 20 def @options ||= CH_OPTIONS end |
#prepare_tmp_files ⇒ Object
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'], [:json_file_path] ) self.callback_file = Tempfile.new( ['callback', '.json'], [:callback_file_path] ) self.image_file = Tempfile.new( ['chart', file_ext], [:image_file_path] ) end |
#process ⇒ Object
40 41 42 43 |
# File 'lib/phantom_graph/convert/highchart.rb', line 40 def process log(to_s) log(`#{build_cmd}`) end |
#random_filename ⇒ Object
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 |
#result ⇒ Object
31 32 33 |
# File 'lib/phantom_graph/convert/highchart.rb', line 31 def result image end |
#to_s ⇒ Object
61 62 63 64 65 66 |
# File 'lib/phantom_graph/convert/highchart.rb', line 61 def to_s "<ImageHighchart path='#{[:phantom_js_path]}' " + "highcharts_convert_path='#{[:highcharts_convert_path]}' " + "config_temp='#{json_file.path}' image_temp='#{image_file.path}' " + "phantom_js_command='#{build_cmd}'\njson: #{json.inspect}\n>" end |