如何更改JFreeChart的大小 [英] How do I change a JFreeChart's size
问题描述
我已将 JFreeChart
添加到 JPanel
(使用 BorderLayout
),它是巨大的。我可以做些什么来缩小它?
I've added a JFreeChart
to a JPanel
(using a BorderLayout
), and it's huge. Is there something I can do to make it smaller?
public void generateChart()
{
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
//set the values of the chart
for(int i=0; i<8; i++)
{
dataset.setValue(income_array[i], "Income",
Double.toString(percent_array[i]));
}
JFreeChart chart = ChartFactory.createBarChart(
"Required Annual Income for a Variety of Interest Rates",
"Percent", "Income", dataset, PlotOrientation.VERTICAL,
false,true, false);
ChartPanel cp = new ChartPanel(chart);
chart.setBackgroundPaint(Color.white);
chart.getTitle().setPaint(Color.black);
CategoryPlot p = chart.getCategoryPlot();
p.setRangeGridlinePaint(Color.blue);
//cp.setMaximumDrawHeight(5);
//cp.setMaximumDrawWidth(5);
//cp.setZoomOutFactor(.1);
JPanel graph = new JPanel();
graph.add(cp);
middle.add(graph, BorderLayout.CENTER);
}
推荐答案
当你创建 ChartPanel
,您有几个影响结果的选项:
When you create your ChartPanel
, you have several options that affect the result:
-
接受
DEFAULT_WIDTH
和DEFAULT_HEIGHT
:680 x 420。
Accept the
DEFAULT_WIDTH
andDEFAULT_HEIGHT
: 680 x 420.
指定首选 width
和构造函数中的 height
。
Specify the preferred width
and height
in the constructor.
调用<$如果适当,请明确c $ c> setPreferredSize()。
Invoke setPreferredSize()
explicitly if appropriate.
覆盖 getPreferredSize()
动态计算大小。
@Override
public Dimension getPreferredSize() {
// given some values of w & h
return new Dimension(w, h);
}
选择布局 ChartPanel
将成为的容器添加。请注意, JPanel
的默认布局是 FlowLayout
,而 JFrame的默认布局
是 BorderLayout
。举一个具体的例子, ThermometerDemo
在构造函数中使用两个首选值容器的 GridLayout
允许动态调整大小。
Choose the layout of the container to which the ChartPanel
will be added. Note that the default layout of JPanel
is FlowLayout
, while that of JFrame
is BorderLayout
. As a concrete example, ThermometerDemo
uses both preferred values in the constructor and a GridLayout
for the container to allow dynamic resizing.
这篇关于如何更改JFreeChart的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!