JFreechart:将 chartPanel 的所有组件保存在 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、另存为或通过 bufferedimage)我得到以下信息:
有没有办法在我的 png 文件中包含图表面板的所有组件?有没有另一种方法可以将信息放在右边距并将完整结果保存在 png 文件中?
感谢您的关注。
performing a dosave, or a doprint, or a save as
我对 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(...));
或者,您可以查看 Screen Image 以获得 class,它允许您创建任何 Swing 组件的图像。
使用 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、另存为或通过 bufferedimage)我得到以下信息:
有没有办法在我的 png 文件中包含图表面板的所有组件?有没有另一种方法可以将信息放在右边距并将完整结果保存在 png 文件中?
感谢您的关注。
performing a dosave, or a doprint, or a save as
我对 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(...));
或者,您可以查看 Screen Image 以获得 class,它允许您创建任何 Swing 组件的图像。