JFreeChart PolarPlot: mathematical orientation

前端 未结 3 2014
萌比男神i
萌比男神i 2020-11-27 23:00

I\'d like to create a polar plot where the data is plotted in mathematical orientation (thus, the series starts and the east and continues counter-clockwise). The default be

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 23:06

    The current version of JFreeChart seems to solve this issue a lot easier: There are three methods available:

    setCounterClockwise(true) // changes the direction of the ticks
    setAxisLocation(PolarAxisLocation.EAST_BELOW) // defines the placement of the axis
    setAngleOffset(0);
    

    Complete example adapted from here:

    import java.awt.Color;
    import java.awt.Dimension;
    import javax.swing.JFrame;
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.axis.NumberAxis;
    import org.jfree.chart.axis.ValueAxis;
    import org.jfree.chart.plot.PolarAxisLocation;
    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://en.wikipedia.org/wiki/Polar_coordinate_system
     * @see https://stackoverflow.com/questions/3458824
     * @see https://stackoverflow.com/questions/6540390
     * @see https://stackoverflow.com/questions/6576911
     * @see https://stackoverflow.com/a/10227275/230513
     */
    public class ArchimedesSpiral extends JFrame {
    
        private static final String title = "Archimedes' Spiral";
    
        public ArchimedesSpiral(String title) {
            super(title);
            JFreeChart chart = createChart(createDataset());
            ChartPanel panel = new ChartPanel(chart);
            panel.setPreferredSize(new Dimension(500, 500));
            panel.setMouseZoomable(false);
            this.add(panel);
        }
    
        private static XYDataset createDataset() {
            XYSeriesCollection result = new XYSeriesCollection();
            XYSeries series = new XYSeries(title);
            for (int t = 0; t <= 3 * 360; t++) {
                series.add(t, t);
            }
            result.addSeries(series);
            return result;
        }
    
        private static JFreeChart createChart(XYDataset dataset) {
            ValueAxis radiusAxis = new NumberAxis();
            radiusAxis.setTickLabelsVisible(false);
            DefaultPolarItemRenderer renderer = new DefaultPolarItemRenderer();
            renderer.setShapesVisible(false);
            PolarPlot plot = new PolarPlot(dataset, radiusAxis, renderer);
            plot.setCounterClockwise(true);
            plot.setAxisLocation(PolarAxisLocation.EAST_BELOW);
            plot.setAngleOffset(0);
            plot.setBackgroundPaint(new Color(0x00f0f0f0));
            plot.setRadiusGridlinePaint(Color.gray);
            plot.addCornerTextItem("r(θ) = θ; 0 < θ < 6π");
            JFreeChart chart = new JFreeChart(
                title, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
            chart.setBackgroundPaint(Color.white);
            return chart;
        }
    
        public static void main(String[] args) {
            ArchimedesSpiral demo = new ArchimedesSpiral(title);
            demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            demo.pack();
            demo.setLocationRelativeTo(null);
            demo.setVisible(true);
        }
    }
    

提交回复
热议问题