    001/* ===========================================================
    002 * JFreeChart : a free chart library for the Java(tm) platform
    003 * ===========================================================
    004 *
    005 * (C) Copyright 2000-2011, by Object Refinery Limited 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 * HeatMapUtilities.java
    029 * ---------------------
    030 * (C) Copyright 2009, by Object Refinery Limited.
    031 *
    032 * Original Author:  David Gilbert (for Object Refinery Limited);
    033 * Contributor(s):   -;
    034 *
    035 * Changes:
    036 * --------
    037 * 28-Jan-2009 : Version 1 (DG);
    038 *
    039 */
    040
    041package org.jfree.data.general;
    042
    043import java.awt.Graphics2D;
    044import java.awt.Paint;
    045import java.awt.image.BufferedImage;
    046import org.jfree.chart.renderer.PaintScale;
    047import org.jfree.data.xy.XYDataset;
    048import org.jfree.data.xy.XYSeries;
    049import org.jfree.data.xy.XYSeriesCollection;
    050
    051/**
    052 * A utility class for the {@link HeatMapDataset}.
    053 *
    054 * @since 1.0.13
    055 */
    056public abstract class HeatMapUtilities {
    057
    058    /**
    059     * Returns a dataset containing one series that holds a copy of the (x, z)
    060     * data from one row (y-index) of the specified dataset.
    061     *
    062     * @param dataset  the dataset (<code>null</code> not permitted).
    063     * @param row  the row (y) index.
    064     * @param seriesName  the series name/key (<code>null</code> not permitted).
    065     *
    066     * @return The dataset.
    067     */
    068    public static XYDataset extractRowFromHeatMapDataset(HeatMapDataset dataset,
    069            int row, Comparable seriesName) {
    070        XYSeries series = new XYSeries(seriesName);
    071        int cols = dataset.getXSampleCount();
    072        for (int c = 0; c < cols; c++) {
    073            series.add(dataset.getXValue(c), dataset.getZValue(c, row));
    074        }
    075        XYSeriesCollection result = new XYSeriesCollection(series);
    076        return result;
    077    }
    078
    079    /**
    080     * Returns a dataset containing one series that holds a copy of the (y, z)
    081     * data from one column (x-index) of the specified dataset.
    082     *
    083     * @param dataset  the dataset (<code>null</code> not permitted).
    084     * @param column  the column (x) index.
    085     * @param seriesName  the series name (<code>null</code> not permitted).
    086     *
    087     * @return The dataset.
    088     */
    089    public static XYDataset extractColumnFromHeatMapDataset(
    090            HeatMapDataset dataset, int column, Comparable seriesName) {
    091        XYSeries series = new XYSeries(seriesName);
    092        int rows = dataset.getYSampleCount();
    093        for (int r = 0; r < rows; r++) {
    094            series.add(dataset.getYValue(r), dataset.getZValue(column, r));
    095        }
    096        XYSeriesCollection result = new XYSeriesCollection(series);
    097        return result;
    098    }
    099
    100    /**
    101     * Creates an image that displays the values from the specified dataset.
    102     *
    103     * @param dataset  the dataset (<code>null</code> not permitted).
    104     * @param paintScale  the paint scale for the z-values (<code>null</code>
    105     *         not permitted).
    106     *
    107     * @return A buffered image.
    108     */
    109    public static BufferedImage createHeatMapImage(HeatMapDataset dataset,
    110            PaintScale paintScale) {
    111
    112        if (dataset == null) {
    113            throw new IllegalArgumentException("Null 'dataset' argument.");
    114        }
    115        if (paintScale == null) {
    116            throw new IllegalArgumentException("Null 'paintScale' argument.");
    117        }
    118        int xCount = dataset.getXSampleCount();
    119        int yCount = dataset.getYSampleCount();
    120        BufferedImage image = new BufferedImage(xCount, yCount,
    121                BufferedImage.TYPE_INT_ARGB);
    122        Graphics2D g2 = image.createGraphics();
    123        for (int xIndex = 0; xIndex < xCount; xIndex++) {
    124            for (int yIndex = 0; yIndex < yCount; yIndex++) {
    125                double z = dataset.getZValue(xIndex, yIndex);
    126                Paint p = paintScale.getPaint(z);
    127                g2.setPaint(p);
    128                g2.fillRect(xIndex, yCount - yIndex - 1, 1, 1);
    129            }
    130        }
    131        return image;
    132    }
    133
    134}