我正在使用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