JFreechart Loop through polar chart sectors
Asked Answered
V

1

1

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);
        }
    }
}
Velez answered 4/7, 2011 at 23:45 Comment(1)
+1 for sscce.Amaliaamalie
A
4

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);
        }
    }
}
Amaliaamalie answered 5/7, 2011 at 16:29 Comment(8)
Thank YOU. But what is the "if(i<8)"?Velez
Otherwise it's an infinite loop. I posted separate answers for clarity; please consider accepting either one. You also have enough reputation to vote, now, too.Amaliaamalie
Ok, but how do you determine what value that should be? you have it set to eight; I want the series to fill the entire plot. how do it determine the value to put in this if statement them?Velez
Ah, I chose eight because i * 45 degrees is an integral multiple of π/8 radians; eight makes a full circle. Try different values; too many looks like mud. :-)Amaliaamalie
So, I want the fill just the upper half circle. What values should I use? Sorry, I tried different values but that did not work too well. Basically, I am trying to fill each region, say for r = 1, create a series on theta = 30 till theta = 60. Thanks for your help.Velez
Sorry, nothing jumps out at me.Amaliaamalie
OK. I have this code for a section of the plot. Hope this helps you to help me for (int e = theta; e < theta+ plot.getAngleTickUnit().getSize(); e++) { series.add(90-e-d*180, r); series.add(90-e-d*180, r- ((NumberAxis) plot.getAxis()).getTickUnit().getSize()); }Velez
Why not post this as a question? It would be easier to read. :-)Amaliaamalie

© 2022 - 2024 — McMap. All rights reserved.