001/* ====================================================== 002 * JFreeChart : a chart library for the Java(tm) platform 003 * ====================================================== 004 * 005 * (C) Copyright 2000-present, by David Gilbert and Contributors. 006 * 007 * Project Info: https://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 private HeatMapUtils() { 054 // no requirement to instantiate 055 } 056 057 /** 058 * Returns a dataset containing one series that holds a copy of the (x, z) 059 * data from one row (y-index) of the specified dataset. 060 * 061 * @param dataset the dataset ({@code null} not permitted). 062 * @param row the row (y) index. 063 * @param seriesName the series name/key ({@code null} not permitted). 064 * 065 * @return The dataset. 066 */ 067 public static XYDataset extractRowFromHeatMapDataset(HeatMapDataset dataset, 068 int row, Comparable seriesName) { 069 XYSeries series = new XYSeries(seriesName); 070 int cols = dataset.getXSampleCount(); 071 for (int c = 0; c < cols; c++) { 072 series.add(dataset.getXValue(c), dataset.getZValue(c, row)); 073 } 074 return new XYSeriesCollection(series); 075 } 076 077 /** 078 * Returns a dataset containing one series that holds a copy of the (y, z) 079 * data from one column (x-index) of the specified dataset. 080 * 081 * @param dataset the dataset ({@code null} not permitted). 082 * @param column the column (x) index. 083 * @param seriesName the series name ({@code null} not permitted). 084 * 085 * @return The dataset. 086 */ 087 public static XYDataset extractColumnFromHeatMapDataset( 088 HeatMapDataset dataset, int column, Comparable seriesName) { 089 XYSeries series = new XYSeries(seriesName); 090 int rows = dataset.getYSampleCount(); 091 for (int r = 0; r < rows; r++) { 092 series.add(dataset.getYValue(r), dataset.getZValue(column, r)); 093 } 094 return new XYSeriesCollection(series); 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}