001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022 * USA.
023 *
024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * -----------------
028 * HeatMapUtils.java
029 * -----------------
030 * (C) Copyright 2009-present, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.data.general;
038
039import java.awt.Graphics2D;
040import java.awt.Paint;
041import java.awt.image.BufferedImage;
042import org.jfree.chart.renderer.PaintScale;
043import org.jfree.chart.util.Args;
044import org.jfree.data.xy.XYDataset;
045import org.jfree.data.xy.XYSeries;
046import org.jfree.data.xy.XYSeriesCollection;
047
048/**
049 * A utility class for the {@link HeatMapDataset}.
050 */
051public abstract class HeatMapUtils {
052
053    /**
054     * Returns a dataset containing one series that holds a copy of the (x, z)
055     * data from one row (y-index) of the specified dataset.
056     *
057     * @param dataset  the dataset ({@code null} not permitted).
058     * @param row  the row (y) index.
059     * @param seriesName  the series name/key ({@code null} not permitted).
060     *
061     * @return The dataset.
062     */
063    public static XYDataset extractRowFromHeatMapDataset(HeatMapDataset dataset,
064            int row, Comparable seriesName) {
065        XYSeries series = new XYSeries(seriesName);
066        int cols = dataset.getXSampleCount();
067        for (int c = 0; c < cols; c++) {
068            series.add(dataset.getXValue(c), dataset.getZValue(c, row));
069        }
070        XYSeriesCollection result = new XYSeriesCollection(series);
071        return result;
072    }
073
074    /**
075     * Returns a dataset containing one series that holds a copy of the (y, z)
076     * data from one column (x-index) of the specified dataset.
077     *
078     * @param dataset  the dataset ({@code null} not permitted).
079     * @param column  the column (x) index.
080     * @param seriesName  the series name ({@code null} not permitted).
081     *
082     * @return The dataset.
083     */
084    public static XYDataset extractColumnFromHeatMapDataset(
085            HeatMapDataset dataset, int column, Comparable seriesName) {
086        XYSeries series = new XYSeries(seriesName);
087        int rows = dataset.getYSampleCount();
088        for (int r = 0; r < rows; r++) {
089            series.add(dataset.getYValue(r), dataset.getZValue(column, r));
090        }
091        XYSeriesCollection result = new XYSeriesCollection(series);
092        return result;
093    }
094
095    /**
096     * Creates an image that displays the values from the specified dataset.
097     *
098     * @param dataset  the dataset ({@code null} not permitted).
099     * @param paintScale  the paint scale for the z-values ({@code null}
100     *         not permitted).
101     *
102     * @return A buffered image.
103     */
104    public static BufferedImage createHeatMapImage(HeatMapDataset dataset,
105            PaintScale paintScale) {
106
107        Args.nullNotPermitted(dataset, "dataset");
108        Args.nullNotPermitted(paintScale, "paintScale");
109        int xCount = dataset.getXSampleCount();
110        int yCount = dataset.getYSampleCount();
111        BufferedImage image = new BufferedImage(xCount, yCount,
112                BufferedImage.TYPE_INT_ARGB);
113        Graphics2D g2 = image.createGraphics();
114        for (int xIndex = 0; xIndex < xCount; xIndex++) {
115            for (int yIndex = 0; yIndex < yCount; yIndex++) {
116                double z = dataset.getZValue(xIndex, yIndex);
117                Paint p = paintScale.getPaint(z);
118                g2.setPaint(p);
119                g2.fillRect(xIndex, yCount - yIndex - 1, 1, 1);
120            }
121        }
122        return image;
123    }
124
125}