meterplot.java

来自「JfreeChart 常用图表例子」· Java 代码 · 共 1,194 行 · 第 1/3 页

JAVA
1,194
字号
            notifyListeners(new PlotChangeEvent(this));        }    }    /**     * Returns the tick label format.     *      * @return The tick label format (never <code>null</code>).     */    public NumberFormat getTickLabelFormat() {        return this.tickLabelFormat;        }        /**     * Sets the format for the tick labels and sends a {@link PlotChangeEvent}      * to all registered listeners.     *      * @param format  the format (<code>null</code> not permitted).     */    public void setTickLabelFormat(NumberFormat format) {        if (format == null) {            throw new IllegalArgumentException("Null 'format' argument.");           }        this.tickLabelFormat = format;        notifyListeners(new PlotChangeEvent(this));    }        /**     * Returns the font for the value label.     *     * @return The font (never <code>null</code>).     */    public Font getValueFont() {        return this.valueFont;    }    /**     * Sets the font used to display the value label and sends a      * {@link PlotChangeEvent} to all registered listeners.     *     * @param font  the font (<code>null</code> not permitted).     */    public void setValueFont(Font font) {        if (font == null) {            throw new IllegalArgumentException("Null 'font' argument.");        }        this.valueFont = font;        notifyListeners(new PlotChangeEvent(this));    }    /**     * Returns the paint for the value label.     *     * @return The paint (never <code>null</code>).     */    public Paint getValuePaint() {        return this.valuePaint;    }    /**     * Sets the paint used to display the value label and sends a      * {@link PlotChangeEvent} to all registered listeners.     *     * @param paint  the paint (<code>null</code> not permitted).     */    public void setValuePaint(Paint paint) {        if (paint == null) {            throw new IllegalArgumentException("Null 'paint' argument.");        }        this.valuePaint = paint;        notifyListeners(new PlotChangeEvent(this));    }    /**     * Returns the paint for the dial background.     *     * @return The paint (possibly <code>null</code>).     */    public Paint getDialBackgroundPaint() {        return this.dialBackgroundPaint;    }    /**     * Sets the paint used to fill the dial background.  Set this to      * <code>null</code> for no background.     *     * @param paint  the paint (<code>null</code> permitted).     */    public void setDialBackgroundPaint(Paint paint) {        this.dialBackgroundPaint = paint;        notifyListeners(new PlotChangeEvent(this));    }    /**     * Returns a flag that controls whether or not a rectangular border is      * drawn around the plot area.     *     * @return A flag.     */    public boolean getDrawBorder() {        return this.drawBorder;    }    /**     * Sets the flag that controls whether or not a rectangular border is drawn     * around the plot area and sends a {@link PlotChangeEvent} to all      * registered listeners.     *     * @param draw  the flag.     */    public void setDrawBorder(boolean draw) {        // TODO: fix output when this flag is set to true        this.drawBorder = draw;        notifyListeners(new PlotChangeEvent(this));    }    /**     * Returns the dial outline paint.     *     * @return The paint.     */    public Paint getDialOutlinePaint() {        return this.dialOutlinePaint;    }    /**     * Sets the dial outline paint and sends a {@link PlotChangeEvent} to all     * registered listeners.     *     * @param paint  the paint.     */    public void setDialOutlinePaint(Paint paint) {        this.dialOutlinePaint = paint;        notifyListeners(new PlotChangeEvent(this));            }    /**     * Returns the dataset for the plot.     *      * @return The dataset (possibly <code>null</code>).     */    public ValueDataset getDataset() {        return this.dataset;    }        /**     * Sets the dataset for the plot, replacing the existing dataset if there      * is one, and triggers a {@link PlotChangeEvent}.     *      * @param dataset  the dataset (<code>null</code> permitted).     */    public void setDataset(ValueDataset dataset) {                // if there is an existing dataset, remove the plot from the list of         // change listeners...        ValueDataset existing = this.dataset;        if (existing != null) {            existing.removeChangeListener(this);        }        // set the new dataset, and register the chart as a change listener...        this.dataset = dataset;        if (dataset != null) {            setDatasetGroup(dataset.getGroup());            dataset.addChangeListener(this);        }        // send a dataset change event to self...        DatasetChangeEvent event = new DatasetChangeEvent(this, dataset);        datasetChanged(event);            }    /**     * Returns an unmodifiable list of the intervals for the plot.     *      * @return A list.     */    public List getIntervals() {        return Collections.unmodifiableList(this.intervals);    }        /**     * Adds an interval and sends a {@link PlotChangeEvent} to all registered     * listeners.     *      * @param interval  the interval (<code>null</code> not permitted).     */    public void addInterval(MeterInterval interval) {        if (interval == null) {            throw new IllegalArgumentException("Null 'interval' argument.");        }        this.intervals.add(interval);        notifyListeners(new PlotChangeEvent(this));    }        /**     * Clears the intervals for the plot and sends a {@link PlotChangeEvent} to     * all registered listeners.      */    public void clearIntervals() {        this.intervals.clear();        notifyListeners(new PlotChangeEvent(this));    }        /**     * Returns an item for each interval.     *     * @return A collection of legend items.     */    public LegendItemCollection getLegendItems() {        LegendItemCollection result = new LegendItemCollection();        Iterator iterator = this.intervals.iterator();        while (iterator.hasNext()) {            MeterInterval mi = (MeterInterval) iterator.next();            LegendItem item = new LegendItem(                mi.getLabel(), mi.getLabel(), null, null,                 new Rectangle2D.Double(-4.0, -4.0, 8.0, 8.0),                 mi.getOutlinePaint()            );            result.add(item);        }        return result;    }    /**     * Draws the plot on a Java 2D graphics device (such as the screen or a      * printer).     *     * @param g2  the graphics device.     * @param area  the area within which the plot should be drawn.     * @param anchor  the anchor point (<code>null</code> permitted).     * @param parentState  the state from the parent plot, if there is one.     * @param info  collects info about the drawing.     */    public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor,                     PlotState parentState,                     PlotRenderingInfo info) {        if (info != null) {            info.setPlotArea(area);        }        // adjust for insets...        RectangleInsets insets = getInsets();        insets.trim(area);        area.setRect(            area.getX() + 4, area.getY() + 4,            area.getWidth() - 8, area.getHeight() - 8        );        // draw the background        if (this.drawBorder) {            drawBackground(g2, area);        }        // adjust the plot area by the interior spacing value        double gapHorizontal = (2 * DEFAULT_BORDER_SIZE);        double gapVertical = (2 * DEFAULT_BORDER_SIZE);        double meterX = area.getX() + gapHorizontal / 2;        double meterY = area.getY() + gapVertical / 2;        double meterW = area.getWidth() - gapHorizontal;        double meterH = area.getHeight() - gapVertical            + ((this.meterAngle <= 180) && (this.shape != DialShape.CIRCLE)                ? area.getHeight() / 1.25 : 0);        double min = Math.min(meterW, meterH) / 2;        meterX = (meterX + meterX + meterW) / 2 - min;        meterY = (meterY + meterY + meterH) / 2 - min;        meterW = 2 * min;        meterH = 2 * min;        Rectangle2D meterArea = new Rectangle2D.Double(            meterX, meterY, meterW, meterH        );        Rectangle2D.Double originalArea = new Rectangle2D.Double(            meterArea.getX() - 4, meterArea.getY() - 4,             meterArea.getWidth() + 8, meterArea.getHeight() + 8        );        double meterMiddleX = meterArea.getCenterX();        double meterMiddleY = meterArea.getCenterY();        // plot the data (unless the dataset is null)...        ValueDataset data = getDataset();        if (data != null) {            double dataMin = this.range.getLowerBound();            double dataMax = this.range.getUpperBound();            Shape savedClip = g2.getClip();            g2.clip(originalArea);            Composite originalComposite = g2.getComposite();            g2.setComposite(AlphaComposite.getInstance(                AlphaComposite.SRC_OVER, getForegroundAlpha())            );            if (this.dialBackgroundPaint != null) {                fillArc(                    g2, originalArea, dataMin, dataMax,                     this.dialBackgroundPaint, true                );            }            drawTicks(g2, meterArea, dataMin, dataMax);            drawArcForInterval(                g2, meterArea,                 new MeterInterval(                    "", this.range, this.dialOutlinePaint,                     new BasicStroke(1.0f), null                )            );                        Iterator iterator = this.intervals.iterator();            while (iterator.hasNext()) {                MeterInterval interval = (MeterInterval) iterator.next();                drawArcForInterval(g2, meterArea, interval);            }            Number n = data.getValue();            if (n != null) {                double value = n.doubleValue();                drawTick(                    g2, meterArea, value, true, this.valuePaint, true,                     getUnits()                );                  if (this.range.contains(value)) {                    g2.setPaint(this.needlePaint);                    g2.setStroke(new BasicStroke(2.0f));                    double radius = (meterArea.getWidth() / 2)                                     + DEFAULT_BORDER_SIZE + 15;                    double valueAngle = valueToAngle(value);                    double valueP1 = meterMiddleX                         + (radius * Math.cos(Math.PI * (valueAngle / 180)));                    double valueP2 = meterMiddleY                         - (radius * Math.sin(Math.PI * (valueAngle / 180)));                    Polygon arrow = new Polygon();                    if ((valueAngle > 135 && valueAngle < 225)                        || (valueAngle < 45 && valueAngle > -45)) {                        double valueP3 = (meterMiddleY                                 - DEFAULT_CIRCLE_SIZE / 4);                        double valueP4 = (meterMiddleY                                 + DEFAULT_CIRCLE_SIZE / 4);                        arrow.addPoint((int) meterMiddleX, (int) valueP3);                        arrow.addPoint((int) meterMiddleX, (int) valueP4);                     }                    else {                        arrow.addPoint(                            (int) (meterMiddleX - DEFAULT_CIRCLE_SIZE / 4),                            (int) meterMiddleY                        );                        arrow.addPoint(                            (int) (meterMiddleX + DEFAULT_CIRCLE_SIZE / 4),                            (int) meterMiddleY                        );                    }                    arrow.addPoint((int) valueP1, (int) valueP2);                    g2.fill(arrow);                    Ellipse2D circle = new Ellipse2D.Double(                        meterMiddleX - DEFAULT_CIRCLE_SIZE / 2,                        meterMiddleY - DEFAULT_CIRCLE_SIZE / 2,                        DEFAULT_CIRCLE_SIZE, DEFAULT_CIRCLE_SIZE                    );                    g2.fill(circle);                }            }                            g2.clip(savedClip);            g2.setComposite(originalComposite);        }        if (this.drawBorder) {            drawOutline(g2, area);        }    }    /**     * Draws the arc to represent an interval.     *     * @param g2  the graphics device.     * @param meterArea  the drawing area.     * @param interval  the interval.     */    protected void drawArcForInterval(Graphics2D g2, Rectangle2D meterArea,                                       MeterInterval interval) {        double minValue = interval.getRange().getLowerBound();        double maxValue = interval.getRange().getUpperBound();        Paint outlinePaint = interval.getOutlinePaint();        Stroke outlineStroke = interval.getOutlineStroke();

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?