Java JFreechart:将chartPanel的所有组件保存在png文件中
使用JFree我想做一个XY图,并在图外的右边空白处写一些信息。。。并将所有内容(即绘图+信息)保存在png文件中。我想我找到了一种通过以下代码实现的方法:Java JFreechart:将chartPanel的所有组件保存在png文件中,java,swing,save,jfreechart,chartpanel,Java,Swing,Save,Jfreechart,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.
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或BuffereImage),我会得到以下结果:
有没有办法将图表面板的所有组件都放在我的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组件的映像
执行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组件的映像 非常感谢。昨天晚上我花了几个小时试图找出如何使用BuffereImage来获得我想要的结果。你帮了我很多。非常感谢。昨天晚上我花了几个小时试图找出如何使用BuffereImage来获得我想要的结果。你帮了我很多。