繁体   English   中英

JFreechart:将图表面板的所有组件保存在 png 文件中

[英]JFreechart: save in png file ALL components of a chartPanel

使用 JFree 我想制作一个 XY 绘图,并在绘图外的右边距中写一些信息......并将所有这些信息(即绘图 + 信息)保存在一个 png 文件中。 我想我找到了一种使用以下代码来做到这一点的方法:

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.EmptyBorder;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisSpace;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;

public class example {

    public static void main(String[] args) {

        XYSeriesCollection dataset = new XYSeriesCollection();
        XYSeries series =new XYSeries("data");
        series.add(1, 1);
        series.add(2, 2);
        series.add(3, 3);
        series.add(4, 4);
        dataset.addSeries(series);

        // create the chart...
        final JFreeChart chart = ChartFactory.createXYLineChart(
                "Example plot",     
                "X AXIS",           
                "Y AXIS",           
                dataset,            
                PlotOrientation.VERTICAL,
                true,               
                true,               
                false               
                );


        int plotWidth = 500;
        int leftMargin = 70;
        int rightMargin = 400;

        final XYPlot plot = chart.getXYPlot();
        // change margin size
        AxisSpace axisSpace=new AxisSpace();
        axisSpace.setRight(rightMargin);
        axisSpace.setLeft(leftMargin);
        plot.setFixedRangeAxisSpace(axisSpace);     


        // add text outside the plot area
        Box b = new Box(BoxLayout.Y_AXIS);
        b.setBorder(new EmptyBorder(50, plotWidth+leftMargin, 100, 210));
        JLabel label = new JLabel();
        label.setText("I want to add some");
        b.add(label);
        label = new JLabel();
        label.setText("information about the plot");
        b.add(label);
        label = new JLabel();
        label.setText("here outside the graph");
        b.add(label);

        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500+400, 500));
        chartPanel.add(b);
        JFrame container = new ApplicationFrame("");
        container.setContentPane(chartPanel);
        container.pack();
        container.setVisible(true);


    }

}

当情节出现在其框架中时,我得到以下信息:

在此处输入图片说明

但是,当我保存它(执行 dosave、doprint、save as 或通过 bufferedimage)时,我得到以下信息:

在此处输入图片说明

有没有办法在我的 png 文件中包含图表面板的所有组件? 还有另一种方法可以将信息放在右边距并将完整结果保存在 png 文件中吗?

感谢您的关注。

执行 dosave,或 doprint,或另存为

我对 JFreeChart API 提供的方法一无所知,所以我不知道这些方法是如何工作的。

但是,在我看来,“chartPanel”似乎是添加到框架中的唯一组件。 因此,如果您创建“chartPanel”的图像,您应该获得添加到框架中的所有内容的图像。

创建任何组件图像的基本代码是:

Dimension d = component.getSize();
BufferedImage image = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
component.print( g2d );
g2d.dispose();
ImageIO.write(image, ".jpg", new File(...));

或者,您可以查看屏幕图像以获取允许您创建任何 Swing 组件图像的类。

暂无
暂无

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

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