【问题标题】:Save jzy3d Chart in PDF using DynamicReports使用 DynamicReports 将 jzy3d 图表保存为 PDF
【发布时间】:2016-07-25 14:35:25
【问题描述】:

我正在使用DynamicReports API 创建一个PDF 报告,显示我的Java 应用程序的结果(表格和图表)。 我必须显示的图表之一是使用jzy3d 0.9.0 构建的3D Surface Plot。

这是我的代码:

import static net.sf.dynamicreports.report.builder.DynamicReports.*;
import net.sf.dynamicreports.examples.Templates;
import net.sf.dynamicreports.examples.complex.invoice.InvoiceDesign;
import net.sf.dynamicreports.examples.complex.sales.SalesDesign;
import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import net.sf.dynamicreports.report.constant.HorizontalImageAlignment;
import net.sf.dynamicreports.report.constant.HorizontalTextAlignment;
import net.sf.dynamicreports.report.constant.PageType;
import net.sf.dynamicreports.report.constant.VerticalImageAlignment;
import net.sf.dynamicreports.report.constant.VerticalTextAlignment;
import net.sf.dynamicreports.report.builder.chart.PieChartBuilder;
import net.sf.dynamicreports.report.builder.component.ComponentBuilder;
import net.sf.dynamicreports.report.builder.component.ComponentBuilders;
import net.sf.dynamicreports.report.builder.component.TextFieldBuilder;
import net.sf.dynamicreports.report.builder.style.FontBuilder;
import net.sf.dynamicreports.report.builder.style.StyleBuilder;
import net.sf.dynamicreports.report.constant.PageOrientation;
import net.sf.dynamicreports.report.datasource.DRDataSource;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.dynamicreports.jasper.builder.export.Exporters;
import net.sf.dynamicreports.report.builder.column.TextColumnBuilder;
import net.sf.dynamicreports.report.builder.component.HorizontalListBuilder;
import javax.swing.JFrame;
import com.fairdynamics.components.FDPalette;
import com.fairdynamics.components.FDGlassPane;
import com.fairdynamics.components.FDOptionPane;
import fairdynamics.lyrica.UI_VisualAnalytics.UIRespondersHeatMap.HeatMapDetails;
import javax.swing.ImageIcon;
import org.jzy3d.maths.*;
import org.jzy3d.plot3d.builder.*;
import org.jzy3d.plot3d.builder.concrete.OrthonormalGrid;
import org.jzy3d.colors.ColorMapper;
import org.jzy3d.colors.colormaps.ColorMapRainbow;
import org.jzy3d.plot3d.primitives.Shape;
import org.jzy3d.chart.Chart;
import org.jzy3d.plot3d.rendering.canvas.Quality;
import org.jzy3d.chart.ChartLauncher;
import org.jzy3d.plot3d.builder.Mapper;

public class UI_ReportBuilder3 extends JasperReportBuilder{
    protected final FontBuilder font18 = stl.font("Roboto Light",false,false,18);
    protected final FontBuilder font16 = stl.font("Roboto Light",false,false,16);
    protected final FontBuilder font14 = stl.font("Roboto Light",false,false,14);
    protected final FontBuilder font12 = stl.font("Roboto Light",false,false,12);
    protected final FontBuilder font10 = stl.font("Roboto Light",false,false,10);
    protected final StyleBuilder titleStyle = stl.style(font12).setVerticalTextAlignment(VerticalTextAlignment.TOP).setForegroundColor(FDPalette.primaryColor);
    protected final StyleBuilder mainTitleStyle = stl.style(font18).setVerticalTextAlignment(VerticalTextAlignment.TOP).setForegroundColor(FDPalette.primaryColor);
    protected final StyleBuilder subTitleStyle = stl.style(font14).setVerticalTextAlignment(VerticalTextAlignment.TOP).setForegroundColor(FDPalette.primaryColor);
    protected final StyleBuilder footerStyle = stl.style(font10).setVerticalTextAlignment(VerticalTextAlignment.MIDDLE).setForegroundColor(FDPalette.secondaryText);
    protected final StyleBuilder colStyle = stl.style(font12).setVerticalTextAlignment(VerticalTextAlignment.MIDDLE);   

    public UI_ReportBuilder3() {        
        try{
            this
            .setTemplate(Templates.reportTemplate)
            .setPageFormat(PageType.A4,PageOrientation.PORTRAIT)
            .setPageMargin(margin(40))
            .pageFooter(
                    cmp.text("Footer").setStyle(footerStyle).setHorizontalTextAlignment(HorizontalTextAlignment.CENTER)
                )
            .title(
                    cmp.horizontalList().add(
                        cmp.text("TITLE").setStyle(mainTitleStyle).setHorizontalTextAlignment(HorizontalTextAlignment.LEFT)
                    )
                    .setFixedHeight(32)
                )
            .summary(
                    cmp.multiPageList(cmp.image(createHeatMap().screenshot())
                        )
                )
            .show()
            ;
        }catch(Exception e){
            e.printStackTrace();
        }
    }


    private org.jzy3d.chart.Chart createHeatMap(){          
        // Define a function to plot
        Mapper mapper = new Mapper() {
            public double f(double x, double y) {
                return 10 * Math.sin(x / 10) * Math.cos(y / 20) * x;
            }
        };

        // Define range and precision for the function to plot
        Range range = new Range(-150, 150);
        int steps = 50;

        // Create a surface drawing that function
        org.jzy3d.plot3d.primitives.Shape surface = Builder.buildOrthonormal(new OrthonormalGrid(range, steps, range, steps), mapper);
        surface.setColorMapper(new ColorMapper(new ColorMapRainbow(), surface.getBounds().getZmin(), surface.getBounds().getZmax(), new org.jzy3d.colors.Color(1, 1, 1, .5f)));
        surface.setFaceDisplayed(true);
        surface.setWireframeDisplayed(false);
        surface.setWireframeColor(org.jzy3d.colors.Color.BLACK);

        // Create a chart and add the surface
        org.jzy3d.chart.Chart chart = new org.jzy3d.chart.Chart(Quality.Advanced);
        chart.getScene().getGraph().add(surface);

        return chart;
    }

}

只有当我从createHeatMap() 调用方法ChartLauncher.openChart(chart)(随后打开一个显示图表的框架)时,此代码才有效 在返回chart 之前,否则createHeatMap().screenshot() 总是返回null。 但是,如果我需要创建和导出此图表的 PDF 而不在我的 Java 应用程序中显示图表本身,该怎么办?

注意:如果可能,我更喜欢使用jzy3d 0.9.0 而不是jzy3d 0.9.1,否则我应该修改很多已经编写和测试过的代码。

【问题讨论】:

  • 将其转换为 java.awt.Image 并照此传递?
  • 我应该将什么转换为图像?我的意思是,org.jzy3d.chart.Chart 类已经提供了一个screenshot() 方法,但是如果图表显示不正确,这个方法返回 null。问题是我不想显示图表。
  • 我没有 jzy3d 图表的经验,只在碧玉报告中。从 jasper 报告的角度来看,您需要传递 Image,这就是为什么我建议隔离 jasper-reports 的问题,只专注于创建一个返回图表的 java.awt.Image 的方法(屏幕截图,绘制到 g2,保存到文件???),最好的方法是使用 jzy3d 但我不知道。
  • 好的,谢谢您的提示。让我们忘记jzy3d,只考虑一个常见的java.awt.Component。 java.awt.Image 未显示时如何从中获取(首选大小 = 0 , 0)?
  • 这看起来也很老套,没有标准的方法将图像保存到文件中,谁将图像绘制到组件 grafics g2 上?我会尝试拦截这个或将另一个 Grafics g2 传递给库......但我又不知道库是如何工作的......

标签: java jasper-reports dynamic-reports jzy3d


【解决方案1】:

可以这样截屏:

Chart c = AWTChartComponentFactory.chart(Quality.Nicest,org.jzy3d.chart.factories.IChartComponentFactory.Toolkit.offscreen);
c.screenshot();
BufferedImage i = ((OffscreenCanvas)c.getCanvas()).getRenderer()).getLastScreenshotImage();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 2016-06-01
    相关资源
    最近更新 更多