Plot points instead of lines? JFreeChart PolarChart
Currently, the PolarChart joins all the coordinates with lines creating a polygon. I just want it to plot each point with a dot and NOT join them together. Is this possible?
I have tried using translateValueThetaRadiusToJava2D()
and Graphics2D to draw circles but it's very clunky and contrived.
Any suggest开发者_开发问答ions welcome!
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);
精彩评论