thermometerplot.java
来自「JfreeChart 常用图表例子」· Java 代码 · 共 1,475 行 · 第 1/3 页
JAVA
1,475 行
); Line2D line = new Line2D.Double(x, y, x + 10, y); g2.setPaint(this.subrangePaint[WARNING]); g2.draw(line); } // draw start of critical range value = this.subrangeInfo[CRITICAL][RANGE_LOW]; if (range.contains(value)) { double x = midX + COLUMN_RADIUS + 2; double y = this.rangeAxis.valueToJava2D( value, dataArea, RectangleEdge.LEFT ); Line2D line = new Line2D.Double(x, y, x + 10, y); g2.setPaint(this.subrangePaint[CRITICAL]); g2.draw(line); } } // draw the axis... if ((this.rangeAxis != null) && (this.axisLocation != NONE)) { int drawWidth = AXIS_GAP; if (this.showValueLines) { drawWidth += COLUMN_DIAMETER; } Rectangle2D drawArea; double cursor = 0; switch (this.axisLocation) { case RIGHT: cursor = midX + COLUMN_RADIUS; drawArea = new Rectangle2D.Double( cursor, stemTop, drawWidth, (stemBottom - stemTop + 1) ); this.rangeAxis.draw( g2, cursor, area, drawArea, RectangleEdge.RIGHT, null ); break; case LEFT: default: //cursor = midX - COLUMN_RADIUS - AXIS_GAP; cursor = midX - COLUMN_RADIUS; drawArea = new Rectangle2D.Double( cursor, stemTop, drawWidth, (stemBottom - stemTop + 1) ); this.rangeAxis.draw( g2, cursor, area, drawArea, RectangleEdge.LEFT, null ); break; } } // draw text value on screen g2.setFont(this.valueFont); g2.setPaint(this.valuePaint); metrics = g2.getFontMetrics(); switch (this.valueLocation) { case RIGHT: g2.drawString( this.valueFormat.format(current), midX + COLUMN_RADIUS + GAP_RADIUS, midY ); break; case LEFT: String valueString = this.valueFormat.format(current); int stringWidth = metrics.stringWidth(valueString); g2.drawString( valueString, midX - COLUMN_RADIUS - GAP_RADIUS - stringWidth, midY ); break; case BULB: temp = this.valueFormat.format(current); i = metrics.stringWidth(temp) / 2; g2.drawString( temp, midX - i, stemBottom + BULB_RADIUS + GAP_RADIUS ); break; default: } /***/ } g2.setPaint(this.thermometerPaint); g2.setFont(this.valueFont); // draw units indicator metrics = g2.getFontMetrics(); int tickX1 = midX - COLUMN_RADIUS - GAP_DIAMETER - metrics.stringWidth(UNITS[this.units]); if (tickX1 > area.getMinX()) { g2.drawString( UNITS[this.units], tickX1, (int) (area.getMinY() + 20) ); } // draw thermometer outline g2.setStroke(this.thermometerStroke); g2.draw(outerThermometer); g2.draw(innerThermometer); drawOutline(g2, area); } /** * A zoom method that does nothing. Plots are required to support the * zoom operation. In the case of a thermometer chart, it doesn't make * sense to zoom in or out, so the method is empty. * * @param percent the zoom percentage. */ public void zoom(double percent) { // intentionally blank } /** * Returns a short string describing the type of plot. * * @return A short string describing the type of plot. */ public String getPlotType() { return localizationResources.getString("Thermometer_Plot"); } /** * Checks to see if a new value means the axis range needs adjusting. * * @param event the dataset change event. */ public void datasetChanged(DatasetChangeEvent event) { Number vn = this.dataset.getValue(); if (vn != null) { double value = vn.doubleValue(); if (inSubrange(NORMAL, value)) { this.subrange = NORMAL; } else if (inSubrange(WARNING, value)) { this.subrange = WARNING; } else if (inSubrange(CRITICAL, value)) { this.subrange = CRITICAL; } else { this.subrange = -1; } setAxisRange(); } super.datasetChanged(event); } /** * Returns the minimum value in either the domain or the range, whichever * is displayed against the vertical axis for the particular type of plot * implementing this interface. * * @return The minimum value in either the domain or the range. */ public Number getMinimumVerticalDataValue() { return new Double(this.lowerBound); } /** * Returns the maximum value in either the domain or the range, whichever * is displayed against the vertical axis for the particular type of plot * implementing this interface. * * @return The maximum value in either the domain or the range */ public Number getMaximumVerticalDataValue() { return new Double(this.upperBound); } /** * Returns the data range. * * @param axis the axis. * * @return The range of data displayed. */ public Range getDataRange(ValueAxis axis) { return new Range(this.lowerBound, this.upperBound); } /** * Sets the axis range to the current values in the rangeInfo array. */ protected void setAxisRange() { if ((this.subrange >= 0) && (this.followDataInSubranges)) { this.rangeAxis.setRange( new Range(this.subrangeInfo[this.subrange][DISPLAY_LOW], this.subrangeInfo[this.subrange][DISPLAY_HIGH]) ); } else { this.rangeAxis.setRange(this.lowerBound, this.upperBound); } } /** * Returns the legend items for the plot. * * @return <code>null</code>. */ public LegendItemCollection getLegendItems() { return null; } /** * Returns the orientation of the plot. * * @return The orientation (always {@link PlotOrientation#VERTICAL}). */ public PlotOrientation getOrientation() { return PlotOrientation.VERTICAL; } /** * Determine whether a number is valid and finite. * * @param d the number to be tested. * * @return <code>true</code> if the number is valid and finite, and * <code>false</code> otherwise. */ protected static boolean isValidNumber(double d) { return (!(Double.isNaN(d) || Double.isInfinite(d))); } /** * Returns true if the value is in the specified range, and false otherwise. * * @param subrange the subrange. * @param value the value to check. * * @return A boolean. */ private boolean inSubrange(int subrange, double value) { return (value > this.subrangeInfo[subrange][RANGE_LOW] && value <= this.subrangeInfo[subrange][RANGE_HIGH]); } /** * Returns the mercury paint corresponding to the current data value. * * @return The paint. */ private Paint getCurrentPaint() { Paint result = this.mercuryPaint; if (this.useSubrangePaint) { double value = this.dataset.getValue().doubleValue(); if (inSubrange(NORMAL, value)) { result = this.subrangePaint[NORMAL]; } else if (inSubrange(WARNING, value)) { result = this.subrangePaint[WARNING]; } else if (inSubrange(CRITICAL, value)) { result = this.subrangePaint[CRITICAL]; } } return result; } /** * Tests this plot for equality with another object. * * @param obj the object. * * @return <code>true</code> or <code>false</code>. */ public boolean equals(Object obj) { if (obj == this) { return true; } if (!(obj instanceof ThermometerPlot)) { return false; } ThermometerPlot that = (ThermometerPlot) obj; if (!super.equals(obj)) { return false; } if (!ObjectUtilities.equal(this.dataset, that.dataset)) { return false; } if (!ObjectUtilities.equal(this.rangeAxis, that.rangeAxis)) { return false; } if (this.lowerBound != that.lowerBound) { return false; } if (this.upperBound != that.upperBound) { return false; } if (!ObjectUtilities.equal(this.padding, that.padding)) { return false; } if (!ObjectUtilities.equal( this.thermometerStroke, that.thermometerStroke )) { return false; } if (!ObjectUtilities.equal( this.thermometerPaint, that.thermometerPaint )) { return false; } if (this.units != that.units) { return false; } if (this.valueLocation != that.valueLocation) { return false; } if (!ObjectUtilities.equal(this.valueFont, that.valueFont)) { return false; } if (!ObjectUtilities.equal(this.valuePaint, that.valuePaint)) { return false; } if (!ObjectUtilities.equal(this.valueFormat, that.valueFormat)) { return false; } if (!ObjectUtilities.equal(this.mercuryPaint, that.mercuryPaint)) { return false; } if (this.showValueLines != that.showValueLines) { return false; } if (this.subrange != that.subrange) { return false; } if (this.followDataInSubranges != that.followDataInSubranges) { return false; } if (this.useSubrangePaint != that.useSubrangePaint) { return false; } return true; } /** * Returns a clone of the plot. * * @return A clone. * * @throws CloneNotSupportedException if the plot cannot be cloned. */ public Object clone() throws CloneNotSupportedException { ThermometerPlot clone = (ThermometerPlot) super.clone(); if (clone.dataset != null) { clone.dataset.addChangeListener(clone); } clone.rangeAxis = (ValueAxis) ObjectUtilities.clone(this.rangeAxis); if (clone.rangeAxis != null) { clone.rangeAxis.setPlot(clone); clone.rangeAxis.addChangeListener(clone); } clone.valueFormat = (NumberFormat) this.valueFormat.clone(); clone.subrangePaint = (Paint[]) this.subrangePaint.clone(); return clone; } /** * 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.writeStroke(this.thermometerStroke, stream); SerialUtilities.writePaint(this.thermometerPaint, stream); SerialUtilities.writePaint(this.valuePaint, stream); SerialUtilities.writePaint(this.mercuryPaint, stream); SerialUtilities.writeStroke(this.subrangeIndicatorStroke, stream); SerialUtilities.writeStroke(this.rangeIndicatorStroke, 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.thermometerStroke = SerialUtilities.readStroke(stream); this.thermometerPaint = SerialUtilities.readPaint(stream); this.valuePaint = SerialUtilities.readPaint(stream); this.mercuryPaint = SerialUtilities.readPaint(stream); this.subrangeIndicatorStroke = SerialUtilities.readStroke(stream); this.rangeIndicatorStroke = SerialUtilities.readStroke(stream); if (this.rangeAxis != null) { this.rangeAxis.addChangeListener(this); } } /** * Multiplies the range on the domain axis/axes by the specified factor. * * @param factor the zoom factor. * @param state the plot state. * @param source the source point. */ public void zoomDomainAxes(double factor, PlotRenderingInfo state, Point2D source) { // TODO: to be implemented. } /** * Multiplies the range on the range axis/axes by the specified factor. * * @param factor the zoom factor. * @param state the plot state. * @param source the source point. */ public void zoomRangeAxes(double factor, PlotRenderingInfo state, Point2D source) { this.rangeAxis.resizeRange(factor); } /** * This method does nothing. * * @param lowerPercent the lower percent. * @param upperPercent the upper percent. * @param state the plot state. * @param source the source point. */ public void zoomDomainAxes(double lowerPercent, double upperPercent, PlotRenderingInfo state, Point2D source) { // no domain axis to zoom } /** * Zooms the range axes. * * @param lowerPercent the lower percent. * @param upperPercent the upper percent. * @param state the plot state. * @param source the source point. */ public void zoomRangeAxes(double lowerPercent, double upperPercent, PlotRenderingInfo state, Point2D source) { this.rangeAxis.zoomRange(lowerPercent, upperPercent); } /** * Returns <code>false</code>. * * @return A boolean. */ public boolean isDomainZoomable() { return false; } /** * Returns <code>true</code>. * * @return A boolean. */ public boolean isRangeZoomable() { return true; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?