2015-09-27 22 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