001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2022, 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-2022, 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.internal.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 *
051 * @since 1.0.13
052 */
053public abstract class HeatMapUtils {
054
055    /**
056     * Returns a dataset containing one series that holds a copy of the (x, z)
057     * data from one row (y-index) of the specified dataset.
058     *
059     * @param dataset  the dataset ({@code null} not permitted).
060     * @param row  the row (y) index.
061     * @param seriesName  the series name/key ({@code null} not permitted).
062     *
063     * @return The dataset.
064     */
065    public static XYDataset extractRowFromHeatMapDataset(HeatMapDataset dataset,
066            int row, Comparable seriesName) {
067        XYSeries series = new XYSeries(seriesName);
068        int cols = dataset.getXSampleCount();
069        for (int c = 0; c < cols; c++) {
070            series.add(dataset.getXValue(c), dataset.getZValue(c, row));
071        }
072        XYSeriesCollection result = new XYSeriesCollection(series);
073        return result;
074    }
075
076    /**
077     * Returns a dataset containing one series that holds a copy of the (y, z)
078     * data from one column (x-index) of the specified dataset.
079     *
080     * @param dataset  the dataset ({@code null} not permitted).
081     * @param column  the column (x) index.
082     * @param seriesName  the series name ({@code null} not permitted).
083     *
084     * @return The dataset.
085     */
086    public static XYDataset extractColumnFromHeatMapDataset(
087            HeatMapDataset dataset, int column, Comparable seriesName) {
088        XYSeries series = new XYSeries(seriesName);
089        int rows = dataset.getYSampleCount();
090        for (int r = 0; r < rows; r++) {
091            series.add(dataset.getYValue(r), dataset.getZValue(column, r));
092        }
093        XYSeriesCollection result = new XYSeriesCollection(series);
094        return result;
095    }
096
097    /**
098     * Creates an image that displays the values from the specified dataset.
099     *
100     * @param dataset  the dataset ({@code null} not permitted).
101     * @param paintScale  the paint scale for the z-values ({@code null}
102     *         not permitted).
103     *
104     * @return A buffered image.
105     */
106    public static BufferedImage createHeatMapImage(HeatMapDataset dataset,
107            PaintScale paintScale) {
108
109        Args.nullNotPermitted(dataset, "dataset");
110        Args.nullNotPermitted(paintScale, "paintScale");
111        int xCount = dataset.getXSampleCount();
112        int yCount = dataset.getYSampleCount();
113        BufferedImage image = new BufferedImage(xCount, yCount,
114                BufferedImage.TYPE_INT_ARGB);
115        Graphics2D g2 = image.createGraphics();
116        for (int xIndex = 0; xIndex < xCount; xIndex++) {
117            for (int yIndex = 0; yIndex < yCount; yIndex++) {
118                double z = dataset.getZValue(xIndex, yIndex);
119                Paint p = paintScale.getPaint(z);
120                g2.setPaint(p);
121                g2.fillRect(xIndex, yCount - yIndex - 1, 1, 1);
122            }
123        }
124        return image;
125    }
126
127}