JFreechart 循环极坐标图扇区

发布于 2024-11-18 14:06:01 字数 5400 浏览 2 评论 0原文

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

有人可以向我解释一下为什么它一直循环吗?为什么创建的系列数与图表上的扇区数不相等?

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);
        }
    }
}

I have the following code to method loop throug the sectors of a polar plot, but the method is being called several times, as if it was in a loop. I want to create a series on each sectors of the plot, the sector being defines by the axis tick unit and the angle tick unit. When I comment out the polarDataset.addSeries(new XYSeries("test")) and the for loop for this new series, the problem disappears, making me believe there might be something up with this. This is a continuation of a question posted earlier

Can someone please explain to me why it keeps looping? And why the number of series created is not equivalent to the number of sectors on the chart?

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);
}

So, I edited trachGod's example to get implement the ChartProgressListener. I also made XYSeriesCollection a global variable and added the following code in the chartprogess method:

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);
    }

But more series than required are still added. Any help is always much appreciated.

The complete code:

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);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无可置疑 2024-11-25 14:06:01

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

在此处输入图像描述

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);
        }
    }
}

It looks like you were adding all six spirals on each iteration. Here's my variation on your 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);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文