如何使用 jfree 极坐标图更改点形状?

how to change dot shape with jfree polar plot?

我想知道改变点极坐标图大小和形状的方法有哪些。 而且如果可以禁用此类绘图的点。

这里是用默认点绘制两个系列的极坐标的代码。

谢谢

package jfreechart;

import java.awt.Color;
import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PolarPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class JFreeChartPolarChartExample extends JFrame {

   private static final long serialVersionUID = 1L;

   public JFreeChartPolarChartExample(String applicationTitle) {
       super(applicationTitle);
       XYSeriesCollection  dataSet = new XYSeriesCollection();
       
       XYSeries series1 = createRandomData("Series 1", 500);
       XYSeries series2 = createRandomData("Series 2", 60);
  
       dataSet.addSeries(series1);
       dataSet.addSeries(series2);
   
      PolarPlot pp = new PolarPlot();
      pp.setAngleGridlinePaint (Color.BLACK);
       
       JFreeChart polarChart = ChartFactory.createPolarChart(null, dataSet, true, true, false);

       // Adding chart into a chart panel
       ChartPanel chartPanel = new ChartPanel(polarChart);
  
       // settind default size
       chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
     
       setContentPane(chartPanel);
   
   }

   private static XYSeries createRandomData(final String name, final double maxvalue ) {
       final XYSeries series = new XYSeries(name);
       for (double az = 0.0; az < 360.0; az =az+10*Math.random()) 
       {
           final double value = maxvalue*(0.8 +0.2*Math.random());
           series.add(az, value);
       }
       return series;
   }

   public static void main(String[] args) {
       JFreeChartPolarChartExample chart = new JFreeChartPolarChartExample(null);
       chart.pack();
       chart.setVisible(true);
   }
}

with it, default dots are square.

是的,DefaultPolarItemRenderer uses a 6 x 6 square DEFAULT_SHAPE:

public static final Shape DEFAULT_SHAPE =
   new Rectangle2D.Double(-3.0, -3.0, 6.0, 6.0);

how to [make] them round and smaller?

按照建议 ,使用自定义 DrawingSupplier。在下面的示例中,所有系列的默认值 Shape 都设置为半径为 2.5 的圆。 ShapeUtils,在评论中显示,也可能有用:

DefaultPolarItemRenderer renderer = (DefaultPolarItemRenderer) polarPlot.getRenderer();
renderer.setAutoPopulateSeriesShape(false);
renderer.setDefaultShape(new Ellipse2D.Double(-2, -2, 5, 5));
//renderer.setDefaultShape(ShapeUtils.createDiamond(3));

how to disabled them?

根据需要调用 setShapesVisible()

renderer.setShapesVisible(true);

此外,

  • 您可以径向缩放图表,如图所示

  • 不是制作一个 new PolarPlot(),而是获取图表工厂实例化的那个。

  • 要确定图表的初始大小,请按照建议覆盖 getPreferredSize() here

  • 使用(几乎)相等的宽度和高度来最大化封闭容器中的绘图。

  • 仅在 event dispatch thread.

    上构造和操作 Swing GUI 对象

经测试:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
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.axis.NumberTickUnit;
import org.jfree.chart.plot.PolarPlot;
import org.jfree.chart.renderer.DefaultPolarItemRenderer;
import org.jfree.chart.util.ShapeUtils;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 * @see 
 * @see 
 */
public class Skyplot {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Skyplot()::plot);
    }

    private void plot() {
        JFrame plotFrame = new JFrame("PolarPlot");
        plotFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        plotFrame.add(new Plotter());
        plotFrame.pack();
        plotFrame.setLocationRelativeTo(null);
        plotFrame.setVisible(true);
    }

    private static final class Plotter extends JPanel {

        public Plotter() {
            super(new GridLayout());
            this.add(createChartPanel(getXYDataset()));
        }

        private ChartPanel createChartPanel(XYDataset dataset) {
            JFreeChart chart = ChartFactory.createPolarChart(
                "Random Data", dataset, true, true, false);

            PolarPlot polarPlot = (PolarPlot) chart.getPlot();
            polarPlot.setAngleGridlinePaint (Color.BLACK);
            polarPlot.setRadiusGridlinePaint(Color.BLACK);

            DefaultPolarItemRenderer renderer = (DefaultPolarItemRenderer) polarPlot.getRenderer();
            renderer.setAutoPopulateSeriesShape(false);
            renderer.setDefaultShape(new Ellipse2D.Double(-2, -2, 5, 5));
            //renderer.setDefaultShape(ShapeUtils.createDiamond(3));
            renderer.setShapesVisible(true);

            NumberAxis rangeAxis = (NumberAxis) polarPlot.getAxis();
            rangeAxis.setTickUnit(new NumberTickUnit(100.0));

            return new ChartPanel(chart) {
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(500, 500);
                }
            };
        }

        private XYDataset getXYDataset() {
            XYSeriesCollection dataset = new XYSeriesCollection();
            dataset.addSeries(createRandomData("Series 1", 500));
            dataset.addSeries(createRandomData("Series 2", 60));
            return dataset;
        }

        private static XYSeries createRandomData(final String name, final double maxvalue) {
            final XYSeries series = new XYSeries(name);
            for (double az = 0.0; az < 360.0; az = az + 10 * Math.random()) {
                final double value = maxvalue * (0.8 + 0.2 * Math.random());
                series.add(az, value);
            }
            return series;
        }
    }
}