如何更改3dPieChart(JFreeChart API)的背景/透明度?

时间:2016-06-06 22:56:29

标签: colors background transparency jfreechart pie-chart

我宣布了一个3DPieChart,我想改变它的背景/透明度。在这种情况下,'背景'是指白色空间。

enter image description here

这是实现此图表的功能,如果它以某种方式有用。

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

注意:使用图表的图表,然后设置plot.setBackgroundPaint(Color.GRAY)没有任何好处。它只会改变轮廓内的背景。我希望这一切都是灰色的。我怎样才能做到这一点 ?

1 个答案:

答案 0 :(得分:1)

示例中图表周围的空白区域是图表的背景。您可以设置图像和/或半透明颜色,如下所示。其他灰色图像用50%透明黄色着色。

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

使用setBackgroundImageAlpha()可以实现略微不同的效果。

image

pieChart.setBackgroundPaint(Color.yellow);
pieChart.setBackgroundImageAlpha(0.5f);