3

Here is the code for the current state of the tug-of-war between England and Scotland:

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);
}

Which leads us to this:

enter image description here

As you can see, the center of the plot's background has been set to white (correct), but the left and right margin are still light grey. How does one set the color of the outer parts of the background ?

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
steffen
  • 2,152
  • 4
  • 19
  • 30
  • Dumb question... what happens if you do `chart.setBackgroundPaint(Color.WHITE);` instead of `plot.` – durron597 Nov 19 '12 at 14:38
  • @durron597 `chart.setBackgroundPaint` controls the color of the background *around* the chart, meanwhile `plot.setBackgroundPaint` controls the color of the background *in* the chart. All of the background ? No, not all of it ... as it seems ;) – steffen Nov 19 '12 at 14:56
  • What about `setInsets`? Edit: i'm trying to help you just by looking at the javadoc, I can't easily test anything myself – durron597 Nov 19 '12 at 15:01

1 Answers1

5

The alternating background is a feature of SymbolAxis, which you can disable.

axis.setGridBandsVisible(false);

enter image description here

trashgod
  • 203,806
  • 29
  • 246
  • 1,045