我在 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 以及许多其他内容。无需查看图像。
关于javascript - rails : save Google chart on server side,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33090970/