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