JFreechart filling sectors series
Asked Answered
G

1

1

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);
        }
    }
}
Guv answered 12/7, 2011 at 19:13 Comment(8)
Could yo expand a little on what you mean by "region"? Perhaps like a pie-shaped wedge?Remarque
I mean sectors; like the sector between radii 8 and radii 10 and the angle between 45 and 90 degrees. That is, the sectors defined by concentric circles(radii) and the cross sectional lines(angles). ThanksGuv
Ah, like a curved bite from a pie-shaped wedge. Does the coordinate system matter? The coordinates used by JFreeChart navigational, rather than analytical. Skipping the transformation would simplify the code.Remarque
Yeah, but check the full code above. The colors are not smooth :(Guv
+1 for a novel sscce. Can you add a picture?Remarque
i1122.photobucket.com/albums/l539/jpo2/polar-1.gifGuv
Most unexpected! It reminds me of this.Remarque
@Remarque let us continue this discussion in chatGuv
R
1

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();
            }
        });
    }
}
Remarque answered 13/7, 2011 at 3:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.