2015-09-27 74 views
0

我正在使用Java HeatMap库(http://www.mbeckler.org/heatMap/)为我的数据生成heatMap。 我正在使用具有NA值的daatset。所以,基本上我不想为具有NA值的像素使用颜色(白色)。但不幸的是,这个库不支持具有NA值的数据,我得到的是具有基本颜色的图像块图。我试图查看源代码,以便进行一些更改。在代码中,使用drawData()方法为bufferedImage中的每个像素着色(可能!)。有人可以帮助我如何实现对NA值的支持并向他们显示无颜色?我对BufferedImageGraphics2D班没有经验。如何在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的:

enter image description here

从R:

enter image description here

请忽略这两个图像

+0

什么是NA值?上面的代码只是将一个值('int index = dataColorIndices [x] [y]')映射到一个颜色('Color c = colors [index]')并绘制它(填充1x1的矩形)。所以如果你将NA值映射到你想要的颜色(或者没有颜色),你会很好。 – haraldK

+0

PS:在上面的代码中还有一个bug(资源泄漏),它似乎无限期地保留着'Graphics2D'实例。相反,'bufferedGraphics'应该是一个局部变量,代码应该被封装在'try/finally'中,'bufferedGraphics'应该在'finally'块中被'dispose()'d。 – haraldK

+0

我的意思是来自NA值的是没有来自该特定像素的信号。所以,这个'索引'当有NA时会显示空像素。所以,我必须把这样的条件变成“Color c”为白色? – novicegeek

回答

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实例,以避免资源泄漏。

+0

精彩,它的工作原理:)虽然它不知道为NA工作,我将NA值转换为零,我得到了预期的结果。另一个快速问题是,代码以某种方式舍弃了这些值?因为,当我在R中生成图像时,我得到了我正在寻找的确切热图,但在Java中,我得到了一个单独的彩色块。 – novicegeek

+0

@novicegeek上面的代码没有四舍五入。但是我不知道你正在使用的库(或者R),因此可能会在代码中的其他位置舍入/缩放输入值。 – haraldK

+0

是的,可能我会寻找那个。我只是更新了上面我的问题中的图片。这些是使用R和Java生成的相同数据矩阵的图像。 – novicegeek