以下是英格兰和苏格兰之间拔河比赛当前状态的代码:
public TugOfWar(String s) {
super(s);
// data set
DefaultKeyedValues2DDataset dataset = new DefaultKeyedValues2DDataset();
dataset.addValue(0.2, "England", "");
dataset.addValue(0.8, "Scotland", "");
JFreeChart chart = ChartFactory.createStackedBarChart("tug-of-war",
"", "", dataset, PlotOrientation.HORIZONTAL, true, false, false);
CategoryPlot plot = chart.getCategoryPlot();
// customize axis
SymbolAxis axis = new SymbolAxis("", new String[]{
"England", "draw", "Scotland"});
axis.setRange(0, 2d);
plot.setRangeAxis(axis);
// customize renderer
BarRenderer renderer = new StackedBarRenderer();
renderer.setBase(0.8);
renderer.setMinimumBarLength(0);
renderer.setMaximumBarWidth(0.5);
renderer.setItemMargin(0.0);
renderer.setDrawBarOutline(true);
renderer.setShadowVisible(true);
renderer.setBarPainter(new StandardBarPainter());
plot.setRenderer(renderer);
// customize background
plot.setBackgroundPaint(Color.WHITE);
plot.setDomainGridlinePaint(Color.lightGray);
plot.setRangeGridlinePaint(Color.lightGray);
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);
ChartPanel chartpanel = new ChartPanel(chart);
chartpanel.setPreferredSize(new Dimension(600, 270));
setContentPane(chartpanel);
}
public static void main(String args[]) {
TugOfWar tugOfWar = new TugOfWar("Tug of war");
tugOfWar.pack();
RefineryUtilities.centerFrameOnScreen(tugOfWar);
tugOfWar.setVisible(true);
}
这导致我们这样做:
如您所见,绘图背景的中心已设置为白色(正确),但左右边距仍为浅灰色。如何设置背景外部的颜色?