Search code examples
ruby-on-railsrubylazy-high-charts

Exporting a chart as image using lazy-highcharts gem


I am able to generate the charts using lazy highcharts but i want those charts to exported as images , here is my code which I am using to generate the charts

@fields= ReportHistory.all_history
@h = LazyHighCharts::HighChart.new('graph') do |f|
f.chart(:renderTo => 'container', :zoomType => 'x',:spacingRight=> 20)
    f.title(:text => 'Reports')
    f.xAxis(:title=>{:text => 'Days'}, :categories =>@fields.map{|x|x.Date}.last(limit=15))
    f.yAxis(:title=>{:text=> 'Jobs_count', :type =>'integer' ,:max => 5000000})
    f.series(:name =>'jobs_count', :data=> @fields.map{|x| x.jobs_count.to_i }.last(limit=15))
    f.export(:type=> 'image/jpeg')
end

and in my view I to show the chart I have this

<%= high_chart("my_id", @h) %>

I want to have the effect like here where I can download the chart image using the export button.


Solution

  • First, you need to ensure that the export module is available and loaded by your view:

    <%= javascript_include_tag "vendor/highcharts/highcharts.js" %>
    <%= javascript_include_tag "vendor/highcharts/modules/exporting.js" %>
    

    Second... there's nothing to do. As soon as the exporting modul is loaded, it will add the menu to export your graph.

    Notes:

    • You may need to adapt the paths depending on your setup.
    • The exporting.js is part of the package you download from Highcharts.