我使用Java HeatMap
库(http://www.mbeckler.org/heatMap/)为我的数据生成heatMap。
我正在使用具有NA值的daatset。所以,基本上我不想要具有NA
值的像素的颜色(白色)。但是,遗憾的是,这个库不支持具有NA值的数据,而我得到的是具有基色的图像的计划块。我试着查看源代码,以便进行一些更改。在代码中,drawData()
方法用于为bufferedImage中的每个像素着色(可能!)。有人可以帮助我如何实现对NA值的支持并显示它们没有颜色吗?我对BufferedImage
和Graphics2D
课程几乎没有经验。
以下是来自库源代码的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 (grey@grevian.org) 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:
请忽略两张图片的方向
答案 0 :(得分:1)
由于BufferedImage
被创建为ARGB,您可能不会在那些应该未定义的像素上绘制任何内容,并且它们将保持透明。类似的东西:
public static int NA = ...;
private void drawData()
{
bufferedImage = new BufferedImage(data.length,data[0].length, BufferedImage.TYPE_INT_ARGB);
Graphics2D bufferedGraphics = bufferedImage.createGraphics();
try {
for (int x = 0; x < data.length; x++)
{
for (int y = 0; y < data[0].length; y++)
{
int colorIndex = dataColorIndices[x][y];
if (colorIndex != NA) {
bufferedGraphics.setColor(colors[colorIndex]);
bufferedGraphics.fillRect(x, y, 1, 1);
}
// Alternate flow, if you really want the pixels to be white
// else {
// bufferedGraphics.setColor(Color.WHITE);
// bufferedGraphics.fillRect(x, y, 1, 1);
// }
}
}
}
finally {
bufferedGraphics.dispose();
}
}
我还处理Graphics2D
实例,以避免资源泄漏。