Plot points instead of lines? JFreeChart PolarChart

那年仲夏 提交于 2019-12-05 06:57:34

So the DefaultPolarItemRenderer takes in all the polar points, converts the polar points to regular Java2D coordinates, makes a Polygon with those points and then draws it. Here's how I got it to draw dots instead of a polygon:

public class MyDefaultPolarItemRenderer extends DefaultPolarItemRenderer {

    @Override
    public void drawSeries(java.awt.Graphics2D g2, java.awt.geom.Rectangle2D dataArea, PlotRenderingInfo info, PolarPlot plot, XYDataset dataset, int seriesIndex) {


        int numPoints = dataset.getItemCount(seriesIndex);
        for (int i = 0; i < numPoints; i++) {

            double theta = dataset.getXValue(seriesIndex, i);
            double radius = dataset.getYValue(seriesIndex, i);
            Point p = plot.translateValueThetaRadiusToJava2D(theta, radius,
                    dataArea);
            Ellipse2D el = new Ellipse2D.Double(p.x, p.y, 5, 5);
            g2.fill(el);
            g2.draw(el);
        }
    }
}

and then instantiated this class elsewhere:

    MyDefaultPolarItemRenderer dpir = new MyDefaultPolarItemRenderer();
    dpir.setPlot(plot);
    plot.setRenderer(dpir);

This one's a little harder. Given a PolarPlot, you can obtain its AbstractRenderer and set the shape. For example,

PolarPlot plot = (PolarPlot) chart.getPlot();
AbstractRenderer ar = (AbstractRenderer) plot.getRenderer();
ar.setSeriesShape(0, ShapeUtilities.createDiamond(5), true);

The diamond will appear in the legend, but the DefaultPolarItemRenderer neither renders shapes, nor provides line control. You'd have to extend the default renderer and override drawSeries(). XYLineAndShapeRenderer is good example for study; you can see how it's used in TimeSeriesChartDemo1.

If this is terra incognita to you, I'd recommend The JFreeChart Developer Guide.

Disclaimer: Not affiliated with Object Refinery Limited; I'm a satisfied customer and very minor contributor.

This is an excellent discussion, in case you want the function to pick up the color assigned by user to the series

add ...

Color c =(Color)this.lookupSeriesPaint(seriesIndex);
g2.setColor(c);

before ...

g.draw(e1);

there are other functions... use code completion to see what else functions are available against series rendereing with name starting from lookupSeries........(int seriesindex)

I found a rather strange way to get the points without any lines connecting them.

I set the Stroke of the renderer to be a thin line, with a dash phase of 0, and length of 1e10:

Stroke dashedStroke = new BasicStroke(
                          0.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
                          0.0f, new float[] {0.0f, 1e10f}, 1.0f );
renderer.setSeriesStroke(0, dashedStroke);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!