简体   繁体   English

如何保存谷歌时间线图表

[英]How to save google Timeline chart

I have created linecharts & timeline charts using google-chart api. 我已经使用google-chart api创建了线图和时间表图表。 I need to save them in .jpg format. 我需要将它们保存为.jpg格式。 I am able to save linecharts using chart.getImageURI() & Base64.decodeBase64 functions . 我可以使用chart.getImageURI()和Base64.decodeBase64函数保存线图。 But I am unable to save timeline chart as getImageURI() function is not defined for timeline charts . 但是我无法保存时间表,因为未为时间表设置getImageURI()函数。

I have even tried using BufferedImage & ImageIO.read as follows : 我什至尝试使用BufferedImage&ImageIO.read如下:

BufferedImage timeLineImage;
URL url = new URL(TimeLineURL);
timeLineImage = ImageIO.read(url);
ImageIO.write(timeLineImage, "jpg",new File("C:\\out.jpg"))

But I am getting Exception saying timeLineImage is null. 但是我收到异常消息说timeLineImage为null。

Any help to save timeline charts would be really appreciated. 保存时间表的任何帮助将不胜感激。

Thanks. 谢谢。

We have a site where there are samples which use Google charts for most all chart types and an external server to render to PDF and image types. 我们有一个站点,其中有一些示例,这些示例对大多数所有图表类型都使用Google图表,并使用外部服务器将其呈现为PDF和图像类型。 You can control the image format and DPI if you like. 您可以根据需要控制图像格式和DPI。

The one key thing is that the Google api does not add the SVG namespace into a generated SVG so there is a callback created that adds this namespace. 关键一件事是Google api不会将SVG名称空间添加到生成的SVG中,因此创建了一个添加此名称空间的回调。 You have to do this or the remote formatting service will not process the SVG. 您必须执行此操作,否则远程格式化服务将不会处理SVG。

http://www.cloudformatter.com/GoogleCharts http://www.cloudformatter.com/GoogleCharts

Line Chart: http://www.cloudformatter.com/GoogleCharts.GoogleChartSamples.GoogleLineCharts or on fiddle: http://jsfiddle.net/22bep76q/ 折线图: http : //www.cloudformatter.com/GoogleCharts.GoogleChartSamples.GoogleLineCharts或小提琴: http : //jsfiddle.net/22bep76q/

Timeline Chart: http://www.cloudformatter.com/GoogleCharts.GoogleChartSamples.GoogleTimelineCharts or on fiddle: http://jsfiddle.net/q2h8quho/ 时间线图表: http : //www.cloudformatter.com/GoogleCharts.GoogleChartSamples.GoogleTimelineCharts或小提琴: http : //jsfiddle.net/q2h8quho/

Sample code for Timeline: 时间轴的示例代码:

<!-- This adds the proper namespace on the generated SVG -->
function AddNamespace(){
var svg = jQuery('#chart_div svg');
svg.attr("xmlns", "http://www.w3.org/2000/svg");
svg.css('overflow','visible');
}
<!-- This generates the google chart -->
google.setOnLoadCallback(drawChart);
             function drawChart() {
             var container = document.getElementById('chart_div');
             var chart = new google.visualization.Timeline(container);
             var dataTable = new google.visualization.DataTable();

             dataTable.addColumn({ type: 'string', id: 'President' });
             dataTable.addColumn({ type: 'date', id: 'Start' });
             dataTable.addColumn({ type: 'date', id: 'End' });
             dataTable.addRows([
             [ 'Washington', new Date(1789, 3, 30), new Date(1797, 2, 4) ],
             [ 'Adams',      new Date(1797, 2, 4),  new Date(1801, 2, 4) ],
             [ 'Jefferson',  new Date(1801, 2, 4),  new Date(1809, 2, 4) ],
             [ 'Madison', new Date(1809, 2, 4), new Date(1817, 2, 4) ],
             [ 'Monroe',      new Date(1817, 2, 4),  new Date(1825, 2, 4) ],
             [ 'Quincy Adams', new Date(1825, 3, 30), new Date(1829, 2, 4) ],
             [ 'Jackson',      new Date(1829, 2, 4),  new Date(1837, 2, 4) ],
             [ 'Van Buren', new Date(1837, 2, 4), new Date(1841, 2, 4) ],
             [ 'Harrison',      new Date(1841, 2, 4),  new Date(1841, 3, 4) ],
             [ 'Tyler', new Date(1841, 3, 4), new Date(1845, 2, 4) ]]);

             google.visualization.events.addListener(chart, 'ready', AddNamespace);
             chart.draw(dataTable);
             }
 <!-- @cloudformatter calls to render the SVG -->

 <!-- Convert the SVG to PDF and download it -->
 var click="return xepOnline.Formatter.Format('JSFiddle', {render:'download', srctype:'svg'})";
 jQuery('#buttons').append('<button onclick="'+ click +'">PDF</button>');
 <!-- Convert the SVG to PNG@120dpi and open it -->
 click="return xepOnline.Formatter.Format('JSFiddle', {render:'newwin', mimeType:'image/png', resolution:'120', srctype:'svg'})";
 jQuery('#buttons').append('<button onclick="'+ click +'">PNG @120dpi</button>');
 <!-- Convert the SVG to JPG@300dpi and open it -->
 click="return xepOnline.Formatter.Format('JSFiddle', {render:'newwin', mimeType:'image/jpg', resolution:'300', srctype:'svg'})";
 jQuery('#buttons').append('<button onclick="'+ click +'">JPG @300dpi</button>');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM