gpt4 book ai didi

java - JFreechart 循环遍历极坐标图扇区

转载 作者:行者123 更新时间:2023-11-29 03:55:33 28 4
gpt4 key购买 nike

我有以下代码来循环遍历极坐标图的扇区,但该方法被调用了多次,就好像它在循环中一样。我想在绘图的每个扇区上创建一个系列,该扇区由轴刻度单位和角度刻度单位定义。当我注释掉 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);
}
}
}

最佳答案

看起来您在每次 迭代中都添加了所有六个螺旋。这是我对你的 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);
}
}
}

关于java - JFreechart 循环遍历极坐标图扇区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6576911/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com