javascript - rails : save Google chart on server side

标签 javascript ruby-on-rails google-visualization prawn

我在 View 页面中使用 JavaScript 来生成 Google 图表。我想将其保存到我的 app/asset/images/文件夹中,以便当用户单击“显示 PDF”按钮时,我可以在我的 prawnto PDF 中显示 Google 图表。

在我的 Rails View 页面中,如何将图表保存到图像文件夹?

这是我的查看页面代码:

  <!--Div that will hold the line chart-->
  <div id="line_chart" style="width:100%; height:100%;"></div>

  <!-- Display PDF -->
  <%= link_to "Display PDF", {:action => "index", :format => 'pdf'}, :target => '_blank' %>

  <!--Load the AJAX API-->
  <!--Load the Google JSAPI library-->
  <script type="text/javascript" src="https://www.google.com/jsapi?autoload={
            'modules':[{ 'name':'visualization', 'version':'1', 'packages':['corechart'] }]}"></script>

  <script type="text/javascript">//<!--Load the Google Visualization and chart libraries-->

    function showGraph() {
      // Load the Visualization API library and the piechart library.
      google.load('visualization', '1', {'packages':['corechart']});
      //Immediately after calling google.load(), your code should call google.setOnLoadCallback(my_handler), a Google JSAPI function that calls your handler as soon as all the libraries are loaded.
      //Your handler function should create and define the chart.
      google.setOnLoadCallback(drawChart);

      // My handler function is called drawChart
      // Callback that creates and populates a data table, 
      // instantiates the pie chart, passes in the data and
      // draws it.
      function drawChart() {

        //First, create a DataTable
        var dt = google.visualization.arrayToDataTable([
          ['Year', 'Number of people'],
          ['2005',   1000 ],
          ['2006',   1170 ],
          ['2007',   660  ],
          ['2008',   1030 ]
        ]);
        //Set chart options, inlcuding title and dimension
        var opt = {
          fontName: 'Arial',
          fontSize: 20,
        };

        //Instantiates a chart and specify which container does this chart will go to
        var ch = new google.visualization.LineChart(document.getElementById('line_chart'));  //Here we instantiate a chart as a PieChart that will go into the container whose id is named line_chart

        //Using the above chart instance, draw a chart according to the datatable and options we defined earlier
        ch.draw(dt, opt);
      }
    }
    window.onload = showGraph();
  </script>

谢谢。

最佳答案

这里有 Javascript 和示例,可以将 Google 图表显示为 PDF 以及许多其他内容。无需查看图像。

http://www.cloudformatter.com/GoogleCharts

关于javascript - rails : save Google chart on server side,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33090970/

相关文章:

javascript - 在客户端格式化字符串

javascript - 导出带有标题(包括图像)的 Excel

javascript - 最接近的 Jquery 顶级

javascript - Rails,将 Ruby 变量放入 Google 日历的 Javascript 中

ruby-on-rails - 为什么 postgreSQL 将小数字段存储为 BigDecimal 哈希?

ruby-on-rails - 在 Rails 中按字母顺序排序

google-visualization - 谷歌半 donut 饼图 : is it possible to set total percentage?

javascript - .css() 方法未被调用,因此 <div> 未更新

javascript - 谷歌图表 slantedText 和最小值不起作用

google-visualization - 谷歌图表栏内的额外矩形标签