JFreeChart Polar chart填充零点

时间:2012-09-26 13:34:00

标签: java jfreechart

可能我无法说出正确的问题。让我解释一下,我想绘制从零开始值并将其填充到所需点的极坐标图。 当我绘制图表时,它显示了这样的东西 This is what I have been able to achieve yet

但我正在寻找类似的东西 This is What I am looking for

以下是示例代码

import java.awt.Color;
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;

public class PolarCharDemo extends ApplicationFrame {
    private static final long serialVersionUID = 1L;
    double [][] myData = {{12.1,7.9,3.29,1.01,0,145},
                {12.6,8.4,3.22,1,0,145},
                {13.1,8.1,3.29,0.97,0,145},
                {10,7.5,3.22,0.93,0,145},
                {11.4,8.2,3.1,0.93,0,145},
                {11.7,7.3,2.99,0.9,0,145},
                {10.3,8,2.97,0.89,0,145},
                {8.8,7,3.05,0.9,0,145},
                {9.8,6.7,3.13,0.93,0,145},
                {11.1,7.1,3.05,0.91,0,145},
                {8.8,6.2,3.12,0.95,0,145},
                {9.6,7,2.82,0.44,2.3,5},
                {10.2,6.8,2.82,0.47,3.2,5},
                {9.4,6.8,2.76,0.41,2.9,5},
                {10,7,2.61,0.44,3.3,45},
                {12.8,8,2.95,0.49,2.2,50},
                {9.9,7.1,4.08,1.01,0.6,45},
                {7.1,4.4,2.23,0.4,1.5,5},
                {5,4,2.1,0.37,2.3,5},
                {7.2,4.1,2.17,0.37,1.5,5},
                {7.1,4.3,2.09,0.36,2,5},
                {5.1,4.1,2.17,0.38,2.1,5},
                {4.2,3.9,2.16,0.41,1.9,5},
                {4.9,3.9,2.08,0.35,2.5,5},
                {4.3,3.5,2.09,0.34,3,5}};
    /**
     * Creates a new instance of the demo.
     * 
     * @param title  the frame title.
     */
    public PolarCharDemo(final String title) {
        super(title);
        final XYDataset dataset = createAvgDataset();
        final JFreeChart chart = createChart(dataset);
        final ChartPanel chartPanel = new PolarChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(1000, 670));
        chartPanel.setEnforceFileExtensions(false);
        setContentPane(chartPanel);
    }
    /**
     * Creates a sample chart.
     * @param dataset  the dataset.
     * @return A sample chart.
     */
    private JFreeChart createChart(final XYDataset dataset) {
        final JFreeChart chart = ChartFactory.createPolarChart("", dataset, true, true, false); 
        final PolarPlot plot = (PolarPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.WHITE);
        plot.setAngleGridlinePaint(Color.BLACK);
        plot.setRadiusGridlinePaint(Color.LIGHT_GRAY);

        final DefaultPolarItemRenderer renderer = (DefaultPolarItemRenderer) plot.getRenderer();
        renderer.setSeriesFilled(0, true);
        renderer.setSeriesPaint(0, Color.BLUE);
        renderer.setSeriesFilled(1, true);
        renderer.setSeriesPaint(1, Color.RED);
        renderer.setSeriesFilled(2, true);
        renderer.setSeriesPaint(2, Color.YELLOW);
        renderer.setSeriesFilled(3, true);
        renderer.setSeriesPaint(3, Color.GREEN);
        return chart;
    }
    /**
     * Creates an Average dataset.
     * @return An average dataset.
     */
    private XYDataset createAvgDataset() {    
        final XYSeriesCollection data = new XYSeriesCollection();
        XYSeries series0 = new XYSeries("Series1");
        XYSeries series1 = new XYSeries("Series2");
        XYSeries series2 = new XYSeries("Series3");
        for(int i=0;i<myData.length;i++)
        {
            series0.add(myData[i][5], myData[i][0]);
            series1.add(myData[i][5], myData[i][1]);
            series2.add(myData[i][5], myData[i][2]);
        }
        data.addSeries(series0);
        data.addSeries(series1);
        data.addSeries(series2);
        return data;
    }    

    /**
     * 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 PolarCharDemo demo = new PolarCharDemo("Polar Chart");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }   
}

1 个答案:

答案 0 :(得分:2)

我不确定我是否了解您的数据,但可能有助于将值标准化为允许将原点(0, 0)添加到数据的角度范围。在下面的例子中,我选择了零。

enter image description here

import java.awt.AlphaComposite;
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/a/12607586/230513 */
public class PolarCharDemo {

    private static final String title = "PolarChart";
    private static final double START = 0;
    private static final double END = 145;

    private void display() {
        JFrame f = new JFrame(title);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ChartPanel panel = new ChartPanel(createChart(createDataset()));
        panel.setPreferredSize(new Dimension(500, 500));
        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.setAngleGridlinePaint(Color.black);
        plot.setRadiusGridlinePaint(Color.lightGray);
        DefaultPolarItemRenderer r = (DefaultPolarItemRenderer) plot.getRenderer();
        r.setFillComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
        for (int i = 0; i < dataset.getSeriesCount(); i++ ) {
            r.setSeriesFilled(i, true);
            r.setShapesVisible(false);
            r.setDrawOutlineWhenFilled(false);
        }
        NumberAxis rangeAxis = (NumberAxis) plot.getAxis();
        rangeAxis.setTickLabelsVisible(false);
        return chart;
    }

    private XYDataset createDataset() {
        XYSeriesCollection result = new XYSeriesCollection();
        for (int r = 4; r > 0; r--) {
            XYSeries series = new XYSeries(title + String.valueOf(r));
            series.add(0, 0);
            for (int t = (int) START; t <= END; 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 PolarCharDemo().display();
            }
        });
    }
}