JFreechart填充扇形系列

发布于 2024-11-24 01:41:08 字数 5106 浏览 2 评论 0原文

我正在尝试在极坐标图的每个区域上创建一个填充系列。但颜色没有填写正确。这是我得到的图像: http://i1122.photobucket .com/albums/l539/jpo2/polar-1.gif 这是我必须循环遍历给定部分的代码如下:

   if (i < 8) {
    for(int r = 0; r< 20; r+=(NumberAxis) plot.getAxis()).getTickUnit().getSize()){
    for(int theta = 0; theta <= 180; theta+=30){
    XYSeries series = new XYSeries(i + "π/8 " + "< θ < 2π+" + i + "π/8");
        for (int e = theta; e < theta+30; e++) {
            series.add(90-e-i*45, r);
            series.add(90-e-i*45, r- ((NumberAxis) plot.getAxis()).getTickUnit().getSize());
            }
        result.addSeries(series);
        setFilled(result);
        i++;
        }
    }
    }
private void setFilled(XYDataset dataset) {
    for (int i = 0; i < dataset.getSeriesCount(); i++) {
        renderers.setSeriesFilled(i, true);
        }
    }

这是对 @trashgod 的绘制方法的修改 JFreechart 极坐标图扇区循环 请帮忙。

完整代码:

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 < 4) {
            for (int g = 0; g < 30; g += 5) {
                for (int h = 0; h < 180; h += 45) {
                    XYSeries series = new XYSeries(i + "π/8 " + "< θ < 2π+" + i + "π/8");
                    for (int t = h; t <= h + 45; t++) {
                        series.add(90 - t, g);
                        series.add(90 - t, g + 5);
                    }
                    result.addSeries(series);
                    setFilled(result);
                    i++;
                }
            }

        }
    }

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

I am trying to create a filled series over each region of a polar plot. But the colors are not filled out right. Here is an image of what I get: http://i1122.photobucket.com/albums/l539/jpo2/polar-1.gif
Here is the code I have to loop through a given section is as follows:

   if (i < 8) {
    for(int r = 0; r< 20; r+=(NumberAxis) plot.getAxis()).getTickUnit().getSize()){
    for(int theta = 0; theta <= 180; theta+=30){
    XYSeries series = new XYSeries(i + "π/8 " + "< θ < 2π+" + i + "π/8");
        for (int e = theta; e < theta+30; e++) {
            series.add(90-e-i*45, r);
            series.add(90-e-i*45, r- ((NumberAxis) plot.getAxis()).getTickUnit().getSize());
            }
        result.addSeries(series);
        setFilled(result);
        i++;
        }
    }
    }
private void setFilled(XYDataset dataset) {
    for (int i = 0; i < dataset.getSeriesCount(); i++) {
        renderers.setSeriesFilled(i, true);
        }
    }

This is a modification of @trashgod's draw method at JFreechart Loop through polar chart sectors
Please help.

Full 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' 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 < 4) {
            for (int g = 0; g < 30; g += 5) {
                for (int h = 0; h < 180; h += 45) {
                    XYSeries series = new XYSeries(i + "π/8 " + "< θ < 2π+" + i + "π/8");
                    for (int t = h; t <= h + 45; t++) {
                        series.add(90 - t, g);
                        series.add(90 - t, g + 5);
                    }
                    result.addSeries(series);
                    setFilled(result);
                    i++;
                }
            }

        }
    }

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

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

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

发布评论

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

评论(1

离笑几人歌 2024-12-01 01:41:08

这是直接使用 PolarPlot 的简化版本,无需任何转换。尝试一下可能会更容易。

极弧图片

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PolarPlot;
import org.jfree.chart.renderer.DefaultPolarItemRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/** @see http://stackoverflow.com/questions/6669734 */
public class PolarArcs {

    private static final String title = "PolarArcs";
    private static final double PI2 = 90d; // π/2 radians = 90°

    private void display() {
        JFrame f = new JFrame(title);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ChartPanel panel = new ChartPanel(createChart(createDataset()));
        panel.setPreferredSize(new Dimension(400, 400));
        f.add(panel);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private JFreeChart createChart(XYDataset dataset) {
        JFreeChart chart = ChartFactory.createPolarChart(
            title, dataset, true, false, false);
        PolarPlot plot = (PolarPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.white);
        plot.setAngleGridlinesVisible(false);
        plot.setRadiusGridlinesVisible(false);
        DefaultPolarItemRenderer r = (DefaultPolarItemRenderer) plot.getRenderer();
        for (int i = 0; i < dataset.getSeriesCount(); i++ ) {
            r.setSeriesFilled(i, true);
        }
        NumberAxis rangeAxis = (NumberAxis) plot.getAxis();
        rangeAxis.setTickLabelsVisible(false);
        return chart;
    }

    private XYDataset createDataset() {
        XYSeriesCollection result = new XYSeriesCollection();
        for (int r = 8; r > 0; r--) {
            XYSeries series = new XYSeries(title + String.valueOf(r));
            for (int t = (int) -PI2; t <= PI2; t++) {
                series.add(t, r);
            }
            result.addSeries(series);
        }
        return result;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new PolarArcs().display();
            }
        });
    }
}

Here's a simplified version the uses PolarPlot directly, without any transformation. It might be easier to experiment with.

Polar arcs picture

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PolarPlot;
import org.jfree.chart.renderer.DefaultPolarItemRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/** @see http://stackoverflow.com/questions/6669734 */
public class PolarArcs {

    private static final String title = "PolarArcs";
    private static final double PI2 = 90d; // π/2 radians = 90°

    private void display() {
        JFrame f = new JFrame(title);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ChartPanel panel = new ChartPanel(createChart(createDataset()));
        panel.setPreferredSize(new Dimension(400, 400));
        f.add(panel);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private JFreeChart createChart(XYDataset dataset) {
        JFreeChart chart = ChartFactory.createPolarChart(
            title, dataset, true, false, false);
        PolarPlot plot = (PolarPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.white);
        plot.setAngleGridlinesVisible(false);
        plot.setRadiusGridlinesVisible(false);
        DefaultPolarItemRenderer r = (DefaultPolarItemRenderer) plot.getRenderer();
        for (int i = 0; i < dataset.getSeriesCount(); i++ ) {
            r.setSeriesFilled(i, true);
        }
        NumberAxis rangeAxis = (NumberAxis) plot.getAxis();
        rangeAxis.setTickLabelsVisible(false);
        return chart;
    }

    private XYDataset createDataset() {
        XYSeriesCollection result = new XYSeriesCollection();
        for (int r = 8; r > 0; r--) {
            XYSeries series = new XYSeries(title + String.valueOf(r));
            for (int t = (int) -PI2; t <= PI2; t++) {
                series.add(t, r);
            }
            result.addSeries(series);
        }
        return result;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new PolarArcs().display();
            }
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文