How to change the color of multiple ellipses using a loop (JFreeChart)

蓝咒 提交于 2019-12-31 02:51:09

问题


I have drawn multiple ellipses using a loop as shown below, and the results are perfect using one color for all the ellipses, but my target is to color each ellipse with different color. Is there any way to let the property Color.BLUE change its value in each iteration?

for (int i = 0; i < 3; i++)
{
    XYShapeAnnotation unitCircle1 = new XYShapeAnnotation(
        new Ellipse2D.Double((FinalArayOfOptpar[s][i] - Math.abs(FinalArayOfOptpar[s][i + 2])),
            (FinalArayOfOptpar[s][i + 1] - Math.abs(FinalArayOfOptpar[s][i + 3])),
            Math.abs(FinalArayOfOptpar[s][i + 2] * 2.0), Math.abs(FinalArayOfOptpar[s][i + 3] * 2.0)),
        new BasicStroke(0.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER,
            10.2f), Color.BLUE);
    xyPlot.addAnnotation(unitCircle1);
}

回答1:


tens of XYShapeAnnotations will be created…so creating multiple instances of XYShapeAnnotation will not work for my purpose.

Happily, an instance XYShapeAnnotation is small—just 48 bytes each in the example below. You'll want to profile to be sure.

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.geom.Ellipse2D;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYShapeAnnotation;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 * @see https://stackoverflow.com/a/35236100/230513
 */
public class AnnotationTest {

    private static final BasicStroke stroke = new BasicStroke(2.0f);
    private static final int N = 16;
    private static final int S = 8;

    public static void main(String[] args) {
        EventQueue.invokeLater(new AnnotationTest()::display);
    }

    private void display() {
        XYDataset data = createDataset();
        JFreeChart chart = ChartFactory.createXYLineChart("ArcTest", "X", "Y",
            data, PlotOrientation.VERTICAL, true, true, false);
        XYPlot plot = chart.getXYPlot();
        XYLineAndShapeRenderer renderer
            = (XYLineAndShapeRenderer) plot.getRenderer();
        renderer.setBaseShapesVisible(true);
        for (int i = 0; i < N; i++) {
            double x = data.getXValue(0, i) - S / 2;
            double y = data.getYValue(0, i) - S / 2;
            Ellipse2D.Double ellipse = new Ellipse2D.Double(x, y, S, S);
            Color color = Color.getHSBColor((float) i / N, 1, 1);
            renderer.addAnnotation(new XYShapeAnnotation(ellipse, stroke, color));
        }
        ChartFrame frame = new ChartFrame("Test", chart);
        frame.pack();
        frame.setVisible(true);
    }

    private static XYDataset createDataset() {
        XYSeriesCollection result = new XYSeriesCollection();
        XYSeries series = new XYSeries("ArcTest");
        for (int i = 0; i < N; i++) {
            series.add(i * S, i * S);
        }
        result.addSeries(series);
        return result;
    }
}


来源:https://stackoverflow.com/questions/35204012/how-to-change-the-color-of-multiple-ellipses-using-a-loop-jfreechart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!