⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ganttrenderer.java

📁 jfreechart安装程序和使用说明
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* ===========================================================
 * JFreeChart : a free chart library for the Java(tm) platform
 * ===========================================================
 *
 * (C) Copyright 2000-2004, 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., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 *
 * ------------------
 * GanttRenderer.java
 * ------------------
 * (C) Copyright 2003, 2004, by Object Refinery Limited.
 *
 * Original Author:  David Gilbert (for Object Refinery Limited);
 * Contributor(s):   -;
 *
 * $Id: GanttRenderer.java,v 1.1 2004/08/31 14:47:48 mungady Exp $
 *
 * Changes
 * -------
 * 16-Sep-2003 : Version 1 (DG);
 * 23-Sep-2003 : Fixed Checkstyle issues (DG);
 * 21-Oct-2003 : Bar width moved into CategoryItemRendererState (DG);
 * 03-Feb-2004 : Added get/set methods for attributes (DG);
 * 12-Aug-2004 : Fixed rendering problem with maxBarWidth attribute (DG);
 * 
 */

package org.jfree.chart.renderer.category;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Stroke;
import java.awt.geom.Rectangle2D;

import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.CategoryItemEntity;
import org.jfree.chart.entity.EntityCollection;
import org.jfree.chart.event.RendererChangeEvent;
import org.jfree.chart.labels.CategoryLabelGenerator;
import org.jfree.chart.labels.CategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.gantt.GanttCategoryDataset;
import org.jfree.ui.RectangleEdge;

/**
 * A renderer for simple Gantt charts.
 */
public class GanttRenderer extends IntervalBarRenderer {
    
    /** The paint for displaying the percentage complete. */
    private Paint completePaint;
    
    /** The paint for displaying the incomplete part of a task. */
    private Paint incompletePaint;
    
    /** 
     * Controls the starting edge of the progress indicator (expressed as a percentage of the
     * overall bar width).
     */
    private double startPercent;
    
    /**
     * Controls the ending edge of the progress indicator (expressed as a percentage of the
     * overall bar width). 
     */
    private double endPercent;
    
    /**
     * Creates a new renderer.
     *
     */
    public GanttRenderer() {
        super();
        this.completePaint = Color.green;
        this.incompletePaint = Color.red;
        this.startPercent = 0.35;
        this.endPercent = 0.65;
    }
    
    /**
     * Returns the paint used to show the percentage complete.
     * 
     * @return the paint (never <code>null</code>.
     */
    public Paint getCompletePaint() {
        return this.completePaint;
    }
    
    /**
     * Sets the paint used to show the percentage complete and sends a {@link RendererChangeEvent}
     * to all registered listeners.
     * 
     * @param paint  the paint (<code>null</code> not permitted).
     */
    public void setCompletePaint(Paint paint) {
        if (paint == null) {
            throw new IllegalArgumentException("Null paint not permitted.");
        }
        this.completePaint = paint;
        notifyListeners(new RendererChangeEvent(this));
    }
    
    /**
     * Returns the paint used to show the percentage incomplete.
     * 
     * @return the paint (never <code>null</code>).
     */
    public Paint getIncompletePaint() {
        return this.incompletePaint;
    }
    
    /**
     * Sets the paint used to show the percentage incomplete and sends a {@link RendererChangeEvent}
     * to all registered listeners.
     * 
     * @param paint  the paint (<code>null</code> not permitted).
     */
    public void setIncompletePaint(Paint paint) {
        if (paint == null) {
            throw new IllegalArgumentException("Null paint not permitted.");
        }
        this.incompletePaint = paint;
        notifyListeners(new RendererChangeEvent(this));
    }
    
    /**
     * Returns the position of the start of the progress indicator, as a percentage of the bar
     * width.
     * 
     * @return The start percent.
     */
    public double getStartPercent() {
        return this.startPercent;
    }
    
    /**
     * Sets the position of the start of the progress indicator, as a percentage of the bar
     * width.
     * 
     * @param percent  the percent.
     */
    public void setStartPercent(double percent) {
        this.startPercent = percent;
        notifyListeners(new RendererChangeEvent(this));
    }
    
    /**
     * Returns the position of the end of the progress indicator, as a percentage of the bar
     * width.
     * 
     * @return The end percent.
     */
    public double getEndPercent() {
        return this.endPercent;
    }
    
    /**
     * Sets the position of the end of the progress indicator, as a percentage of the bar
     * width.
     * 
     * @param percent  the percent.
     */
    public void setEndPercent(double percent) {
        this.endPercent = percent;
        notifyListeners(new RendererChangeEvent(this));
    }
    
    /**
     * Draws the bar for a single (series, category) data item.
     *
     * @param g2  the graphics device.
     * @param state  the renderer state.
     * @param dataArea  the data area.
     * @param plot  the plot.
     * @param domainAxis  the domain axis.
     * @param rangeAxis  the range axis.
     * @param dataset  the dataset.
     * @param row  the row index (zero-based).
     * @param column  the column index (zero-based).
     */
    public void drawItem(Graphics2D g2,
                         CategoryItemRendererState state,
                         Rectangle2D dataArea,
                         CategoryPlot plot,
                         CategoryAxis domainAxis,
                         ValueAxis rangeAxis,
                         CategoryDataset dataset,
                         int row,
                         int column) {

         if (dataset instanceof GanttCategoryDataset) {
             GanttCategoryDataset gcd = (GanttCategoryDataset) dataset;
             drawTasks(g2, state, dataArea, plot, domainAxis, rangeAxis, gcd, row, column);
         }
         else {  // let the superclass handle it...
             super.drawItem(g2, state, dataArea, plot, domainAxis, rangeAxis, dataset, row, column);
         }
 
     }
                          
    /**
     * Draws the tasks/subtasks for one item.
     *
     * @param g2  the graphics device.
     * @param state  the renderer state.
     * @param dataArea  the data plot area.
     * @param plot  the plot.
     * @param domainAxis  the domain axis.
     * @param rangeAxis  the range axis.
     * @param dataset  the data.
     * @param row  the row index (zero-based).
     * @param column  the column index (zero-based).
     */
    protected void drawTasks(Graphics2D g2,
                             CategoryItemRendererState state,
                             Rectangle2D dataArea,
                             CategoryPlot plot,
                             CategoryAxis domainAxis,
                             ValueAxis rangeAxis,
                             GanttCategoryDataset dataset,
                             int row,
                             int column) {

        int count = dataset.getSubIntervalCount(row, column);
        if (count == 0) {
            drawTask(g2, state, dataArea, plot, domainAxis, rangeAxis, dataset, row, column);
        }

        for (int subinterval = 0; subinterval < count; subinterval++) {
            
            RectangleEdge rangeAxisLocation = plot.getRangeAxisEdge();

            // value 0
            Number value0 = dataset.getStartValue(row, column, subinterval);
            if (value0 == null) {
                return;
            }
            double translatedValue0 = rangeAxis.valueToJava2D(
                value0.doubleValue(), dataArea, rangeAxisLocation
            );
    
            // value 1
            Number value1 = dataset.getEndValue(row, column, subinterval);
            if (value1 == null) {
                return;

⌨️ 快捷键说明

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