📄 spiderwebplot.java
字号:
* @param headH the data point height
* @param headW the data point width
*/
protected void drawRadarPoly(Graphics2D g2,
Rectangle2D plotArea,
Point2D centre,
PlotRenderingInfo info,
int series, int catCount,
double headH, double headW) {
Polygon polygon = new Polygon();
// plot the data...
for (int cat = 0; cat < catCount; cat++) {
Number dataValue = getPlotValue(series, cat);
if (dataValue != null) {
double value = dataValue.doubleValue();
if (value > 0) { // draw the polygon series...
// Finds our starting angle from the centre for this axis
double angle = getStartAngle()
+ (getDirection().getFactor() * cat * 360 / catCount);
// The following angle calc will ensure there isn't a top
// vertical axis - this may be useful if you don't want any
// given criteria to 'appear' move important than the
// others..
// + (getDirection().getFactor()
// * (cat + 0.5) * 360 / catCount);
// find the point at the appropriate distance end point
// along the axis/angle identified above and add it to the
// polygon
Point2D point = getWebPoint(
plotArea, angle, value / this.maxValue
);
polygon.addPoint((int) point.getX(), (int) point.getY());
// put an elipse at the point being plotted..
// TODO add tooltip/URL capability to this elipse
Paint paint = getSeriesPaint(series);
Paint outlinePaint = getSeriesOutlinePaint(series);
Stroke outlineStroke = getSeriesOutlineStroke(series);
Ellipse2D head = new Ellipse2D.Double(
point.getX() - headW / 2,
point.getY() - headH / 2,
headW, headH
);
g2.setPaint(paint);
g2.fill(head);
g2.setStroke(outlineStroke);
g2.setPaint(outlinePaint);
g2.draw(head);
// then draw the axis and category label, but only on the
// first time through.....
if (series == 0) {
Point2D endPoint = getWebPoint(plotArea, angle, 1);
// 1 = end of axis
Line2D line = new Line2D.Double(centre, endPoint);
g2.draw(line);
drawLabel(
g2, plotArea, value, cat, angle, 360.0 / catCount
);
}
}
}
}
// Plot the polygon
Paint paint = getSeriesPaint(series);
g2.setPaint(paint);
g2.draw(polygon);
// Lastly, fill the web polygon if this is required
if (this.webFilled) {
g2.setComposite(
AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f)
);
g2.fill(polygon);
g2.setComposite(
AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, getForegroundAlpha()
)
);
}
}
/**
* Returns the value to be plotted at the interseries of the
* series and the category. This allows us to plot
* BY_ROW or BY_COLUMN which basically is just reversing the
* definition of the categories and data series being plotted
*
* @param series the series to be plotted
* @param cat the category within the series to be plotted
*
* @return The value to be plotted
*/
Number getPlotValue(int series, int cat) {
Number value = null;
if (this.dataExtractOrder == TableOrder.BY_ROW) {
value = this.dataset.getValue(series, cat);
}
else if (this.dataExtractOrder == TableOrder.BY_COLUMN) {
value = this.dataset.getValue(cat, series);
}
return value;
}
/**
* Draws the label for one axis.
*
* @param g2 the graphics device.
* @param plotArea the plot area
* @param value the value of the label.
* @param cat the category (zero-based index).
* @param startAngle the starting angle.
* @param extent the extent of the arc.
*/
protected void drawLabel(Graphics2D g2, Rectangle2D plotArea, double value,
int cat, double startAngle, double extent) {
FontRenderContext frc = g2.getFontRenderContext();
String label = null;
if (this.dataExtractOrder == TableOrder.BY_ROW) {
// if series are in rows, then the categories are the column keys
label = this.labelGenerator.generateColumnLabel(this.dataset, cat);
}
else {
// if series are in columns, then the categories are the row keys
label = this.labelGenerator.generateRowLabel(this.dataset, cat);
}
Rectangle2D labelBounds = getLabelFont().getStringBounds(label, frc);
LineMetrics lm = getLabelFont().getLineMetrics(label, frc);
double ascent = lm.getAscent();
Point2D labelLocation = calculateLabelLocation(
labelBounds, ascent, plotArea, startAngle
);
Composite saveComposite = g2.getComposite();
g2.setComposite(
AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f)
);
g2.setPaint(getLabelPaint());
g2.setFont(getLabelFont());
g2.drawString(
label, (float) labelLocation.getX(), (float) labelLocation.getY()
);
g2.setComposite(saveComposite);
}
/**
* Returns the location for a label
*
* @param labelBounds the label bounds.
* @param ascent the ascent (height of font).
* @param plotArea the plot area
* @param startAngle the start angle for the pie series.
*
* @return The location for a label.
*/
protected Point2D calculateLabelLocation(Rectangle2D labelBounds,
double ascent,
Rectangle2D plotArea,
double startAngle)
{
Arc2D arc1 = new Arc2D.Double(plotArea, startAngle, 0, Arc2D.OPEN);
Point2D point1 = arc1.getEndPoint();
double deltaX = -(point1.getX() - plotArea.getCenterX())
* this.axisLabelGap;
double deltaY = -(point1.getY() - plotArea.getCenterY())
* this.axisLabelGap;
double labelX = point1.getX() - deltaX;
double labelY = point1.getY() - deltaY;
if (labelX < plotArea.getCenterX()) {
labelX -= labelBounds.getWidth();
}
if (labelX == plotArea.getCenterX()) {
labelX -= labelBounds.getWidth() / 2;
}
if (labelY > plotArea.getCenterY()) {
labelY += ascent;
}
return new Point2D.Double(labelX, labelY);
}
/**
* Tests this plot for equality with an arbitrary object.
*
* @param obj the object (<code>null</code> permitted).
*
* @return A boolean.
*/
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof SpiderWebPlot)) {
return false;
}
if (!super.equals(obj)) {
return false;
}
SpiderWebPlot that = (SpiderWebPlot) obj;
if (!this.dataExtractOrder.equals(that.dataExtractOrder)) {
return false;
}
if (this.headPercent != that.headPercent) {
return false;
}
if (this.interiorGap != that.interiorGap) {
return false;
}
if (this.startAngle != that.startAngle) {
return false;
}
if (!this.direction.equals(that.direction)) {
return false;
}
if (this.maxValue != that.maxValue) {
return false;
}
if (this.webFilled != that.webFilled) {
return false;
}
if (!ShapeUtilities.equal(this.legendItemShape, that.legendItemShape)) {
return false;
}
if (!PaintUtilities.equal(this.seriesPaint, that.seriesPaint)) {
return false;
}
if (!this.seriesPaintList.equals(that.seriesPaintList)) {
return false;
}
if (!PaintUtilities.equal(this.baseSeriesPaint, that.baseSeriesPaint)) {
return false;
}
if (!PaintUtilities.equal(this.seriesOutlinePaint,
that.seriesOutlinePaint)) {
return false;
}
if (!this.seriesOutlinePaintList.equals(that.seriesOutlinePaintList)) {
return false;
}
if (!PaintUtilities.equal(this.baseSeriesOutlinePaint,
that.baseSeriesOutlinePaint)) {
return false;
}
if (!ObjectUtilities.equal(this.seriesOutlineStroke,
that.seriesOutlineStroke)) {
return false;
}
if (!this.seriesOutlineStrokeList.equals(
that.seriesOutlineStrokeList)) {
return false;
}
if (!this.baseSeriesOutlineStroke.equals(
that.baseSeriesOutlineStroke)) {
return false;
}
if (!this.labelFont.equals(that.labelFont)) {
return false;
}
if (!PaintUtilities.equal(this.labelPaint, that.labelPaint)) {
return false;
}
if (!this.labelGenerator.equals(that.labelGenerator)) {
return false;
}
return true;
}
/**
* Provides serialization support.
*
* @param stream
* the output stream.
*
* @throws IOException
* if there is an I/O error.
*/
private void writeObject(ObjectOutputStream stream) throws IOException {
stream.defaultWriteObject();
SerialUtilities.writeShape(this.legendItemShape, stream);
SerialUtilities.writePaint(this.seriesPaint, stream);
SerialUtilities.writePaint(this.baseSeriesPaint, stream);
SerialUtilities.writePaint(this.seriesOutlinePaint, stream);
SerialUtilities.writePaint(this.baseSeriesOutlinePaint, stream);
SerialUtilities.writeStroke(this.seriesOutlineStroke, stream);
SerialUtilities.writeStroke(this.baseSeriesOutlineStroke, stream);
SerialUtilities.writePaint(this.labelPaint, stream);
}
/**
* Provides serialization support.
*
* @param stream
* the input stream.
*
* @throws IOException
* if there is an I/O error.
* @throws ClassNotFoundException
* if there is a classpath problem.
*/
private void readObject(ObjectInputStream stream) throws IOException,
ClassNotFoundException {
stream.defaultReadObject();
this.legendItemShape = SerialUtilities.readShape(stream);
this.seriesPaint = SerialUtilities.readPaint(stream);
this.baseSeriesPaint = SerialUtilities.readPaint(stream);
this.seriesOutlinePaint = SerialUtilities.readPaint(stream);
this.baseSeriesOutlinePaint = SerialUtilities.readPaint(stream);
this.seriesOutlineStroke = SerialUtilities.readStroke(stream);
this.baseSeriesOutlineStroke = SerialUtilities.readStroke(stream);
this.labelPaint = SerialUtilities.readPaint(stream);
if (dataset != null) {
dataset.addChangeListener(this);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -