向极坐标图的点添加标签

时间:2018-08-20 20:07:54

标签: java jfreechart

以下是在Java2s网站上找到的示例。

它按预期工作,但是我尝试在每个点的末尾添加值(或标签)。

    import java.awt.Dimension;

    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.PolarChartPanel;
    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;
    import org.jfree.ui.ApplicationFrame;
    import org.jfree.ui.RefineryUtilities;

    /**
     * <code>PolarChartDemo</code> demonstrates the capabilities of the {@link PolarPlot}.
     * 
     * @author  Daniel Bridenbecker, Solution Engineering, Inc.
     */
    public class PolarChartDemo extends ApplicationFrame {

        /**
         * Creates a new instance of the demo.
         * 
         * @param title  the frame title.
         */
        public PolarChartDemo(final String title) {
            super(title);
            final XYDataset dataset = createDataset();
            final JFreeChart chart = createChart(dataset);
            final ChartPanel chartPanel = new PolarChartPanel(chart);
            chartPanel.setPreferredSize(new Dimension(500, 270));
            chartPanel.setEnforceFileExtensions(false);
            setContentPane(chartPanel);
        }

        /**
         * Creates a sample dataset.
         * 
         * @return A sample dataset.
         */
        private XYDataset createDataset() {    
            final XYSeriesCollection data = new XYSeriesCollection();
            final XYSeries series1 = createRandomData("Series 1", 75.0, 10.0);
            final XYSeries series2 = createRandomData("Series 2", 50.0, 5.0);
            final XYSeries series3 = createRandomData("Series 3", 25.0, 1.0);
            data.addSeries(series1);
    //        data.addSeries(series2);
    //        data.addSeries(series3);
            return data;
        }

        /**
         * Creates a sample chart.
         * 
         * @param dataset  the dataset.
         * 
         * @return A sample chart.
         */
        private JFreeChart createChart(final XYDataset dataset) {
            final JFreeChart chart = ChartFactory.createPolarChart(
                "Polar Chart Demo", dataset, true, true, false
            ); 
            final PolarPlot plot = (PolarPlot) chart.getPlot();
            final DefaultPolarItemRenderer renderer = (DefaultPolarItemRenderer) plot.getRenderer();
            renderer.setSeriesFilled(2, true);

    //Shotgun approach to just turn everything on to get the label. 
    renderer.setBaseCreateEntities(true);
    renderer.setBaseItemLabelsVisible(true);
    renderer.setBaseItemLabelsVisible(true, true);
    renderer.setBaseSeriesVisible(true);
    renderer.setBaseSeriesVisible(true, true);
    renderer.setItemLabelsVisible(true);
    renderer.setSeriesVisible(true);
    renderer.setSeriesVisibleInLegend(true);

            return chart;
        }

        /**
         * Creates a series containing random data.
         * 
         * @param name  the series name.
         * @param baseRadius  the base radius.
         * @param thetaInc  the angle increment.
         * 
         * @return The series.
         */
        private static XYSeries createRandomData(final String name, 
                                                 final double baseRadius, 
                                                 final double thetaInc) {
            final XYSeries series = new XYSeries(name);
            for (double theta = 0.0; theta < 360.0; theta += thetaInc) {
                final double radius = baseRadius * (1.0 + Math.random());
                series.add(theta, radius);
            }
            return series;
        }

        /**
         * Main program that creates a thermometer and places it into
         * a JFrame.
         *
         * @param argv Command line arguements - none used.
         */
        public static void main(final String[] argv) {

            final PolarChartDemo demo = new PolarChartDemo("Polar Chart Demo");
            demo.pack();
            RefineryUtilities.centerFrameOnScreen(demo);
            demo.setVisible(true);

        }   

    }

以下网站说明了我要执行的操作,但是本示例使用了另一种类型的图表。

https://community.jaspersoft.com/wiki/how-display-values-xy-line-jfreechart

上述URL中的修复程序指示使用以下命令

  renderer.setBaseItemLabelsVisible(true);

此命令与其他许多布尔过程一起尝试过,希望将其打开,但没有帮助。

使用Polar Charts打开此功能是否有其他要求?

0 个答案:

没有答案