1

我可以XYPlot在 JFreeChart 中的图表中添加渐变填充吗?

下面的例子:

图像

4

3 回答 3

1

我没有尝试过,但是org.jfree.chart.renderer.xy.StackedXYAreaRenderer有一个setSeriesFillPaint()方法并java.awt.GradientPaint实现了Paint接口。

于 2013-01-18T19:27:58.220 回答
0

我发现这适用于XYAreaRenderer

plot.getRenderer().setSeriesPaint(seriesIndex, new GradientPaint(0.0f, 0.0f, new Color(255, 0, 0), 0.0f, 0.0f, new Color(0, 255, 255)));  
于 2016-12-08T12:45:37.840 回答
0

只需共享工作代码 - 修复其他公开可用代码中的错误。

package gui;

/* ===========================================================
 * JFreeChart : a free chart library for the Java(tm) platform
 * ===========================================================
 *
 * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
 *
 * Project Info:  http://www.jfree.org/jfreechart/index.html
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
 * in the United States and other countries.]
 *
 * ---------------------
 * MyXYAreaRenderer.java
 * ---------------------
 * (C) Copyright 2005, by Object Refinery Limited.
 *
 * Original Author:  David Gilbert (for Object Refinery Limited);
 * Contributor(s):   -;
 *
 * $Id: XYAreaRenderer.java,v 1.12.2.5 2005/12/22 15:53:05 mungady Exp $
 *
 * Changes:
 * --------
 * 22-Dec-2005 : Version 1 (DG);
 *
 *
 */

import model.Globals;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.XYToolTipGenerator;
import org.jfree.chart.plot.CrosshairState;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.PlotRenderingInfo;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYAreaRenderer;
import org.jfree.chart.renderer.xy.XYItemRendererState;
import org.jfree.chart.ui.GradientPaintTransformer;
import org.jfree.chart.ui.StandardGradientPaintTransformer;
import org.jfree.chart.urls.XYURLGenerator;
import org.jfree.data.xy.XYDataset;
import org.jfree.util.ShapeUtilities;

import java.awt.*;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;

/**
 * Custom renderer.
 */
public class GraphAreaRenderer extends XYAreaRenderer {

    /**
     * A state object used by this renderer.
     */
    static class XYAreaRendererState extends XYItemRendererState {

        /**
         * Working storage for the area under one series.
         */
        public Polygon area;

        /**
         * Working line that can be recycled.
         */
        public Line2D line;

        /**
         * Creates a new state.
         *
         * @param info the plot rendering info.
         */
        public XYAreaRendererState(PlotRenderingInfo info) {
            super(info);
            this.area = null;  //new Polygon();
            this.line = new Line2D.Double();
        }

    }

    /**
     * Constructs a new renderer.
     */
    public GraphAreaRenderer() {
        this(AREA);
    }

    /**
     * Constructs a new renderer.
     *
     * @param type the type of the renderer.
     */
    public GraphAreaRenderer(int type) {
        this(type, null, null);
    }

    /**
     * Constructs a new renderer.
     * <p>
     * To specify the type of renderer, use one of the constants: SHAPES, LINES,
     * SHAPES_AND_LINES, AREA or AREA_AND_SHAPES.
     *
     * @param type             the type of renderer.
     * @param toolTipGenerator the tool tip generator to use
     *                         (<code>null</code> permitted).
     * @param urlGenerator     the URL generator (<code>null</code> permitted).
     */
    public GraphAreaRenderer(int type, XYToolTipGenerator toolTipGenerator,
                             XYURLGenerator urlGenerator) {

        super(type, toolTipGenerator, urlGenerator);
    }

    /**
     * Initialises the renderer and returns a state object that should be
     * passed to all subsequent calls to the drawItem() method.
     *
     * @param g2       the graphics device.
     * @param dataArea the area inside the axes.
     * @param plot     the plot.
     * @param data     the data.
     * @param info     an optional info collection object to return data back to
     *                 the caller.
     * @return A state object for use by the renderer.
     */
    public XYItemRendererState initialise(Graphics2D g2, Rectangle2D dataArea,
                                          XYPlot plot, XYDataset data, PlotRenderingInfo info) {
        XYAreaRendererState state = new XYAreaRendererState(info);
        return state;
    }

    /**
     * Draws the visual representation of a single data item.
     *
     * @param g2             the graphics device.
     * @param state          the renderer state.
     * @param dataArea       the area within which the data is being drawn.
     * @param info           collects information about the drawing.
     * @param plot           the plot (can be used to obtain standard color information
     *                       etc).
     * @param domainAxis     the domain axis.
     * @param rangeAxis      the range axis.
     * @param dataset        the dataset.
     * @param series         the series index (zero-based).
     * @param item           the item index (zero-based).
     * @param crosshairState crosshair information for the plot
     *                       (<code>null</code> permitted).
     * @param pass           the pass index.
     */
    public void drawItem(Graphics2D g2, XYItemRendererState state,
                         Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot,
                         ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset,
                         int series, int item, CrosshairState crosshairState, int pass) {

        Globals.LogManager().logDbg(String.format("Item %d ", item));

        if (!getItemVisible(series, item))
            return;

        assert (plot.getOrientation() == PlotOrientation.VERTICAL);

        XYAreaRendererState areaState = (XYAreaRendererState) state;

        // get the data point...
        double x1 = dataset.getXValue(series, item);
        double y1 = dataset.getYValue(series, item);

        if (Double.isNaN(y1))
            y1 = 0.0;

        double transX1 = domainAxis.valueToJava2D(x1, dataArea,
                plot.getDomainAxisEdge());
        double transY1 = rangeAxis.valueToJava2D(y1, dataArea,
                plot.getRangeAxisEdge());

        // get the previous point and the next point so we can calculate a
        // "hot spot" for the area (used by the chart entity)...
        int itemCount = dataset.getItemCount(series);
        double x0 = dataset.getXValue(series, Math.max(item - 1, 0));
        double y0 = dataset.getYValue(series, Math.max(item - 1, 0));

        if (Double.isNaN(y0))
            y0 = 0.0;

        double transX0 = domainAxis.valueToJava2D(x0, dataArea,
                plot.getDomainAxisEdge());
        double transY0 = rangeAxis.valueToJava2D(y0, dataArea,
                plot.getRangeAxisEdge());

        double x2 = dataset.getXValue(series, Math.min(item + 1,
                itemCount - 1));
        double y2 = dataset.getYValue(series, Math.min(item + 1,
                itemCount - 1));

        if (Double.isNaN(y2))
            y2 = 0.0;
/*
        double transX2 = domainAxis.valueToJava2D(x2, dataArea,
                plot.getDomainAxisEdge());
        double transY2 = rangeAxis.valueToJava2D(y2, dataArea,
                plot.getRangeAxisEdge());
 */

        double transZero = rangeAxis.valueToJava2D(0.0, dataArea,
                plot.getRangeAxisEdge());
/*
        Polygon hotspot = null;

        hotspot = new Polygon();
        hotspot.addPoint((int) ((transX0 + transX1) / 2.0),
                (int) transZero);
        hotspot.addPoint((int) ((transX0 + transX1) / 2.0),
                (int) ((transY0 + transY1) / 2.0));
        hotspot.addPoint((int) transX1, (int) transY1);
        hotspot.addPoint((int) ((transX1 + transX2) / 2.0),
                (int) ((transY1 + transY2) / 2.0));
        hotspot.addPoint((int) ((transX1 + transX2) / 2.0),
                (int) transZero);
 */

        if (state.getFirstItemIndex() == item) {  // create a new area polygon for the series
            areaState.area = new Polygon();
            // the first point is (x, 0)
            double zero = rangeAxis.valueToJava2D(0.0, dataArea,
                    plot.getRangeAxisEdge());
            areaState.area.addPoint((int) transX1, (int) zero);
            Globals.LogManager().logDbg(String.format("addPoint1 %d %d", (int) transX1, (int) zero));
        }

        // Add each point to Area (x, y)
        areaState.area.addPoint((int) transX1, (int) transY1);
        Globals.LogManager().logDbg(String.format("addPoint2 %d %d", (int) transX1, (int) transY1));

        PlotOrientation orientation = plot.getOrientation();
        Paint paint = getItemPaint(series, item);
        Stroke stroke = getItemStroke(series, item);
        g2.setPaint(paint);
        g2.setStroke(stroke);

        Shape shape = null;
        if (getPlotShapes()) {
            shape = getItemShape(series, item);
            shape = ShapeUtilities.createTranslatedShape(shape, transX1, transY1);
            g2.draw(shape);
        }

        if (getPlotLines()) {
            if (item > 0) {
                areaState.line.setLine(transX0, transY0, transX1, transY1);
                g2.draw(areaState.line);
            }
        }

        // Check if the item is the last item for the series.
        // and number of items > 0.  We can't draw an area for a single point.
        if (getPlotArea() && item > 0 && state.getLastItemIndex() == item) {

            areaState.area.addPoint((int) transX1, (int) transZero);
            Globals.LogManager().logDbg(String.format("addPoint3 %d %d", (int) transX1, (int) transZero));

            Paint fillPaint = getItemFillPaint(series, item);
            if (fillPaint instanceof GradientPaint) {
                GradientPaint gp = (GradientPaint) fillPaint;
                GradientPaintTransformer t = new StandardGradientPaintTransformer();
                fillPaint = t.transform(gp, areaState.area.getBounds());
            }
            g2.setPaint(fillPaint);
            g2.fill(areaState.area);

            for(int i=0;i<areaState.area.npoints;i++)
                Globals.LogManager().logDbg(String.format("Polygon:  %d %d ", areaState.area.xpoints[i], areaState.area.ypoints[i]));

            // draw an outline around the Area.
            if (isOutline()) {
                g2.setStroke(getItemOutlineStroke(series, item));
                g2.setPaint(getItemOutlinePaint(series, item));
                g2.draw(areaState.area);
            }

        }

        //updateCrosshairValues(  crosshairState, x1, y1, transX1, transY1, orientation );

        // collect entity and tool tip information...
       /* if (state.getInfo() != null) {
            EntityCollection entities = state.getEntityCollection();
            if (entities != null && hotspot != null) {
                String tip = null;
                XYToolTipGenerator generator
                        = getToolTipGenerator(series, item);
                if (generator != null) {
                    tip = generator.generateToolTip(dataset, series, item);
                }
                String url = null;
                if (getURLGenerator() != null) {
                    url = getURLGenerator().generateURL(dataset, series, item);
                }
                XYItemEntity entity = new XYItemEntity(hotspot, dataset,
                        series, item, tip, url);
                entities.add(entity);
            }
        }

        */

    }

}
于 2019-10-24T21:26:35.017 回答