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