JFreechart filling sectors series

后端 未结 1 1033
渐次进展
渐次进展 2021-01-26 20:15

I am trying to create a filled series over each region of a polar plot. But the colors are not filled out right. Here is an image of what I get: http://i1122.photobucket.com/alb

相关标签:
1条回答
  • 2021-01-26 21:06

    Here's a simplified version the uses PolarPlot directly, without any transformation. It might be easier to experiment with.

    Polar arcs picture

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import javax.swing.JFrame;
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.axis.NumberAxis;
    import org.jfree.chart.plot.PolarPlot;
    import org.jfree.chart.renderer.DefaultPolarItemRenderer;
    import org.jfree.data.xy.XYDataset;
    import org.jfree.data.xy.XYSeries;
    import org.jfree.data.xy.XYSeriesCollection;
    
    /** @see http://stackoverflow.com/questions/6669734 */
    public class PolarArcs {
    
        private static final String title = "PolarArcs";
        private static final double PI2 = 90d; // π/2 radians = 90°
    
        private void display() {
            JFrame f = new JFrame(title);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            ChartPanel panel = new ChartPanel(createChart(createDataset()));
            panel.setPreferredSize(new Dimension(400, 400));
            f.add(panel);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    
        private JFreeChart createChart(XYDataset dataset) {
            JFreeChart chart = ChartFactory.createPolarChart(
                title, dataset, true, false, false);
            PolarPlot plot = (PolarPlot) chart.getPlot();
            plot.setBackgroundPaint(Color.white);
            plot.setAngleGridlinesVisible(false);
            plot.setRadiusGridlinesVisible(false);
            DefaultPolarItemRenderer r = (DefaultPolarItemRenderer) plot.getRenderer();
            for (int i = 0; i < dataset.getSeriesCount(); i++ ) {
                r.setSeriesFilled(i, true);
            }
            NumberAxis rangeAxis = (NumberAxis) plot.getAxis();
            rangeAxis.setTickLabelsVisible(false);
            return chart;
        }
    
        private XYDataset createDataset() {
            XYSeriesCollection result = new XYSeriesCollection();
            for (int r = 8; r > 0; r--) {
                XYSeries series = new XYSeries(title + String.valueOf(r));
                for (int t = (int) -PI2; t <= PI2; t++) {
                    series.add(t, r);
                }
                result.addSeries(series);
            }
            return result;
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new PolarArcs().display();
                }
            });
        }
    }
    
    0 讨论(0)
提交回复
热议问题