JFreechart极坐标图形状注释

时间:2011-06-30 19:57:27

标签: plot annotations jfreechart

我试图用不同的颜色为极地图的不同区域着色。例如,使角度20和60之间以及半径2和4之间的区域着色。 我怎样才能做到这一点?我正在考虑使用形状注释并从那里绘制弧形,但似乎没有极坐标图的形状注释。 有任何想法吗? 谢谢

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.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 test2 extends JFrame {

    private static final String title = "Archimedes' Spiral";

    public test2(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() {
        XYSeriesCollection result = new XYSeriesCollection();
        XYSeries series = new XYSeries(title);
        XYSeries ser = new XYSeries("test");
        for (int t = 0; t <= 1 * 360; t++) {
            series.add(90 - t, t);
        }

        for (int t = 0; t <= 1 * 120; t++) {
            ser.add(90 - t, 40);
            ser.add(90 - t, 120);
        }
        result.addSeries(series);
        result.addSeries(ser);
        return result;
    }

    private static 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 ren = new DefaultPolarItemRenderer();
        ren.setSeriesFilled(0, true);
        ren.setSeriesFilled(1, true);
        plot.setRenderer(ren);
        JFreeChart chart = new JFreeChart(
            title, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
        chart.setBackgroundPaint(Color.white);
        return chart;
    }

    public static void main(String[] args) {
        test2 demo = new test2(title);
        demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        demo.pack();
        demo.setLocationRelativeTo(null);
        demo.setVisible(true);
    }
}

1 个答案:

答案 0 :(得分:1)

DefaultPolarItemRenderer中通常使用的PolarPlot使用方法setSeriesFilled(),该方法控制系列是否已填充。渲染器指定AlphaComposite.SRC_OVER模式,其值为50%,因此重叠填充看起来特别好。

附录:要创建下面的图表,请从example开始,将数据集的域从createDataset()中的6π减少到2π:

for (int t = 0; t <= 1 * 360; t++) { ...

然后将系列填入createChart()

...
DefaultPolarItemRenderer renderer = new DefaultPolarItemRenderer();
renderer.setSeriesFilled(0, true);
...

enter image description here