JFreechart循环通过极地图表部门

时间:2011-07-04 23:45:37

标签: java loops plot jfreechart

我有以下代码方法循环通过极坐标图的扇区,但该方法被多次调用,就像它在循环中一样。我想在图的每个扇区上创建一个系列,该扇区由轴刻度单位和角度刻度单位定义。当我注释掉这个新系列的polarDataset.addSeries(new XYSeries("test"))和for循环时,问题就消失了,让我相信可能会有这个问题。这是发布earlier

的问题的延续

有人可以向我解释为什么它会循环播放吗?为什么创建的系列数量不等于图表上的扇区数量?

int intSeries = 0;
public void createSeriesOnSectors() {
    for (int r = 0; r <= plot.getMaxRadius(); r += ((NumberAxis) plot
            .getAxis()).getTickUnit().getSize()) {
        for (int i = 0; i <= 360; i += plot.getAngleTickUnit()
                .getSize()) {
            dataset.addSeries(new XYSeries("test"));
            intSeries++;

            for (int e = i; e < i+ plot.getAngleTickUnit().getSize(); e++) {
                dataset.getSeries(polarDataset.getSeriesCount() - 1).add(90 - e, r- ((NumberAxis) plot.getAxis()).getTickUnit().getSize());
                }
        }
    }System.out.println("The number of sec is : " + intSeries);
}

所以,我编辑了trachGod的例子来实现ChartProgressListener。我还将XYSeriesCollection作为全局变量,并在chartprogess方法中添加了以下代码:

for (int i = 0; i < 6; i++) {
        XYSeries seriesy = new XYSeries(i + "π/8 " + "< θ < 2π+" + i + "π/8");
        for (int t = 0; t <= 1 * 360; t++) {
            seriesy.add(90 - t - i * 45, t);
        }
        result.addSeries(seriesy);
    }

但仍然增加了比所需更多的系列。任何帮助总是非常感激。

完整的代码:

import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
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.NumberTick;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.event.ChartProgressEvent;
import org.jfree.chart.event.ChartProgressListener;
import org.jfree.chart.plot.PolarPlot;
import org.jfree.chart.renderer.DefaultPolarItemRenderer;
import org.jfree.chart.renderer.PolarItemRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.TextAnchor;


public class tests extends JFrame implements ChartProgressListener {

    private static final String title = "Archimedes' Spiral";
    public static XYSeriesCollection result;

    public tests(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() {
        result = new XYSeriesCollection();
        XYSeries series = new XYSeries(title);
        XYSeries series2 = new XYSeries(title);
        for(int i = 90; i<180; i++){
            series2.add(90-i, 200);
            series2.add(90-i, 0);
        }
        result.addSeries(series);
        result.addSeries(series2);
        return result;
    }

    private JFreeChart createChart(XYDataset dataset) {
        ValueAxis radiusAxis = new NumberAxis();
        radiusAxis.setTickLabelsVisible(false);
        PolarItemRenderer renderer = new DefaultPolarItemRenderer();
        PolarPlot plot = new PolarPlot(dataset, radiusAxis, renderer) {

            @Override
            protected List refreshAngleTicks() {
                List<NumberTick> ticks = new ArrayList<NumberTick>();
                int delta = (int) this.getAngleTickUnit().getSize();
                for (int t = 0; t < 360; t += delta) {
                    int tp = (360 + 90 - t) % 360;
                    NumberTick tick = new NumberTick(
                        Double.valueOf(t), String.valueOf(tp),
                        TextAnchor.CENTER, TextAnchor.CENTER, 0.0);
                    ticks.add(tick);
                }
                return ticks;
            }
        };
        plot.setBackgroundPaint(new Color(0x00f0f0f0));
        plot.setRadiusGridlinePaint(Color.gray);
        plot.addCornerTextItem("r(θ) = θ; 0 < θ < 6π");
        DefaultPolarItemRenderer renderers = new DefaultPolarItemRenderer();
        for (int i = 0; i < dataset.getSeriesCount(); i++) {
            renderers.setSeriesFilled(i, true);
        }
        plot.setRenderer(renderers);
        JFreeChart chart = new JFreeChart(
            title, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
        chart.setBackgroundPaint(Color.white);
        chart.addProgressListener(this);
        return chart;
    }


    public static void main(String[] args) {
        tests demo = new tests(title);
        demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        demo.pack();
        demo.setLocationRelativeTo(null);
        demo.setVisible(true);
    }

    @Override
    public void chartProgress(ChartProgressEvent e) {
        if (e.getType() == ChartProgressEvent.DRAWING_FINISHED) {
            JFreeChart chart = e.getChart();
            draw();
        }
    }

    public void draw(){
        for (int i = 0; i < 6; i++) {
            XYSeries seriesy = new XYSeries(i + "π/8 " + "< θ < 2π+" + i + "π/8");
            for (int t = 0; t <= 1 * 360; t++) {
                seriesy.add(90 - t - i * 45, t);
            }
            result.addSeries(seriesy);
        }
    }
}

1 个答案:

答案 0 :(得分:4)

看起来你在每次迭代上添加了所有六个螺旋。这是我对sscce的变体。

enter image description here

import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
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.NumberTick;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.event.ChartProgressEvent;
import org.jfree.chart.event.ChartProgressListener;
import org.jfree.chart.plot.PolarPlot;
import org.jfree.chart.renderer.DefaultPolarItemRenderer;
import org.jfree.chart.renderer.PolarItemRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.TextAnchor;

public class tests extends JFrame implements ChartProgressListener {

    private static final String title = "Archimedes' Spirals";
    private XYSeriesCollection result = new XYSeriesCollection();
    private DefaultPolarItemRenderer renderers = new DefaultPolarItemRenderer();
    private int i;

    public tests(String title) {
        super(title);
        JFreeChart chart = createChart(result);
        ChartPanel panel = new ChartPanel(chart);
        panel.setPreferredSize(new Dimension(500, 500));
        panel.setMouseZoomable(false);
        this.add(panel);
    }

    private JFreeChart createChart(XYDataset dataset) {
        ValueAxis radiusAxis = new NumberAxis();
        radiusAxis.setTickLabelsVisible(false);
        PolarItemRenderer renderer = new DefaultPolarItemRenderer();
        PolarPlot plot = new PolarPlot(dataset, radiusAxis, renderer) {

            @Override
            protected List refreshAngleTicks() {
                List<NumberTick> ticks = new ArrayList<NumberTick>();
                int delta = (int) this.getAngleTickUnit().getSize();
                for (int t = 0; t < 360; t += delta) {
                    int tp = (360 + 90 - t) % 360;
                    NumberTick tick = new NumberTick(
                        Double.valueOf(t), String.valueOf(tp),
                        TextAnchor.CENTER, TextAnchor.CENTER, 0.0);
                    ticks.add(tick);
                }
                return ticks;
            }
        };
        plot.setBackgroundPaint(new Color(0x00f0f0f0));
        plot.setRadiusGridlinePaint(Color.gray);
        plot.addCornerTextItem("r(θ) = θ; 0 < θ < 2π; +iπ/8");
        setFilled(dataset);
        plot.setRenderer(renderers);
        JFreeChart chart = new JFreeChart(
            title, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
        chart.setBackgroundPaint(Color.white);
        chart.addProgressListener(this);
        return chart;
    }

    public static void main(String[] args) {
        tests demo = new tests(title);
        demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        demo.pack();
        demo.setLocationRelativeTo(null);
        demo.setVisible(true);
    }

    @Override
    public void chartProgress(ChartProgressEvent e) {
        if (e.getType() == ChartProgressEvent.DRAWING_FINISHED) {
            System.out.println(e);
            JFreeChart chart = e.getChart();
            draw();
        }
    }

    public void draw() {
        if (i < 8) {
            XYSeries series = new XYSeries(i + "π/8 " + "< θ < 2π+" + i + "π/8");
            for (int t = 0; t <= 1 * 360; t++) {
                series.add(90 - t - i * 45, t);
            }
            result.addSeries(series);
            setFilled(result);
            i++;
        }
    }

    private void setFilled(XYDataset dataset) {
        for (int i = 0; i < dataset.getSeriesCount(); i++) {
            renderers.setSeriesFilled(i, true);
        }
    }
}