Skip to content

Instantly share code, notes, and snippets.

@guiferrpereira
Last active December 23, 2015 12:19
Show Gist options
  • Save guiferrpereira/6634813 to your computer and use it in GitHub Desktop.
Save guiferrpereira/6634813 to your computer and use it in GitHub Desktop.
Highcharts generate image from chart
# To use this you will need:
# lazy_high_charts gem (I used version 1.4.3) - https://github.com/michelson/lazy_high_charts
# phantomJS installed on your machine - http://phantomjs.org/
# Responsible for convertion LazyHighChart object into image
# Receives LazyHighChart Object
def render_chart(high_chart, image_format="png", options = {})
options = options.select {|key, value| RENDER_OPTIONS.include?(key.to_s) }
begin
# First we will create file that contains json properties from our chart
infile = Tempfile.new('options')
infile.write(high_chart.full_options)
infile.rewind
# Create file where to store our image info
outfile = Tempfile.new(%W(chart .#{image_format}))
# Command that process image through phantomJS
system_call = "phantomjs highcharts-convert.js -infile #{infile.path} -outfile #{outfile.path}"
RENDER_OPTIONS.each do |key|
system_call += " -#{key} #{options[key.to_sym]}" unless options[key.to_sym].nil?
end
silence_stream(STDOUT) do
system system_call
end
outfile.close
# Moving temporary File to fixed location
FileUtils.mv(outfile.path, "/some_directory_where_you_want_your_file/")
# returning image path
return outfile.path.split("/").last
ensure
infile.close
infile.unlink
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment