【TeeChart for Java教程】(九)导出和导入图表

【下载TeeChart for Java最新版本】

(一)导出图表

1.1 可用的导出格式

可以将所有格式复制到文件或剪贴板或Stream。

图像格式

  • JPEG:JPEGFormat类
  • BMP:BMPFormat类
  • PNG:PNGFormat类

数据格式

  • Text:TextFormat类
  • XML:XMLFormat类
  • HTML:HTMLFormat类
  • Excel:ExcelFormat类

其他格式

Tej格式是一种灵活的格式,可存储图表属性信息以及(可选)图表数据。文件很小(取决于数据),非常适合网络用于更新基于实时客户端的图表。

  • TEJ(TeeChart)TemplateExport类

1.2 示例导出

导出到文件是合理的,在大多数情况下,您只需要定义目标文件名,例:

try {
                tChart1.getExport().getImage().getBMP().save(fileName);
            } catch (IOException ex) {
                System.out.println(ex);
            }

1.3 JPEG

JPEG文件导出具有速度和质量的附加参数,例:

int tmpResult = fc.showSaveDialog(JpegExportDemo.this);
            if (tmpResult == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                String tmpName = file.getAbsolutePath();
                if (!ExportUtils.isExtension(file, ExportUtils.JPG)) {
                    tmpName = ExportUtils.replaceExtension(file, ExportUtils.JPG);
                }                
                myChart.getExport().getImage().getJPEG().save(tmpName);
            }     

1.4 BMP

代码示例:

 int tmpResult = fc.showSaveDialog(BmpExportDemo.this);
            if (tmpResult == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                String tmpName = file.getAbsolutePath();
                if (!ExportUtils.isExtension(file, ExportUtils.BMP)) {
                    tmpName = ExportUtils.replaceExtension(file, ExportUtils.BMP);
                }
                myChart.getExport().getImage().getBMP().save(tmpName);
            } 

1.5 PNG

PNG格式保留了GIF格式的许多优点,但也提供了超出GIF格式的功能。PNG改进了GIF逐步显示图像的能力; 也就是说,当图像通过网络连接到达时,显示更好和更好的图像近似值,例:

int tmpResult = fc.showSaveDialog(PngExportDemo.this);
            if (tmpResult == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                String tmpName = file.getAbsolutePath();
                if (!ExportUtils.isExtension(file, ExportUtils.PNG)) {
                    tmpName = ExportUtils.replaceExtension(file, ExportUtils.PNG);
                }
                myChart.getExport().getImage().getPNG().save(tmpName);
            } 

(二)导出数据

以下示例以Text或XML格式从Chart Series导出数据。它们可以创建并与图表系列相关联,从中可以将数据导出为文件,流或剪贴板。

2.1 文本文件

 try {
                    myChart.getExport().getData().getText().save(tmpName);
                }

2.2 XML文件

try {
                    myChart.getExport().getTemplate().toXML(tmpName);
                    showSavedFile(tmpName);
                }

(三)TeeChart的'Tej'模板和数据导出/导入格式

3.1 Tej文件

Tej文件是TeeChart自己的模板格式,用于保存图表及其数据,并在Java IDE中具有.tej扩展名。修改的图表属性随模板一起保存,并在模板导入新图表时重现,优点:

  • Tej文件的大小非常小,在大多数情况下,它比纯图形格式(Quicker)更具优势。
  • 模板的目标图表是“live”,可以进行缩放和滚动,并修改其属性。
  • 根据您的偏好,数据可以选择包含在tej模板中。
 try {
        	    tChart1.getExport().getTemplate().toFile("/temp/chart1.tej");       
        	} catch (IOException ex1) {
          	    ex1.printStackTrace();
        	}

(四)导入

4.1 从URL导入XML

显示如何从xml导出和加载的示例:

保存到xml

try {
                    myChart.getExport().getTemplate().toXML(tmpName);
                    showSavedFile(tmpName);
                } catch (FileNotFoundException e) {
                    System.out.println(e);
                }

从XML加载

try {                               
            	    myChart.setChart(myChart.getImport().getTemplate().fromXML(tmpName));
                    myChart.repaint();
                }
                catch (FileNotFoundException e) {
                    System.out.println(e);
                }

4.2 导入Tej格式文件

从本地文件源或http数据源导入已保存的Tej文件。

4.3 导入示例

文件导入

tChart1.getImport().getTemplate().fromFile("myFile.tej");

XML

tChart1.getImport().getTemplate().fromXML("myFile.xml");

Stream

try {
                //( 1) Save the template into a Stream...
                ByteArrayOutputStream m = new ByteArrayOutputStream();                 
                myChart.getExport().getTemplate().toStream(m);
                //( 2) Load the template into other Chart...
                copyChart.setChart(copyChart.getImport().getTemplate().fromStream(
                        new ByteArrayInputStream(m.toByteArray())
                ));
                //( 3) repaint the Chart
                copyChart.repaint();
            } catch (IOException ex) {
                System.out.println(ex);
            } catch (ClassNotFoundException ex) {
                System.out.println(ex);
            }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值