我正在使用Java HeatMap
库(http://www.mbeckler.org/heatMap/)为我的数据生成heatMap。 我正在使用具有NA值的daatset。所以,基本上我不想为具有NA
值的像素使用颜色(白色)。但不幸的是,这个库不支持具有NA值的数据,我得到的是具有基本颜色的图像块图。我试图查看源代码,以便进行一些更改。在代码中,使用drawData()
方法为bufferedImage中的每个像素着色(可能!)。有人可以帮助我如何实现对NA值的支持并向他们显示无颜色?我对BufferedImage
和Graphics2D
班没有经验。如何在Java中将NA值表示为空白像素(白色)?
这里是从库的源代码drawData()
方法:
/**
* Creates a BufferedImage of the actual data plot.
*
* After doing some profiling, it was discovered that 90% of the drawing
* time was spend drawing the actual data (not on the axes or tick marks).
* Since the Graphics2D has a drawImage method that can do scaling, we are
* using that instead of scaling it ourselves. We only need to draw the
* data into the bufferedImage on startup, or if the data or gradient
* changes. This saves us an enormous amount of time. Thanks to
* Josh Hayes-Sheen ([email protected]) for the suggestion and initial code
* to use the BufferedImage technique.
*
* Since the scaling of the data plot will be handled by the drawImage in
* paintComponent, we take the easy way out and draw our bufferedImage with
* 1 pixel per data point. Too bad there isn't a setPixel method in the
* Graphics2D class, it seems a bit silly to fill a rectangle just to set a
* single pixel...
*
* This function should be called whenever the data or the gradient changes.
*/
private void drawData()
{
// System.out.println("Column: " + data.length + " row: " + data[0].length);
bufferedImage = new BufferedImage(data.length,data[0].length, BufferedImage.TYPE_INT_ARGB);
bufferedGraphics = bufferedImage.createGraphics();
for (int x = 0; x < data.length; x++)
{
for (int y = 0; y < data[0].length; y++)
{
bufferedGraphics.setColor(colors[dataColorIndices[x][y]]);
bufferedGraphics.fillRect(x, y, 1, 1);
}
}
}
样本数据,可以发现:http://www.filedropper.com/data_13
所以,这是它的外观的时刻:
来自Java的:
从R:
请忽略这两个图像
什么是NA值?上面的代码只是将一个值('int index = dataColorIndices [x] [y]')映射到一个颜色('Color c = colors [index]')并绘制它(填充1x1的矩形)。所以如果你将NA值映射到你想要的颜色(或者没有颜色),你会很好。 – haraldK
PS:在上面的代码中还有一个bug(资源泄漏),它似乎无限期地保留着'Graphics2D'实例。相反,'bufferedGraphics'应该是一个局部变量,代码应该被封装在'try/finally'中,'bufferedGraphics'应该在'finally'块中被'dispose()'d。 – haraldK
我的意思是来自NA值的是没有来自该特定像素的信号。所以,这个'索引'当有NA时会显示空像素。所以,我必须把这样的条件变成“Color c”为白色? – novicegeek