1

I have declared an 3DPieChart, and I want to change its background / transparency. In this case, by 'background' I mean that white space.

enter image description here

Here's the function that implements this chart, if it's helpful somehow.

    //customize panel for visualising the statistics
    public void customizeStatisticsPanelWhenButtoninIsPressed()
    {   
    transactionsCenterFinalPanel.setLayout(new BorderLayout());
    //center side of then main panel
    JPanel statisticsPanelCenter = new JPanel();
    statisticsPanelCenter.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

    //create a dataset    
    DefaultPieDataset pieDataSet = new DefaultPieDataset();

    //add values to dataset
    pieDataSet.setValue("Deposit:", depositCount);
    pieDataSet.setValue("Withdraw:", withdrawCount);
    pieDataSet.setValue("Transfer:", transferCount);

    //create a 3D chart with the given title that appears above the chart
    JFreeChart chart = ChartFactory.createPieChart3D(" ", pieDataSet, true, false, true);
    final ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new Dimension(315, 195));
    chartPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

    //declare a pieplot so we can customize the chart
    PiePlot3D plot = (PiePlot3D) chart.getPlot();
    plot.setInsets(new RectangleInsets(-6.0, 5.0, 5.0, 5.0));
    plot.setDirection(Rotation.CLOCKWISE);
    plot.setIgnoreZeroValues(true);

    plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} {1} ({2})"));
    plot.setDepthFactor(0.1);
    plot.setCircular(true);
    plot.setDarkerSides(true);

    //create a timer for animation
    rotate3DPieChart = new Timer(150, new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            //set the start angle, this increases everytime timer is executed
            plot.setStartAngle(pieChartThreadIterator = pieChartThreadIterator + 0.6);               
        }
    });

    //south side
    JPanel statisticsPanelSouth = new JPanel();
    statisticsPanelSouth.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

    JButton transactionsCount = new JButton("Count");
    statisticsPanelSouth.add(transactionsCount);
    transactionsCenterFinalPanel.add(chartPanel, BorderLayout.CENTER);
    transactionsCenterFinalPanel.add(statisticsPanelSouth, BorderLayout.SOUTH);

    transactions.add(transactionsCenterFinalPanel, BorderLayout.CENTER);

    //start the timer (animation starts!)
    rotate3DPieChart.start();   

    revalidate();
}

Note: Using a plot for the chart, then set plot.setBackgroundPaint(Color.GRAY) doesn't do any good. It only changes the background inside outline. I want it all to be gray. How can I do that ?

miTzuliK
  • 93
  • 1
  • 1
  • 11

1 Answers1

2

The white space surrounding the plot in your example is the chart's background. You can set an image and/or translucent color as shown below. The otherwise gray image in tinted with a 50% transparent yellow.

image

JFreeChart pieChart = ChartFactory.createPieChart3D(…);
try {//https://www.pinterest.com/pin/79516749641165970/
    pieChart.setBackgroundImage(ImageIO.read(new URL(
        "https://s-media-cache-ak0.pinimg.com/736x/bb/bf/bf/bbbfbfd869f20db50d3dd4943790020f.jpg")));
} catch (IOException ex) {
    ex.printStackTrace(System.err);
}
pieChart.setBackgroundPaint(new Color(0x7fffff00, true));

A slightly different effect can be achieved using setBackgroundImageAlpha().

image

pieChart.setBackgroundPaint(Color.yellow);
pieChart.setBackgroundImageAlpha(0.5f);
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    It works perfectly fine. I'll use this instead. Thank you ! – miTzuliK Jun 06 '16 at 23:40
  • Another question, if I may: what function did you use for showing decimals of the number (84,67%) inside labels ? – miTzuliK Jun 07 '16 at 08:54
  • @miTzuliK: It looks like the stray tooltip, enabled by setting `tooltips` to `true` in the factory; here's the [original](http://stackoverflow.com/a/36889641/230513). – trashgod Jun 07 '16 at 09:10
  • Any tips about how I can change the size of the labels and of the text inside them ? – miTzuliK Jun 07 '16 at 12:55
  • `PiePlot` has methods that control the labels' appearance; the plot's `PieSectionLabelGenerator` controls the tooltip content. – trashgod Jun 07 '16 at 15:48